From f600f38c6c23a282b61ed4db1b2da094d695276f Mon Sep 17 00:00:00 2001
From: zhangfeng <1603559716@qq.com>
Date: 星期五, 25 十一月 2022 16:43:52 +0800
Subject: [PATCH] 安全物资和设备管理调整

---
 equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/service/impl/SafeMaterialServiceImpl.java |  215 +++++++++++++++++++++++++++++++++++------------------
 1 files changed, 142 insertions(+), 73 deletions(-)

diff --git a/equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/service/impl/SafeMaterialServiceImpl.java b/equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/service/impl/SafeMaterialServiceImpl.java
index c3eeab9..4b9107e 100644
--- a/equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/service/impl/SafeMaterialServiceImpl.java
+++ b/equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/service/impl/SafeMaterialServiceImpl.java
@@ -5,6 +5,7 @@
 import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepInfoRPCRespDTO;
 import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepRPCRespDTO;
 import com.gkhy.safePlatform.commons.enums.ResultCodes;
+import com.gkhy.safePlatform.commons.exception.BusinessException;
 import com.gkhy.safePlatform.commons.query.PageQuery;
 import com.gkhy.safePlatform.commons.vo.ResultVO;
 import com.gkhy.safePlatform.commons.vo.SearchResultVO;
@@ -19,21 +20,22 @@
 import com.gkhy.safePlatform.equipment.model.dto.resp.MaterialClassificationDto;
 import com.gkhy.safePlatform.equipment.model.dto.resp.MaterialDepartmentDto;
 import com.gkhy.safePlatform.equipment.model.dto.resp.SafeMaterialDto;
-import com.gkhy.safePlatform.equipment.repository.SafeMaterialDetailInfoRepository;
 import com.gkhy.safePlatform.equipment.service.SafeMaterialService;
 import com.gkhy.safePlatform.equipment.service.baseService.SafeMaterialClassifyInfoService;
 import com.gkhy.safePlatform.equipment.service.baseService.SafeMaterialDetailInfoService;
 import com.gkhy.safePlatform.equipment.service.baseService.SafeMaterialInfoService;
 import org.apache.dubbo.config.annotation.DubboReference;
+import org.redisson.api.RLock;
+import org.redisson.api.RedissonClient;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.security.core.parameters.P;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 @Service("SafeMaterialService")
@@ -46,70 +48,103 @@
     private SafeMaterialClassifyInfoService safeMaterialClassifyInfoService;
     @Autowired
     private SafeMaterialDetailInfoService safeMaterialDetailInfoService;
+    @Autowired
+    private RedissonClient redissonClient;
 
     @Override
     public ResultVO save(SafeMaterialAddReq req) {
-        ResultVO resultVO = null;
-        //获取部门信息
-        DepInfoRPCRespDTO depInfo = getDepInfoByDepId(req.getDepId());
-        if(null == ConsumableEnum.getByCode(req.getConsumable())){
-            throw new EquipmentException(ResultCodes.CLIENT_PARAM_ILLEGAL,"耗材类型不合法!");
-        }
-        //获取物资类型
-        SafeMaterialClassifyInfo classifyInfo = safeMaterialClassifyInfoService.queryById(req.getSmallClassifyId());
-        if(null == classifyInfo){
-            throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST,"物资类型不存在!");
-        }
-        //判断该部门是否已经创建物资
-        boolean flag = safeMaterialInfoService.checkMaterial(req.getSmallClassifyId(), req.getDepId(), null);
-        if(flag){
-            throw new EquipmentException(EquipmentResultCodes.DATA_EXIST,"该种物资已存在不可重复创建");
-        }
+        //加分布式锁,防止重复创建规则
+        RLock lock = redissonClient.getLock("LOCK_SM_INSERT");
+        try {
+            lock.lock(10, TimeUnit.SECONDS);
+            //获取部门信息
+            DepInfoRPCRespDTO depInfo = getDepInfoByDepId(req.getDepId());
+            if(null == ConsumableEnum.getByCode(req.getConsumable())){
+                throw new EquipmentException(ResultCodes.CLIENT_PARAM_ILLEGAL,"耗材类型不合法!");
+            }
+            //获取物资类型
+            SafeMaterialClassifyInfo classifyInfo = safeMaterialClassifyInfoService.queryById(req.getSmallClassifyId());
+            if(null == classifyInfo){
+                throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST,"物资类型不存在!");
+            }
+            //判断该部门是否已经创建物资
+            boolean flag = safeMaterialInfoService.checkMaterial(req.getSmallClassifyId(), req.getDepId(), null);
+            if(flag){
+                throw new EquipmentException(EquipmentResultCodes.DATA_EXIST,"该种物资已存在不可重复创建");
+            }
 
