From 006ca71bd84497f2ff53630a3f9257f7a6e29391 Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@63.com>
Date: 星期四, 06 一月 2022 08:49:10 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java |  302 +++++++++++++++++++++++++++++++++----------------
 1 files changed, 202 insertions(+), 100 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 fe74358..aa13b7e 100644
--- a/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
+++ b/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
@@ -4,6 +4,7 @@
 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.*;
@@ -57,8 +58,8 @@
 @Transactional
 public class MainServiceImpl {
 
-    @Autowired
-    private RowLastnoService rowLastnoService;
+
+    public static final long COMMAND_TIMEOUT = 5 * 1000;
 
     @Autowired
     private CommonService commonService;
@@ -99,10 +100,17 @@
             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)) {
-                    System.err.println(barcode);
+                    log.info("{}鍙锋潯鐮佹壂鎻忓櫒妫�娴嬫潯鐮佷俊鎭細{}", inSta.getBarcode(), barcode);
+                    if("NG".endsWith(barcode) || "NoRead".equals(barcode)) {
+                        continue;
+                    }
                 }
+
                 // 鑾峰彇鍏ュ簱绔欎俊鎭�
                 DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                 StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo());
@@ -114,7 +122,7 @@
                 // 鍒ゆ柇鏄惁婊¤冻鍏ュ簱鏉′欢
                 if (staProtocol.isAutoing() && staProtocol.isLoading()
                         && staProtocol.isInEnable()
-                        && !staProtocol.isEmptyMk() && staProtocol.getWorkNo() == 0
+                        && !staProtocol.isEmptyMk() && (staProtocol.getWorkNo() == 0 || staProtocol.getWorkNo() == 9999)
                         && staProtocol.isPakMk() && !Cools.isEmpty(barcode)) {
 
                     // 鍒ゆ柇閲嶅宸ヤ綔妗�
@@ -134,7 +142,7 @@
                         // 妫�绱㈠簱浣�
                         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);
+                        StartupDto startupDto = commonService.getLocNo(1, 1, inSta.getStaNo(), matNos, locTypeDto, 0);
                         // 宸ヤ綔鍙�
                         int workNo = startupDto.getWorkNo();
                         // 鎻掑叆宸ヤ綔鏄庣粏妗�
@@ -219,8 +227,8 @@
                     staProtocol = staProtocol.clone();
                 }
                 // 鍒ゆ柇鏄惁婊¤冻鍏ュ簱鏉′欢
