From 3a1b824203d9e6a4c2a55e2d0d8adc0bae4f03d0 Mon Sep 17 00:00:00 2001
From: zf <1603559716@qq.com>
Date: 星期一, 25 三月 2024 13:11:32 +0800
Subject: [PATCH] 安全物资和设备管理调整v2

---
 equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/service/impl/SafeMaterialServiceImpl.java |  313 +++++++++++++++++++++++++++++++++-------------------
 1 files changed, 198 insertions(+), 115 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..5ee82e9 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
@@ -1,10 +1,13 @@
 package com.gkhy.safePlatform.equipment.service.impl;
 
+import cn.hutool.core.util.IdUtil;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService;
 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.co.ContextCacheUser;
 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;
@@ -12,6 +15,7 @@
 import com.gkhy.safePlatform.equipment.enums.ConsumableEnum;
 import com.gkhy.safePlatform.equipment.enums.EquipmentResultCodes;
 import com.gkhy.safePlatform.equipment.excepiton.EquipmentException;
+import com.gkhy.safePlatform.equipment.model.dto.req.ParamForm;
 import com.gkhy.safePlatform.equipment.model.dto.req.SafeMaterialAddReq;
 import com.gkhy.safePlatform.equipment.model.dto.req.SafeMaterialModReq;
 import com.gkhy.safePlatform.equipment.model.dto.req.SafeMaterialQuery;
@@ -19,21 +23,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.apache.ibatis.annotations.Param;
+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,145 +51,237 @@
     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,"该种物资已存在不可重复创建");
-        }
+    public ResultVO save(ContextCacheUser currentUser,SafeMaterialAddReq req) {
+        //加分布式锁,防止重复创建规则
+        RLock lock = redissonClient.getLock("LOCK_SM_INSERT");
+        try {
+            lock.lock(10, TimeUnit.SECONDS);
+            //获取物资类型
+            SafeMaterialClassifyInfo classifyInfo = safeMaterialClassifyInfoService.queryById(req.getSmallClassifyId());
+            if(null == classifyInfo){
+                throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST,"物资类型不存在!");
+            }
+            //判断该部门是否已经创建物资
+            boolean flag = safeMaterialInfoService.checkMaterial(req.getSmallClassifyId(), currentUser.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.setId(IdUtil.getSnowflake(0,0).nextId());
+            safeMaterialInfo.setConsumable(classifyInfo.getConsumable());
+            safeMaterialInfo.setDepId(currentUser.getDepId());
+            safeMaterialInfo.setSmallClassifyId(req.getSmallClassifyId());
+            safeMaterialInfo.setMaterialName(classifyInfo.getMaterialClassifyName());
+            safeMaterialInfo.setDepName(currentUser.getDepName());
+            safeMaterialInfo.setSerialNum(this.generateSerialNum(safeMaterialTotalCount));
+            safeMaterialInfo.setBigClassifyId(req.getBigClassifyId());
+            safeMaterialInfo.setTotalCount(0);
+            safeMaterialInfo.setStockCount(0);
+            //插入
+            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());
+    public ResultVO update(ContextCacheUser currentUser,SafeMaterialModReq req) {
+        //加分布式锁,防止重复创建规则
+        RLock lock = redissonClient.getLock("LOCK_SM_UPDATE");
+        try {
+            lock.lock(10, TimeUnit.SECONDS);
 
-        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);
 
+            //获取物资类型
+            SafeMaterialClassifyInfo classifyInfo = safeMaterialClassifyInfoService.queryById(req.getSmallClassifyId());
+            if(null == classifyInfo){
+                throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST,"物资类型不存在!");
+            }
+            boolean flag = safeMaterialInfoService.checkMaterial(req.getSmallClassifyId(), currentUser.getDepId(), req.getId());
+            if(flag){
+                throw new EquipmentException(EquipmentResultCodes.DATA_EXIST,"该种物资已存在不可重复创建");
+            }
+            SafeMaterialInfo safeMaterialInfo = new SafeMaterialInfo();
+            safeMaterialInfo.setConsumable(classifyInfo.getConsumable());
+            safeMaterialInfo.setSmallClassifyId(req.getSmallClassifyId());
+            safeMaterialInfo.setMaterialName(classifyInfo.getMaterialClassifyName());
+            safeMaterialInfo.setDepName(currentUser.getDepName());
+            safeMaterialInfo.setBigClassifyId(req.getBigClassifyId());
+            safeMaterialInfo.setId(req.getId());
+            //跟新
+            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();
+            }
+        }
         return new ResultVO(ResultCodes.OK);
     }
 
     @Override
