mirror of https://github.com/elunez/eladmin
产品类别
parent
c179169c9e
commit
ff47bd95b5
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue