多条件分页查询物料信息

pull/451/head
starrysky 2019-07-30 11:22:52 +08:00
parent dc88fe1634
commit 6466d75b6a
2 changed files with 72 additions and 15 deletions

View File

@ -17,4 +17,6 @@ public class MaterialInfoQueryCriteria {
@Query(type = Query.Type.INNER_LIKE)
private String materialCode;
private Long materialCategoryId;
}

View File

@ -13,20 +13,31 @@ import me.zhengjie.modules.wms.bd.service.dto.MaterialInfoDTO;
import me.zhengjie.modules.wms.bd.service.dto.MaterialInfoQueryCriteria;
import me.zhengjie.modules.wms.bd.service.mapper.MaterialInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
/**
* @author
* @date 2019-07-27
*/
* @author
* @date 2019-07-27
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MaterialInfoServiceImpl implements MaterialInfoService {
@ -44,20 +55,64 @@ public class MaterialInfoServiceImpl implements MaterialInfoService {
private MeasureUnitRepository measureUnitRepository;
@Override
public Object queryAll(MaterialInfoQueryCriteria criteria, Pageable pageable){
Page<MaterialInfo> page = materialInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
public Object queryAll(MaterialInfoQueryCriteria criteria, Pageable pageable) {
Specification<MaterialInfo> specification = new Specification<MaterialInfo>() {
@Override
public Predicate toPredicate(Root<MaterialInfo> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> targetPredicateList = new ArrayList<>();
List<Predicate> nameOrMaterialCodeList = new ArrayList<>();
String name = criteria.getName();
if (!StringUtils.isEmpty(name)) {
Predicate namePredicate = criteriaBuilder.like(root.get("name"), "%" + name + "%");
nameOrMaterialCodeList.add(namePredicate);
}
String materialCode = criteria.getMaterialCode();
if (!StringUtils.isEmpty(materialCode)) {
Predicate materialCodePredicate = criteriaBuilder.like(root.get("materialCode"), "%" + materialCode + "%");
nameOrMaterialCodeList.add(materialCodePredicate);
}
//name与materialCode组装成
Predicate nameOrMaterialCodePredicate = null;
if (!CollectionUtils.isEmpty(nameOrMaterialCodeList)) {
nameOrMaterialCodePredicate = criteriaBuilder.or(nameOrMaterialCodeList.toArray(new Predicate[nameOrMaterialCodeList.size()]));
targetPredicateList.add(nameOrMaterialCodePredicate);
}
Predicate materialCategoryIdPredicate = null;
Long materialCategoryId = criteria.getMaterialCategoryId();
if (null != materialCategoryId) {
materialCategoryIdPredicate = criteriaBuilder.equal(root.get("materialCategoryId"), materialCategoryId);
targetPredicateList.add(materialCategoryIdPredicate);
}
if(CollectionUtils.isEmpty(targetPredicateList)){
return null;
}else{
return criteriaBuilder.and(targetPredicateList.toArray(new Predicate[targetPredicateList.size()]));
}
}
};
Page<MaterialInfo> page = materialInfoRepository.findAll(specification, pageable);
return PageUtil.toPage(page.map(materialInfoMapper::toDto));
}
@Override
public Object queryAll(MaterialInfoQueryCriteria criteria){
return materialInfoMapper.toDto(materialInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
public Object queryAll(MaterialInfoQueryCriteria criteria) {
return materialInfoMapper.toDto(materialInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
}
@Override
public MaterialInfoDTO findById(Long id) {
Optional<MaterialInfo> bdMaterialInfo = materialInfoRepository.findById(id);
ValidationUtil.isNull(bdMaterialInfo,"BdMaterialInfo","id",id);
ValidationUtil.isNull(bdMaterialInfo, "BdMaterialInfo", "id", id);
return materialInfoMapper.toDto(bdMaterialInfo.get());
}
@ -66,30 +121,30 @@ public class MaterialInfoServiceImpl implements MaterialInfoService {
public MaterialInfoDTO create(MaterialInfo resources) {
Long materialCategoryId = resources.getMaterialCategoryId();
String materialCategoryName = resources.getMaterialCategoryName();
if(null == materialCategoryId || StringUtils.isEmpty(materialCategoryName)){
if (null == materialCategoryId || StringUtils.isEmpty(materialCategoryName)) {
throw new BadRequestException("物料类别不能为空!");
}
MaterialCategory materialCategory = materialCategoryRepository.findByIdAndStatusTrue(materialCategoryId);
if(null == materialCategory){
if (null == materialCategory) {
throw new BadRequestException("物料类别不存在!");
}
Long measureUnitId = resources.getMeasureUnitId();
String measureUnitName = resources.getMeasureUnitName();
if(null == measureUnitId || StringUtils.isEmpty(measureUnitName)){
if (null == measureUnitId || StringUtils.isEmpty(measureUnitName)) {
throw new BadRequestException("计量单位不能为空!");
}
MeasureUnit measureUnit = measureUnitRepository.findByIdAndStatusTrue(measureUnitId);
if(null == measureUnit){
if (null == measureUnit) {
throw new BadRequestException("计量单位不存在!");
}
// 物料编码
String materialCode = resources.getMaterialCode();
MaterialInfo byMaterialCodeAndStatusTrue = materialInfoRepository.findByMaterialCodeAndStatusTrue(materialCode);
if(null != byMaterialCodeAndStatusTrue){
if (null != byMaterialCodeAndStatusTrue) {
throw new BadRequestException("物料编码已存在!");
}
return materialInfoMapper.toDto(materialInfoRepository.save(resources));
@ -99,7 +154,7 @@ public class MaterialInfoServiceImpl implements MaterialInfoService {
@Transactional(rollbackFor = Exception.class)
public void update(MaterialInfo resources) {
Optional<MaterialInfo> optionalBdMaterialInfo = materialInfoRepository.findById(resources.getId());
ValidationUtil.isNull( optionalBdMaterialInfo,"MaterialInfo","id",resources.getId());
ValidationUtil.isNull(optionalBdMaterialInfo, "MaterialInfo", "id", resources.getId());
MaterialInfo materialInfo = optionalBdMaterialInfo.get();
materialInfo.copy(resources);
materialInfoRepository.save(materialInfo);
@ -109,7 +164,7 @@ public class MaterialInfoServiceImpl implements MaterialInfoService {
@Transactional(rollbackFor = Exception.class)
public void delete(long id) {
MaterialInfo materialInfo = materialInfoRepository.findByIdAndStatusTrue(id);
if(null == materialInfo){
if (null == materialInfo) {
throw new BadRequestException("物料资料不存在!");
}
materialInfoRepository.deleteMaterialInfo(id);