mirror of https://github.com/elunez/eladmin
供应商类别三
parent
56d5af66cd
commit
b37d944d72
|
@ -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.SupplierCategory;
|
||||
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,37 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
|||
*/
|
||||
public interface SupplierCategoryRepository extends JpaRepository<SupplierCategory, Long >, JpaSpecificationExecutor {
|
||||
|
||||
/**
|
||||
* 根据主键查询状态正常的供应商类别
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
SupplierCategory findByIdAndStatusTrue(long id);
|
||||
|
||||
/**
|
||||
* 根据name查询状态正常的供应商类别
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
SupplierCategory findByNameAndStatusTrue(String name);
|
||||
|
||||
/**
|
||||
* 根据name查询状态删除的供应商类别
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
SupplierCategory findByNameAndStatusFalse(String name);
|
||||
|
||||
|
||||
@Modifying
|
||||
@Query(value = "update bd_supplier_category set status = 1 where id = ?1",nativeQuery = true)
|
||||
void updateStatusToTrue(long id);
|
||||
|
||||
/**
|
||||
* 删除供应商类别(逻辑删除)
|
||||
* @param id
|
||||
*/
|
||||
@Modifying
|
||||
@Query(value = "update bd_supplier_category set status = 0 where id = ?1",nativeQuery = true)
|
||||
void deleteSupplierCategory(long id);
|
||||
}
|
||||
|
|
|
@ -48,9 +48,9 @@ public class SupplierCategoryController {
|
|||
return new ResponseEntity(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Log("查询供应商类别")
|
||||
@GetMapping(value = "/supplierCategory")
|
||||
public ResponseEntity getDicts(SupplierCategoryDTO resources, Pageable pageable){
|
||||
@Log("分页查询供应商类别")
|
||||
@GetMapping(value = "/querySupplierCategoryPage")
|
||||
public ResponseEntity querySupplierCategoryPage(SupplierCategoryDTO resources, Pageable pageable){
|
||||
return new ResponseEntity(supplierCategoryService.queryAll(resources,pageable),HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
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.SupplierCategory;
|
||||
import me.zhengjie.modules.wms.bd.repository.SupplierCategoryRepository;
|
||||
import me.zhengjie.modules.wms.bd.service.SupplierCategoryService;
|
||||
|
@ -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 SupplierCategoryServiceImpl implements SupplierCategoryService {
|
|||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public SupplierCategoryDTO create(SupplierCategory resources) {
|
||||
return supplierCategoryMapper.toDto(supplierCategoryRepository.save(resources));
|
||||
/**
|
||||
* 查看状态正常的情况下该供应商类别是否存在,如果存在,则提示供应商类别已存在
|
||||
* 查看删除状态下该名字的供应商类别,如果供应商类别存在,则修改供应商类别状态
|
||||
* 否则直接插入新的记录
|
||||
*/
|
||||
SupplierCategory byNameAndStatusTrue = supplierCategoryRepository.findByNameAndStatusTrue(resources.getName());
|
||||
if(null != byNameAndStatusTrue){
|
||||
throw new BadRequestException("该计量单位已经存在");
|
||||
}
|
||||
SupplierCategory byNameAndStatusFalse = supplierCategoryRepository.findByNameAndStatusFalse(resources.getName());
|
||||
if(null != byNameAndStatusFalse){
|
||||
resources.setStatus(true);
|
||||
supplierCategoryRepository.updateStatusToTrue(byNameAndStatusFalse.getId());
|
||||
Optional<SupplierCategory> supplierCategoryOptional = supplierCategoryRepository.findById(byNameAndStatusFalse.getId());
|
||||
SupplierCategory supplierCategory = supplierCategoryOptional.get();
|
||||
return supplierCategoryMapper.toDto(supplierCategory);
|
||||
}else{
|
||||
resources.getName();
|
||||
resources.setStatus(true);
|
||||
SupplierCategory supplierCategory = supplierCategoryRepository.save(resources);
|
||||
return supplierCategoryMapper.toDto(supplierCategory);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -48,12 +80,29 @@ public class SupplierCategoryServiceImpl implements SupplierCategoryService {
|
|||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(Long id) {
|
||||
supplierCategoryRepository.deleteById(id);
|
||||
supplierCategoryRepository.deleteSupplierCategory(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object queryAll(SupplierCategoryDTO supplierCategory, Pageable pageable) {
|
||||
Page<SupplierCategory> page = supplierCategoryRepository.findAll((root, query, cb) -> QueryHelp.getPredicate(root, supplierCategory, cb), pageable);
|
||||
Specification<SupplierCategory> specification = new Specification<SupplierCategory>() {
|
||||
@Override
|
||||
public Predicate toPredicate(Root<SupplierCategory> 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<SupplierCategory> page = supplierCategoryRepository.findAll(specification, pageable);
|
||||
return PageUtil.toPage(page.map(supplierCategoryMapper::toDto));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue