mirror of https://github.com/elunez/eladmin
物料类别
parent
7952a76969
commit
56d5af66cd
|
@ -1,8 +1,11 @@
|
|||
package me.zhengjie.modules.wms.bd.repository;
|
||||
|
||||
import me.zhengjie.modules.wms.bd.domain.MaterialCategory;
|
||||
import me.zhengjie.modules.wms.bd.domain.ProductCategory;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
/**
|
||||
* @author 黄星星
|
||||
|
@ -11,9 +14,31 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
|||
public interface MaterialCategoryRepository extends JpaRepository<MaterialCategory, Long >, JpaSpecificationExecutor {
|
||||
|
||||
/**
|
||||
* 查询存在的物料资料
|
||||
* 查询存在的物料类别
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
MaterialCategory findByIdAndStatusTrue(long id);
|
||||
|
||||
/**
|
||||
* 根据name查询状态正常的类别类别
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
MaterialCategory findByNameAndStatusTrue(String name);
|
||||
|
||||
/**
|
||||
* 根据name查询状态删除的类别类别
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
MaterialCategory findByNameAndStatusFalse(String name);
|
||||
|
||||
/**
|
||||
* 更新指定类别类别状态为true
|
||||
* @param id
|
||||
*/
|
||||
@Modifying
|
||||
@Query(value = "update bd_product_category set status = 1 where id = ?1",nativeQuery = true)
|
||||
void updateStatusToTrue(long id);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package me.zhengjie.modules.wms.bd.service.impl;
|
||||
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.modules.wms.bd.domain.MaterialCategory;
|
||||
import me.zhengjie.modules.wms.bd.domain.MeasureUnit;
|
||||
import me.zhengjie.modules.wms.bd.domain.ProductCategory;
|
||||
import me.zhengjie.modules.wms.bd.repository.MaterialCategoryRepository;
|
||||
import me.zhengjie.modules.wms.bd.service.MaterialCategoryService;
|
||||
import me.zhengjie.modules.wms.bd.service.dto.MaterialCategoryDTO;
|
||||
|
@ -11,10 +14,18 @@ import me.zhengjie.utils.ValidationUtil;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
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 org.springframework.util.CollectionUtils;
|
||||
|
||||
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;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
|
@ -34,7 +45,28 @@ public class MaterialCategoryServiceImpl implements MaterialCategoryService {
|
|||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public MaterialCategoryDTO create(MaterialCategory resources) {
|
||||
return materialCategoryMapper.toDto(materialCategoryRepository.save(resources));
|
||||
/**
|
||||
* 查看状态正常的情况下该物料类别是否存在,如果存在,则提示物料类别已存在
|
||||
* 查看删除状态下该名字的物料类别,如果产品类别存在,则修改物料类别状态
|
||||
* 否则直接插入新的记录
|
||||
*/
|
||||
MaterialCategory byNameAndStatusTrue = materialCategoryRepository.findByNameAndStatusTrue(resources.getName());
|
||||
if(null != byNameAndStatusTrue){
|
||||
throw new BadRequestException("该物料类别已经存在");
|
||||
}
|
||||
MaterialCategory byNameAndStatusFalse = materialCategoryRepository.findByNameAndStatusFalse(resources.getName());
|
||||
if(null != byNameAndStatusFalse){
|
||||
resources.setStatus(true);
|
||||
materialCategoryRepository.updateStatusToTrue(byNameAndStatusFalse.getId());
|
||||
Optional<MaterialCategory> productCategoryOptional = materialCategoryRepository.findById(byNameAndStatusFalse.getId());
|
||||
MaterialCategory materialCategory = productCategoryOptional.get();
|
||||
return materialCategoryMapper.toDto(materialCategory);
|
||||
}else{
|
||||
resources.getName();
|
||||
resources.setStatus(true);
|
||||
MaterialCategory materialCategory = materialCategoryRepository.save(resources);
|
||||
return materialCategoryMapper.toDto(materialCategory);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -53,7 +85,24 @@ public class MaterialCategoryServiceImpl implements MaterialCategoryService {
|
|||
|
||||
@Override
|
||||
public Object queryAll(MaterialCategoryDTO maaterialCategory, Pageable pageable) {
|
||||
Page<MaterialCategory> page = materialCategoryRepository.findAll((root, query, cb) -> QueryHelp.getPredicate(root, maaterialCategory, cb), pageable);
|
||||
Specification<MaterialCategory> specification = new Specification<MaterialCategory>() {
|
||||
@Override
|
||||
public Predicate toPredicate(Root<MaterialCategory> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
|
||||
|
||||
List<Predicate> targetPredicateList = new ArrayList<>();
|
||||
|
||||
//状态
|
||||
Predicate statusPredicate = criteriaBuilder.equal(root.get("status"), 1);
|
||||
targetPredicateList.add(statusPredicate);
|
||||
|
||||
if(CollectionUtils.isEmpty(targetPredicateList)){
|
||||
return null;
|
||||
}else{
|
||||
return criteriaBuilder.and(targetPredicateList.toArray(new Predicate[targetPredicateList.size()]));
|
||||
}
|
||||
}
|
||||
};
|
||||
Page<MaterialCategory> page = materialCategoryRepository.findAll(specification, pageable);
|
||||
return PageUtil.toPage(page.map(materialCategoryMapper::toDto));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue