package com.izouma.nineth.service;

import com.izouma.nineth.domain.UserWareHouse;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.repo.UserWareHouseRepo;
import com.izouma.nineth.utils.JpaUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@AllArgsConstructor
public class UserWareHouseService {

    private UserWareHouseRepo userWareHouseRepo;

    /**
     * 查询列表
     * @param pageQuery
     * @return
     */
    public Page<UserWareHouse> all(PageQuery pageQuery){
        Object queryType = pageQuery.getQuery().remove("queryType");
        Specification<UserWareHouse> specification = JpaUtils.toSpecification(pageQuery, UserWareHouse.class);
        specification = specification.and((Specification<UserWareHouse>) (root, criteriaQuery, criteriaBuilder) -> {
            Predicate predicate = null;
            if ("1".equals(queryType)) {
                predicate = criteriaBuilder.isNotNull(root.get("assetId"));
            } else if("2".equals(queryType)) {
                predicate = criteriaBuilder.isNull(root.get("assetId"));
            }
            return predicate;
        });

        return userWareHouseRepo.findAll(specification, JpaUtils.toPageRequest(pageQuery));
    }


}
