From 8fbd60d7e7fc0e35b3bff5a0c63e2d1bd80be8cf Mon Sep 17 00:00:00 2001
From: czkh <czkh@163.com>
Date: 星期四, 27 十一月 2025 08:08:40 +0800
Subject: [PATCH] #1

---
 src/main/java/com/zy/common/service/CommonService.java |  352 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 330 insertions(+), 22 deletions(-)

diff --git a/src/main/java/com/zy/common/service/CommonService.java b/src/main/java/com/zy/common/service/CommonService.java
index fd12d84..6711f06 100644
--- a/src/main/java/com/zy/common/service/CommonService.java
+++ b/src/main/java/com/zy/common/service/CommonService.java
@@ -3,15 +3,14 @@
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.mapper.Wrapper;
-import com.core.common.Arith;
 import com.core.common.Cools;
 import com.core.exception.CoolException;
 import com.zy.asrs.entity.*;
 import com.zy.asrs.service.*;
+import com.zy.asrs.service.impl.MatServiceImpl;
 import com.zy.asrs.utils.Utils;
 import com.zy.asrs.utils.VersionUtils;
 import com.zy.common.model.LocTypeDto;
-import com.zy.common.model.Shelves;
 import com.zy.common.model.StartupDto;
 import com.zy.common.properties.SlaveProperties;
 import lombok.extern.slf4j.Slf4j;
