package com.xkrs.utils; import com.xkrs.dao.FirePointDao; import com.xkrs.model.entity.FirePointEntity; 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.util.ArrayList; import java.util.List; /** * @author XinYi Song */ @Component public class Query { @Resource private FirePointDao firePointDao; /** * 动态多条件查询项目信息 * @param * @return */ public List selectFirePoint(String cityCode,String satelliteType,String landType,String startTime,String endTime) { Specification specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) { List list = new ArrayList<>(); if (cityCode != null && !"".equals(cityCode)) { String substring = cityCode.substring(0, 4); list.add(criteriaBuilder.like(root.get("countyCode").as(String.class), "%" + substring + "%")); } if (satelliteType != null && !"".equals(satelliteType)) { list.add(criteriaBuilder.equal(root.get("satelliteType").as(String.class), satelliteType)); } if (landType != null && !"".equals(landType)) { list.add(criteriaBuilder.equal(root.get("landType").as(String.class), landType)); } if(startTime != null && !"".equals(startTime)){ list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("addTime").as(String.class), startTime)); } if(endTime != null && !"".equals(endTime)){ list.add(criteriaBuilder.lessThanOrEqualTo(root.get("addTime").as(String.class), endTime)); } Predicate[] predicates = new Predicate[list.size()]; return criteriaBuilder.and(list.toArray(predicates)); } }; return firePointDao.findAll(specification); } }