From 76318723fe233b38d8a65331a45909027304a159 Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Tue, 10 Sep 2024 20:12:00 +0800 Subject: [PATCH] =?UTF-8?q?=E7=85=A4=E6=BA=90=E4=BF=A1=E6=81=AF=E5=94=AF?= =?UTF-8?q?=E4=B8=80=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coalInfo/service/CoalInfoService.java | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/main/java/cn/lihongjie/coal/coalInfo/service/CoalInfoService.java b/src/main/java/cn/lihongjie/coal/coalInfo/service/CoalInfoService.java index dba34aaf..bab39bd6 100644 --- a/src/main/java/cn/lihongjie/coal/coalInfo/service/CoalInfoService.java +++ b/src/main/java/cn/lihongjie/coal/coalInfo/service/CoalInfoService.java @@ -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 { +public class CoalInfoService extends BaseService { @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 { 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())) {