package com.ff.weijinxiao.service;


import com.ff.weijinxiao.domain.SupplierPayBack;
import com.ff.weijinxiao.repository.SupplierPayBackRepository;
import com.ff.weijinxiao.util.SpecificationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class SupplierPayBackService {

    @Resource
    private SupplierPayBackRepository supplierPayBackRepository;

    /**
     * 创建实体
     *
     * @param supplierPayBack
     */
    public SupplierPayBack saveSupplierPayBack(SupplierPayBack supplierPayBack) {
        return supplierPayBackRepository.save(supplierPayBack);
    }

    /**
     * 查找实体
     *
     * @param id
     * @return
     */
    public SupplierPayBack findById(Integer id) {
        Optional<SupplierPayBack> optional = supplierPayBackRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        } else {
            return null;
        }
    }

    public SupplierPayBack findByOnlyID(String onlyID) {
        return supplierPayBackRepository.findByOnlyID(onlyID);
    }

    public void deleteById(Integer id) {
        supplierPayBackRepository.deleteById(id);
    }

    /**
     * 分页条件查询
     *
     * @param page
     * @param size
     * @param param
     * @return
     */
    public Page<SupplierPayBack> findPage(int page, int size, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";
        Pageable pageable = new PageRequest(page - 1, size, direction, sort);
        return supplierPayBackRepository.findAll(SpecificationUtil.buildSpecification(param, SupplierPayBack.class), pageable);
    }

    /**
     * 根据条件查询List
     */
    public List<SupplierPayBack> findList(Map<String, Object> param) {
        return supplierPayBackRepository.findAll(SpecificationUtil.buildSpecification(param, SupplierPayBack.class));
    }


}
