package com.ff.weijinxiao.service;


import com.ff.weijinxiao.domain.WareHouse;
import com.ff.weijinxiao.repository.WareHouseRepository;
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 WareHouseService {

    @Resource
    private WareHouseRepository wareHouseRepository;

    /**
     * 创建实体
     *
     * @param wareHouse
     */
    public WareHouse saveWareHouse(WareHouse wareHouse) {
        return wareHouseRepository.save(wareHouse);
    }

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

    public WareHouse findByOnlyID(String onlyID) {
        return wareHouseRepository.findByOnlyID(onlyID);
    }

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

    /**
     * 分页条件查询
     *
     * @param page
     * @param size
     * @param param
     * @return
     */
    public Page<WareHouse> 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 wareHouseRepository.findAll(SpecificationUtil.buildSpecification(param, WareHouse.class), pageable);
    }

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

}
