From 3c58f9f254fb3a2184ba5b66ce6aed1e7e41666a Mon Sep 17 00:00:00 2001 From: chenhao <852066789@qq.com> Date: Thu, 11 Sep 2025 15:50:16 +0800 Subject: [PATCH] =?UTF-8?q?```=20refactor(sip-service):=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=BA=BF=E7=A8=8B=E5=AE=89=E5=85=A8=E9=94=81=E4=BB=A5?= =?UTF-8?q?=E7=A1=AE=E4=BF=9D=E7=BC=96=E7=A0=81=E7=94=9F=E6=88=90=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E7=9A=84=E7=BA=BF=E7=A8=8B=E5=AE=89=E5=85=A8=E6=80=A7?= =?UTF-8?q?=E5=9C=A8=20`AgentInfoServiceImpl`,=20`CustomerInfoServiceImpl`?= =?UTF-8?q?,=20`PartnerInfoServiceImpl`=20=E5=92=8C=20`VendorInfoServiceIm?= =?UTF-8?q?pl`=20=E4=B8=AD=E6=B7=BB=E5=8A=A0=E4=BA=86=20`ReentrantLock`?= =?UTF-8?q?=E4=BB=A5=E7=A1=AE=E4=BF=9D=E7=94=9F=E6=88=90=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E7=9A=84=E6=93=8D=E4=BD=9C=E6=98=AF=E7=BA=BF=E7=A8=8B=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E7=9A=84=E3=80=82=E5=85=B7=E4=BD=93=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=8C=85=E6=8B=AC=EF=BC=9A=20-=20=E5=9C=A8=E6=AF=8F=E4=B8=AA?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=AE=9E=E7=8E=B0=E7=B1=BB=E4=B8=AD=E5=BC=95?= =?UTF-8?q?=E5=85=A5=20`Lock`=20=E5=92=8C=20`ReentrantLock`=E3=80=82-=20?= =?UTF-8?q?=E5=9C=A8=E6=8F=92=E5=85=A5=E5=92=8C=E6=9B=B4=E6=96=B0=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E4=B8=AD=E4=BD=BF=E7=94=A8=E9=94=81=E6=9D=A5=E4=BF=9D?= =?UTF-8?q?=E8=AF=81=E7=BC=96=E7=A0=81=E7=94=9F=E6=88=90=E7=9A=84=E5=94=AF?= =?UTF-8?q?=E4=B8=80=E6=80=A7=E3=80=82=20-=20=E5=9C=A8=20`ProjectOrderInfo?= =?UTF-8?q?Mapper.xml`=20=E4=B8=AD=E4=BF=AE=E6=AD=A3=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=20SQL=20=E8=AF=AD=E5=8F=A5=E7=9A=84=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 这些改动有助于避免多线程环境下可能发生的编码冲突问题。 ``` --- .../service/impl/AgentInfoServiceImpl.java | 3 +- .../service/impl/CustomerInfoServiceImpl.java | 41 +++++++++----- .../service/impl/PartnerInfoServiceImpl.java | 54 ++++++++++++------- .../service/impl/VendorInfoServiceImpl.java | 40 ++++++++++---- .../mapper/sip/ProjectOrderInfoMapper.xml | 2 +- 5 files changed, 98 insertions(+), 42 deletions(-) diff --git a/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/AgentInfoServiceImpl.java b/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/AgentInfoServiceImpl.java index 2c1f4105..a6460d69 100644 --- a/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/AgentInfoServiceImpl.java +++ b/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/AgentInfoServiceImpl.java @@ -1,6 +1,8 @@ package com.ruoyi.sip.service.impl; import java.util.List; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.ShiroUtils; @@ -22,7 +24,6 @@ public class AgentInfoServiceImpl implements IAgentInfoService { @Autowired private AgentInfoMapper agentInfoMapper; - /** * 查询办事处信息 * diff --git a/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/CustomerInfoServiceImpl.java b/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/CustomerInfoServiceImpl.java index 2db35e90..5f471892 100644 --- a/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/CustomerInfoServiceImpl.java +++ b/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/CustomerInfoServiceImpl.java @@ -1,6 +1,8 @@ package com.ruoyi.sip.service.impl; import java.util.List; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import cn.hutool.core.collection.CollUtil; import com.ruoyi.common.exception.ServiceException; @@ -29,6 +31,8 @@ public class CustomerInfoServiceImpl implements ICustomerInfoService private CustomerInfoMapper customerInfoMapper; @Autowired private ICnareaService cnareaService; + // 添加静态锁,确保生成编码的操作是线程安全的 + private static final Lock lock = new ReentrantLock(); /** * 查询客户信息 * @@ -63,12 +67,18 @@ public class CustomerInfoServiceImpl implements ICustomerInfoService public int insertCustomerInfo(CustomerInfo customerInfo) { customerInfo.setCreateBy(ShiroUtils.getUserId().toString()); + lock.lock(); + try { customerInfo.setCustomerCode(generateCode(customerInfo.getProvince())); - int i = customerInfoMapper.selectCountByCode(customerInfo); - if (i > 0){ - throw new ServiceException("客户编码已存在"); + int i = customerInfoMapper.selectCountByCode(customerInfo); + if (i > 0) { + throw new ServiceException("客户编码已存在"); + } + return customerInfoMapper.insertCustomerInfo(customerInfo); + } finally { + lock.unlock(); } - return customerInfoMapper.insertCustomerInfo(customerInfo); + } private String generateCode(String province) { @@ -120,16 +130,23 @@ public class CustomerInfoServiceImpl implements ICustomerInfoService // 如果省份发生变化,需要重新生成客户编码 CustomerInfo oldCustomerInfo = customerInfoMapper.selectCustomerInfoById(customerInfo.getId()); if (oldCustomerInfo != null && !oldCustomerInfo.getProvince().equals(customerInfo.getProvince())) { - String newCustomerCode = generateCode(customerInfo.getProvince()); - customerInfo.setCustomerCode(newCustomerCode); + lock.lock(); + try { + String newCustomerCode = generateCode(customerInfo.getProvince()); + customerInfo.setCustomerCode(newCustomerCode); - // 检查新生成的编码是否已存在 - CustomerInfo codeCheckParam = new CustomerInfo(); - codeCheckParam.setCustomerCode(newCustomerCode); - int count = customerInfoMapper.selectCountByCode(codeCheckParam); - if (count > 0) { - throw new ServiceException("客户编码已存在"); + // 检查新生成的编码是否已存在 + CustomerInfo codeCheckParam = new CustomerInfo(); + codeCheckParam.setCustomerCode(newCustomerCode); + int count = customerInfoMapper.selectCountByCode(codeCheckParam); + if (count > 0) { + throw new ServiceException("客户编码已存在"); + } + return customerInfoMapper.updateCustomerInfo(customerInfo); + } finally { + lock.unlock(); } + } return customerInfoMapper.updateCustomerInfo(customerInfo); diff --git a/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/PartnerInfoServiceImpl.java b/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/PartnerInfoServiceImpl.java index d44b48d7..c9b1ac32 100644 --- a/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/PartnerInfoServiceImpl.java +++ b/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/PartnerInfoServiceImpl.java @@ -2,6 +2,7 @@ package com.ruoyi.sip.service.impl; import java.util.Collections; import java.util.List; +import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -21,20 +22,23 @@ import com.ruoyi.common.core.text.Convert; /** * 供应商管理Service业务层处理 - * + * * @author mula * @date 2025-04-30 */ @Service -public class PartnerInfoServiceImpl implements IPartnerInfoService +public class PartnerInfoServiceImpl implements IPartnerInfoService { @Autowired private PartnerInfoMapper partnerInfoMapper; @Autowired private ICnareaService cnareaService; + + // 添加静态锁,确保生成编码的操作是线程安全的 + private static final Lock lock = new ReentrantLock(); /** * 查询供应商管理 - * + * * @param id 供应商管理主键 * @return 供应商管理 */ @@ -54,7 +58,7 @@ public class PartnerInfoServiceImpl implements IPartnerInfoService /** * 查询供应商管理列表 - * + * * @param partnerInfo 供应商管理 * @return 供应商管理 */ @@ -66,7 +70,7 @@ public class PartnerInfoServiceImpl implements IPartnerInfoService /** * 新增供应商管理 - * + * * @param partnerInfo 供应商管理 * @return 结果 */ @@ -74,14 +78,20 @@ public class PartnerInfoServiceImpl implements IPartnerInfoService public int insertPartnerInfo(PartnerInfo partnerInfo) { partnerInfo.setCreateBy(ShiroUtils.getUserId().toString()); - //生成编码 - partnerInfo.setPartnerCode(generateCode(partnerInfo.getProvince())); + // 生成编码时加锁,确保线程安全 + lock.lock(); + try { + //生成编码 + partnerInfo.setPartnerCode(generateCode(partnerInfo.getProvince())); - int i = partnerInfoMapper.selectCountByCode(partnerInfo); - if (i>0){ - throw new ServiceException("该编码已存在"); + int i = partnerInfoMapper.selectCountByCode(partnerInfo); + if (i > 0) { + throw new ServiceException("该编码已存在"); + } + return partnerInfoMapper.insertPartnerInfo(partnerInfo); + } finally { + lock.unlock(); } - return partnerInfoMapper.insertPartnerInfo(partnerInfo); } private String generateCode(String province) { @@ -109,7 +119,7 @@ public class PartnerInfoServiceImpl implements IPartnerInfoService /** * 修改供应商管理 - * + * * @param partnerInfo 供应商管理 * @return 结果 */ @@ -120,12 +130,20 @@ public class PartnerInfoServiceImpl implements IPartnerInfoService // 如果省份发生变化,需要重新生成客户编码 PartnerInfo existsInfo = partnerInfoMapper.selectPartnerInfoById(partnerInfo.getId()); if (existsInfo != null && !existsInfo.getProvince().equals(partnerInfo.getProvince())) { - String newPartnerCode = generateCode(partnerInfo.getProvince()); - partnerInfo.setPartnerCode(newPartnerCode); - } - int i = partnerInfoMapper.selectCountByCode(partnerInfo); - if (i>0){ - throw new ServiceException("该编码已存在"); + // 重新生成编码时加锁,确保线程安全 + lock.lock(); + try { + String newPartnerCode = generateCode(partnerInfo.getProvince()); + partnerInfo.setPartnerCode(newPartnerCode); + + int i = partnerInfoMapper.selectCountByCode(partnerInfo); + if (i > 0) { + throw new ServiceException("该编码已存在"); + } + return partnerInfoMapper.updatePartnerInfo(partnerInfo); + } finally { + lock.unlock(); + } } return partnerInfoMapper.updatePartnerInfo(partnerInfo); } diff --git a/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/VendorInfoServiceImpl.java b/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/VendorInfoServiceImpl.java index 716fc930..eb9df985 100644 --- a/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/VendorInfoServiceImpl.java +++ b/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/VendorInfoServiceImpl.java @@ -1,6 +1,9 @@ package com.ruoyi.sip.service.impl; import java.util.*; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; import cn.hutool.core.collection.CollUtil; @@ -32,7 +35,8 @@ public class VendorInfoServiceImpl implements IVendorInfoService { private VendorInfoMapper vendorInfoMapper; @Autowired private IOmsWarehouseInfoService warehouseInfoService; - + // 添加静态锁,确保生成编码的操作是线程安全的 + private static final Lock lock = new ReentrantLock(); /** * 查询制造商信息 * @@ -63,11 +67,20 @@ public class VendorInfoServiceImpl implements IVendorInfoService { */ @Override public int insertVendorInfo(VendorInfo vendorInfo) { - checkUnqCode(vendorInfo); - vendorInfo.setVendorStatus(VendorInfo.VendorStatusEnum.NORMAL.getCode()); - vendorInfo.setCreateTime(DateUtils.getNowDate()); - vendorInfo.setCreateBy(ShiroUtils.getUserId().toString()); - return vendorInfoMapper.insertVendorInfo(vendorInfo); + lock.lock(); + try { + + checkUnqCode(vendorInfo); + vendorInfo.setVendorStatus(VendorInfo.VendorStatusEnum.NORMAL.getCode()); + vendorInfo.setCreateTime(DateUtils.getNowDate()); + vendorInfo.setCreateBy(ShiroUtils.getUserId().toString()); + return vendorInfoMapper.insertVendorInfo(vendorInfo); + } catch (Exception e) { + throw new ServiceException("保存超时,请重试"); + } finally { + lock.unlock(); + } + } private void checkUnqCode(VendorInfo vendorInfo) { @@ -91,10 +104,17 @@ public class VendorInfoServiceImpl implements IVendorInfoService { */ @Override public int updateVendorInfo(VendorInfo vendorInfo) { - checkUnqCode(vendorInfo); - vendorInfo.setUpdateTime(DateUtils.getNowDate()); - vendorInfo.setUpdateBy(ShiroUtils.getUserId().toString()); - return vendorInfoMapper.updateVendorInfo(vendorInfo); + lock.lock(); + try { + checkUnqCode(vendorInfo); + vendorInfo.setUpdateTime(DateUtils.getNowDate()); + vendorInfo.setUpdateBy(ShiroUtils.getUserId().toString()); + return vendorInfoMapper.updateVendorInfo(vendorInfo); + } catch (Exception e) { + throw new ServiceException("保存超时,请重试"); + } finally { + lock.unlock(); + } } /** diff --git a/ruoyi-sip/src/main/resources/mapper/sip/ProjectOrderInfoMapper.xml b/ruoyi-sip/src/main/resources/mapper/sip/ProjectOrderInfoMapper.xml index 7ea9f571..581b0d8c 100644 --- a/ruoyi-sip/src/main/resources/mapper/sip/ProjectOrderInfoMapper.xml +++ b/ruoyi-sip/src/main/resources/mapper/sip/ProjectOrderInfoMapper.xml @@ -537,7 +537,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - delete from project_order_info where id in + delete from project_order_info where id in #{id}