zhangfeng
2022-11-25 f600f38c6c23a282b61ed4db1b2da094d695276f
equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/service/impl/SafeMaterialDetailServiceImpl.java
@@ -5,6 +5,7 @@
import com.gkhy.safePlatform.account.rpc.apimodel.AccountUserService;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.UserInfoRPCRespDTO;
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.utils.StringUtils;
import com.gkhy.safePlatform.commons.vo.ResultVO;
@@ -24,6 +25,8 @@
import com.gkhy.safePlatform.equipment.service.baseService.SafeMaterialInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
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.beans.factory.annotation.Value;
@@ -35,6 +38,7 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Service("SafeMaterialDetailService")
@@ -56,6 +60,8 @@
    @Autowired
    private RocketMQTemplateHelper rocketMQTemplateHelper;
    @Autowired
    private RedissonClient redissonClient;
    @Override
    public ResultVO save(SafeMaterialDetailAddReq req) {
        //判断物资是否存在
@@ -131,6 +137,10 @@
            }
        }
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_SMD_SAVE");
        try {
            lock.lock(10, TimeUnit.SECONDS);
        //批量入库
        safeMaterialDetailInfoService.saveBatch(list);
@@ -143,6 +153,20 @@
                rocketMQTemplateHelper.syncSend(safeMaterialTopic,safeMaterialMsg);
            }
        }
            //创建成功,释放锁
            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);
    }
@@ -187,6 +211,10 @@
            materialDetailInfo.setMaterialNo(generateMaterialNo(totalCount+i));
            list.add(materialDetailInfo);
        }
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_SMD_SAVEBATCH");
        try {
            lock.lock(10, TimeUnit.SECONDS);
        //批量入库
        safeMaterialDetailInfoService.saveBatch(list);
        //发送消息
@@ -197,6 +225,17 @@
                safeMaterialMsg.setId(materialInfo.getId());
                safeMaterialMsg.setValidTime(conversionTimeType(materialDetailInfo.getValidTime()));
                rocketMQTemplateHelper.syncSend(safeMaterialTopic,safeMaterialMsg);
                }
            }
        }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);
@@ -232,6 +271,10 @@
        safeMaterialDetailInfo.setSmId(req.getSmId());
        safeMaterialDetailInfo.setValidType(req.getValidType());
        safeMaterialDetailInfo.setValidTime(req.getValidTime());
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_SMD_UPDATE");
        try {
            lock.lock(10, TimeUnit.SECONDS);
        //修改
        safeMaterialDetailInfoService.updateById(safeMaterialDetailInfo);
        //如果是非常期
@@ -242,6 +285,17 @@
            safeMaterialMsg.setValidTime(conversionTimeType(req.getValidTime()));
            rocketMQTemplateHelper.syncSend(safeMaterialTopic,safeMaterialMsg);
        }
        }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);
    }
@@ -251,23 +305,33 @@
     * @return
     */
    @Override
    public ResultVO receiptBatch(Long[] ids) {
        if (ids.length == 0) {
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
        }
        List<Long> idList = new ArrayList<>();
        Collections.addAll(idList,ids);
    public ResultVO receiptBatch(ParamForm paramForm) {
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_SMD_RECEIPTBATCH");
        try {
            lock.lock(10, TimeUnit.SECONDS);
        //验证
        List<SafeMaterialDetailInfo> list = safeMaterialDetailInfoService.getListByIds(idList);
            List<SafeMaterialDetailInfo> list = safeMaterialDetailInfoService.getListByIds(paramForm.getIds());
        List<SafeMaterialDetailInfo> selectList = list
                .stream()
                .filter(item -> item.getIrStatus().equals(IssueReceiptEnum.OUT_OF_LIBRARY.getCode()) && item.getConsumable().equals(ConsumableEnum.YES.getCode()))
                .collect(Collectors.toList());
        if(selectList.size() != ids.length){
            if(selectList.size() != paramForm.getIds().size()){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_ILLEGAL,"已入库或耗材物资不可重复入库!");
        }
        //修改为入库状态
        safeMaterialDetailInfoService.updateReceiptStatusByIds(idList,IssueReceiptEnum.IN_THE_LIBRARY.getCode());
            safeMaterialDetailInfoService.updateReceiptStatusByIds(paramForm.getIds(),IssueReceiptEnum.IN_THE_LIBRARY.getCode());
        }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);
    }
@@ -277,11 +341,8 @@
     * @return
     */
    @Override
    public ResultVO deleteBatch(Long[] ids) {
        if (ids.length == 0) {
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
        }
        safeMaterialDetailInfoService.deleteBatch(ids);
    public ResultVO deleteBatch(ParamForm paramForm) {
        safeMaterialDetailInfoService.deleteBatch(paramForm.getIds());
        return new ResultVO(ResultCodes.OK);
    }
@@ -315,6 +376,10 @@
    public ResultVO deliveryBatchRandom(MterialRandomDeliveryReq req) {
        UserInfoRPCRespDTO userInfo = getUserInfo(req.getClaimantId());
        List<Long> idList = new ArrayList<>();
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_SMD_DELIVERYBATCHRANDOM");
        try {
            lock.lock(10, TimeUnit.SECONDS);
        if(StringUtils.isNotBlank(req.getRfid())){
            idList = safeMaterialDetailInfoService.getIdListByRfid(req.getCount(),req.getSmId(),req.getRfid());
@@ -335,6 +400,17 @@
        detailBO.setClaimantId(req.getClaimantId());
        detailBO.setIds(idList);
        safeMaterialDetailInfoService.updateDeliveryStatusByIds(detailBO);
        }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);
    }
@@ -501,6 +577,10 @@
     */
    @Override
    public ResultVO singleDelivery(SafeMaterialDetailReq req) {
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_SMD_SINGLEDELIVERY");
        try {
            lock.lock(10, TimeUnit.SECONDS);
        //获取该条数据
        SafeMaterialDetailInfo vo = safeMaterialDetailInfoService.queryById(req.getId());
        if(null == vo){
@@ -522,6 +602,17 @@
        materialDetailInfo.setDeliveryTime(LocalDateTime.now());
        //修改出入库状态为出库
        safeMaterialDetailInfoService.updateById(materialDetailInfo);
        }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);
    }
@@ -536,6 +627,10 @@
        if(null == id){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
        }
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_SMD_SINGLERECEIPT");
        try {
            lock.lock(10, TimeUnit.SECONDS);
        //获取该条数据
        SafeMaterialDetailInfo vo = safeMaterialDetailInfoService.queryById(id);
        if(null == vo){
@@ -550,6 +645,17 @@
        }
        //修改出入库状态为入库
        safeMaterialDetailInfoService.updateReceiptStatus(id,IssueReceiptEnum.IN_THE_LIBRARY.getCode());
        }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);
    }