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,64 +48,104 @@
    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());
        //加分布式锁,防止重复创建规则
        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,"该种物资已存在不可重复创建");
            }
        if(null == ConsumableEnum.getByCode(req.getConsumable())){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_ILLEGAL,"耗材类型不合法!");
        }
        //获取物资类型
        SafeMaterialClassifyInfo classifyInfo = safeMaterialClassifyInfoService.queryById(req.getMaterialClassifyId());
        if(null == classifyInfo){
            throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST,"物资类型不存在!");
        }
        //获取数量
        int safeMaterialTotalCount = safeMaterialInfoService.getTotalCount();
        SafeMaterialInfo safeMaterialInfo = new SafeMaterialInfo();
        BeanUtils.copyProperties(req,safeMaterialInfo);
        safeMaterialInfo.setDepName(depInfo.getDepName());
        safeMaterialInfo.setSerialNum(this.generateSerialNum(safeMaterialTotalCount));
        //插入
        boolean flag = safeMaterialInfoService.save(safeMaterialInfo);
        if(flag){
            resultVO = new ResultVO(ResultCodes.OK);
        }else {
            resultVO = new ResultVO(ResultCodes.SERVER_ADD_ERROR);
        }
            //获取数量
            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);
        return resultVO;
            //创建成功,释放锁
            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.getMaterialClassifyId());
        if(null == classifyInfo){
            throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST,"物资类型不存在!");
        }
        SafeMaterialInfo safeMaterialInfo = new SafeMaterialInfo();
        BeanUtils.copyProperties(req,safeMaterialInfo);
        safeMaterialInfo.setDepName(depInfo.getDepName());
        //跟新
        boolean flag = safeMaterialInfoService.updateById(safeMaterialInfo);
        if(flag){
            resultVO = new ResultVO(ResultCodes.OK);
        }else {
            resultVO = new ResultVO(ResultCodes.SERVER_UPDATE_ERROR);
        }
        return resultVO;
        return new ResultVO(ResultCodes.OK);
    }
    @Override
@@ -114,12 +156,12 @@
        SafeMaterialInfo safeMaterialInfo = safeMaterialInfoService.queryById(id);
        SafeMaterialDto safeMaterialDto = new SafeMaterialDto();
        if(null != safeMaterialInfo){
            SafeMaterialClassifyInfo classifyInfo = safeMaterialClassifyInfoService.queryById(safeMaterialInfo.getMaterialClassifyId());
            SafeMaterialClassifyDO classifyDO = safeMaterialClassifyInfoService.getBigAndSmallClassify(safeMaterialInfo.getSmallClassifyId());
            Integer validStockCount = safeMaterialDetailInfoService.getValidStockCount(safeMaterialInfo.getId());
            BeanUtils.copyProperties(safeMaterialInfo,safeMaterialDto);
            safeMaterialDto.setConsumable(ConsumableEnum.getByCode(safeMaterialInfo.getConsumable()).getCode());
            if(null != classifyInfo){
                safeMaterialDto.setMaterialClassifyName(classifyInfo.getMaterialClassifyName());
            safeMaterialDto.setConsumableName(ConsumableEnum.getByCode(safeMaterialInfo.getConsumable()).getValue());
            if(null != classifyDO){
                safeMaterialDto.setBigClassifyName(classifyDO.getBigClassifyName());
            }
            safeMaterialDto.setValidStockCount(validStockCount);
@@ -129,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;
    }
@@ -151,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);
    }