-                if (staProtocol.isAutoing() && staProtocol.isLoading()
-                        && !staProtocol.isEmptyMk() && staProtocol.getWorkNo() == 0
+                if (staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.isInEnable()
+                        && !staProtocol.isEmptyMk() && (staProtocol.getWorkNo() == 0 || staProtocol.getWorkNo() == 9999)
                         && staProtocol.isPakMk()) {
                     // 鍒ゆ柇閲嶅宸ヤ綔妗�
                     WrkMast wrkMast = wrkMastMapper.selectPakInStep11(inSta.getStaNo());
@@ -279,14 +287,24 @@
                         || Cools.isEmpty(wrkMast.getStaNo()) || Cools.isEmpty(wrkMast.getSourceStaNo()) ) {
                         continue;
                     }
+
+                    // 鎷c�佺洏銆佸苟 浣滀笟绔欒浆鎹�
+//                    int stnNo = 0;
+//                    if (wrkMast.getStaNo() == 109) {
+//                        stnNo = 127;
+//                    } else if (wrkMast.getStaNo() == 113) {
+//                        stnNo = 128;
+//                    } else {
+//                        log.error("{}鍙蜂换鍔℃暟鎹紓甯革紒", wrkMast.getWrkNo());
+//                    }
                     // 鑾峰彇鐩爣绔�
                     Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
                             .eq("type_no", wrkMast.getIoType() - 50)
-                            .eq("stn_no", wrkMast.getStaNo()) // 浣滀笟绔欑偣 = 鎷f枡鍑哄簱鐨勭洰鏍囩珯
+                            .eq("stn_no", pickSta.getStaNo()) // 浣滀笟绔欑偣 = 鎷f枡鍑哄簱鐨勭洰鏍囩珯
                             .eq("crn_no", wrkMast.getCrnNo()); // 鍫嗗灈鏈哄彿
                     StaDesc staDesc = staDescService.selectOne(wrapper);
                     if (Cools.isEmpty(staDesc)) {
-                        log.error("鍏ュ簱璺緞涓嶅瓨鍦�");
+                        log.error("鍏ュ簱璺緞涓嶅瓨鍦紒type_no={},stn_no={},crn_no={}", wrkMast.getIoType(), pickSta.getStaNo(), wrkMast.getCrnNo());
                         continue;
                     }
 
@@ -377,7 +395,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) {
 
                         // 鍛戒护涓嬪彂鍖� --------------------------------------------------------------------------
@@ -409,8 +427,7 @@
     /**
      * 鍏ュ嚭搴�  ===>>  鍫嗗灈鏈哄叆鍑哄簱浣滀笟涓嬪彂
      */
-    @Async
-    public void crnIoExecute(){
+    public synchronized void crnIoExecute(){
         for (CrnSlave crn : slaveProperties.getCrn()) {
             // 鑾峰彇鍫嗗灈鏈轰俊鎭�
             CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crn.getId());
@@ -452,7 +469,6 @@
     /**
      * 鍏ュ簱  ===>>  鍫嗗灈鏈虹珯鍒板簱浣�
      */
-    @Async
     public void crnStnToLoc(CrnSlave slave, CrnProtocol crnProtocol){
         for (CrnSlave.CrnStn crnStn : slave.getCrnInStn()) {
             boolean flag = false;
@@ -478,9 +494,9 @@
                 continue;
             }
             // 鑾峰彇宸ヤ綔鐘舵�佷负2锛堣澶囦笂璧帮級鐨勫叆搴撳伐浣滄。
-            WrkMast wrkMast = wrkMastMapper.selectPakInStep2(slave.getId(), staProtocol.getWorkNo().intValue());
+            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;
             }
             // 鑾峰彇搴撲綅淇℃伅
@@ -499,6 +515,11 @@
                 continue;
             }
 
+            // 宸茬粡瀛樺湪鍚婅溅鎵ц浠诲姟鏃讹紝鍒欒繃婊�
+            if (wrkMastMapper.selectWorking(slave.getId()) != null) {
+                return;
+            }
+
             // 鍛戒护涓嬪彂鍖� --------------------------------------------------------------------------
             CrnCommand crnCommand = new CrnCommand();
             crnCommand.setCrnNo(slave.getId()); // 鍫嗗灈鏈虹紪鍙�
@@ -514,6 +535,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);
@@ -529,11 +563,10 @@
     /**
      * 鍑哄簱  ===>>  搴撲綅鍒板爢鍨涙満绔�
      */
-    @Async
     public void locToCrnStn(CrnSlave slave, CrnProtocol crnProtocol){
         for (CrnSlave.CrnStn crnStn : slave.getCrnOutStn()) {
             // 鑾峰彇宸ヤ綔鐘舵�佷负11锛堢敓鎴愬嚭搴揑D锛夌殑鍑哄簱宸ヤ綔妗�
-            WrkMast wrkMast = wrkMastMapper.selectPakOutStep1(slave.getId());
+            WrkMast wrkMast = wrkMastMapper.selectPakOutStep1(slave.getId(), crnStn.getStaNo());
             if (wrkMast == null) {
                 continue;
             }
@@ -573,8 +606,8 @@
                 }
 
                 // 鍙屾繁搴撲綅涓旀祬搴撲綅鏈夎揣锛屽垯闇�鍏堝娴呭簱浣嶈繘琛屽簱浣嶇Щ杞�
-                if (Utils.isDeepLoc(slaveProperties, wrkMast.getLocNo())) {
-                    String shallowLocNo = Utils.getShallowLoc(slaveProperties, wrkMast.getLocNo());
+                if (Utils.isDeepLoc(slaveProperties, wrkMast.getSourceLocNo())) {
+                    String shallowLocNo = Utils.getShallowLoc(slaveProperties, wrkMast.getSourceLocNo());
                     LocMast shallowLoc = locMastService.selectById(shallowLocNo);
                     // O.绌哄簱浣嶃�丵.鎷f枡/鐩樼偣/骞舵澘鍐嶅叆搴撱�丼.鍏ュ簱棰勭害銆乆.绂佺敤 鐩存帴鎼紒
                     if (shallowLoc.getLocSts().equals("P") || shallowLoc.getLocSts().equals("R")) {
@@ -599,6 +632,11 @@
                         }
                         continue;
                     }
+                }
+
+                // 宸茬粡瀛樺湪鍚婅溅鎵ц浠诲姟鏃讹紝鍒欒繃婊�
+                if (wrkMastMapper.selectWorking(slave.getId()) != null) {
+                    return;
                 }
 
                 // 1.鍫嗗灈鏈哄紑濮嬬Щ鍔�
@@ -632,8 +670,7 @@
     /**
      * 搴撲綅绉昏浆
      */
-    @Async
-    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) {
@@ -663,6 +700,11 @@
 
         // 鍫嗗灈鏈烘帶鍒惰繃婊�
         if (!crnProtocol.getStatusType().equals(CrnStatusType.IDLE) || crnProtocol.getTaskNo() != 0) {
+            return;
+        }
+
+        // 宸茬粡瀛樺湪鍚婅溅鎵ц浠诲姟鏃讹紝鍒欒繃婊�
+        if (wrkMastMapper.selectLocMoving(slave.getId()) != null) {
             return;
         }
 
@@ -704,8 +746,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) {
@@ -751,13 +792,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(crn.getId()==2?(crnProtocol.getAlarm1()+1000):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(),    // 宸ヤ綔鍙�
@@ -772,7 +813,7 @@
                                     wrkMast.getSourceStaNo(),    // 婧愮珯
                                     wrkMast.getSourceLocNo(),    // 婧愬簱浣�
                                     wrkMast.getBarcode(),    // 鏉$爜
-                                    crnProtocol.getAlarm1(),    // 寮傚父鐮�
+                                    (int) crnProtocol.getAlarm(),    // 寮傚父鐮�
                                     errName,    // 寮傚父
                                     1,    // 寮傚父鎯呭喌
                                     now,    // 娣诲姞鏃堕棿
@@ -787,7 +828,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);
@@ -800,11 +841,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(crn.getId()==2?(crnProtocol.getAlarm1()+1000):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,    // 宸ヤ綔鍙�
@@ -819,7 +860,7 @@
                                     null,    // 婧愮珯
                                     null,    // 婧愬簱浣�
                                     null,    // 鏉$爜
-                                    crnProtocol.getAlarm1(),    // 寮傚父鐮�
+                                    (int)crnProtocol.getAlarm(),    // 寮傚父鐮�
                                     errName,    // 寮傚父
                                     1,    // 寮傚父鎯呭喌
                                     now,    // 娣诲姞鏃堕棿
@@ -871,10 +912,10 @@
                 }
                 // 绔欑偣鏉′欢鍒ゆ柇
                 if (staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.isInEnable()
-                        && staProtocol.isEmptyMk() && staProtocol.getWorkNo() == 0 && staProtocol.isPakMk()) {
+                        && staProtocol.isEmptyMk() && (staProtocol.getWorkNo() == 0 || staProtocol.getWorkNo() == 9999 || staProtocol.getWorkNo() == 9990 || staProtocol.getWorkNo() == 9997) && staProtocol.isPakMk()) {
                     // 妫�绱㈠簱浣�
                     LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
-                    StartupDto startupDto = commonService.getLocNo(1, 10, emptyInSta.getStaNo(), null, locTypeDto);
+                    StartupDto startupDto = commonService.getLocNo(1, 10, emptyInSta.getStaNo(), null, locTypeDto, 0);
                     // 宸ヤ綔鍙�
                     int workNo = startupDto.getWorkNo();
 
@@ -1051,7 +1092,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());
                 }
@@ -1150,77 +1191,138 @@
     /**
      * 鍫嗗灈鏈烘紨绀�  ===>> 搴撲綅绉昏浆
      */
-    @Async
-    public void crnDemoOfLocMove(Integer crn){
-        for (int i = 0; i < 5; i++) {
+    public synchronized void crnDemoOfLocMove1(){
+        for (CrnSlave crn : slaveProperties.getCrn()) {
+            if (!crn.getDemo()) { continue; }   // 蹇呴』涓烘紨绀虹姸鎬�
 
-            LocMast sourceLoc = locMastService.queryDemoSourceLoc(crn);
-            LocMast loc = locMastService.queryDemoLoc(crn);
-            if (null == sourceLoc || null == loc) {
-                break;
-            }
-            String sourceLocNo = sourceLoc.getLocNo();
-            String locNo = loc.getLocNo();
+            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crn.getId());
+            CrnProtocol crnProtocol = crnThread.getCrnProtocol();
+            if (crnProtocol == null) { continue; }
 
-            // 鑾峰彇宸ヤ綔鍙�
-            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 (crnProtocol.getStatusType() == CrnStatusType.IDLE && crnProtocol.getTaskNo() == 0 && crnProtocol.getModeType() == CrnModeType.AUTO) {
+                // 鑾峰彇绉诲簱宸ヤ綔妗d俊鎭�
+                WrkMast wrkMast = wrkMastMapper.selectLocMove(crn.getId());
+                if (null != wrkMast) { continue; }
+
+                LocMast sourceLoc = locMastService.queryDemoSourceLoc(crn.getId());
+                LocMast loc = locMastService.queryDemoLoc(crn.getId());
+                if (null == sourceLoc || null == loc) { continue; }
+
+                String sourceLocNo = sourceLoc.getLocNo();
+                String locNo = loc.getLocNo();
+
+                // 鑾峰彇宸ヤ綔鍙�
+                int workNo = commonService.getWorkNo(WorkNoType.PICK.type);
+                // 淇濆瓨宸ヤ綔妗�
+                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.getId());
+                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け璐�");
                 }
-            }
-            // 淇敼婧愬簱浣嶇姸鎬�
-            if (sourceLoc.getLocSts().equals("D") || sourceLoc.getLocSts().equals("F")) {
-                sourceLoc.setLocSts("R"); // R.鍑哄簱棰勭害
-                sourceLoc.setModiTime(new Date());
-                if (!locMastService.updateById(sourceLoc)){
-                    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槑缁嗗け璐�");
+                    }
                 }
-            } else {
-                throw new CoolException("婧愬簱浣嶅嚭搴撳け璐�");
-            }
-            // 淇敼鐩爣搴撲綅鐘舵��
-            if (loc.getLocSts().equals("O")) {
-                loc.setLocSts("S"); // S.鍏ュ簱棰勭害
-                loc.setModiTime(new Date());
-                if (!locMastService.updateById(loc)) {
-                    throw new CoolException("鏇存柊鐩爣搴撲綅鐘舵�佸け璐�");
+                // 淇敼婧愬簱浣嶇姸鎬�
+                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("婧愬簱浣嶅嚭搴撳け璐�");
                 }
-            } 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 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