-        //获取数量
-        int safeMaterialTotalCount = safeMaterialInfoService.getTotalCount();
-        SafeMaterialInfo safeMaterialInfo = new SafeMaterialInfo();
-        safeMaterialInfo.setConsumable(req.getConsumable());
-        safeMaterialInfo.setDepId(req.getDepId());
-        safeMaterialInfo.setSmallClassifyId(req.getSmallClassifyId());
-        safeMaterialInfo.setMaterialName(classifyInfo.getMaterialClassifyName());
-        safeMaterialInfo.setDepName(depInfo.getDepName());
-        safeMaterialInfo.setSerialNum(this.generateSerialNum(safeMaterialTotalCount));
-        safeMaterialInfo.setBigClassifyId(req.getBigClassifyId());
-        //插入
-        safeMaterialInfoService.save(safeMaterialInfo);
+            //获取数量
+            int safeMaterialTotalCount = safeMaterialInfoService.getTotalCount();
+            SafeMaterialInfo safeMaterialInfo = new SafeMaterialInfo();
+            safeMaterialInfo.setConsumable(req.getConsumable());
+            safeMaterialInfo.setDepId(req.getDepId());
+            safeMaterialInfo.setSmallClassifyId(req.getSmallClassifyId());
+            safeMaterialInfo.setMaterialName(classifyInfo.getMaterialClassifyName());
+            safeMaterialInfo.setDepName(depInfo.getDepName());
+            safeMaterialInfo.setSerialNum(this.generateSerialNum(safeMaterialTotalCount));
+            safeMaterialInfo.setBigClassifyId(req.getBigClassifyId());
+            //插入
+            safeMaterialInfoService.save(safeMaterialInfo);
 
+            //创建成功,释放锁
+            lock.unlock();
+        }catch (EquipmentException e) {
+            e.printStackTrace();
+            throw new EquipmentException(e.getCode(), e.getMessage());
+        }catch (Exception e) {
+            e.printStackTrace();
+            throw new BusinessException(ResultCodes.SERVER_ERROR);
+        }finally {
+            if(lock.isLocked()){
+                lock.unlock();
+            }
+        }
         return new ResultVO(ResultCodes.OK);
     }
 
     @Override
     public ResultVO update(SafeMaterialModReq req) {
-        ResultVO resultVO = null;
-        //获取部门信息
-        DepInfoRPCRespDTO depInfo = getDepInfoByDepId(req.getDepId());
+        //加分布式锁,防止重复创建规则
+        RLock lock = redissonClient.getLock("LOCK_SM_UPDATE");
+        try {
+            lock.lock(10, TimeUnit.SECONDS);
+            //获取部门信息
+            DepInfoRPCRespDTO depInfo = getDepInfoByDepId(req.getDepId());
 
-        if(null == ConsumableEnum.getByCode(req.getConsumable())){
-            throw new EquipmentException(ResultCodes.CLIENT_PARAM_ILLEGAL,"耗材类型不合法!");
+            if(null == ConsumableEnum.getByCode(req.getConsumable())){
+                throw new EquipmentException(ResultCodes.CLIENT_PARAM_ILLEGAL,"耗材类型不合法!");
+            }
+            //获取物资类型
+            SafeMaterialClassifyInfo classifyInfo = safeMaterialClassifyInfoService.queryById(req.getSmallClassifyId());
+            if(null == classifyInfo){
+                throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST,"物资类型不存在!");
+            }
+            boolean flag = safeMaterialInfoService.checkMaterial(req.getSmallClassifyId(), req.getDepId(), req.getDepId());
+            if(flag){
+                throw new EquipmentException(EquipmentResultCodes.DATA_EXIST,"该种物资已存在不可重复创建");
+            }
+            SafeMaterialInfo safeMaterialInfo = new SafeMaterialInfo();
+            safeMaterialInfo.setConsumable(req.getConsumable());
+            safeMaterialInfo.setDepId(req.getDepId());
+            safeMaterialInfo.setSmallClassifyId(req.getSmallClassifyId());
+            safeMaterialInfo.setMaterialName(classifyInfo.getMaterialClassifyName());
+            safeMaterialInfo.setDepName(depInfo.getDepName());
+            safeMaterialInfo.setBigClassifyId(req.getBigClassifyId());
+            //跟新
+            safeMaterialInfoService.updateById(safeMaterialInfo);
+        //创建成功,释放锁
+            lock.unlock();
+        }catch (EquipmentException e) {
+            e.printStackTrace();
+            throw new EquipmentException(e.getCode(), e.getMessage());
+        }catch (Exception e) {
+            e.printStackTrace();
+            throw new BusinessException(ResultCodes.SERVER_ERROR);
+        }finally {
+            if(lock.isLocked()){
+                lock.unlock();
+            }
         }
-        //获取物资类型
-        SafeMaterialClassifyInfo classifyInfo = safeMaterialClassifyInfoService.queryById(req.getSmallClassifyId());
-        if(null == classifyInfo){
-            throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST,"物资类型不存在!");
-        }
-        boolean flag = safeMaterialInfoService.checkMaterial(req.getSmallClassifyId(), req.getDepId(), req.getDepId());
-        if(flag){
-            throw new EquipmentException(EquipmentResultCodes.DATA_EXIST,"该种物资已存在不可重复创建");
-        }
-        SafeMaterialInfo safeMaterialInfo = new SafeMaterialInfo();
-        safeMaterialInfo.setConsumable(req.getConsumable());
-        safeMaterialInfo.setDepId(req.getDepId());
-        safeMaterialInfo.setSmallClassifyId(req.getSmallClassifyId());
-        safeMaterialInfo.setMaterialName(classifyInfo.getMaterialClassifyName());
-        safeMaterialInfo.setDepName(depInfo.getDepName());
-        safeMaterialInfo.setBigClassifyId(req.getBigClassifyId());
-        //跟新
-        safeMaterialInfoService.updateById(safeMaterialInfo);
-
         return new ResultVO(ResultCodes.OK);
     }
 
