package com.ff.weijinxiao.service;


import com.ff.weijinxiao.domain.Order;
import com.ff.weijinxiao.repository.OrderRepository;
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 OrderService {

    @Resource
    private OrderRepository orderRepository;

    /**
     * 创建实体
     *
     * @param order
     */
    public Order saveOrder(Order order) {
        return orderRepository.save(order);
    }

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

    public Order findByOnlyID(String onlyID) {
        return orderRepository.findByOnlyID(onlyID);
    }

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

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

    /**
     * 根据条件查询List
     */
    public List<Order> findList(Map<String, Object> param) {
        return orderRepository.findAll(SpecificationUtil.buildSpecificationOrder(param, Order.class,"createTime"));
    }

}