@@ -19,6 +18,8 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.List;
 
 /**
@@ -47,6 +48,8 @@
     private LocDetlService locDetlService;
     @Autowired
     private SlaveProperties slaveProperties;
+    @Autowired
+    private MatServiceImpl matService;
 
     /**
      * 鐢熸垚宸ヤ綔鍙�
@@ -86,6 +89,37 @@
             }
         }
         return workNo;
+    }
+
+    //鎷嗙洏鏈哄绌烘澘鎵爜锛岄┍鍔ㄦ墭鐩樺悜鐮佸灈浣嶏紝涓嶅叆搴�
+    @Transactional
+    public StartupDto getScanBarcodeEmptyBoard() {
+        StartupDto startupDto = new StartupDto();
+        Integer staNo = 0;
+        if (wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("io_type", 201).eq("staNo", 216)) < 2) {
+            staNo = 216;
+        }
+        if (wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("io_type", 201).eq("staNo", 220)) < 2) {
+            staNo = 220;
+        }
+
+        startupDto.setStaNo(staNo);
+        return startupDto;
+    }
+
+    /**
+     * 妫�绱㈠簱浣嶅彿
+     *
+     * @param whsType     绫诲瀷 1:鍙屾繁寮忚揣鏋�
+     * @param staDescId   璺緞ID
+     * @param sourceStaNo 婧愮珯
+     * @param matnr       鐗╂枡鍙烽泦鍚�
+     * @return locNo 妫�绱㈠埌鐨勫簱浣嶅彿
+     */
+    @Transactional
+    public StartupDto getLocNo(Integer whsType, Integer staDescId, Integer sourceStaNo, String matnr, String batch, String grade, LocTypeDto locTypeDto) {
+        whsType = Utils.GetWhsType(sourceStaNo);
+        return getLocNo(whsType, staDescId, sourceStaNo, matnr, batch, grade, 0, locTypeDto, 0);
     }
 
     /**
@@ -128,12 +162,12 @@
         // ===============>>>> 寮�濮嬫墽琛�
         curRow = rowLastno.getCurrentRow();
 
-        if (!Cools.isEmpty(moveCrnNo) && moveCrnNo!=0){
+        if (!Cools.isEmpty(moveCrnNo) && moveCrnNo != 0) {
             crnNumber = moveCrnNo;
-            if (times==0){
-                curRow = moveCrnNo*4-1;
-            }else {
-                curRow = moveCrnNo*4-2;
+            if (times == 0) {
+                curRow = moveCrnNo * 4 - 1;
+            } else {
+                curRow = moveCrnNo * 4 - 2;
             }
         }
 
@@ -143,7 +177,7 @@
             curRow = locNecessaryParameters[1];
             crnNo = locNecessaryParameters[2];
             if (basCrnpService.checkSiteError(crnNo, true)) {
-                rowCount =  locNecessaryParameters[0];
+                rowCount = locNecessaryParameters[0];
                 nearRow = locNecessaryParameters[3];
                 break;
             } else {
@@ -167,19 +201,19 @@
             signRule2 = true;
         }
 
-        if (!Cools.isEmpty(matnr) &&  (staDescId == 11 || staDescId == 111)) {
+        if (!Cools.isEmpty(matnr) && (staDescId == 11 || staDescId == 111)) {
             signRule1 = true;
         }
 
-        if (signRule1){
+        if (signRule1) {
             List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow).eq("loc_sts", "O"));
-            for (LocMast locMast1:locMasts){
+            for (LocMast locMast1 : locMasts) {
                 if (VersionUtils.locMoveCheckLocTypeComplete(locMast1, locTypeDto)) {
                     continue;
                 }
-                String shallowLoc = Utils.getDeepLoc(slaveProperties,locMast1.getLocNo());
-                LocMast locMast2 = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no",shallowLoc));
-                if (!Cools.isEmpty(locMast2) && locMast2.getLocSts().equals("F")){
+                String shallowLoc = Utils.getDeepLoc(slaveProperties, locMast1.getLocNo());
+                LocMast locMast2 = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", shallowLoc));
+                if (!Cools.isEmpty(locMast2) && locMast2.getLocSts().equals("F")) {
                     LocDetl locDetl = locDetlService.selectOne(new EntityWrapper<LocDetl>().eq("loc_no", locMast2.getLocNo()));
                     if (!Cools.isEmpty(locDetl) && matnr.equals(locDetl.getMatnr())) {
                         locMast = locMast1;
@@ -187,8 +221,8 @@
                     }
                 }
             }
-        }else if (signRule2){
-            List<String> locNos = locDetlService.getSameDetlToday(matnr,batch, sRow, eRow);
+        } else if (signRule2) {
+            List<String> locNos = locDetlService.getSameDetlToday(matnr, batch, sRow, eRow);
             for (String locNo : locNos) {
                 if (Utils.isShallowLoc(slaveProperties, locNo)) {
                     continue;
@@ -209,7 +243,7 @@
         }
 
 //        // 闈犺繎鎽嗘斁瑙勫垯 --- 绌烘墭  //鍒嗙鐗�
-//        if (staDescId == 10) {
+//       if (staDescId == 10) {
 //            List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow).eq("loc_sts", "O"));
 //            for (LocMast locMast1:locMasts){
 //                if (VersionUtils.locMoveCheckLocTypeComplete(locMast1, locTypeDto)) {
@@ -282,17 +316,66 @@
 
         // 1.鎸夎鍒欐煡鎵惧簱浣�
         if (Cools.isEmpty(locMast) && crnNo != 0) {
-            List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow).eq("loc_sts", "O"));
+            List<LocMast> locMasts = new ArrayList<>();
+            locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow)
+                    .eq("loc_sts", "O")
+                    .eq("loc_type1",locTypeDto.getLocType1()));
+            if (Cools.isEmpty(locMasts) || locMasts.size() <= 5){
+                if (nearRow == 2){
+                    locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow + 1)
+                            .eq("loc_sts", "O")
+                            .eq("loc_type1",locTypeDto.getLocType1()));
+                }else if (nearRow == 3){
+                    locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow - 1)
+                            .eq("loc_sts", "O")
+                            .eq("loc_type1",locTypeDto.getLocType1()));
+
+                }
+            }
+            if (locMasts.size() <= 5){
+                throw new CoolException("鏃犵┖搴撲綅鍙敤");
+            }
+            List<LocMast> locMastList = new ArrayList<>();
+            locMasts.sort(new Comparator<LocMast>() {
+                @Override
+                public int compare(LocMast o1, LocMast o2) {
+                    int b1 = o1.getBay1();
+                    int b2 = o2.getBay1();
+                    if (o1.getBay1() >= 15) {
+                        b1 = Math.abs(15 - o1.getBay1());
+                    }
+                    if (o2.getBay1() >= 15) {
+                        b2 = Math.abs(15 - o2.getBay1());
+                    }
+                    return b2 - b1;
+                }
+            });
+
             for (LocMast locMast1 : locMasts) {
-                if (VersionUtils.locMoveCheckLocTypeComplete(locMast1, locTypeDto)) {
+                if (!VersionUtils.locMoveCheckLocTypeComplete(locMast1, locTypeDto)) {
                     continue;
                 }
                 String shallowLoc = Utils.getDeepLoc(slaveProperties, locMast1.getLocNo());
-                LocMast locMast2 = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", shallowLoc).eq("loc_sts", "O"));
-                if (!Cools.isEmpty(locMast2)) {
+                LocMast locMast2 = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", shallowLoc));
+                if (Cools.isEmpty(locMast2)){
+                    continue;
+                }
+                if (locMast2.getLocSts().equals("P")||locMast2.getLocSts().equals("Q")||locMast2.getLocSts().equals("R")||locMast2.getLocSts().equals("S")){
+                    continue;
+                }
+                if (!Cools.isEmpty(locMast2)&&locMast2.getLocSts().equals("O")){
                     locMast = locMast2;
                     break;
                 }
+                if (!Cools.isEmpty(locMast2)&&(locMast2.getLocSts().equals("F")||locMast2.getLocSts().equals("D"))){
+                    locMast = locMast1;
+                    break;
+                }
+//                LocMast locMast2 = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", shallowLoc).eq("loc_sts", "O"));
+//                if (Cools.isEmpty(locMast2)) {
+//                    locMast = locMast1;
+//                    break;
+//                }
             }
         }
 
@@ -305,7 +388,232 @@
             // 褰撳墠宸烽亾鏃犵┖搴撲綅鏃讹紝閫掑綊璋冩暣鑷充笅涓�宸烽亾锛屾绱㈠叏閮ㄥ贩閬撴棤鏋滃悗锛岃烦鍑洪�掑綊
             if (times < rowCount) {
                 times = times + 1;
-                return getLocNo(1, staDescId, sourceStaNo, matnr, batch, grade,moveCrnNo, locTypeDto, times);
+                return getLocNo(1, staDescId, sourceStaNo, matnr, batch, grade, moveCrnNo, locTypeDto, times);
+            }
+//            // 2.搴撲綅褰撳墠鎵�灞炲昂瀵告棤绌哄簱浣嶆椂锛岃皟鏁村昂瀵稿弬鏁帮紝鍚戜笂鍏煎妫�绱㈠簱浣�
+//            if (locTypeDto.getLocType1() < 2) {
+//                int i = locTypeDto.getLocType1() + 1;
+//                locTypeDto.setLocType1((short)i);
+//                return getLocNo(1, staDescId, sourceStaNo, matnr,batch,grade, locTypeDto, 0);
+//            }
+            log.error("绯荤粺娌℃湁绌哄簱浣嶏紒锛侊紒 灏哄瑙勬牸锛� {}锛� 杞娆℃暟锛歿}", JSON.toJSONString(locTypeDto), times);
+            throw new CoolException("娌℃湁绌哄簱浣�");
+        }
+        String locNo = locMast.getLocNo();
+
+        // 鐢熸垚宸ヤ綔鍙�
+        int workNo = getWorkNo(0);
+        // 杩斿洖dto
+        startupDto.setWorkNo(workNo);
+        startupDto.setCrnNo(crnNo);
+        startupDto.setSourceStaNo(sourceStaNo);
+        startupDto.setLocNo(locNo);
+        return startupDto;
+    }
+
+    @Transactional
+    public StartupDto getLocNoEmpty(Integer whsType, Integer staDescId, Integer sourceStaNo, String matnr, String batch, String grade, LocTypeDto locTypeDto) {
+        whsType = Utils.GetWhsType(sourceStaNo);
+        return getLocNoEmpty(whsType, staDescId, sourceStaNo, matnr, batch, grade, 0, locTypeDto, 0);
+    }
+
+    @Transactional
+    public StartupDto getLocNoEmpty(Integer whsType, Integer staDescId, Integer sourceStaNo, String matnr, String batch, String grade, Integer moveCrnNo, LocTypeDto locTypeDto, int times) {
+        if (Cools.isEmpty(matnr)) {  //鐗╂枡鍙�
+            matnr = "";
+        }
+        if (Cools.isEmpty(batch)) {  //绠卞彿
+            batch = "";
+        }
+        if (Cools.isEmpty(grade)) {  //澶囩敤
+            grade = "";
+        }
+
+        // 鍒濆鍖栧弬鏁�
+        int crnNo = 0;      //鍫嗗灈鏈哄彿
+        int nearRow = 0;    //鏈�娴呭簱浣嶆帓
+        int curRow = 0;     //鏈�娣卞簱浣嶆帓
+        int rowCount = 0;   //杞杞
+        LocMast locMast = null;     // 鐩爣搴撲綅
+
+        StartupDto startupDto = new StartupDto();
+        RowLastno rowLastno = rowLastnoService.selectById(whsType);
+        int sRow = rowLastno.getsRow();
+        int eRow = rowLastno.geteRow();
+        int crnNumber = rowLastno.getCrnQty();
+
+        if (Cools.isEmpty(rowLastno)) {
+            throw new CoolException("鏁版嵁寮傚父锛岃鑱旂郴绠$悊鍛�");
+        }
+        // ===============>>>> 寮�濮嬫墽琛�
+        curRow = rowLastno.getCurrentRow();
+
+        if (!Cools.isEmpty(moveCrnNo) && moveCrnNo != 0) {
+            crnNumber = moveCrnNo;
+            if (times == 0) {
+                curRow = moveCrnNo * 4 - 1;
+            } else {
+                curRow = moveCrnNo * 4 - 2;
+            }
+        }
+
+        //姝ょ▼搴忕敤浜庝紭鍖栧爢鍨涙満寮傚父鏃剁殑杩愯鏃堕棿
+        for (int i = times; i < crnNumber; i++) {
+            int[] locNecessaryParameters = Utils.LocNecessaryParameters(whsType, curRow, crnNumber);
+            curRow = locNecessaryParameters[1];
+            crnNo = locNecessaryParameters[2];
+            if (basCrnpService.checkSiteError(crnNo, true)) {
+                rowCount = locNecessaryParameters[0];
+                nearRow = locNecessaryParameters[3];
+                break;
+            } else {
+                times++;
+            }
+        }
+        if (nearRow == 0) {
+            throw new CoolException("鏃犲彲鐢ㄥ爢鍨涙満");
+        }
+
+        boolean signRule1 = false;
+        boolean signRule2 = false;
+
+        Wrapper<StaDesc> wrapper = null;
+        StaDesc staDesc = null;
+        BasDevp staNo = null;
+
+        if (Utils.BooleanWhsTypeSta(whsType, staDescId)) {
+            // 鑾峰彇鐩爣绔�
+            wrapper = new EntityWrapper<StaDesc>()
+                    .eq("type_no", staDescId)
+                    .eq("stn_no", sourceStaNo)
+                    .eq("crn_no", crnNo);
+            staDesc = staDescService.selectOne(wrapper);
+            if (Cools.isEmpty(staDesc)) {
+                log.error("type_no={},stn_no={},crn_no={}", staDescId, sourceStaNo, crnNo);
+//                throw new CoolException("鍏ュ簱璺緞涓嶅瓨鍦�");
+                crnNo = 0;
+            }
+            staNo = basDevpService.selectById(staDesc.getCrnStn());
+            if (!staNo.getAutoing().equals("Y")) {
+//                throw new CoolException("鐩爣绔�"+staDesc.getCrnStn()+"涓嶅彲鐢�");
+                crnNo = 0;
+            }
+            startupDto.setStaNo(staNo.getDevNo());
+        }
+
+        // 鏇存柊搴撲綅鎺掑彿
+        if (Utils.BooleanWhsTypeSta(whsType, staDescId)) {
+            rowLastno.setCurrentRow(curRow);
+            rowLastnoService.updateById(rowLastno);
+        }
+
+        // 寮�濮嬫煡鎵惧簱浣� ==============================>>
+
+        // 1.鎸夎鍒欐煡鎵惧簱浣�
+        if (Cools.isEmpty(locMast) && crnNo != 0) {
+            List<LocMast> locMasts = new ArrayList<>();
+            List<LocMast> locMastsLow2 = new ArrayList<>();
+            List<LocMast> locMastsLow3 = new ArrayList<>();
+            locMastsLow2 = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", 2)
+                    .eq("loc_sts", "O").eq("loc_type1",locTypeDto.getLocType1()));
+            locMastsLow3 = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", 3)
+                    .eq("loc_sts", "O").eq("loc_type1",locTypeDto.getLocType1()));
+            if (locMastsLow2.size() <= 5 && locMastsLow3.size() <= 5){
+                locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow)
+                        .eq("loc_sts", "O").ge("lev1",8));
+                if (Cools.isEmpty(locMasts)){
+                    if (nearRow == 2){
+                        locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow + 1)
+                                .eq("loc_sts", "O").ge("lev1",8));
+                    }else if (nearRow == 3){
+                        locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow - 1)
+                                .eq("loc_sts", "O").ge("lev1",8));
+
+                    }
+                }
+            }else {
+                locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow)
+                        .eq("loc_sts", "O"));
+                if (Cools.isEmpty(locMasts)){
+                    if (nearRow == 2){
+                        locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow + 1)
+                                .eq("loc_sts", "O"));
+                    }else if (nearRow == 3){
+                        locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow - 1)
+                                .eq("loc_sts", "O"));
+
+                    }
+                }
+            }
+//            locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow)
+//                    .eq("loc_sts", "O").eq("loc_type1",locTypeDto.getLocType1()));
+//            if (Cools.isEmpty(locMasts) || locMasts.size() <= 5){
+//                if (nearRow == 2){
+//                    locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow + 1)
+//                            .eq("loc_sts", "O"));
+//                }else if (nearRow == 3){
+//                    locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow - 1)
+//                            .eq("loc_sts", "O"));
+//
+//                }
+//            }
+            if (locMasts.size() <= 5){
+                throw new CoolException("鏃犵┖搴撲綅鍙敤");
+            }
+            List<LocMast> locMastList = new ArrayList<>();
+            locMasts.sort(new Comparator<LocMast>() {
+                @Override
+                public int compare(LocMast o1, LocMast o2) {
+                    int b1 = o1.getBay1();
+                    int b2 = o2.getBay1();
+                    if (o1.getBay1() >= 15) {
+                        b1 = Math.abs(15 - o1.getBay1());
+                    }
+                    if (o2.getBay1() >= 15) {
+                        b2 = Math.abs(15 - o2.getBay1());
+                    }
+                    return b2 - b1;
+                }
+            });
+
+            for (LocMast locMast1 : locMasts) {
+//                if (!VersionUtils.locMoveCheckLocTypeComplete(locMast1, locTypeDto)) {
+//                    continue;
+//                }
+                String shallowLoc = Utils.getDeepLoc(slaveProperties, locMast1.getLocNo());
+                LocMast locMast2 = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", shallowLoc));
+                if (Cools.isEmpty(locMast2)){
+                    continue;
+                }
+                if (locMast2.getLocSts().equals("P")||locMast2.getLocSts().equals("Q")||locMast2.getLocSts().equals("R")||locMast2.getLocSts().equals("S")){
+                    continue;
+                }
+                if (!Cools.isEmpty(locMast2)&&locMast2.getLocSts().equals("O")){
+                    locMast = locMast2;
+                    break;
+                }
+                if (!Cools.isEmpty(locMast2)&&(locMast2.getLocSts().equals("F")||locMast2.getLocSts().equals("D"))){
+                    locMast = locMast1;
+                    break;
+                }
+//                LocMast locMast2 = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", shallowLoc).eq("loc_sts", "O"));
+//                if (Cools.isEmpty(locMast2)) {
+//                    locMast = locMast1;
+//                    break;
+//                }
+            }
+        }
+
+        if (!Cools.isEmpty(locMast) && !basCrnpService.checkSiteError(crnNo, true)) {
+            locMast = null;
+        }
+
+        // 閫掑綊鏌ヨ
+        if (Cools.isEmpty(locMast) || !locMast.getLocSts().equals("O")) {
+            // 褰撳墠宸烽亾鏃犵┖搴撲綅鏃讹紝閫掑綊璋冩暣鑷充笅涓�宸烽亾锛屾绱㈠叏閮ㄥ贩閬撴棤鏋滃悗锛岃烦鍑洪�掑綊
+            if (times < rowCount) {
+                times = times + 1;
+                return getLocNo(1, staDescId, sourceStaNo, matnr, batch, grade, moveCrnNo, locTypeDto, times);
             }
 //            // 2.搴撲綅褰撳墠鎵�灞炲昂瀵告棤绌哄簱浣嶆椂锛岃皟鏁村昂瀵稿弬鏁帮紝鍚戜笂鍏煎妫�绱㈠簱浣�
 //            if (locTypeDto.getLocType1() < 2) {

--
Gitblit v1.9.1