煤源信息唯一校验

This commit is contained in:
2024-09-10 20:12:00 +08:00
parent 96f5c9e871
commit 76318723fe

View File

@@ -13,6 +13,9 @@ import cn.lihongjie.coal.coalPrice.entity.CoalPriceEntity;
import cn.lihongjie.coal.coalPrice.service.CoalPriceService;
import cn.lihongjie.coal.exception.BizException;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -28,16 +31,32 @@ import java.util.Objects;
@Service
@Slf4j
@Transactional
public
class CoalInfoService extends BaseService<CoalInfoEntity, CoalInfoRepository> {
public class CoalInfoService extends BaseService<CoalInfoEntity, CoalInfoRepository> {
@Autowired CoalPriceService coalPriceService;
@Autowired private CoalInfoRepository repository;
@Autowired private CoalInfoMapper mapper;
@Autowired private ConversionService conversionService;
@PersistenceContext EntityManager em;
public CoalInfoDto create(CreateCoalInfoDto request) {
CoalInfoEntity entity = mapper.toEntity(request);
Long cnt =
em.createQuery(
"select count(1) from CoalInfoEntity where name = :name and supplier.id = :supplierId and organizationId = :organizationId and coalType = :coalType",
Long.class)
.setParameter("name", entity.getName())
.setParameter("supplierId", entity.getSupplier().getId())
.setParameter("organizationId", entity.getOrganizationId())
.setParameter("coalType", entity.getCoalType())
.getSingleResult();
if (cnt > 0) {
throw new BizException("煤源信息已存在");
}
this.repository.save(entity);
updatePrice(entity);
@@ -57,6 +76,24 @@ class CoalInfoService extends BaseService<CoalInfoEntity, CoalInfoRepository> {
Double old = entity.getInitPrice();
this.mapper.updateEntity(entity, request);
Long cnt =
em.createQuery(
"select count(1) from CoalInfoEntity where name = :name and supplier.id = :supplierId and organizationId = :organizationId and coalType = :coalType and id != :id",
Long.class)
.setParameter("name", entity.getName())
.setParameter("supplierId", entity.getSupplier().getId())
.setParameter("organizationId", entity.getOrganizationId())
.setParameter("coalType", entity.getCoalType())
.setParameter("id", entity.getId())
.getSingleResult();
if (cnt > 0) {
throw new BizException("煤源信息已存在");
}
this.repository.save(entity);
if (!Objects.equals(old, request.getInitPrice())) {