zhangfeng
2022-11-25 f600f38c6c23a282b61ed4db1b2da094d695276f
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,10 +48,15 @@
    private SafeMaterialClassifyInfoService safeMaterialClassifyInfoService;
    @Autowired
    private SafeMaterialDetailInfoService safeMaterialDetailInfoService;
    @Autowired
    private RedissonClient redissonClient;
    @Override
    public ResultVO save(SafeMaterialAddReq req) {
        ResultVO resultVO = null;
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_SM_INSERT");
        try {
            lock.lock(10, TimeUnit.SECONDS);
        //获取部门信息
        DepInfoRPCRespDTO depInfo = getDepInfoByDepId(req.getDepId());
        if(null == ConsumableEnum.getByCode(req.getConsumable())){
@@ -79,12 +86,28 @@
        //插入
        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;
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_SM_UPDATE");
        try {
            lock.lock(10, TimeUnit.SECONDS);
        //获取部门信息
        DepInfoRPCRespDTO depInfo = getDepInfoByDepId(req.getDepId());
@@ -109,7 +132,19 @@
        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();
            }
        }
        return new ResultVO(ResultCodes.OK);
    }
@@ -136,6 +171,10 @@
    public ResultVO delete(Long id) {
        ResultVO resultVO = null;
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_SM_DELETE");
        try {
            lock.lock(10, TimeUnit.SECONDS);
        //删除之前检查详表中是否还有该物物资数据
        int count = safeMaterialDetailInfoService.getCountBySmId(id);
        if(count > 0){
@@ -150,6 +189,19 @@
        }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,6 +210,10 @@
        if(ids.length == 0){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
        }
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_SM_DELETEBATCH");
        try {
            lock.lock(10, TimeUnit.SECONDS);
        List<Long> idList = Arrays.asList(ids);
        int count = safeMaterialDetailInfoService.getCountBySmIds(idList);
        //判断是否绑定具体安全物资数据
@@ -165,6 +221,19 @@
            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();
            }
        }
        return new ResultVO(ResultCodes.OK);
    }