package com.xkrs.util; import com.xkrs.dao.DataSourceDao; import com.xkrs.model.entity.DataSource; import org.apache.http.util.TextUtils; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Component; import javax.annotation.Resource; import javax.persistence.criteria.Predicate; import java.util.ArrayList; import java.util.List; /** * @Author: XinYi Song * @Date: 2022/1/19 13:56 */ @Component public class DataSourceQuery { @Resource private DataSourceDao dataSourceDao; /** * 动态多条件查询采集数据信息 * * @return */ public List selectDataSource(String lotNo, String machineNo, String materialNo, String modelNo, String machineTypeNo, String craftItemName) { Specification specification = (root, criteriaQuery, criteriaBuilder) -> { List list = new ArrayList<>(); if (!TextUtils.isEmpty(lotNo)) { list.add(criteriaBuilder.equal(root.get("lotNo").as(String.class), lotNo)); } if (!TextUtils.isEmpty(machineNo)) { list.add(criteriaBuilder.equal(root.get("machineNo").as(String.class), machineNo)); } if (!TextUtils.isEmpty(materialNo)) { list.add(criteriaBuilder.equal(root.get("materialNo").as(String.class), materialNo)); } if (!TextUtils.isEmpty(modelNo)) { list.add(criteriaBuilder.equal(root.get("modelNo").as(String.class), modelNo)); } if (!TextUtils.isEmpty(machineTypeNo)) { list.add(criteriaBuilder.equal(root.get("machineTypeNo").as(String.class), machineTypeNo)); } if (!TextUtils.isEmpty(craftItemName)) { list.add(criteriaBuilder.equal(root.get("craftItemName").as(String.class), craftItemName)); } Predicate[] predicates = new Predicate[list.size()]; return criteriaBuilder.and(list.toArray(predicates)); }; return dataSourceDao.findAll(specification); } }