From da012358699d5881a4e1c72ed3b9275b3da49a17 Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@63.com>
Date: 星期一, 17 一月 2022 13:15:31 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java |  502 +++++++++++++++++++++++++++++++------------------------
 1 files changed, 283 insertions(+), 219 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 f8b3fb2..75eeaa1 100644
--- a/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
+++ b/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
@@ -1,9 +1,11 @@
 package com.zy.asrs.service.impl;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 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.domain.enums.WorkNoType;
 import com.zy.asrs.entity.*;
@@ -18,6 +20,8 @@
 import com.zy.common.model.StartupDto;
 import com.zy.common.service.CommonService;
 import com.zy.common.utils.CollectionUtils;
+import com.zy.common.utils.HttpHandler;
+import com.zy.common.web.param.SearchLocParam;
 import com.zy.core.CrnThread;
 import com.zy.core.DevpThread;
 import com.zy.core.cache.MessageQueue;
@@ -36,7 +40,7 @@
 import com.zy.core.thread.LedThread;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -57,8 +61,7 @@
 @Transactional
 public class MainServiceImpl {
 
-    @Autowired
-    private RowLastnoService rowLastnoService;
+    public static final long COMMAND_TIMEOUT = 5 * 1000;
 
     @Autowired
     private CommonService commonService;
@@ -84,8 +87,8 @@
     private BasErrLogService basErrLogService;
     @Autowired
     private BasCrnErrorMapper basCrnErrorMapper;
-    @Autowired
-    private JdbcTemplate jdbcTemplate;
+    @Value("${wms.url}")
+    private String wmsUrl;
 
     /**
      * 缁勬墭
@@ -99,6 +102,9 @@
             for (DevpSlave.Sta inSta : devp.getInSta()) {
                 // 鑾峰彇鏉$爜鎵弿浠俊鎭�
                 BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, inSta.getBarcode());
+                if (barcodeThread == null) {
+                    continue;
+                }
                 String barcode = barcodeThread.getBarcode();
                 if(!Cools.isEmpty(barcode)) {
                     log.info("{}鍙锋潯鐮佹壂鎻忓櫒妫�娴嬫潯鐮佷俊鎭細{}", inSta.getBarcode(), barcode);
@@ -127,72 +133,104 @@
                         log.error("宸ヤ綔妗d腑宸插瓨鍦ㄨ绔欑姸鎬佷负锛� 2.璁惧涓婅蛋 锛夌殑鏁版嵁,宸ヤ綔鍙�={}", wrkMast.getWrkNo());
                         continue;
                     }
-                    // 鑾峰彇鍏ュ簱閫氱煡妗�
-                    List<WaitPakin> waitPakins = waitPakinMapper.selectList(new EntityWrapper<WaitPakin>().eq("zpallet", barcode).eq("io_status", "N"));
-                    if (waitPakins.isEmpty()) {
-                        log.error("鏃犳鍏ュ簱鏉$爜鏁版嵁銆傛潯鐮佸彿={}", barcode);
-                        continue;
-                    }
+//                    // 鑾峰彇鍏ュ簱閫氱煡妗�
+//                    List<WaitPakin> waitPakins = waitPakinMapper.selectList(new EntityWrapper<WaitPakin>().eq("zpallet", barcode).eq("io_status", "N"));
+//                    if (waitPakins.isEmpty()) {
+//                        log.error("鏃犳鍏ュ簱鏉$爜鏁版嵁銆傛潯鐮佸彿={}", barcode);
+//                        continue;
+//                    }
 
                     try {
-                        // 妫�绱㈠簱浣�
+
                         LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
-                        List<String> matNos = waitPakins.stream().map(WaitPakin::getMatnr).distinct().collect(Collectors.toList());
-                        StartupDto startupDto = commonService.getLocNo(1, 1, inSta.getStaNo(), matNos, locTypeDto, 0);
-                        // 宸ヤ綔鍙�
-                        int workNo = startupDto.getWorkNo();
-                        // 鎻掑叆宸ヤ綔鏄庣粏妗�
-                        wrkDetlService.createWorkDetail(workNo, waitPakins, barcode);
 
-                        // 鎻掑叆宸ヤ綔涓绘。
-                        wrkMast = new WrkMast();
-                        wrkMast.setWrkNo(workNo);
-                        wrkMast.setIoTime(new Date());
-                        wrkMast.setWrkSts(2L); // 宸ヤ綔鐘舵�侊細2.璁惧涓婅蛋
-                        wrkMast.setIoType(1); // 鍏ュ嚭搴撶姸鎬侊細1.鍏ュ簱
-                        wrkMast.setIoPri(10D); // 浼樺厛绾э細10
-                        wrkMast.setCrnNo(startupDto.getCrnNo());
-                        wrkMast.setSourceStaNo(startupDto.getSourceStaNo());
-                        wrkMast.setStaNo(startupDto.getStaNo());
-                        wrkMast.setLocNo(startupDto.getLocNo());
-                        wrkMast.setBarcode(barcode); // 鎵樼洏鐮�
-                        wrkMast.setFullPlt("Y"); // 婊℃澘锛歒
-                        wrkMast.setPicking("N"); // 鎷f枡
-                        wrkMast.setExitMk("N"); // 閫�鍑�
-                        wrkMast.setEmptyMk("N"); // 绌烘澘
-                        wrkMast.setLinkMis("N");
-                        // 鎿嶄綔浜哄憳鏁版嵁
-                        wrkMast.setAppeTime(new Date());
-                        wrkMast.setModiTime(new Date());
-                        Integer insert = wrkMastMapper.insert(wrkMast);
-                        if (insert == 0) {
-                            throw new CoolException("淇濆瓨宸ヤ綔妗eけ璐�");
-                        }
-                        // 鏇存柊鐩爣搴撲綅鐘舵��
-                        LocMast locMast = locMastService.selectById(startupDto.getLocNo());
-                        locMast.setLocSts("S"); // S.鍏ュ簱棰勭害
-                        locMast.setModiTime(new Date());
-                        if (!locMastService.updateById(locMast)){
-                            throw new CoolException("鏀瑰彉搴撲綅鐘舵�佸け璐�");
-                        }
-                        // 灏嗗叆搴撻�氱煡妗d慨鏀逛负宸插惎鍔�
-                        if (wrkMastMapper.updateWaitPakInStep1(barcode) == 0) {
-                            throw new CoolException("淇敼鍏ュ簱閫氱煡妗g姸鎬佷负宸插惎鍔ㄥけ璐�");
+                        SearchLocParam param = new SearchLocParam();
+                        param.setBarcode(barcode);
+                        param.setIoType(1);
+                        param.setSourceStaNo(inSta.getStaNo());
+                        param.setLocType1(locTypeDto.getLocType1());
+                        String response = new HttpHandler.Builder()
+                                .setUri(wmsUrl)
+                                .setPath("/rpc/pakin/loc/v1")
+                                .setJson(JSON.toJSONString(param))
+                                .build()
+                                .doPost();
+                        JSONObject jsonObject = JSON.parseObject(response);
+                        if (jsonObject.getInteger("code").equals(200)) {
+                            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
+
+                            barcodeThread.setBarcode("");
+                            staProtocol.setWorkNo(dto.getWorkNo().shortValue());
+                            staProtocol.setStaNo(dto.getStaNo().shortValue());
+                            devpThread.setPakMk(staProtocol.getSiteId(), false);
+                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
+                            if (!result) {
+                                throw new CoolException("鏇存柊plc绔欑偣淇℃伅澶辫触");
+                            }
+                        } else {
+                            log.error("璇锋眰鎺ュ彛澶辫触锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", wmsUrl+"/rpc/pakin/loc/v1", JSON.toJSONString(param), response);
                         }
 
-                        // 鍛戒护涓嬪彂鍖� --------------------------------------------------------------------------
 
-                        // 鏇存柊绔欑偣淇℃伅 涓� 涓嬪彂plc鍛戒护
-                        barcodeThread.setBarcode("");
-                        staProtocol.setWorkNo((short) workNo);
-                        staProtocol.setStaNo(startupDto.getStaNo().shortValue());
-                        devpThread.setPakMk(staProtocol.getSiteId(), false);
-                        boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
-                        if (!result) {
-                            throw new CoolException("鏇存柊plc绔欑偣淇℃伅澶辫触");
-                        }
+//                        // 妫�绱㈠簱浣�
+//                        LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
+//                        List<String> matNos = waitPakins.stream().map(WaitPakin::getMatnr).distinct().collect(Collectors.toList());
+//                        StartupDto startupDto = commonService.getLocNo(1, 1, inSta.getStaNo(), matNos, locTypeDto, 0);
+//                        // 宸ヤ綔鍙�
+//                        int workNo = startupDto.getWorkNo();
+//                        // 鎻掑叆宸ヤ綔鏄庣粏妗�
+//                        wrkDetlService.createWorkDetail(workNo, waitPakins, barcode);
+//
+//                        // 鎻掑叆宸ヤ綔涓绘。
+//                        wrkMast = new WrkMast();
+//                        wrkMast.setWrkNo(workNo);
+//                        wrkMast.setIoTime(new Date());
+//                        wrkMast.setWrkSts(2L); // 宸ヤ綔鐘舵�侊細2.璁惧涓婅蛋
+//                        wrkMast.setIoType(1); // 鍏ュ嚭搴撶姸鎬侊細1.鍏ュ簱
+//                        wrkMast.setIoPri(10D); // 浼樺厛绾э細10
+//                        wrkMast.setCrnNo(startupDto.getCrnNo());
+//                        wrkMast.setSourceStaNo(startupDto.getSourceStaNo());
+//                        wrkMast.setStaNo(startupDto.getStaNo());
+//                        wrkMast.setLocNo(startupDto.getLocNo());
+//                        wrkMast.setBarcode(barcode); // 鎵樼洏鐮�
+//                        wrkMast.setFullPlt("Y"); // 婊℃澘锛歒
+//                        wrkMast.setPicking("N"); // 鎷f枡
+//                        wrkMast.setExitMk("N"); // 閫�鍑�
+//                        wrkMast.setEmptyMk("N"); // 绌烘澘
+//                        wrkMast.setLinkMis("N");
+//                        // 鎿嶄綔浜哄憳鏁版嵁
+//                        wrkMast.setAppeTime(new Date());
+//                        wrkMast.setModiTime(new Date());
+//                        Integer insert = wrkMastMapper.insert(wrkMast);
+//                        if (insert == 0) {
+//                            throw new CoolException("淇濆瓨宸ヤ綔妗eけ璐�");
+//                        }
+//                        // 鏇存柊鐩爣搴撲綅鐘舵��
+//                        LocMast locMast = locMastService.selectById(startupDto.getLocNo());
+//                        locMast.setLocSts("S"); // S.鍏ュ簱棰勭害
+//                        locMast.setModiTime(new Date());
+//                        if (!locMastService.updateById(locMast)){
+//                            throw new CoolException("鏀瑰彉搴撲綅鐘舵�佸け璐�");
+//                        }
+//                        // 灏嗗叆搴撻�氱煡妗d慨鏀逛负宸插惎鍔�
+//                        if (wrkMastMapper.updateWaitPakInStep1(barcode) == 0) {
+//                            throw new CoolException("淇敼鍏ュ簱閫氱煡妗g姸鎬佷负宸插惎鍔ㄥけ璐�");
+//                        }
+//
+//                        // 鍛戒护涓嬪彂鍖� --------------------------------------------------------------------------
+//
+//                        // 鏇存柊绔欑偣淇℃伅 涓� 涓嬪彂plc鍛戒护
+//                        barcodeThread.setBarcode("");
+//                        staProtocol.setWorkNo((short) workNo);
+//                        staProtocol.setStaNo(startupDto.getStaNo().shortValue());
+//                        devpThread.setPakMk(staProtocol.getSiteId(), false);
+//                        boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
+//                        if (!result) {
+//                            throw new CoolException("鏇存柊plc绔欑偣淇℃伅澶辫触");
+//                        }
 
                     } catch (Exception e) {
+                        e.printStackTrace();
                         TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                     }
 
@@ -223,7 +261,7 @@
                     staProtocol = staProtocol.clone();
                 }
                 // 鍒ゆ柇鏄惁婊¤冻鍏ュ簱鏉′欢
-                if (staProtocol.isAutoing() && staProtocol.isLoading()
+                if (staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.isInEnable()
                         && !staProtocol.isEmptyMk() && (staProtocol.getWorkNo() == 0 || staProtocol.getWorkNo() == 9999)
                         && staProtocol.isPakMk()) {
                     // 鍒ゆ柇閲嶅宸ヤ綔妗�
@@ -391,7 +429,7 @@
                     }
                     //  鍒ゆ柇鍫嗗灈鏈虹姸鎬佺瓑寰呯‘璁�
                     if (crnProtocol.modeType == CrnModeType.AUTO && crnProtocol.getTaskNo().equals(wrkMast.getWrkNo().shortValue())
-                            && crnProtocol.getTaskFinish() == 1  // todo:luxiaotao 绛夊緟纭
+                            && crnProtocol.statusType == CrnStatusType.WAITING
                             && crnProtocol.forkPosType == CrnForkPosType.HOME) {
 
                         // 鍛戒护涓嬪彂鍖� --------------------------------------------------------------------------
@@ -423,8 +461,7 @@
     /**
      * 鍏ュ嚭搴�  ===>>  鍫嗗灈鏈哄叆鍑哄簱浣滀笟涓嬪彂
      */
-    @Async
-    public void crnIoExecute(){
+    public synchronized void crnIoExecute(){
         for (CrnSlave crn : slaveProperties.getCrn()) {
             // 鑾峰彇鍫嗗灈鏈轰俊鎭�
             CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crn.getId());
@@ -466,7 +503,7 @@
     /**
      * 鍏ュ簱  ===>>  鍫嗗灈鏈虹珯鍒板簱浣�
      */
-    public synchronized void crnStnToLoc(CrnSlave slave, CrnProtocol crnProtocol){
+    public void crnStnToLoc(CrnSlave slave, CrnProtocol crnProtocol){
         for (CrnSlave.CrnStn crnStn : slave.getCrnInStn()) {
             boolean flag = false;
             // 鑾峰彇鍫嗗灈鏈哄叆搴撶珯淇℃伅
@@ -493,7 +530,7 @@
             // 鑾峰彇宸ヤ綔鐘舵�佷负2锛堣澶囦笂璧帮級鐨勫叆搴撳伐浣滄。
             WrkMast wrkMast = wrkMastMapper.selectPakInStep2(slave.getId(), staProtocol.getWorkNo().intValue(), crnStn.getStaNo());
             if(null == wrkMast) {
-                log.error("鏌ヨ鏃犲緟鍏ュ簱鏁版嵁--wrk_sts=2, 宸ヤ綔鍙�={}", staProtocol.getWorkNo());
+//                log.error("鏌ヨ鏃犲緟鍏ュ簱鏁版嵁--wrk_sts=2, 宸ヤ綔鍙�={}", staProtocol.getWorkNo());
                 continue;
             }
             // 鑾峰彇搴撲綅淇℃伅
@@ -512,6 +549,11 @@
                 continue;
             }
 
+            // 宸茬粡瀛樺湪鍚婅溅鎵ц浠诲姟鏃讹紝鍒欒繃婊�
+            if (wrkMastMapper.selectWorking(slave.getId()) != null) {
+                return;
+            }
+
             // 鍛戒护涓嬪彂鍖� --------------------------------------------------------------------------
             CrnCommand crnCommand = new CrnCommand();
             crnCommand.setCrnNo(slave.getId()); // 鍫嗗灈鏈虹紪鍙�
@@ -527,6 +569,19 @@
             if (!MessageQueue.offer(SlaveType.Crn, wrkMast.getCrnNo(), new Task(2, crnCommand))) {
                 log.error("鍫嗗灈鏈哄懡浠や笅鍙戝け璐ワ紝鍫嗗灈鏈哄彿={}锛屼换鍔℃暟鎹�={}", wrkMast.getCrnNo(), JSON.toJSON(crnCommand));
             } else {
+
+//                long startTime = System.currentTimeMillis();
+//                while ((System.currentTimeMillis() - startTime) < COMMAND_TIMEOUT) {
+//
+//                    if (true) {
+//                        break;
+//                    }
+//
+//                    try{
+//                        Thread.sleep(500);
+//                    }catch(Exception ignore){}
+//                }
+
                 // 淇敼宸ヤ綔妗g姸鎬� 2.璁惧涓婅蛋 => 3.鍚婅溅鍏ュ簱涓�
                 Date now = new Date();
                 wrkMast.setWrkSts(3L);
@@ -542,7 +597,7 @@
     /**
      * 鍑哄簱  ===>>  搴撲綅鍒板爢鍨涙満绔�
      */
-    public synchronized void locToCrnStn(CrnSlave slave, CrnProtocol crnProtocol){
+    public void locToCrnStn(CrnSlave slave, CrnProtocol crnProtocol){
         for (CrnSlave.CrnStn crnStn : slave.getCrnOutStn()) {
             // 鑾峰彇宸ヤ綔鐘舵�佷负11锛堢敓鎴愬嚭搴揑D锛夌殑鍑哄簱宸ヤ綔妗�
             WrkMast wrkMast = wrkMastMapper.selectPakOutStep1(slave.getId(), crnStn.getStaNo());
@@ -607,8 +662,9 @@
                             wrkMast.setUpdMk("Y");
                             wrkMastMapper.updateById(wrkMast);
                             // 鐢熸垚宸ヤ綔妗c�佹敼鍙樻祬搴撲綅鐨勬簮搴�/鐩爣搴� 搴撲綅鐘舵�併�佷笅鍙戝爢鍨涙満鍛戒护锛堢珛椹墽琛�)
-                            moveLocForDeepLoc(slave, shallowLoc);
+//                            moveLocForDeepLoc(slave, shallowLoc);
                         }
+                        log.error("{}浠诲姟鍑哄簱澶辫触锛屾祬搴撲綅鍫靛锛�", wrkMast.getWrkNo());
                         continue;
                     }
                 }
@@ -649,7 +705,7 @@
     /**
      * 搴撲綅绉昏浆
      */
-    public synchronized void locToLoc(CrnSlave slave, CrnProtocol crnProtocol){
+    public void locToLoc(CrnSlave slave, CrnProtocol crnProtocol){
         // 鑾峰彇宸ヤ綔妗d俊鎭�
         WrkMast wrkMast = wrkMastMapper.selectLocMove(slave.getId());
         if (null == wrkMast) {
@@ -725,8 +781,7 @@
             CrnProtocol crnProtocol = crnThread.getCrnProtocol();
             if (crnProtocol == null) { continue; }
             //  鐘舵�侊細绛夊緟纭 骞朵笖  浠诲姟瀹屾垚浣� = 1
-            if (crnProtocol.getTaskFinish() == 1    // todo:luxiaotao 绛夊緟纭
-                    && crnProtocol.getTaskNo() != 0) {
+            if (crnProtocol.statusType == CrnStatusType.WAITING && crnProtocol.getTaskNo() != 0) {
                 // 鑾峰彇鍏ュ簱寰呯‘璁ゅ伐浣滄。
                 WrkMast wrkMast = wrkMastMapper.selectPakInStep3(crnProtocol.getTaskNo().intValue());
                 if (wrkMast == null) {
@@ -772,13 +827,13 @@
                     BasErrLog latest = basErrLogService.findLatestByTaskNo(crn.getId(), crnProtocol.getTaskNo().intValue());
                     // 鏈夊紓甯�
                     if (latest == null) {
-                        if (crnProtocol.getAlarm1() != null && crnProtocol.getAlarm1() > 0) {
+                        if (crnProtocol.getAlarm() != null && crnProtocol.getAlarm() > 0) {
                             WrkMast wrkMast = wrkMastMapper.selectById(crnProtocol.getTaskNo());
                             if (wrkMast == null) {
                                 continue;
                             }
-                            BasCrnError crnError = basCrnErrorMapper.selectById(crnProtocol.getAlarm1());
-                            String errName = crnError==null? String.valueOf(crnProtocol.getAlarm1()):crnError.getErrName();
+                            BasCrnError crnError = basCrnErrorMapper.selectById(crnProtocol.getAlarm());
+                            String errName = crnError==null? String.valueOf(crnProtocol.getAlarm()):crnError.getErrName();
                             BasErrLog basErrLog = new BasErrLog(
                                     null,    // 缂栧彿
                                     wrkMast.getWrkNo(),    // 宸ヤ綔鍙�
@@ -793,7 +848,7 @@
                                     wrkMast.getSourceStaNo(),    // 婧愮珯
                                     wrkMast.getSourceLocNo(),    // 婧愬簱浣�
                                     wrkMast.getBarcode(),    // 鏉$爜
-                                    crnProtocol.getAlarm1(),    // 寮傚父鐮�
+                                    (int) crnProtocol.getAlarm(),    // 寮傚父鐮�
                                     errName,    // 寮傚父
                                     1,    // 寮傚父鎯呭喌
                                     now,    // 娣诲姞鏃堕棿
@@ -808,7 +863,7 @@
                         }
                     } else {
                         // 寮傚父淇
-                        if (crnProtocol.getAlarm1() == null || crnProtocol.getAlarm1() == 0) {
+                        if (crnProtocol.getAlarm() == null || crnProtocol.getAlarm() == 0) {
                             latest.setEndTime(now);
                             latest.setUpdateTime(now);
                             latest.setStatus(2);
@@ -821,11 +876,11 @@
                 } else {
                     BasErrLog latest = basErrLogService.findLatest(crn.getId());
                     // 鏈夊紓甯�
-                    if (crnProtocol.getAlarm1() != null && crnProtocol.getAlarm1() > 0) {
+                    if (crnProtocol.getAlarm() != null && crnProtocol.getAlarm() > 0) {
                         // 璁板綍鏂板紓甯�
-                        if (latest == null || (latest.getErrCode() != crnProtocol.getAlarm1().intValue())) {
-                            BasCrnError crnError = basCrnErrorMapper.selectById(crnProtocol.getAlarm1());
-                            String errName = crnError==null? String.valueOf(crnProtocol.getAlarm1()):crnError.getErrName();
+                        if (latest == null || (latest.getErrCode() != crnProtocol.getAlarm().intValue())) {
+                            BasCrnError crnError = basCrnErrorMapper.selectById(crnProtocol.getAlarm());
+                            String errName = crnError==null? String.valueOf(crnProtocol.getAlarm()):crnError.getErrName();
                             BasErrLog basErrLog = new BasErrLog(
                                     null,    // 缂栧彿
                                     null,    // 宸ヤ綔鍙�
@@ -840,7 +895,7 @@
                                     null,    // 婧愮珯
                                     null,    // 婧愬簱浣�
                                     null,    // 鏉$爜
-                                    crnProtocol.getAlarm1(),    // 寮傚父鐮�
+                                    (int)crnProtocol.getAlarm(),    // 寮傚父鐮�
                                     errName,    // 寮傚父
                                     1,    // 寮傚父鎯呭喌
                                     now,    // 娣诲姞鏃堕棿
@@ -892,59 +947,96 @@
                 }
                 // 绔欑偣鏉′欢鍒ゆ柇
                 if (staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.isInEnable()
-                        && staProtocol.isEmptyMk() && (staProtocol.getWorkNo() == 0 || staProtocol.getWorkNo() == 9999 || staProtocol.getWorkNo() == 9990) && staProtocol.isPakMk()) {
-                    // 妫�绱㈠簱浣�
-                    LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
-                    StartupDto startupDto = commonService.getLocNo(1, 10, emptyInSta.getStaNo(), null, locTypeDto, 0);
-                    // 宸ヤ綔鍙�
-                    int workNo = startupDto.getWorkNo();
+                        && staProtocol.isEmptyMk() && (staProtocol.getWorkNo() == 0 || staProtocol.getWorkNo() == 9999 || staProtocol.getWorkNo() == 9990 || staProtocol.getWorkNo() == 9997) && staProtocol.isPakMk()) {
 
                     try {
-                        // 鎻掑叆宸ヤ綔涓绘。
-                        WrkMast wrkMast = new WrkMast();
-                        wrkMast.setWrkNo(workNo);
-                        wrkMast.setIoTime(new Date());
-                        wrkMast.setWrkSts(2L); // 宸ヤ綔鐘舵�侊細2.璁惧涓婅蛋
-                        wrkMast.setIoType(10); // 鍏ュ嚭搴撶姸鎬侊細10.绌烘澘鍏ュ簱
-                        wrkMast.setIoPri(10D); // 浼樺厛绾э細10
-                        wrkMast.setCrnNo(startupDto.getCrnNo());
-                        wrkMast.setSourceStaNo(startupDto.getSourceStaNo());
-                        wrkMast.setStaNo(startupDto.getStaNo());
-                        wrkMast.setLocNo(startupDto.getLocNo());
-                        wrkMast.setFullPlt("N"); // 婊℃澘
-                        wrkMast.setPicking("N"); // 鎷f枡
-                        wrkMast.setExitMk("N"); // 閫�鍑�
-                        wrkMast.setEmptyMk("Y"); // 绌烘澘
-                        wrkMast.setLinkMis("N");
-//                    wrkMast.setCtnType(sourceStaNo.getCtnType()); // 瀹瑰櫒绫诲瀷
-                        // 鎿嶄綔浜哄憳鏁版嵁
-                        wrkMast.setAppeTime(new Date());
-                        wrkMast.setModiTime(new Date());
-                        Integer insert = wrkMastMapper.insert(wrkMast);
-                        if (insert == 0) {
-                            throw new CoolException("淇濆瓨宸ヤ綔妗eけ璐�");
-                        }
-                        // 鏇存柊鐩爣搴撲綅鐘舵��
-                        LocMast locMast = locMastService.selectById(startupDto.getLocNo());
-                        locMast.setLocSts("S"); // S.鍏ュ簱棰勭害
-                        locMast.setModiTime(new Date());
-                        if (!locMastService.updateById(locMast)){
-                            throw new CoolException("鏀瑰彉搴撲綅鐘舵�佸け璐�");
-                        }
-                        // 鍛戒护涓嬪彂鍖� --------------------------------------------------------------------------
+                        LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
 
-                        // 鏇存柊绔欑偣淇℃伅 涓� 涓嬪彂plc鍛戒护
-                        staProtocol.setWorkNo((short) workNo);
-                        staProtocol.setStaNo(startupDto.getStaNo().shortValue());
-                        devpThread.setPakMk(staProtocol.getSiteId(), false);
-                        boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
-                        if (!result) {
-                            throw new CoolException("鏇存柊plc绔欑偣淇℃伅澶辫触");
+                        SearchLocParam param = new SearchLocParam();
+                        param.setIoType(10);
+                        param.setSourceStaNo(emptyInSta.getStaNo());
+                        param.setLocType1(locTypeDto.getLocType1());
+                        String response = new HttpHandler.Builder()
+                                .setUri(wmsUrl)
+                                .setPath("/rpc/pakin/loc/v1")
+                                .setJson(JSON.toJSONString(param))
+                                .build()
+                                .doPost();
+                        JSONObject jsonObject = JSON.parseObject(response);
+                        if (jsonObject.getInteger("code").equals(200)) {
+                            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
+
+                            // 鏇存柊绔欑偣淇℃伅 涓� 涓嬪彂plc鍛戒护
+                            staProtocol.setWorkNo(dto.getWorkNo().shortValue());
+                            staProtocol.setStaNo(dto.getStaNo().shortValue());
+                            devpThread.setPakMk(staProtocol.getSiteId(), false);
+                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
+                            if (!result) {
+                                throw new CoolException("鏇存柊plc绔欑偣淇℃伅澶辫触");
+                            }
+                        } else {
+                            log.error("璇锋眰鎺ュ彛澶辫触锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", wmsUrl+"/rpc/pakin/loc/v1", JSON.toJSONString(param), response);
                         }
                     } catch (Exception e) {
                         e.printStackTrace();
                         TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                     }
+
+
+
+
+//                    // 妫�绱㈠簱浣�
+//                    LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
+//                    StartupDto startupDto = commonService.getLocNo(1, 10, emptyInSta.getStaNo(), null, locTypeDto, 0);
+//                    // 宸ヤ綔鍙�
+//                    int workNo = startupDto.getWorkNo();
+//
+//                    try {
+//                        // 鎻掑叆宸ヤ綔涓绘。
+//                        WrkMast wrkMast = new WrkMast();
+//                        wrkMast.setWrkNo(workNo);
+//                        wrkMast.setIoTime(new Date());
+//                        wrkMast.setWrkSts(2L); // 宸ヤ綔鐘舵�侊細2.璁惧涓婅蛋
+//                        wrkMast.setIoType(10); // 鍏ュ嚭搴撶姸鎬侊細10.绌烘澘鍏ュ簱
+//                        wrkMast.setIoPri(10D); // 浼樺厛绾э細10
+//                        wrkMast.setCrnNo(startupDto.getCrnNo());
+//                        wrkMast.setSourceStaNo(startupDto.getSourceStaNo());
+//                        wrkMast.setStaNo(startupDto.getStaNo());
+//                        wrkMast.setLocNo(startupDto.getLocNo());
+//                        wrkMast.setFullPlt("N"); // 婊℃澘
+//                        wrkMast.setPicking("N"); // 鎷f枡
+//                        wrkMast.setExitMk("N"); // 閫�鍑�
+//                        wrkMast.setEmptyMk("Y"); // 绌烘澘
+//                        wrkMast.setLinkMis("N");
+////                    wrkMast.setCtnType(sourceStaNo.getCtnType()); // 瀹瑰櫒绫诲瀷
+//                        // 鎿嶄綔浜哄憳鏁版嵁
+//                        wrkMast.setAppeTime(new Date());
+//                        wrkMast.setModiTime(new Date());
+//                        Integer insert = wrkMastMapper.insert(wrkMast);
+//                        if (insert == 0) {
+//                            throw new CoolException("淇濆瓨宸ヤ綔妗eけ璐�");
+//                        }
+//                        // 鏇存柊鐩爣搴撲綅鐘舵��
+//                        LocMast locMast = locMastService.selectById(startupDto.getLocNo());
+//                        locMast.setLocSts("S"); // S.鍏ュ簱棰勭害
+//                        locMast.setModiTime(new Date());
+//                        if (!locMastService.updateById(locMast)){
+//                            throw new CoolException("鏀瑰彉搴撲綅鐘舵�佸け璐�");
+//                        }
+//                        // 鍛戒护涓嬪彂鍖� --------------------------------------------------------------------------
+//
+//                        // 鏇存柊绔欑偣淇℃伅 涓� 涓嬪彂plc鍛戒护
+//                        staProtocol.setWorkNo((short) workNo);
+//                        staProtocol.setStaNo(startupDto.getStaNo().shortValue());
+//                        devpThread.setPakMk(staProtocol.getSiteId(), false);
+//                        boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
+//                        if (!result) {
+//                            throw new CoolException("鏇存柊plc绔欑偣淇℃伅澶辫触");
+//                        }
+//                    } catch (Exception e) {
+//                        e.printStackTrace();
+//                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
+//                    }
 
                 }
 
@@ -1072,7 +1164,7 @@
             // 鑾峰彇led绾跨▼
             LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, led.getDevpPlcId());
             // led鏄剧ず榛樿鍐呭
-            if (!ledThread.isResetStatus() && reset) {
+            if (reset) {
                 if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(2, new ArrayList<>()))) {
                     log.error("{}鍙稬ED鍛戒护涓嬪彂澶辫触锛侊紒锛乕ip锛歿}] [port锛歿}]", led.getId(), led.getIp(), led.getPort());
                 }
@@ -1171,82 +1263,6 @@
     /**
      * 鍫嗗灈鏈烘紨绀�  ===>> 搴撲綅绉昏浆
      */
-    @Async
-    public void crnDemoOfLocMove(Integer crn){
-        for (int i = 0; i < 5; i++) {
-
-            LocMast sourceLoc = locMastService.queryDemoSourceLoc(crn);
-            LocMast loc = locMastService.queryDemoLoc(crn);
-            if (null == sourceLoc || null == loc) {
-                break;
-            }
-            String sourceLocNo = sourceLoc.getLocNo();
-            String locNo = loc.getLocNo();
-
-            // 鑾峰彇宸ヤ綔鍙�
-            int workNo = commonService.getWorkNo(WorkNoType.PICK.type);
-            // 淇濆瓨宸ヤ綔妗�
-            WrkMast wrkMast = new WrkMast();
-            wrkMast.setWrkNo(workNo);
-            wrkMast.setIoTime(new Date());
-            wrkMast.setWrkSts(11L); // 宸ヤ綔鐘舵�侊細11.鐢熸垚鍑哄簱ID
-            wrkMast.setIoType(11); // 鍏ュ嚭搴撶姸鎬侊細 11.搴撴牸绉昏浇
-            wrkMast.setIoPri(13D);
-            wrkMast.setCrnNo(crn);
-            wrkMast.setSourceLocNo(sourceLocNo); // 婧愬簱浣�
-            wrkMast.setLocNo(locNo); // 鐩爣搴撲綅
-            wrkMast.setFullPlt("N"); // 婊℃澘锛歒
-            wrkMast.setPicking("N"); // 鎷f枡
-            wrkMast.setExitMk("N"); // 閫�鍑�
-            wrkMast.setEmptyMk(sourceLoc.getLocSts().equals("D")?"Y":"N"); // 绌烘澘
-            wrkMast.setBarcode(sourceLoc.getBarcode()); // 鎵樼洏鐮�
-            wrkMast.setLinkMis("N");
-            wrkMast.setAppeTime(new Date());
-            wrkMast.setModiTime(new Date());
-            int res = wrkMastMapper.insert(wrkMast);
-            if (res == 0) {
-                throw new CoolException("淇濆瓨宸ヤ綔妗eけ璐�");
-            }
-            // 宸ヤ綔妗f槑缁嗕繚瀛�
-            List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", sourceLocNo));
-            for (LocDetl locDetl : locDetls) {
-                WrkDetl wrkDetl = new WrkDetl();
-                wrkDetl.setWrkNo(workNo);
-                wrkDetl.setIoTime(new Date());
-                wrkDetl.setAnfme(locDetl.getAnfme());
-                VersionUtils.setWrkDetl(wrkDetl, locDetl); // 鐗堟湰鎺у埗
-                wrkDetl.setAppeTime(new Date());
-                wrkDetl.setModiTime(new Date());
-                if (!wrkDetlService.insert(wrkDetl)) {
-                    throw new CoolException("淇濆瓨宸ヤ綔妗f槑缁嗗け璐�");
-                }
-            }
-            // 淇敼婧愬簱浣嶇姸鎬�
-            if (sourceLoc.getLocSts().equals("D") || sourceLoc.getLocSts().equals("F")) {
-                sourceLoc.setLocSts("R"); // R.鍑哄簱棰勭害
-                sourceLoc.setModiTime(new Date());
-                if (!locMastService.updateById(sourceLoc)){
-                    throw new CoolException("鏇存柊婧愬簱浣嶇姸鎬佸け璐�");
-                }
-            } else {
-                throw new CoolException("婧愬簱浣嶅嚭搴撳け璐�");
-            }
-            // 淇敼鐩爣搴撲綅鐘舵��
-            if (loc.getLocSts().equals("O")) {
-                loc.setLocSts("S"); // S.鍏ュ簱棰勭害
-                loc.setModiTime(new Date());
-                if (!locMastService.updateById(loc)) {
-                    throw new CoolException("鏇存柊鐩爣搴撲綅鐘舵�佸け璐�");
-                }
-            } else {
-                throw new CoolException("绉昏浆澶辫触");
-            }
-        }
-    }
-
-    /**
-     * 鍫嗗灈鏈烘紨绀�  ===>> 搴撲綅绉昏浆
-     */
     public synchronized void crnDemoOfLocMove1(){
         for (CrnSlave crn : slaveProperties.getCrn()) {
             if (!crn.getDemo()) { continue; }   // 蹇呴』涓烘紨绀虹姸鎬�
@@ -1293,21 +1309,21 @@
                     throw new CoolException("淇濆瓨宸ヤ綔妗eけ璐�");
                 }
                 // 宸ヤ綔妗f槑缁嗕繚瀛�
-                List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", sourceLocNo));
-                for (LocDetl locDetl : locDetls) {
-                    WrkDetl wrkDetl = new WrkDetl();
-                    wrkDetl.setWrkNo(workNo);
-                    wrkDetl.setIoTime(new Date());
-                    wrkDetl.setAnfme(locDetl.getAnfme());
-                    VersionUtils.setWrkDetl(wrkDetl, locDetl); // 鐗堟湰鎺у埗
-                    wrkDetl.setAppeTime(new Date());
-                    wrkDetl.setModiTime(new Date());
-                    if (!wrkDetlService.insert(wrkDetl)) {
-                        throw new CoolException("淇濆瓨宸ヤ綔妗f槑缁嗗け璐�");
-                    }
-                }
+//                List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", sourceLocNo));
+//                for (LocDetl locDetl : locDetls) {
+//                    WrkDetl wrkDetl = new WrkDetl();
+//                    wrkDetl.setWrkNo(workNo);
+//                    wrkDetl.setIoTime(new Date());
+//                    wrkDetl.setAnfme(locDetl.getAnfme());
+//                    VersionUtils.setWrkDetl(wrkDetl, locDetl); // 鐗堟湰鎺у埗
+//                    wrkDetl.setAppeTime(new Date());
+//                    wrkDetl.setModiTime(new Date());
+//                    if (!wrkDetlService.insert(wrkDetl)) {
+//                        throw new CoolException("淇濆瓨宸ヤ綔妗f槑缁嗗け璐�");
+//                    }
+//                }
                 // 淇敼婧愬簱浣嶇姸鎬�
-                if (sourceLoc.getLocSts().equals("D") || sourceLoc.getLocSts().equals("F")) {
+                if (sourceLoc.getLocSts().equals("D")) {
                     sourceLoc.setLocSts("R"); // R.鍑哄簱棰勭害
                     sourceLoc.setModiTime(new Date());
                     if (!locMastService.updateById(sourceLoc)){
@@ -1332,5 +1348,53 @@
         }
     }
 
+    /**
+     * 鍫嗗灈鏈哄懡浠や笅鍙戝悗锛屽紓姝ヤ慨鏀瑰伐浣滄。鐘舵��
+     */
+    public synchronized void crnIoWrkMast(){
+        for (CrnSlave crn : slaveProperties.getCrn()) {
+            // 鑾峰彇鍫嗗灈鏈轰俊鎭�
+            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crn.getId());
+            CrnProtocol crnProtocol = crnThread.getCrnProtocol();
+            if (crnProtocol == null) {
+                continue;
+            }
+            Date now = new Date();
+            // 鍫嗗灈鏈烘鍦ㄨ繍琛�
+            if (crnProtocol.getStatusType() != CrnStatusType.IDLE && crnProtocol.getTaskNo() != 0 && crnProtocol.getModeType() == CrnModeType.AUTO) {
+                // 鑾峰彇宸ヤ綔妗�
+                WrkMast wrkMast = wrkMastMapper.selectById(crnProtocol.getTaskNo());
+                if (wrkMast == null) { continue; }
+                // 鍏ュ簱
+                if (wrkMast.getWrkSts() == 1 || wrkMast.getWrkSts() == 2) {
+                    log.warn("鍫嗗灈鏈洪潪绌洪棽鎯呭喌涓嬶紝寮�濮嬩慨鏀瑰伐浣滄。鐘舵�併�俒id:{},鏃堕棿锛歿}] >>>>> 鍫嗗灈鏈哄綋鍓嶇姸鎬佷负锛歿}銆備换鍔″彿:{}", crn.getId(), DateUtils.convert(now, DateUtils.yyyyMMddHHmmsssss_F), crnProtocol.getStatusType().desc, crnProtocol.getTaskNo());
+                    // 淇敼宸ヤ綔妗g姸鎬� 2.璁惧涓婅蛋 => 3.鍚婅溅鍏ュ簱涓�
+                    wrkMast.setWrkSts(3L);
+                    wrkMast.setCrnStrTime(now);
+                    wrkMast.setModiTime(now);
+                    if (wrkMastMapper.updateById(wrkMast) == 0) {
+                        log.error("淇敼宸ヤ綔妗g姸鎬� 2.璁惧涓婅蛋 => 3.鍚婅溅鍏ュ簱涓� 澶辫触锛侊紒锛屽伐浣滃彿={}", wrkMast.getWrkNo());
+                    }
+                    log.warn("淇敼宸ヤ綔妗g姸鎬佹垚鍔熴�俒鏃堕棿锛歿}] >>>>> 浠诲姟鍙�:{}",  DateUtils.convert(now, DateUtils.yyyyMMddHHmmsssss_F),  wrkMast.getWrkNo());
+                }
+                // 鍑哄簱銆佺Щ搴�
+                if (wrkMast.getWrkSts() == 11) {
+                    log.warn("鍫嗗灈鏈洪潪绌洪棽鎯呭喌涓嬶紝寮�濮嬩慨鏀瑰伐浣滄。鐘舵�併�俒id:{},鏃堕棿锛歿}] >>>>> 鍫嗗灈鏈哄綋鍓嶇姸鎬佷负锛歿}銆備换鍔″彿:{}", crn.getId(), DateUtils.convert(now, DateUtils.yyyyMMddHHmmsssss_F), crnProtocol.getStatusType().desc, crnProtocol.getTaskNo());
+                    // 淇敼宸ヤ綔妗g姸鎬� 11.鐢熸垚鍑哄簱ID => 12.鍚婅溅鍑哄簱涓�
+                    wrkMast.setWrkSts(12L);
+                    wrkMast.setCrnStrTime(now);
+                    wrkMast.setModiTime(now);
+                    if (wrkMastMapper.updateById(wrkMast) == 0) {
+                        log.error("淇敼宸ヤ綔妗g姸鎬� 11.鐢熸垚鍑哄簱ID => 12.鍚婅溅鍑哄簱涓� 澶辫触锛侊紒锛屽伐浣滃彿={}", wrkMast.getWrkNo());
+                    }
+                    log.warn("淇敼宸ヤ綔妗g姸鎬佹垚鍔熴�俒鏃堕棿锛歿}] >>>>> 浠诲姟鍙�:{}",  DateUtils.convert(now, DateUtils.yyyyMMddHHmmsssss_F),  wrkMast.getWrkNo());
+                }
+
+
+            }
+
+        }
+    }
+
 
 }

--
Gitblit v1.9.1