供应商类别三

pull/451/head
starrysky 2019-08-14 23:34:02 +08:00
parent 56d5af66cd
commit b37d944d72
3 changed files with 91 additions and 6 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.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);
}

View File

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

View File

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