瀏覽代碼

aps-锁增加原子性

sucheng 3 月之前
父節點
當前提交
191258dc38

+ 22 - 10
cx-aps/cx-aps-common/src/main/java/com/rongwei/bscommon/sys/service/impl/ApsProductionOrderServiceImpl.java

@@ -254,12 +254,18 @@ public class ApsProductionOrderServiceImpl extends ServiceImpl<ApsProductionOrde
         String id = apsProductionOrderDo.getId();
 
         SysUserVo currentUser = CXCommonUtils.getCurrentUser();
-        if (productOrderLock.containsKey(id)) {
-            log.error("校验-该订单已被:{}提交", productOrderLock.get(id));
-            return R.error("该订单已被提交,请刷新页面后重新操作");
-        }
-        productOrderLock.put(id, currentUser.getId());
+//        if (productOrderLock.containsKey(id)) {
+//            log.error("校验-该订单已被:{}提交", productOrderLock.get(id));
+//            return R.error("该订单已被提交,请刷新页面后重新操作");
+//        }
+//        productOrderLock.put(id, currentUser.getId());
         try {
+            // 使用 putIfAbsent 实现原子性的检查+放入操作
+            String existingUser = productOrderLock.putIfAbsent(id, currentUser.getId());
+            if (existingUser != null) {
+                log.error("校验-该订单已被:{}提交", existingUser);
+                return R.error("该订单已被提交,请刷新页面后重新操作");
+            }
             //数据库中的订单数据
             ApsProductionOrderDo nowApsProductionOrderDo = this.getById(id);
             //页面上的订单数据
@@ -1124,12 +1130,18 @@ public class ApsProductionOrderServiceImpl extends ServiceImpl<ApsProductionOrde
         MasterSlaveUpdateVo masterSlaveUpdate = req.getMasterSlaveUpdate();
         String id = masterSlaveUpdate.getMasterUpdate().getColumnQueryTypes().get(0).getColumns().get(0).getValue();
         SysUserVo currentUser = CXCommonUtils.getCurrentUser();
-        if (productOrderLock.containsKey(id)) {
-            log.error("修改-该订单已被:{}提交", productOrderLock.get(id));
-            return R.error("该订单已被提交");
-        }
-        productOrderLock.put(id, currentUser.getId());
+//        if (productOrderLock.containsKey(id)) {
+//            log.error("修改-该订单已被:{}提交", productOrderLock.get(id));
+//            return R.error("该订单已被提交");
+//        }
+//        productOrderLock.put(id, currentUser.getId());
         try {
+            // 使用 putIfAbsent 实现原子性的检查+放入操作
+            String existingUser = productOrderLock.putIfAbsent(id, currentUser.getId());
+            if (existingUser != null) {
+                log.error("修改-该订单已被:{}提交", existingUser);
+                return R.error("该订单已被提交");
+            }
             //数据库中的订单数据
             ApsProductionOrderDo nowApsProductionOrderDo = this.getById(id);
             //页面上的订单数据

+ 33 - 17
cx-aps/cx-aps-common/src/main/java/com/rongwei/bscommon/sys/service/impl/ApsReportRecordsServiceImpl.java

@@ -346,12 +346,18 @@ public class ApsReportRecordsServiceImpl extends ServiceImpl<ApsReportRecordsDao
     public R startReport(StartRePortReq req) {
 
         SysUserVo currentUser = CXCommonUtils.getCurrentUser();
-        if (startWorkLock.containsKey(req.getUseDeviceId())) {
-            log.error("该作业已被:{}开工", startWorkLock.get(req.getUseDeviceId()));
-            return R.error("该作业已被开工");
-        }
-        startWorkLock.put(req.getUseDeviceId(), currentUser.getId());
+//        if (startWorkLock.containsKey(req.getUseDeviceId())) {
+//            log.error("该作业已被:{}开工", startWorkLock.get(req.getUseDeviceId()));
+//            return R.error("该作业已被开工");
+//        }
+//        startWorkLock.put(req.getUseDeviceId(), currentUser.getId());
         try {
+            // 使用 putIfAbsent 实现原子性的检查+放入操作
+            String existingUser = startWorkLock.putIfAbsent(req.getUseDeviceId(), currentUser.getId());
+            if (existingUser != null) {
+                log.error("该作业已被:{}开工", existingUser);
+                return R.error("该作业已被开工");
+            }
 
             //根据作业明细ID查询作业明细详情
             ApsProcessOperationProcessEquDo apsProcessOperationProcessEquDo = apsProcessOperationProcessEquService.getById(req.getUseDeviceId());
@@ -940,13 +946,18 @@ public class ApsReportRecordsServiceImpl extends ServiceImpl<ApsReportRecordsDao
         ApsReportRecordsDo apsReportRecordsDo = req.getApsReportRecordsDo();
 
         SysUserVo currentUser = CXCommonUtils.getCurrentUser();
-        if (reportWorkLock.containsKey(apsReportRecordsDo.getId())) {
-            log.error("该作业已被:{}报工", reportWorkLock.get(apsReportRecordsDo.getId()));
-            return R.error("该作业已被报工");
-        }
-        reportWorkLock.put(apsReportRecordsDo.getId(), currentUser.getId());
+//        if (reportWorkLock.containsKey(apsReportRecordsDo.getId())) {
+//            log.error("该作业已被:{}报工", reportWorkLock.get(apsReportRecordsDo.getId()));
+//            return R.error("该作业已被报工");
+//        }
+//        reportWorkLock.put(apsReportRecordsDo.getId(), currentUser.getId());
         try {
-
+            // 使用 putIfAbsent 实现原子性的检查+放入操作
+            String existingUser = reportWorkLock.putIfAbsent(apsReportRecordsDo.getId(), currentUser.getId());
+            if (existingUser != null) {
+                log.error("该作业已被:{}报工", existingUser);
+                return R.error("该作业已被报工");
+            }
 
             //防止同时报工
             ApsReportRecordsDo recordsDo = this.getById(apsReportRecordsDo.getId());
@@ -1615,13 +1626,18 @@ public class ApsReportRecordsServiceImpl extends ServiceImpl<ApsReportRecordsDao
         }
 
         SysUserVo currentUser = CXCommonUtils.getCurrentUser();
-        if (startWorkLock.containsKey(req.getUseDeviceId())) {
-            log.error("该小卷作业已被:{}开工", startWorkLock.get(req.getUseDeviceId()));
-            return R.error("该作业已被开工");
-        }
-        startWorkLock.put(req.getUseDeviceId(), currentUser.getId());
+//        if (startWorkLock.containsKey(req.getUseDeviceId())) {
+//            log.error("该小卷作业已被:{}开工", startWorkLock.get(req.getUseDeviceId()));
+//            return R.error("该作业已被开工");
+//        }
+//        startWorkLock.put(req.getUseDeviceId(), currentUser.getId());
         try {
-
+            // 使用 putIfAbsent 实现原子性的检查+放入操作
+            String existingUser = startWorkLock.putIfAbsent(req.getUseDeviceId(), currentUser.getId());
+            if (existingUser != null) {
+                log.error("该小卷作业已被:{}开工", existingUser);
+                return R.error("该作业已被开工");
+            }
             //根据作业明细ID查询作业明细详情
             ApsProcessOperationProcessEquDo apsProcessOperationProcessEquDo = apsProcessOperationProcessEquService.getById(req.getUseDeviceId());
             //根据加工设备ID查询设备