物料类别

pull/451/head
starrysky 2019-08-14 22:15:58 +08:00
parent 7952a76969
commit 56d5af66cd
2 changed files with 77 additions and 3 deletions

View File

@ -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);
}

View File

@ -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));
}