Просмотр исходного кода

pms 保养计划校验(相同设备只能存在1个保养计划)

zhangcl 3 дней назад
Родитель
Сommit
4618f1d086

+ 1 - 1
yudao-module-pms/yudao-module-pms-api/src/main/java/cn/iocoder/yudao/module/pms/enums/ErrorCodeConstant.java

@@ -33,7 +33,7 @@ public interface ErrorCodeConstant{
     ErrorCode IOT_MAINTAIN_MATERIAL_NOT_EXISTS = new ErrorCode(129, "工单物料不存在");
     ErrorCode IOT_INFORMATION_DB_NOT_EXISTS = new ErrorCode(130, "知识库不存在");
     ErrorCode IOT_LOCK_STOCK_FUSHU = new ErrorCode(131, "超过本地库存");
-    ErrorCode IOT_MAINTENANCE_SAME_DEVICE_EXISTS = new ErrorCode(163, "已经存在相同设备的保养计划");
+    ErrorCode IOT_MAINTENANCE_SAME_DEVICE_EXISTS = new ErrorCode(163, "已经存在相同设备的保养计划 {}");
     ErrorCode IOT_MAINTENANCE_PLAN_NOT_EXISTS = new ErrorCode(133, "保养计划明细不存在");
     ErrorCode IOT_MAINTENANCE_DETAILS_NOT_EXISTS = new ErrorCode(134, "保养计划明细不存在");
     ErrorCode IOT_STORAGE_AREA_NOT_EXISTS = new ErrorCode(131, "PMS 库区不存在");

+ 50 - 2
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/service/maintenance/IotMaintenancePlanServiceImpl.java

@@ -12,6 +12,7 @@ import cn.iocoder.yudao.module.pms.controller.admin.maintenance.vo.IotMaintenanc
 import cn.iocoder.yudao.module.pms.controller.admin.maintenance.vo.IotMaintenancePlanSaveReqVO;
 import cn.iocoder.yudao.module.pms.controller.admin.maintenance.vo.IotMaintenanceSaveVO;
 import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDevicePageReqVO;
+import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDeviceRespVO;
 import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
 import cn.iocoder.yudao.module.pms.dal.dataobject.iotmaintenancebom.IotMaintenanceBomDO;
 import cn.iocoder.yudao.module.pms.dal.dataobject.maintenance.IotMaintenancePlanDO;
@@ -19,6 +20,7 @@ import cn.iocoder.yudao.module.pms.dal.mysql.IotDeviceMapper;
 import cn.iocoder.yudao.module.pms.dal.mysql.iotmaintenancebom.IotMaintenanceBomMapper;
 import cn.iocoder.yudao.module.pms.dal.mysql.maintenance.IotMaintenancePlanMapper;
 import cn.iocoder.yudao.module.pms.dal.redis.BizNoRedisDAO;
+import cn.iocoder.yudao.module.pms.service.IotDeviceService;
 import cn.iocoder.yudao.module.system.service.dept.DeptService;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -26,6 +28,7 @@ import org.springframework.validation.annotation.Validated;
 
 import javax.annotation.Resource;
 import java.util.*;
+import java.util.stream.Collectors;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
@@ -50,6 +53,8 @@ public class IotMaintenancePlanServiceImpl implements IotMaintenancePlanService
     private DeptService deptService;
     @Resource
     private IotDeviceMapper iotDeviceMapper;
+    @Resource
+    private IotDeviceService iotDeviceService;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -73,8 +78,31 @@ public class IotMaintenancePlanServiceImpl implements IotMaintenancePlanService
         IotMaintenanceBomPageReqVO reqVO = new IotMaintenanceBomPageReqVO();
         reqVO.setDeviceIds(new ArrayList<>(deviceIds));
         List<IotMaintenanceBomDO> boms = iotMaintenanceBomMapper.selectList(reqVO);
+        // 需要提示出具体哪些设备已经存在于其它保养计划中
         if (CollUtil.isNotEmpty(boms)) {
-            throw exception(IOT_MAINTENANCE_SAME_DEVICE_EXISTS);
+            Set<String> deviceCodes = new HashSet<>();
+            Set<Long> existDeviceIds = new HashSet<>();
+            boms.forEach(bom -> {
+                existDeviceIds.add(bom.getDeviceId());
+            });
+            // 根据设备id查询设备编码
+            // 组装bom关联的设备信息
+            Map<Long, IotDeviceRespVO> deviceMap = iotDeviceService.getDeviceMap(new ArrayList<>(existDeviceIds));
+            if (CollUtil.isNotEmpty(deviceMap)) {
+                deviceMap.forEach((k,v) -> {
+                    deviceCodes.add(v.getDeviceCode());
+                });
+            }
+            // 拼接设备编码(如果大于3个,显示前3个并加省略号)
+            String deviceCodesString = deviceCodes.stream()
+                    .limit(3)  // 只取前3个元素
+                    .collect(Collectors.joining(","));  // 用逗号连接
+
+            // 如果设备编码超过3个,添加省略号
+            if (deviceCodes.size() > 3) {
+                deviceCodesString += "...";
+            }
+            throw exception(IOT_MAINTENANCE_SAME_DEVICE_EXISTS, deviceCodesString);
         }
         // 先保存保养计划主表 再根据主表id更新明细
         IotMaintenancePlanSaveReqVO mainPlanSaveVO = createReqVO.getMainPlan();
@@ -203,7 +231,27 @@ public class IotMaintenancePlanServiceImpl implements IotMaintenancePlanService
         reqVO.setDeviceIds(new ArrayList<>(deviceIds));
         List<IotMaintenanceBomDO> boms = iotMaintenanceBomMapper.selectList(reqVO);
         if (CollUtil.isNotEmpty(boms)) {
-            throw exception(IOT_MAINTENANCE_SAME_DEVICE_EXISTS);
+            Set<String> deviceCodes = new HashSet<>();
+            Set<Long> existDeviceIds = new HashSet<>();
+            boms.forEach(bom -> {
+                existDeviceIds.add(bom.getDeviceId());
+            });
+            // 组装bom关联的设备信息
+            Map<Long, IotDeviceRespVO> deviceMap = iotDeviceService.getDeviceMap(new ArrayList<>(existDeviceIds));
+            if (CollUtil.isNotEmpty(deviceMap)) {
+                deviceMap.forEach((k,v) -> {
+                    deviceCodes.add(v.getDeviceCode());
+                });
+            }
+            // 拼接设备编码(如果大于3个,显示前3个并加省略号)
+            String deviceCodesString = deviceCodes.stream()
+                    .limit(3)  // 只取前3个元素
+                    .collect(Collectors.joining(","));  // 用逗号连接
+            // 如果设备编码超过3个,添加省略号
+            if (deviceCodes.size() > 3) {
+                deviceCodesString += "...";
+            }
+            throw exception(IOT_MAINTENANCE_SAME_DEVICE_EXISTS, deviceCodesString);
         }
         IotMaintenancePlanSaveReqVO mainPlanSaveVO = updateReqVO.getMainPlan();
         IotMaintenancePlanDO iotMaintenancePlan = BeanUtils.toBean(mainPlanSaveVO, IotMaintenancePlanDO.class);