产品类别

pull/451/head
starrysky 2019-08-14 16:38:35 +08:00
parent c179169c9e
commit ff47bd95b5
2 changed files with 99 additions and 3 deletions

View File

@ -1,8 +1,11 @@
package me.zhengjie.modules.wms.bd.repository;
import me.zhengjie.modules.wms.bd.domain.MeasureUnit;
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
@ -10,4 +13,49 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
*/
public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Long >, JpaSpecificationExecutor {
/**
*
* @param id
* @return
*/
ProductCategory findByIdAndStatusTrue(long id);
/**
* name
* @param name
* @return
*/
ProductCategory findByNameAndStatusTrue(String name);
/**
* name
* @param name
* @return
*/
ProductCategory 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);
/**
* ()
* @param id
*/
@Modifying
@Query(value = "update bd_product_category set status = 0 where id = ?1",nativeQuery = true)
void deleteProductCategory(long id);
/**
*
* @param name
* @param id
*/
@Modifying
@Query(value = "update bd_product_category set name = ?1 where id = ?2",nativeQuery = true)
void updateNameById(String name, long id);
}

View File

@ -1,5 +1,7 @@
package me.zhengjie.modules.wms.bd.service.impl;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.wms.bd.domain.MeasureUnit;
import me.zhengjie.modules.wms.bd.domain.ProductCategory;
import me.zhengjie.modules.wms.bd.repository.ProductCategoryRepository;
import me.zhengjie.modules.wms.bd.service.ProductCategoryService;
@ -11,10 +13,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 +44,28 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
@Override
@Transactional(rollbackFor = Exception.class)
public ProductCategoryDTO create(ProductCategory resources) {
return productCategoryMapper.toDto(productCategoryRepository.save(resources));
/**
*
*
*
*/
ProductCategory byNameAndStatusTrue = productCategoryRepository.findByNameAndStatusTrue(resources.getName());
if(null != byNameAndStatusTrue){
throw new BadRequestException("该计量单位已经存在");
}
ProductCategory byNameAndStatusFalse = productCategoryRepository.findByNameAndStatusFalse(resources.getName());
if(null != byNameAndStatusFalse){
resources.setStatus(true);
productCategoryRepository.updateStatusToTrue(byNameAndStatusFalse.getId());
Optional<ProductCategory> productCategoryOptional = productCategoryRepository.findById(byNameAndStatusFalse.getId());
ProductCategory productCategory = productCategoryOptional.get();
return productCategoryMapper.toDto(productCategory);
}else{
resources.getName();
resources.setStatus(true);
ProductCategory productCategory = productCategoryRepository.save(resources);
return productCategoryMapper.toDto(productCategory);
}
}
@Override
@ -48,12 +79,29 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(Long id) {
productCategoryRepository.deleteById(id);
productCategoryRepository.deleteProductCategory(id);
}
@Override
public Object queryAll(ProductCategoryDTO productCategory, Pageable pageable) {
Page<ProductCategory> page = productCategoryRepository.findAll((root, query, cb) -> QueryHelp.getPredicate(root, productCategory, cb), pageable);
Specification<ProductCategory> specification = new Specification<ProductCategory>() {
@Override
public Predicate toPredicate(Root<ProductCategory> 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<ProductCategory> page = productCategoryRepository.findAll(specification, pageable);
return PageUtil.toPage(page.map(productCategoryMapper::toDto));
}