@@ -173,27 +249,29 @@
        List<DepRPCRespDTO> depInfoList = getDepInfoList();
        //获取所有物资类型
        List<SafeMaterialClassifyInfo> classifyInfoList = safeMaterialClassifyInfoService.getList();
        //循环物资分类
        List<MaterialClassificationDto> classificationDtoList = new ArrayList<>();
        for (SafeMaterialClassifyInfo classifyInfo:classifyInfoList){
            MaterialClassificationDto classificationDto = new MaterialClassificationDto();
            classificationDto.setMaterialClassifyId(classifyInfo.getId());
            classificationDto.setMaterialClassifyName(classifyInfo.getMaterialClassifyName());
            //循环部门
            List<MaterialDepartmentDto> departmentDtoList = new ArrayList<>();
            for (DepRPCRespDTO dep:depInfoList){
        //循环部门
        List<MaterialDepartmentDto> departmentDtoList = new ArrayList<>();
        recursiveDep(depInfoList,departmentDtoList,materialInfoList,classifyInfoList);
        return new ResultVO(ResultCodes.OK,departmentDtoList);
    }
    private void recursiveDep(List<DepRPCRespDTO> depInfoList,List<MaterialDepartmentDto> departmentDtoList,List<SafeMaterialInfo> materialInfoList,List<SafeMaterialClassifyInfo> classifyInfoList){
        for (DepRPCRespDTO dep:depInfoList){
            MaterialDepartmentDto materialDepartmentDto = new MaterialDepartmentDto();
            materialDepartmentDto.setDepId(dep.getDepId());
            materialDepartmentDto.setDepName(dep.getDepName());
            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.getMaterialClassifyId()) && dep.getDepId().equals(item.getDepId()))
                        .filter(item -> classifyInfo.getId().equals(item.getBigClassifyId()) && dep.getDepId().equals(item.getDepId()))
                        .collect(Collectors.toList());
                List<BaseMaterialDto> baseMaterialDtoList = new ArrayList<>();
                if(selectMaterialList.size()>0){
                    //填充部门数据
                    MaterialDepartmentDto departmentDto = new MaterialDepartmentDto();
                    departmentDto.setDepId(dep.getDepId());
                    departmentDto.setDepName(dep.getDepName());
                    List<BaseMaterialDto> baseMaterialDtoList = new ArrayList<>();
                    //循环物资
                    for (SafeMaterialInfo materialInfo:selectMaterialList){
                        //填充基础物资数据
@@ -202,48 +280,17 @@
                        baseMaterialDto.setSmId(materialInfo.getId());
                        baseMaterialDtoList.add(baseMaterialDto);
                    }
                    departmentDto.setBaseMaterialList(baseMaterialDtoList);
                    departmentDtoList.add(departmentDto);
                }
                //子集部门
                if(!CollectionUtils.isEmpty(dep.getChildren())){
                    this.recursiveDep(dep.getChildren(),departmentDtoList,materialInfoList,classifyInfo.getId());
                    classificationDto.setBaseMaterialList(baseMaterialDtoList);
                    classificationDtoList.add(classificationDto);
                }
            }
            classificationDto.setDepartmentList(departmentDtoList);
            classificationDtoList.add(classificationDto);
        }
        return new ResultVO(ResultCodes.OK,classificationDtoList);
    }
    private void recursiveDep(List<DepRPCRespDTO> depList,List<MaterialDepartmentDto> departmentDtoList,List<SafeMaterialInfo> materialInfoList,Long classifyId){
        for(DepRPCRespDTO dep:depList){
            //过滤出物资数据
            List<SafeMaterialInfo> selectMaterialList = materialInfoList
                    .stream()
                    .filter(item -> classifyId.equals(item.getMaterialClassifyId()) && dep.getDepId().equals(item.getDepId()))
                    .collect(Collectors.toList());
            if(selectMaterialList.size()>0){
                //填充部门数据
                MaterialDepartmentDto departmentDto = new MaterialDepartmentDto();
                departmentDto.setDepId(dep.getDepId());
                departmentDto.setDepName(dep.getDepName());
                List<BaseMaterialDto> baseMaterialDtoList = new ArrayList<>();
                //循环物资
                for (SafeMaterialInfo materialInfo:selectMaterialList){
                    //填充基础物资数据
                    BaseMaterialDto baseMaterialDto = new BaseMaterialDto();
                    baseMaterialDto.setMaterialName(materialInfo.getMaterialName());
                    baseMaterialDto.setSmId(materialInfo.getId());
                    baseMaterialDtoList.add(baseMaterialDto);
                }
                departmentDto.setBaseMaterialList(baseMaterialDtoList);
                departmentDtoList.add(departmentDto);
            if(classificationDtoList.size()>0){
                materialDepartmentDto.setClassificationList(classificationDtoList);
                departmentDtoList.add(materialDepartmentDto);
            }
            //子集
            //子集部门
            if(!CollectionUtils.isEmpty(dep.getChildren())){
                this.recursiveDep(dep.getChildren(),departmentDtoList,materialInfoList,classifyId);
                this.recursiveDep(dep.getChildren(),departmentDtoList,materialInfoList,classifyInfoList);
            }
        }
    }