From 67a2c5fb41efcbcc028c8f4da0783c45f6b90f41 Mon Sep 17 00:00:00 2001
From: 1 <1@123>
Date: 星期一, 16 三月 2026 16:37:35 +0800
Subject: [PATCH] lsh#

---
 rsf-server/src/main/java/com/vincent/rsf/server/api/service/impl/AgvServiceImpl.java |  269 ++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 224 insertions(+), 45 deletions(-)

diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/api/service/impl/AgvServiceImpl.java b/rsf-server/src/main/java/com/vincent/rsf/server/api/service/impl/AgvServiceImpl.java
index f6ee6f4..af790ef 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/api/service/impl/AgvServiceImpl.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/api/service/impl/AgvServiceImpl.java
@@ -1,27 +1,48 @@
 package com.vincent.rsf.server.api.service.impl;
 
-import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.vincent.rsf.framework.common.Cools;
 import com.vincent.rsf.framework.common.R;
 import com.vincent.rsf.framework.exception.CoolException;
+import com.vincent.rsf.server.api.controller.erp.params.SyncOrderParams;
+import com.vincent.rsf.server.api.controller.erp.params.SyncOrdersItem;
 import com.vincent.rsf.server.api.service.AgvService;
+import com.vincent.rsf.server.api.service.ReceiveMsgService;
 import com.vincent.rsf.server.manager.controller.params.WaitPakinParam;
 import com.vincent.rsf.server.manager.entity.*;
+import com.vincent.rsf.server.manager.entity.param.AgvBindAndInTParam;
 import com.vincent.rsf.server.manager.enums.LocStsType;
 import com.vincent.rsf.server.manager.enums.PakinIOStatus;
+import com.vincent.rsf.server.manager.enums.StationTypeEnum;
+import com.vincent.rsf.server.manager.enums.TaskType;
 import com.vincent.rsf.server.manager.service.*;
+import com.vincent.rsf.server.manager.service.impl.DeviceSiteServiceImpl;
+import com.vincent.rsf.server.manager.service.impl.MatnrServiceImpl;
 import com.vincent.rsf.server.manager.utils.LocManageUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.CompletableFuture;
