From 8f2b1fc281755c110faff41b75d0182b44243811 Mon Sep 17 00:00:00 2001
From: Junjie <540245094@qq.com>
Date: 星期二, 18 十一月 2025 15:59:39 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java |  272 ++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 182 insertions(+), 90 deletions(-)

diff --git a/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java b/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
index 5142e6d..1f0c5aa 100644
--- a/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
+++ b/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
@@ -5,15 +5,12 @@
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.mapper.Wrapper;
 import com.core.common.Cools;
-import com.core.common.DateUtils;
 import com.core.exception.CoolException;
 import com.zy.asrs.entity.*;
 import com.zy.asrs.mapper.BasCrnErrorMapper;
-import com.zy.asrs.mapper.WaitPakinMapper;
 import com.zy.asrs.mapper.WrkMastMapper;
 import com.zy.asrs.service.*;
 import com.zy.asrs.utils.Utils;
-import com.zy.common.constant.RedisConstantType;
 import com.zy.common.model.LocTypeDto;
 import com.zy.common.model.MatDto;
 import com.zy.common.model.SearchLocParam;
@@ -29,28 +26,23 @@
 import com.zy.core.cache.SlaveConnection;
 import com.zy.core.enums.*;
 import com.zy.core.model.*;
-import com.zy.core.model.command.CrnCommand;
 import com.zy.core.model.command.LedCommand;
-import com.zy.core.model.command.RgvCommand;
+import com.zy.core.model.command.LiftCommand;
 import com.zy.core.model.protocol.CrnProtocol;
-import com.zy.core.model.protocol.RgvProtocol;
+import com.zy.core.model.protocol.LiftProtocol;
 import com.zy.core.model.protocol.StaProtocol;
 import com.zy.core.properties.SlaveProperties;
 import com.zy.core.thread.BarcodeThread;
 import com.zy.core.thread.LedThread;
-import com.zy.core.thread.RgvThread;
 import com.zy.core.thread.SiemensDevpThread;
-import com.zy.system.entity.Config;
 import com.zy.system.service.ConfigService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
 import java.util.*;
