package com.xkrs.utils;

import com.xkrs.dao.BusinessDao;
import com.xkrs.dao.ProductDao;
import com.xkrs.model.entity.BusinessEntity;
import com.xkrs.model.entity.ProductEntity;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

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.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author XinYi Song
 */

@Component
public class Query {

    @Resource
    private ProductDao productDao;

    @Resource
    private BusinessDao businessDao;

    /**
     * 查看要审核的商品信息
     * @param
     * @param productCounty
     * @param reviewType
     * @return
     */
    public List<ProductEntity> selectProductCounty(String productCity, String productCounty, String reviewType) {
        Specification<ProductEntity> specification = new Specification<ProductEntity>() {
            @Override
            public Predicate toPredicate(Root<ProductEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                /*if(businessId != null){
                    list.add(criteriaBuilder.equal(root.get("businessId").as(Integer.class), businessId));
                }*/
                if(productCity != null && !"".equals(productCity)){
                    list.add(criteriaBuilder.equal(root.get("productCity").as(String.class), productCity));
                }
                if(productCounty != null && !"".equals(productCounty)){
                    list.add(criteriaBuilder.equal(root.get("productCounty").as(String.class), productCounty));
                }
                if(reviewType != null && !"".equals(reviewType)){
                    list.add(criteriaBuilder.equal(root.get("reviewType").as(String.class), reviewType));
                }
                Predicate[] predicates = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(predicates));
            }
        };
        return productDao.findAll(specification);
    }

    /**
     * 查看商品信息
     * @param businessId
     * @param productCounty
     * @param shelfType
     * @return
     */
    public List<ProductEntity> selectAllProduct(Integer businessId, String productCounty, String shelfType) {
        Specification<ProductEntity> specification = new Specification<ProductEntity>() {
            @Override
            public Predicate toPredicate(Root<ProductEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                if(businessId != null){
                    list.add(criteriaBuilder.equal(root.get("businessId").as(Integer.class), businessId));
                }
                if(productCounty != null && !"".equals(productCounty)){
                    list.add(criteriaBuilder.equal(root.get("productCounty").as(String.class), productCounty));
                }
                if(shelfType != null && !"".equals(shelfType)){
                    list.add(criteriaBuilder.equal(root.get("shelfType").as(String.class), shelfType));
                }
                Predicate[] predicates = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(predicates));
            }
        };
        return productDao.findAll(specification);
    }

    /**
     * 各市区县代理商查看商家入驻信息
     * @param businessCity
     * @param businessCountry
     * @param businessName
     * @return
     */
    public List<BusinessEntity> selectAllBusiness(String businessCity, String businessCountry, String businessName) {
        Specification<BusinessEntity> specification = new Specification<BusinessEntity>() {
            @Override
            public Predicate toPredicate(Root<BusinessEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                if(businessCity != null && !"".equals(businessCity)){
                    list.add(criteriaBuilder.equal(root.get("businessCity").as(Integer.class), businessCity));
                }
                if(businessCountry != null && !"".equals(businessCountry)){
                    list.add(criteriaBuilder.equal(root.get("businessCountry").as(String.class), businessCountry));
                }
                if(businessName != null && !"".equals(businessName)){
                    list.add(criteriaBuilder.like(root.get("businessName").as(String.class), "%" + businessName + "%"));
                }
                list.add(criteriaBuilder.equal(root.get("businessType").as(String.class), "1"));
                Predicate[] predicates = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(predicates));
            }
        };
        return businessDao.findAll(specification);
    }

    /**
     * 总部查看要审核的商家的信息
     * @param businessCity
     * @param businessCountry
     * @param businessName
     * @return
     */
    public List<BusinessEntity> selectDefaultBusiness(String businessCity, String businessCountry, String businessName) {
        Specification<BusinessEntity> specification = new Specification<BusinessEntity>() {
            @Override
            public Predicate toPredicate(Root<BusinessEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                if(businessCity != null && !"".equals(businessCity)){
                    list.add(criteriaBuilder.equal(root.get("businessCity").as(Integer.class), businessCity));
                }
                if(businessCountry != null && !"".equals(businessCountry)){
                    list.add(criteriaBuilder.equal(root.get("businessCountry").as(String.class), businessCountry));
                }
                if(businessName != null && !"".equals(businessName)){
                    list.add(criteriaBuilder.like(root.get("businessName").as(String.class), "%" + businessName + "%"));
                }
                list.add(criteriaBuilder.equal(root.get("businessType").as(String.class), "0"));
                Predicate[] predicates = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(predicates));
            }
        };
        return businessDao.findAll(specification);
    }


}