+import java.util.stream.Collectors;
 
 @Service
 public class AgvServiceImpl implements AgvService {
+
+    private static final String AGV_MISC_ORDER_PREFIX = "AGVT";
+    private static final DateTimeFormatter BATCH_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
+
+    @Autowired
+    private ReceiveMsgService receiveMsgService;
 
     @Autowired
     private WaitPakinService waitPakinService;
@@ -32,9 +53,15 @@
     @Autowired
     private WarehouseAreasService warehouseAreasService;
     @Autowired
+    private LocService locService;
+    @Autowired
     private TaskService taskService;
     @Autowired
     private BasContainerService basContainerService;
+    @Autowired
+    private DeviceSiteServiceImpl deviceSiteService;
+    @Autowired
+    private MatnrServiceImpl matnrService;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -58,6 +85,148 @@
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
+    public boolean AGVBindAndInTaskStart(String barcode, String sta) {
+        //楠岃瘉鏉$爜
+        checkStaStatus(barcode,sta);
+        return true;
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public R AGVBindAndInTaskStartT(AgvBindAndInTParam param, Long loginUserId) {
+        if (Objects.isNull(param)) {
+            throw new CoolException("鍙傛暟涓嶈兘涓虹┖锛侊紒");
+        }
+        if (Cools.isEmpty(param.getMatNr())) {
+            throw new CoolException("鐗╂枡缂栫爜涓嶈兘涓虹┖锛侊紒");
+        }
+        if (Cools.isEmpty(param.getPalletBarcode())) {
+            throw new CoolException("鎵樼洏鐮佷笉鑳戒负绌猴紒锛�");
+        }
+        if (Cools.isEmpty(param.getPalletSta())) {
+            throw new CoolException("鎿嶄綔绔欑偣涓嶈兘涓虹┖锛侊紒");
+        }
+
+        checkPalletBarcodeAvailable(param.getPalletBarcode());
+
+        Matnr matnr = matnrService.getOne(new LambdaQueryWrapper<Matnr>()
+                .eq(Matnr::getCode, param.getMatNr())
+                .last("limit 1"));
+        if (Objects.isNull(matnr)) {
+            throw new CoolException("鐗╂枡缂栫爜涓嶅瓨鍦細" + param.getMatNr());
+        }
+
+        long nowMillis = System.currentTimeMillis();
+        long nowSeconds = nowMillis / 1000;
+        String orderNo = buildAgvMiscOrderNo(param.getPalletBarcode(), nowMillis);
+        SyncOrderParams syncOrder = new SyncOrderParams()
+                .setWkType(SyncOrderParams.BusinessType.STK_MISCELLANEOUS)
+                .setType("in")
+                .setOrderNo(orderNo)
+                .setOrderInternalCode(nowMillis)
+                .setCreateTime(nowSeconds)
+                .setBusinessTime(nowSeconds)
+                .setAnfme(1.0)
+                .setOrderItems(Collections.singletonList(buildOrderItem(param, matnr, nowMillis, nowSeconds).setBarcode(param.getPalletBarcode())));
+        syncOrder.setStationId(param.getPalletSta());
+        R r = receiveMsgService.syncCheckOrder(Collections.singletonList(syncOrder), loginUserId);
+        if (!Objects.isNull(r) && Objects.equals(String.valueOf(r.get("code")), "200")) {
+            autoCallAgvInTask(param, loginUserId);
+        }
+        return R.ok(Cools.add("orderNo", orderNo).add("palletBarcode", param.getPalletBarcode()));
+    }
+
+    private void autoCallAgvInTask(AgvBindAndInTParam param, Long loginUserId) {
+        CompletableFuture.runAsync(() -> {
+            int retry = 0;
+            while (retry < 5) {
+                retry++;
+                try {
+                    Thread.sleep(3000);
+                    WaitPakin waitPakin = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>()
+                            .eq(WaitPakin::getBarcode, param.getPalletBarcode())
+                            .eq(WaitPakin::getIoStatus, PakinIOStatus.PAKIN_IO_STATUS_DONE.val)
+                            .last("limit 1"));
+                    if (Objects.isNull(waitPakin)) {
+                        continue;
+                    }
+
+                    BasStation basStation = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
+                            .eq(BasStation::getStationName, param.getPalletSta())
+                            .last("limit 1"));
+                    if (Objects.isNull(basStation)) {
+                        return;
+                    }
+//                    if (Objects.isNull(basStation) || Objects.isNull(basStation.getArea())) {
+//                        return;
+//                    }
+
+                    WaitPakinParam waitPakinPda = new WaitPakinParam()
+                            .setBarcode(param.getPalletBarcode())
+                            .setStaNo(param.getPalletSta())
+                            .setArea(param.getArea());
+                    AGVInTaskStart(waitPakinPda, loginUserId);
+                    return;
+                } catch (Exception ignored) {
+                }
+            }
+        });
+    }
+
+    private SyncOrdersItem buildOrderItem(AgvBindAndInTParam param, Matnr matnr, long uniqueSeed, long nowSeconds) {
+        String suffix = String.valueOf(uniqueSeed);
+        String batch = LocalDateTime.ofInstant(Instant.ofEpochSecond(nowSeconds), ZoneId.systemDefault())
+                .format(BATCH_FORMATTER);
+        SyncOrdersItem syncOrdersItem = new SyncOrdersItem()
+                .setModel(matnr.getModel())
+                .setAnfme(1.0)
+                .setUnit(matnr.getUnit())
+                .setBaseUnitId(matnr.getUnit())
+                .setPriceUnitId(matnr.getUnit())
+                .setBatch(batch)
+                .setPlanNo(AGV_MISC_ORDER_PREFIX + "-PLAN-" + suffix)
+                .setLineId(AGV_MISC_ORDER_PREFIX + "-LINE-" + suffix)
+                .setPlatItemId("1")
+                .setPalletId(param.getPalletBarcode());
+        syncOrdersItem.setMatnr(matnr.getCode());
+        syncOrdersItem.setMaktx(matnr.getName());
+        syncOrdersItem.setSpec(matnr.getSpec());
+        return syncOrdersItem;
+    }
+
+    private String buildAgvMiscOrderNo(String palletBarcode, long uniqueSeed) {
+        String cleanBarcode = palletBarcode.replaceAll("[^A-Za-z0-9]", "");
+        if (cleanBarcode.length() > 24) {
+            cleanBarcode = cleanBarcode.substring(cleanBarcode.length() - 24);
+        }
+        return AGV_MISC_ORDER_PREFIX + cleanBarcode + uniqueSeed;
+    }
+
+    private void checkPalletBarcodeAvailable(String palletBarcode) {
+        WaitPakin waitPakin = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>()
+                .eq(WaitPakin::getBarcode, palletBarcode)
+                .last("limit 1"));
+        if (!Objects.isNull(waitPakin)) {
+            throw new CoolException("鎵樼洏鐮侊細" + palletBarcode + "宸茶缁勬墭鍗曪細" + waitPakin.getCode() + "浣跨敤锛侊紒");
+        }
+
+        BasStation basStation = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
+                .eq(BasStation::getBarcode, palletBarcode)
+                .last("limit 1"));
+        if (!Objects.isNull(basStation)) {
+            throw new CoolException("鎵樼洏鐮侊細" + palletBarcode + "宸茶绔欑偣锛�" + basStation.getStationName() + "缁戝畾锛侊紒");
+        }
+
+        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>()
+                .eq(Loc::getBarcode, palletBarcode)
+                .last("limit 1"));
+        if (!Objects.isNull(loc)) {
+            throw new CoolException("鎵樼洏鐮侊細" + palletBarcode + "宸茶搴撲綅锛�" + loc.getCode() + "浣跨敤锛侊紒");
+        }
+    }
+
+    @Override
     public R getStaMsgSelect(Map<String, Object> params) {
         String sta = params.get("sta").toString();
         if (Cools.isEmpty(sta)) {
@@ -70,15 +239,17 @@
         if (Cools.isEmpty(basStation)) {
             throw new CoolException("鏈壘鍒版帴椹崇珯鐐逛俊鎭紝璇锋鏌ョ珯鐐圭姸鎬�");
         }
-        List<Long> ids = new ArrayList<>();
-        ids.add(basStation.getArea());
-        if (basStation.getIsCrossZone() == 1) {
-            String content = basStation.getCrossZoneArea().substring(1, basStation.getCrossZoneArea().length() - 1);
-            String[] parts = content.split(",");
-            for (int i = 0; i < parts.length; i++) {
-                ids.add(Long.parseLong(parts[i].trim()));
-            }
-        }
+        List<Long> ids = basStation.getCrossZoneArea().stream()
+                .map(Integer::longValue)
+                .collect(Collectors.toList());
+//        ids.add(basStation.getArea());
+//        if (basStation.getIsCrossZone() == 1) {
+//            String content = basStation.getCrossZoneArea().substring(1,.length() - 1);
+//            String[] parts = content.split(",");
+//            for (int i = 0; i < parts.length; i++) {
+//                ids.add(Long.parseLong(parts[i].trim()));
+//            }
+//        }
         List<WarehouseAreas> warehouseAreasList = warehouseAreasService.list(new LambdaQueryWrapper<WarehouseAreas>()
                 .in(WarehouseAreas::getId, ids)
         );
@@ -97,17 +268,26 @@
         String sta = waitPakinPda.getStaNo();
         String area = waitPakinPda.getArea();
         if (Cools.isEmpty(sta)) {
-            throw new CoolException("鎺ラ┏浣嶆潯鐮佷笉鑳戒负绌�");
+            throw new CoolException("璧风偣涓嶈兘涓虹┖");
         }
         if (Cools.isEmpty(area)) {
             throw new CoolException("鐩爣搴撳尯涓嶈兘涓虹┖");
         }
+
+        DeviceSite deviceSite = deviceSiteService.getOne(new LambdaQueryWrapper<DeviceSite>()
+                .eq(DeviceSite::getSite, sta)
+                .eq(DeviceSite::getAreaIdEnd, Long.parseLong(area))
+                .eq(DeviceSite::getType, TaskType.TASK_TYPE_IN.type).last("limit 1"));
+        if (Cools.isEmpty(deviceSite)) {
+            throw new CoolException("鏃犲彲鐢ㄨ矾寰勶紒锛�");
+        }
+
         BasStation basStation = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                 .eq(BasStation::getStationName, sta)
                 .eq(BasStation::getUseStatus, LocStsType.LOC_STS_TYPE_F.type)
         );
         if (Cools.isEmpty(basStation)) {
-            throw new CoolException("鏈壘鍒版帴椹崇珯鐐逛俊鎭紝璇锋鏌ョ珯鐐圭姸鎬�");
+            throw new CoolException("鏈壘鍒拌捣鐐圭珯鐐逛俊鎭紝璇锋鏌ョ珯鐐圭姸鎬�");
         }
         if (Cools.isEmpty(basStation.getBarcode())) {
             throw new CoolException("鏁版嵁閿欒锛屾帴椹崇珯鏃犳潯鐮佷俊鎭�");
@@ -121,9 +301,10 @@
         }
         String targetLoc = LocManageUtil.getTargetLoc(Long.parseLong(area));
 
-        taskService.generateAGVTasks(waitPakin, targetLoc, sta, loginUserId);
-
-        basStation.setUseStatus(LocStsType.LOC_STS_TYPE_R.type);
+        taskService.generateAGVTasks(waitPakin, targetLoc, sta, deviceSite.getDeviceCode(),loginUserId);
+        if (!basStation.getType().equals(0)){
+            basStation.setUseStatus(LocStsType.LOC_STS_TYPE_R.type);
+        }
         if (!basStationService.updateById(basStation)) {
             throw new CoolException("鏇存柊绔欑偣鐘舵�佸け璐�");
         }
@@ -191,15 +372,17 @@
         if (Cools.isEmpty(waitPakinItems)) {
             throw new CoolException("鏁版嵁閿欒锛屾湭鎵惧埌缁勬墭鏄庣粏");
         }
-        List<Long> ids = new ArrayList<>();
-        ids.add(basStation.getArea());
-        if (basStation.getIsCrossZone() == 1) {
-            String content = basStation.getCrossZoneArea().substring(1, basStation.getCrossZoneArea().length() - 1);
-            String[] parts = content.split(",");
-            for (int i = 0; i < parts.length; i++) {
-                ids.add(Long.parseLong(parts[i].trim()));
-            }
-        }
+        List<Long> ids = basStation.getCrossZoneArea().stream()
+                .map(Integer::longValue)
+                .collect(Collectors.toList());
+//        ids.add(basStation.getArea());
+//        if (basStation.getIsCrossZone() == 1) {
+//            String content = basStation.getCrossZoneArea().substring(1, basStation.getCrossZoneArea().length() - 1);
+//            String[] parts = content.split(",");
+//            for (int i = 0; i < parts.length; i++) {
+//                ids.add(Long.parseLong(parts[i].trim()));
+//            }
+//        }
         List<WarehouseAreas> warehouseAreasList = warehouseAreasService.list(new LambdaQueryWrapper<WarehouseAreas>()
                 .in(WarehouseAreas::getId, ids)
         );
@@ -221,7 +404,9 @@
         //楠岃瘉鍩虹淇℃伅
         BasStation basStation = checkStaStatus(barcode, sta,waitPakinPda.getArea());
         //鏇存柊绔欑偣鐘舵��
-        basStation.setUseStatus(LocStsType.LOC_STS_TYPE_F.type);
+        if (!basStation.getType().equals(0)){
+            basStation.setUseStatus(LocStsType.LOC_STS_TYPE_F.type);
+        }
         basStation.setBarcode(barcode);
         if (!basStationService.updateById(basStation)) {
             throw new CoolException("鏇存柊绔欑偣鐘舵�佸け璐�");
@@ -235,18 +420,12 @@
             throw new CoolException("瀹瑰櫒鐮佷笉鑳戒负绌�");
         }
         if (Cools.isEmpty(sta)) {
-            throw new CoolException("鎺ラ┏浣嶆潯鐮佷笉鑳戒负绌�");
+            throw new CoolException("绔欑偣淇℃伅涓嶈兘涓虹┖");
         }
-        WaitPakin waitPakin = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>()
-                .eq(WaitPakin::getBarcode, barcode)
-                .eq(WaitPakin::getIoStatus, PakinIOStatus.PAKIN_IO_STATUS_DONE.val)
-        );
-        if (Cools.isEmpty(waitPakin)) {
-            throw new CoolException("瀹瑰櫒鐮佹湭鎵惧埌缁勬墭淇℃伅,璇锋鏌ョ粍鎵樼姸鎬�");
-        }
+
         BasStation isBarcodeSta = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                         .eq(BasStation::getBarcode, barcode)