-    public ResultVO queryById(Long id) {
+    public ResultVO queryById(ContextCacheUser currentUser,Long id) {
         if(null == id){
             throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
         }
-        SafeMaterialInfo safeMaterialInfo = safeMaterialInfoService.queryById(id);
+        SafeMaterialDO safeMaterialDO = safeMaterialInfoService.queryById(id);
         SafeMaterialDto safeMaterialDto = new SafeMaterialDto();
-        if(null != safeMaterialInfo){
-            SafeMaterialClassifyDO classifyDO = safeMaterialClassifyInfoService.getBigAndSmallClassify(safeMaterialInfo.getSmallClassifyId());
-            Integer validStockCount = safeMaterialDetailInfoService.getValidStockCount(safeMaterialInfo.getId());
-            BeanUtils.copyProperties(safeMaterialInfo,safeMaterialDto);
-            safeMaterialDto.setConsumableName(ConsumableEnum.getByCode(safeMaterialInfo.getConsumable()).getValue());
-            if(null != classifyDO){
-                safeMaterialDto.setBigClassifyName(classifyDO.getBigClassifyName());
+        if(null != safeMaterialDO){
+            BeanUtils.copyProperties(safeMaterialDO,safeMaterialDto);
+            if(null != ConsumableEnum.getByCode(safeMaterialDO.getConsumable())){
+                safeMaterialDto.setConsumableName(ConsumableEnum.getByCode(safeMaterialDO.getConsumable()).getValue());
             }
-            safeMaterialDto.setValidStockCount(validStockCount);
-
         }
         return new ResultVO(ResultCodes.OK,safeMaterialDto);
     }
 
-    public ResultVO delete(Long id) {
+    public ResultVO delete(ContextCacheUser currentUser,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;
     }
 
     @Override
-    public ResultVO deleteBatch(Long[] ids) {
-        if(ids.length == 0){
-            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
+    public ResultVO deleteBatch(ContextCacheUser currentUser, ParamForm paramForm) {
+
+        //加分布式锁,防止重复创建规则
+        RLock lock = redissonClient.getLock("LOCK_SM_DELETEBATCH");
+        try {
+            lock.lock(10, TimeUnit.SECONDS);
+
+            int count = safeMaterialDetailInfoService.getCountBySmIds(paramForm.getIds());
+            //判断是否绑定具体安全物资数据
+            if(count > 0){
+                throw new EquipmentException(EquipmentResultCodes.DATA_HAS_BEEN_BOND,"物资已被绑定物资详情数据,不可删除!");
+            }
+            safeMaterialInfoService.deleteBatch(paramForm.getIds());
+            //创建成功,释放锁
+            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();
+            }
         }
-        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);
         return new ResultVO(ResultCodes.OK);
     }
+
+
 
     /**
      * 获取列表
      * @return
      */
     @Override
-    public ResultVO list() {
+    public List<MaterialClassificationDto> listByDepId(ContextCacheUser currentUser) {
+        //获取所有数据
+        List<SafeMaterialInfo> materialInfoList = safeMaterialInfoService.listByDepId(currentUser.getDepId());
+        //获取所有物资类型
+        List<SafeMaterialClassifyInfo> classifyInfoList = safeMaterialClassifyInfoService.getParentList();
+
+        List<MaterialClassificationDto> classificationDtoList = new ArrayList<>();
+        for (SafeMaterialClassifyInfo classifyInfo:classifyInfoList) {
+            MaterialClassificationDto classificationDto = new MaterialClassificationDto();
+            classificationDto.setMaterialClassifyName(classifyInfo.getMaterialClassifyName());
+            classificationDto.setMaterialClassifyId(classifyInfo.getId());
+            //过滤出物资数据
+            List<SafeMaterialInfo> selectMaterialList = materialInfoList
+                    .stream()
+                    .filter(item -> classifyInfo.getId().equals(item.getBigClassifyId()))
+                    .collect(Collectors.toList());
+            List<BaseMaterialDto> baseMaterialDtoList = new ArrayList<>();
+            if(selectMaterialList.size()>0){
+                //循环物资
+                for (SafeMaterialInfo materialInfo:selectMaterialList){
+                    //填充基础物资数据
+                    BaseMaterialDto baseMaterialDto = new BaseMaterialDto();
+                    baseMaterialDto.setMaterialName(materialInfo.getMaterialName());
+                    baseMaterialDto.setSmId(materialInfo.getId());
+                    baseMaterialDto.setMaterialName(materialInfo.getMaterialName());
+                    baseMaterialDto.setConsumable(materialInfo.getConsumable());
+                    baseMaterialDto.setConsumableName(ConsumableEnum.getByCode(materialInfo.getConsumable()).getValue());
+                    baseMaterialDtoList.add(baseMaterialDto);
+                }
+                classificationDto.setBaseMaterialList(baseMaterialDtoList);
+                classificationDtoList.add(classificationDto);
+            }
+        }
+        return classificationDtoList;
+    }
+    @Override
+    public List<MaterialDepartmentDto> list(ContextCacheUser currentUser) {
         //获取所有数据
         List<SafeMaterialInfo> materialInfoList = safeMaterialInfoService.list();
         //获取所有部门
         List<DepRPCRespDTO> depInfoList = getDepInfoList();
         //获取所有物资类型
-        List<SafeMaterialClassifyInfo> classifyInfoList = safeMaterialClassifyInfoService.getList();
+        List<SafeMaterialClassifyInfo> classifyInfoList = safeMaterialClassifyInfoService.getParentList();
         //循环部门
         List<MaterialDepartmentDto> departmentDtoList = new ArrayList<>();
         recursiveDep(depInfoList,departmentDtoList,materialInfoList,classifyInfoList);
 
-        return new ResultVO(ResultCodes.OK,departmentDtoList);
+        return departmentDtoList;
     }
     private void recursiveDep(List<DepRPCRespDTO> depInfoList,List<MaterialDepartmentDto> departmentDtoList,List<SafeMaterialInfo> materialInfoList,List<SafeMaterialClassifyInfo> classifyInfoList){
         for (DepRPCRespDTO dep:depInfoList){
@@ -226,31 +323,17 @@
         }
     }
     @Override
-    public SearchResultVO<List<SafeMaterialDto>> listByPage(PageQuery<SafeMaterialQuery> pageQuery) {
+    public SearchResultVO<List<SafeMaterialDto>> listByPage(ContextCacheUser currentUser,PageQuery<SafeMaterialQuery> pageQuery) {
         Page<SafeMaterialDO> page= new Page(pageQuery.getPageIndex(),pageQuery.getPageSize());
-        List<SafeMaterialDO> materialInfoList = safeMaterialInfoService.listByPage(page, pageQuery.getSearchParams());
-        List<Long> materialIdList = new ArrayList<>();
-        List<SafeMaterialDetailCountDO> statisticsList = new ArrayList<>();
-        if(materialInfoList.size()>0){
-            materialIdList = materialInfoList.stream().map(SafeMaterialDO::getId).collect(Collectors.toList());
-        }
-        if(materialIdList.size()>0){
-           statisticsList = safeMaterialDetailInfoService.getStatisticsValidStock(materialIdList);
-        }
+        SafeMaterialQuery query = pageQuery.getSearchParams();
+        query.setDepId(currentUser.getDepId());
+        List<SafeMaterialDO> materialInfoList = safeMaterialInfoService.listByPage(page, query);
         List<SafeMaterialDto> materialDtoList = new ArrayList<>();
         for (SafeMaterialDO materialDO:materialInfoList){
             SafeMaterialDto materialDto = new SafeMaterialDto();
             BeanUtils.copyProperties(materialDO,materialDto);
-            materialDto.setConsumableName(ConsumableEnum.getByCode(materialDO.getConsumable()).getValue());
-            //过滤出当前物资详细
-            List<SafeMaterialDetailCountDO> selectList = statisticsList
-                    .stream()
-                    .filter(item -> item.getSmId().equals(materialDO.getId()))
-                    .collect(Collectors.toList());
-            if(selectList.size()>0){
-                materialDto.setValidStockCount(selectList.get(0).getCount());
-            }else{
-                materialDto.setValidStockCount(0);
+            if(null != ConsumableEnum.getByCode(materialDO.getConsumable())){
+                materialDto.setConsumableName(ConsumableEnum.getByCode(materialDO.getConsumable()).getValue());
             }
             materialDtoList.add(materialDto);
         }

--
Gitblit v1.9.2