@@ -136,19 +171,36 @@
 
     public ResultVO delete(Long id) {
         ResultVO resultVO = null;
-        //删除之前检查详表中是否还有该物物资数据
-        int count = safeMaterialDetailInfoService.getCountBySmId(id);
-        if(count > 0){
-            throw new EquipmentException(EquipmentResultCodes.DATA_HAS_BEEN_BOND,"该种物资已被绑定物资详情数据,不可删除!");
-        }
-        SafeMaterialInfo materialInfo = new SafeMaterialInfo();
-        materialInfo.setId(id);
-        materialInfo.setDelFlag(1);
-        boolean flag = safeMaterialInfoService.updateById(materialInfo);
-        if(flag){
-            resultVO = new ResultVO(ResultCodes.OK);
-        }else{
-            resultVO = new ResultVO(ResultCodes.SERVER_DEL_ERROR);
+        //加分布式锁,防止重复创建规则
+        RLock lock = redissonClient.getLock("LOCK_SM_DELETE");
+        try {
+            lock.lock(10, TimeUnit.SECONDS);
+            //删除之前检查详表中是否还有该物物资数据
+            int count = safeMaterialDetailInfoService.getCountBySmId(id);
+            if(count > 0){
+                throw new EquipmentException(EquipmentResultCodes.DATA_HAS_BEEN_BOND,"该种物资已被绑定物资详情数据,不可删除!");
+            }
+            SafeMaterialInfo materialInfo = new SafeMaterialInfo();
+            materialInfo.setId(id);
+            materialInfo.setDelFlag(1);
+            boolean flag = safeMaterialInfoService.updateById(materialInfo);
+            if(flag){
+                resultVO = new ResultVO(ResultCodes.OK);
+            }else{
+                resultVO = new ResultVO(ResultCodes.SERVER_DEL_ERROR);
+            }
+            //创建成功,释放锁
+            lock.unlock();
+        }catch (EquipmentException e) {
+            e.printStackTrace();
+            throw new EquipmentException(e.getCode(), e.getMessage());
+        }catch (Exception e) {
+            e.printStackTrace();
+            throw new BusinessException(ResultCodes.SERVER_ERROR);
+        }finally {
+            if(lock.isLocked()){
+                lock.unlock();
+            }
         }
         return resultVO;
     }
@@ -158,13 +210,30 @@
         if(ids.length == 0){
             throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
         }
-        List<Long> idList = Arrays.asList(ids);
-        int count = safeMaterialDetailInfoService.getCountBySmIds(idList);
-        //判断是否绑定具体安全物资数据
-        if(count > 0){
-            throw new EquipmentException(EquipmentResultCodes.DATA_HAS_BEEN_BOND,"物资已被绑定物资详情数据,不可删除!");
+        //加分布式锁,防止重复创建规则
+        RLock lock = redissonClient.getLock("LOCK_SM_DELETEBATCH");
+        try {
+            lock.lock(10, TimeUnit.SECONDS);
+            List<Long> idList = Arrays.asList(ids);
+            int count = safeMaterialDetailInfoService.getCountBySmIds(idList);
+            //判断是否绑定具体安全物资数据
+            if(count > 0){
+                throw new EquipmentException(EquipmentResultCodes.DATA_HAS_BEEN_BOND,"物资已被绑定物资详情数据,不可删除!");
+            }
+            safeMaterialInfoService.deleteBatch(idList);
+            //创建成功,释放锁
+            lock.unlock();
+        }catch (EquipmentException e) {
+            e.printStackTrace();
+            throw new EquipmentException(e.getCode(), e.getMessage());
+        }catch (Exception e) {
+            e.printStackTrace();
+            throw new BusinessException(ResultCodes.SERVER_ERROR);
+        }finally {
+            if(lock.isLocked()){
+                lock.unlock();
+            }
         }
-        safeMaterialInfoService.deleteBatch(idList);
         return new ResultVO(ResultCodes.OK);
     }
 

--
Gitblit v1.9.2