-                , false
+                .last("limit 1")
         );
         if (!Cools.isEmpty(isBarcodeSta)) {
             throw new CoolException("璇ユ潯鐮佸凡琚�" + isBarcodeSta.getStationName() + "绔欑粦瀹�");
@@ -257,11 +436,10 @@
         if (Cools.isEmpty(basStation)) {
             throw new CoolException("鏈壘鍒扮珯鐐逛俊鎭�");
         }
-        if (!basStation.getUseStatus().equals("O")) {
-            throw new CoolException("绔欑偣鐘舵�佷笉涓虹┖闂�");
-        }
         if (!Cools.isEmpty(basStation.getContainerType())) {
-            List<Long> longs1 = JSONObject.parseArray(basStation.getContainerType(), Long.class);
+            List<Long> longs1 = basStation.getContainerType().stream()
+                    .map(Integer::longValue)
+                    .collect(Collectors.toList());
             List<BasContainer> containers = basContainerService.list(
                     new LambdaQueryWrapper<BasContainer>()
                             .in(BasContainer::getContainerType, longs1)
@@ -274,9 +452,6 @@
                     break;  // 鎵惧埌鍖归厤鐨勫氨閫�鍑哄惊鐜�
                 }
             }
-//            boolean matches = containers.stream()
-//                    .map(BasContainer::getCodeType)
-//                    .anyMatch(codeType -> barcode.matches(codeType));
             if (!matches) {
                 throw new CoolException("鏉$爜涓庣珯鐐逛笉鍖归厤");
             }
@@ -316,13 +491,17 @@
         if (!basStation.getUseStatus().equals("O")) {
             throw new CoolException("绔欑偣鐘舵�佷笉涓虹┖闂�");
         }
+        if (basStation.getType().equals(StationTypeEnum.STATION_TYPE_MUTI.type)) {
+            throw new CoolException("绔欑偣涓哄厜鐢电珯鐐癸紝绂佹鍛煎彨AGV");
+        }
 
-        List<String> areaList = JSONObject.parseArray(basStation.getCrossZoneArea(), String.class);
-        if (!areaList.contains(area)) {
+        if (!basStation.getCrossZoneArea().contains(Integer.parseInt(area))) {
             throw new CoolException("褰撳墠绔欑偣涓嶆敮鎸佺洰鏍囧簱鍖�");
         }
         if (!Cools.isEmpty(basStation.getContainerType())) {
-            List<Long> longs1 = JSONObject.parseArray(basStation.getContainerType(), Long.class);
+            List<Long> longs1 = basStation.getContainerType().stream()
+                    .map(Integer::longValue)
+                    .collect(Collectors.toList());
             List<BasContainer> containers = basContainerService.list(
                     new LambdaQueryWrapper<BasContainer>()
                             .in(BasContainer::getContainerType, longs1)
@@ -332,7 +511,7 @@
             for (BasContainer container : containers) {
                 String codeType = container.getCodeType();  // 鑾峰彇姝e垯琛ㄨ揪寮�
                 if (barcode.matches(codeType)) {  // 鍒ゆ柇鏉$爜鏄惁绗﹀悎杩欎釜姝e垯
-                    List<Integer> areaList2 = container.getAreas();
+                    List<Integer> areaList2 = container.getAreasIds();
                     if (!areaList2.contains(Integer.valueOf(area))) {
                         matches2 = false;
                         continue;

--
Gitblit v1.9.1