-import java.util.stream.Collectors;
 
 /**
  * 绔嬩綋浠撳簱WCS绯荤粺涓绘祦绋嬩笟鍔�
@@ -74,8 +66,6 @@
     private WrkDetlService wrkDetlService;
     @Autowired
     private WrkDetlLogService wrkDetlLogService;
-    @Autowired
-    private WaitPakinMapper waitPakinMapper;
     @Autowired
     private LocMastService locMastService;
     @Autowired
@@ -136,17 +126,13 @@
                         && staProtocol.isInEnable()
                         && !staProtocol.isEmptyMk()
                         && staProtocol.getWorkNo() == 9999
-                        && staProtocol.isPakMk() && (staProtocol.getEmptyInType() != 1 && staProtocol.getEmptyInType() != 2)) {
+                        && staProtocol.isPakMk()) {
                     News.warnNoLog("" + mark + " - 0" + " - 寮�濮嬫墽琛�");
 
                     String barcode = barcodeThread.getBarcode();
                     if (!Cools.isEmpty(barcode)) {
                         News.info("" + mark + " - 1" + " - {}鍙锋潯鐮佹壂鎻忓櫒妫�娴嬫潯鐮佷俊鎭細{}", inSta.getBarcode(), barcode);
                         if ("NG".endsWith(barcode) || "NoRead".equals(barcode) || "empty".equals(barcode)) {
-//                            staProtocol.setWorkNo((short) 9992);
-//                            staProtocol.setStaNo(inSta.getBackSta().shortValue());
-//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
-//                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                             News.info("" + mark + " - 2" + " - 鎵爜澶辫触1 ===>> {}鍙锋潯鐮佹壂鎻忓櫒妫�娴嬫潯鐮佷俊鎭細{},绔欑偣:{}", inSta.getBarcode(), barcode, inSta.getStaNo());
                             // led 寮傚父鏄剧ず
                             if (ledThread != null) {
@@ -156,11 +142,6 @@
                             continue;
                         }
                     } else {
-//                        staProtocol.setWorkNo((short) 9992);
-//                        staProtocol.setStaNo(inSta.getBackSta().shortValue());
-//                        devpThread.setPakMk(staProtocol.getSiteId(), false);
-//                        MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
-
                         News.info("" + mark + " - 3" + " - 鎵爜澶辫触2 ===>> {}鍙锋潯鐮佹壂鎻忓櫒妫�娴嬫潯鐮佷俊鎭細{},绔欑偣:{}", inSta.getBarcode(), barcode, inSta.getStaNo());
                         // led 寮傚父鏄剧ず
                         if (ledThread != null) {
@@ -181,13 +162,6 @@
                     }
                     if (wrkMast != null) {
                         News.error("" + mark + " - 4" + " - 宸ヤ綔妗d腑宸插瓨鍦ㄨ绔欑姸鎬佷负锛� 2.璁惧涓婅蛋 锛夌殑鏁版嵁,宸ヤ綔鍙�={}", wrkMast.getWrkNo());
-//                        staProtocol.setWorkNo((short)9992);
-//                        staProtocol.setStaNo(inSta.getBackSta().shortValue());
-//                        devpThread.setPakMk(staProtocol.getSiteId(), false);
-//                        boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
-//                        if (!result) {
-//                            throw new CoolException("鏇存柊plc绔欑偣淇℃伅澶辫触");
-//                        }
 
                         // led 寮傚父鏄剧ず
                         if (ledThread != null) {
@@ -197,16 +171,21 @@
                         continue;
                     }
 
-                    if (inSta.getStaNo() == 1002 || inSta.getStaNo() == 1007) {
-                        //妫�娴嬫槸鍚︽湁鍑哄簱浠诲姟
-                        List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>()
-                                .in("io_type", 101, 103, 107)
-                                .in("sta_no", 1003, 1004)
-                        );
-                        if (!wrkMasts.isEmpty()) {
-                            News.error("" + mark + " - 4" + " - 鍏ュ簱锛屾娴嬪瓨鍦ㄥ嚭搴撲换鍔★紝绛夊緟鍑哄簱浠诲姟鎵ц瀹屾垚,鎵樼洏鐮�={}", barcode);
-                            continue;
-                        }
+                    LiftProtocol liftProtocol = devpThread.getLiftStatus();
+                    if(liftProtocol == null) {
+                        continue;
+                    }
+
+                    if (liftProtocol.getMode() != 1) {
+                        continue;
+                    }
+
+                    if (liftProtocol.getDeviceStatus() != 0) {
+                        continue;
+                    }
+
+                    if (liftProtocol.getWorkNo() > 0) {
+                        continue;
                     }
 
                     try {
@@ -227,54 +206,29 @@
                         if (jsonObject.getInteger("code").equals(200)) {
                             StartupDto dto = jsonObject.getObject("data", StartupDto.class);
 
-                            LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>()
-                                    .eq("loc_no", dto.getLocNo()));
-
-                            short staNo = dto.getStaNo().shortValue();
-
-                            int sourceStaNo = dto.getSourceStaNo();
-                            if (sourceStaNo == 1007) {
-                                staNo = 2002;
+                            WrkMast currentWrkMast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("wrk_no", dto.getWorkNo()));
+                            if (currentWrkMast == null) {
+                                throw new CoolException("WMS鐢熸垚浠诲姟鍚庯紝WCS鏌ヨ涓虹┖");
                             }
 
-                            if (inSta.getStaNo() == 1002) {
-                                staNo = 2002;
-                            }
+                            Integer staNo = Utils.getStaNoByLocNo(dto.getLocNo());
+
+                            LiftCommand command = new LiftCommand();
+                            command.setWorkNo(dto.getWorkNo());
+                            command.setSourceStaNo(inSta.getStaNo());
+                            command.setTargetStaNo(staNo);
 
                             barcodeThread.setBarcode("");
-                            staProtocol.setWorkNo(dto.getWorkNo().shortValue());
-                            staProtocol.setStaNo(staNo);
-                            staProtocol.setPalletSize(locMast.getLocType2());
-                            devpThread.setPakMk(staProtocol.getSiteId(), false);
-                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
+                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, command));
                             if (!result) {
                                 throw new CoolException("鏇存柊plc绔欑偣淇℃伅澶辫触");
                             }
 
-//                            // led 鍏ュ簱淇℃伅鏄剧ず
-//                            if (ledThread != null) {
-//                                // 鍛戒护闆嗗悎
-//                                List<LedCommand> commands = new ArrayList<>();
-//                                // 缁勮鍛戒护
-//                                LedCommand ledCommand = new LedCommand();
-//                                ledCommand.setWorkNo(dto.getWorkNo());
-//                                ledCommand.setIoType(1);
-//                                ledCommand.setTitle("鍏ㄦ澘鍏ュ簱");
-//                                ledCommand.setLocNo(dto.getLocNo());
-//                                ledCommand.setStaNo(dto.getStaNo());
-//                                commands.add(ledCommand);
-//                                MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, commands));
-////                                ledThread.errorReset();
-//                            }
+                            currentWrkMast.setWrkSts(3L);// 3.璁惧鎵ц涓�
+                            currentWrkMast.setCrnStrTime(new Date());
+                            wrkMastService.updateById(currentWrkMast);
                         } else {
                             News.error("" + mark + " - 5" + " - 璇锋眰鎺ュ彛澶辫触锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", wmsUrl + "/rpc/pakin/loc/v1", JSON.toJSONString(param), response);
-//                            staProtocol.setWorkNo((short)9992);
-//                            staProtocol.setStaNo(inSta.getBackSta().shortValue());
-//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
-//                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
-//                            if (!result) {
-//                                throw new CoolException("鏇存柊plc绔欑偣淇℃伅澶辫触");
-//                            }
 
                             // led 寮傚父鏄剧ず
                             if (ledThread != null) {
@@ -358,11 +312,6 @@
                                 .eq("crn_no", wrkMast.getCrnNo()); // 鍫嗗灈鏈哄彿
                         StaDesc staDesc = staDescService.selectOne(wrapper);
                         if (Cools.isEmpty(staDesc)) {
-//                        News.error(""+mark+" - 2"+" - 鍏ュ簱璺緞涓嶅瓨鍦紒type_no={},stn_no={},crn_no={}", wrkMast.getIoType(), pickSta.getStaNo(), wrkMast.getCrnNo());
-//                        staProtocol.setWorkNo((short) 9989);
-//                        staProtocol.setStaNo((short) (pickSta.getStaNo().shortValue()-(short)1));
-//                        devpThread.setPakMk(staProtocol.getSiteId(), false);
-//                        MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                             //LED
                             LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, pickSta.getLed());
                             // led 寮傚父鏄剧ず
@@ -370,6 +319,23 @@
                                 String errorMsg = "姝や负鎷f枡銆佸苟鏉裤�佺洏鐐瑰啀鍏ュ簱.璇锋斁鍦�" + pickSta.getBackSta().shortValue() + "绔欑偣";
                                 MessageQueue.offer(SlaveType.Led, pickSta.getLed(), new Task(5, errorMsg));
                             }
+                            continue;
+                        }
+
+                        LiftProtocol liftProtocol = devpThread.getLiftStatus();
+                        if(liftProtocol == null) {
+                            continue;
+                        }
+
+                        if (liftProtocol.getMode() != 1) {
+                            continue;
+                        }
+
+                        if (liftProtocol.getDeviceStatus() != 0) {
+                            continue;
+                        }
+
+                        if (liftProtocol.getWorkNo() > 0) {
                             continue;
                         }
 
@@ -405,16 +371,20 @@
                                     continue;
                                 }
 
-                                LocMast locMast = locMastService.selectById(newWrkMast.getLocNo());
-                                // 鏇存柊绔欑偣淇℃伅 涓� 涓嬪彂plc鍛戒护
-                                staProtocol.setWorkNo(newWrkMast.getWrkNo().shortValue());
-                                staProtocol.setStaNo(newWrkMast.getStaNo().shortValue());
-                                staProtocol.setPalletSize(locMast.getLocType2());
-                                devpThread.setPakMk(staProtocol.getSiteId(), false);
-                                boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
+                                Integer staNo = Utils.getStaNoByLocNo(newWrkMast.getLocNo());
+                                LiftCommand command = new LiftCommand();
+                                command.setWorkNo(newWrkMast.getWrkNo());
+                                command.setSourceStaNo(pickSta.getStaNo());
+                                command.setTargetStaNo(staNo);
+
+                                boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, command));
                                 if (!result) {
-                                    News.error("" + mark + " - 3" + " - 鍙戝竷鍛戒护鑷宠緭閫佺嚎闃熷垪澶辫触锛侊紒锛� [plc缂栧彿锛歿}]", devp.getId());
+                                    throw new CoolException("鏇存柊plc绔欑偣淇℃伅澶辫触");
                                 }
+
+                                newWrkMast.setWrkSts(3L);// 3.璁惧鎵ц涓�
+                                newWrkMast.setCrnStrTime(new Date());
+                                wrkMastService.updateById(newWrkMast);
                             } else {
                                 News.error("" + mark + " - 5" + " - 璇锋眰鎺ュ彛澶辫触锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", wmsUrl + "/rpc/pakin/pick/loc/v1", JSON.toJSONString(param), response);
                             }
@@ -436,6 +406,128 @@
     }
 
     /**
+     * 鎵ц鍑哄簱浠诲姟
+     */
+    public synchronized void executeOutTask() {
+        DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, 1);
+        if(devpThread == null) {
+            return;
+        }
+
+        LiftProtocol liftProtocol = devpThread.getLiftStatus();
+        if(liftProtocol == null) {
+            return;
+        }
+
+        if (liftProtocol.getMode() != 1) {
+            return;
+        }
+
+        if (liftProtocol.getDeviceStatus() != 0) {
+            return;
+        }
+
+        if (liftProtocol.getWorkNo() > 0) {
+            return;
+        }
+
+        //妫�娴嬪嚭搴撶珯鏄惁鍙嚭
+        StaProtocol staProtocol = devpThread.getStation().get(100);
+        if (staProtocol == null) {
+            return;
+        }
+
+        if (!staProtocol.isAutoing()) {
+            return;
+        }
+
+        if (staProtocol.isLoading()) {
+            return;
+        }
+
+        if (!staProtocol.isOutEnable()) {
+            return;
+        }
+
+        //妫�鏌ユ槸鍚︽湁姝e湪鎵ц鐨勪换鍔�
+        List<WrkMast> workingWrkMast = wrkMastService.selectList(new EntityWrapper<WrkMast>().in("wrk_sts", 3, 12));
+        if (!workingWrkMast.isEmpty()) {
+            return;
+        }
+
+        List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("wrk_sts", 11));
+        if (wrkMasts.isEmpty()) {
+            return;
+        }
+
+        WrkMast wrkMast = wrkMasts.get(0);
+        Integer sourceStaNo = Utils.getStaNoByLocNo(wrkMast.getSourceLocNo());
+
+        LiftCommand command = new LiftCommand();
+        command.setWorkNo(wrkMast.getWrkNo());
+        command.setSourceStaNo(sourceStaNo);
+        command.setTargetStaNo(100);
+
+        boolean result = MessageQueue.offer(SlaveType.Devp, 1, new Task(2, command));
+        if (!result) {
+            throw new CoolException("鏇存柊plc绔欑偣淇℃伅澶辫触");
+        }
+
+        wrkMast.setWrkSts(12L);// 12.璁惧鎵ц涓�
+        wrkMast.setCrnStrTime(new Date());
+        wrkMastService.updateById(wrkMast);
+    }
+
+    /**
+     * 妫�娴嬩换鍔″畬鎴�
+     */
+    public synchronized void deviceFinished() {
+        // 鏍规嵁plc閬嶅巻
+        for (DevpSlave devp : slaveProperties.getDevp()) {
+            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
+            if (devpThread == null) {
+                continue;
+            }
+
+            LiftProtocol liftProtocol = devpThread.getLiftStatus();
+            if (liftProtocol == null) {
+                continue;
+            }
+
+            if (liftProtocol.getWorkNo() <= 0) {
+                continue;
+            }
+
+            if (liftProtocol.getMode() != 1) {
+                continue;
+            }
+
+            if (liftProtocol.getDeviceStatus() != 0) {
+                continue;
+            }
+
+            WrkMast wrkMast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("wrk_no", liftProtocol.getWorkNo()));
+            if (wrkMast == null) {
+                continue;
+            }
+
+            if (wrkMast.getWrkSts() == 3) {
+                wrkMast.setWrkSts(4L);
+                wrkMast.setCrnEndTime(new Date());
+                wrkMast.setIoTime(new Date());
+                wrkMastService.updateById(wrkMast);
+            }else if (wrkMast.getWrkSts() == 12) {
+                wrkMast.setWrkSts(14L);
+                wrkMast.setCrnEndTime(new Date());
+                wrkMast.setIoTime(new Date());
+                wrkMastService.updateById(wrkMast);
+            }else {
+                News.error("鏈煡鐨勫伐浣滅姸鎬�");
+            }
+        }
+    }
+
+    /**
      * 鍫嗗灈鏈哄紓甯镐俊鎭褰�
      */
     public synchronized void recCrnErr(Integer mark) {

--
Gitblit v1.9.1