package com.ff.weijinxiao.service;


import com.ff.weijinxiao.domain.Supplier;
import com.ff.weijinxiao.repository.SupplierRepository;
import com.ff.weijinxiao.util.SpecificationUtil;
import org.apache.commons.lang.StringUtils;
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.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class SupplierService {

    @Resource
    private SupplierRepository supplierRepository;

    /**
     * 创建实体
     *
     * @param supplier
     */
    public Supplier saveSupplier(Supplier supplier) {
        return supplierRepository.save(supplier);
    }

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

    public Supplier findByOnlyID(String onlyID) {
        return supplierRepository.findByOnlyID(onlyID);
    }

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

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

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


    public Page<Supplier> findPage(int page, int size, String tag) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";
        Pageable pageable = new PageRequest(page - 1, size, direction, sort);
        return supplierRepository.findAll(new Specification<Supplier>() {
            @Override
            public Predicate toPredicate(Root<Supplier> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate p = criteriaBuilder.and(criteriaBuilder.greaterThan(root.get("id").as(Integer.class), 0));
                if (null != tag && tag.length() > 0) {
                    Predicate p1 = criteriaBuilder.like(root.get("name").as(String.class), "%" + tag + "%");
                    Predicate orp = criteriaBuilder.or(p1);
                    p = criteriaBuilder.and(p, orp);
                }
                return p;
            }
        }, pageable);
    }

    public List<Supplier> findList(String uid, String tag) {

        return supplierRepository.findAll(new Specification<Supplier>() {
            @Override
            public Predicate toPredicate(Root<Supplier> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate p = criteriaBuilder.and(criteriaBuilder.equal(root.get("uid").as(String.class), uid));
                if (StringUtils.isNotBlank(tag)) {
                    Predicate p1 = criteriaBuilder.like(root.get("name").as(String.class), "%" + tag + "%");
                    Predicate p2 = criteriaBuilder.like(root.get("tel").as(String.class), "%" + tag + "%");
                    Predicate p3 = criteriaBuilder.like(root.get("address").as(String.class), "%" + tag + "%");
                    Predicate orp = criteriaBuilder.or(p1);
                    orp = criteriaBuilder.or(p2, orp);
                    orp = criteriaBuilder.or(p3, orp);
                    p = criteriaBuilder.and(p, orp);
                }
                return p;
            }
        });
    }

    public List<Supplier> findListByAccountBook(Integer accountBookId, String tag) {

        return supplierRepository.findAll(new Specification<Supplier>() {
            @Override
            public Predicate toPredicate(Root<Supplier> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate p = criteriaBuilder.and(criteriaBuilder.equal(root.get("accountBook").get("id").as(Integer.class), accountBookId));
                p = criteriaBuilder.and(criteriaBuilder.equal(root.get("isDelete").as(Integer.class), 0), p);
                if (StringUtils.isNotBlank(tag)) {
                    Predicate p1 = criteriaBuilder.like(root.get("name").as(String.class), "%" + tag + "%");
                    Predicate p2 = criteriaBuilder.like(root.get("tel").as(String.class), "%" + tag + "%");
                    Predicate p3 = criteriaBuilder.like(root.get("address").as(String.class), "%" + tag + "%");
                    Predicate orp = criteriaBuilder.or(p1);
                    orp = criteriaBuilder.or(p2, orp);
                    orp = criteriaBuilder.or(p3, orp);
                    p = criteriaBuilder.and(p, orp);
                }
                return p;
            }
        });
    }

    public Double getSupplierTotalAmount(String uid) {
        return supplierRepository.getSupplierTotalAmount(uid);
    }

    public Double getSupplierAlreadyPayed1(String uid) {
        return supplierRepository.getSupplierAlreadyPayed1(uid);
    }

    public Double getSupplierAlreadyPayed2(String uid) {
        return supplierRepository.getSupplierAlreadyPayed2(uid);
    }

    public Double getSupplierDiscount(String uid) {
        return supplierRepository.getSupplierDiscount(uid);
    }

    public Double getSupplierTotalAmountBySupplierOnlyId(String uid, String supplierOnlyId) {
        return supplierRepository.getSupplierTotalAmountBySupplierOnlyId(uid, supplierOnlyId);
    }

    public Double getSupplierAlreadyPayed1BySupplierOnlyId(String uid, String supplierOnlyId) {
        return supplierRepository.getSupplierAlreadyPayed1BySupplierOnlyId(uid, supplierOnlyId);
    }

    public Double getSupplierAlreadyPayed2BySupplierOnlyId(String uid, String supplierOnlyId) {
        return supplierRepository.getSupplierAlreadyPayed2BySupplierOnlyId(uid, supplierOnlyId);
    }

    public Double getSupplierDiscountBySupplierOnlyId(String uid, String supplierOnlyId) {
        return supplierRepository.getSupplierDiscountBySupplierOnlyId(uid, supplierOnlyId);
    }

    public Double getSupplierPurchaseNumBySupplierOnlyId(String uid, String supplierOnlyId) {
        return supplierRepository.getSupplierPurchaseNumBySupplierOnlyId(uid, supplierOnlyId);
    }

    public Double getSupplierStockNumBySupplierOnlyId(String uid, String supplierOnlyId) {
        return supplierRepository.getSupplierStockNumBySupplierOnlyId(uid, supplierOnlyId);
    }

    public List<Object[]> getSupplierExtInfo(String uid, String supplierId) {
        return supplierRepository.getSupplierExtInfo(uid, supplierId);
    }
}
