From a1f7856f0f450883c7060444a4fc2b721720a051 Mon Sep 17 00:00:00 2001
From: zwl <1051256694@qq.com>
Date: 星期三, 15 四月 2026 11:00:05 +0800
Subject: [PATCH] 1.erp对接新增字段 2.电视机数据

---
 src/main/java/com/zy/asrs/service/impl/OpenServiceImpl.java |  896 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 831 insertions(+), 65 deletions(-)

diff --git a/src/main/java/com/zy/asrs/service/impl/OpenServiceImpl.java b/src/main/java/com/zy/asrs/service/impl/OpenServiceImpl.java
index 47ea86e..e9f10cb 100644
--- a/src/main/java/com/zy/asrs/service/impl/OpenServiceImpl.java
+++ b/src/main/java/com/zy/asrs/service/impl/OpenServiceImpl.java
@@ -19,6 +19,7 @@
 import com.zy.asrs.utils.Utils;
 import com.zy.common.constant.AgvConstant;
 import com.zy.common.constant.ArmConstant;
+import com.zy.common.entity.Parameter;
 import com.zy.common.model.DetlDto;
 import com.zy.common.model.LocDetlDto;
 import com.zy.common.model.LocDto;
@@ -33,7 +34,8 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import javax.rmi.CORBA.Util;
+import java.math.BigDecimal;
+import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -43,6 +45,15 @@
 @Slf4j
 @Service
 public class OpenServiceImpl implements OpenService {
+
+    private static final Map<Integer, BigDecimal> INBOUND_WEIGHT_FACTOR_BY_SOURCE_STA;
+    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
+
+    static {
+        Map<Integer, BigDecimal> factorMap = new HashMap<>();
+        factorMap.put(112, new BigDecimal("0.98"));
+        INBOUND_WEIGHT_FACTOR_BY_SOURCE_STA = Collections.unmodifiableMap(factorMap);
+    }
 
     @Autowired
     private OrderService orderService;
@@ -88,16 +99,32 @@
     private String mesUrl;
     @Value("${mes.stationaddress}")
     private String stationAddress;
+    @Value("${erp.address.URL:}")
+    private String erpUrl;
+    @Value("${erp.switch.ErpReportOld}")
+    private boolean erpReportOld;
+    @Value("${erp.address.Inaddress:}")
+    private String erpInAddress;
+    @Value("${erp.address.OutErroraddress:}")
+    private String erpOutErrorAddress;
     @Autowired
     private WaitPakinService waitPakinService;
     @Autowired
+    private WaitPakinLogService waitPakinLogService;
+    @Autowired
     private WrkMastService wrkMastService;
+    @Autowired
+    private WrkMastLogService wrkMastLogService;
+    @Autowired
+    private WrkDetlLogService wrkDetlLogService;
     @Autowired
     private WcsApiService wcsApiService;
     @Autowired
     private WorkService workService;
     @Autowired
     private BasCrnpService basCrnpService;
+    @Autowired
+    private ApiLogService apiLogService;
 
     @Override
     @Transactional
@@ -375,58 +402,87 @@
     @Transactional
     public R pakoutOrderPause(OpenOrderPakoutPauseParam param) {
         if (param == null || Cools.isEmpty(param.getOrderId())) {
-            throw new CoolException("orderNo is empty");
+            throw new CoolException("orderNo涓嶈兘涓虹┖");
+        }
+        if (param.getExecute() == null) {
+            throw new CoolException("execute涓嶈兘涓虹┖");
         }
 
         List<WrkMast> activeTasks = findActiveOutboundTasks(param.getOrderId());
-        Map<String, Object> result = new HashMap<>();
-        result.put("orderNo", param.getOrderId());
-        result.put("taskCount", activeTasks.size());
-        if (activeTasks.isEmpty()) {
-            result.put("confirmedCount", 0);
-            return R.ok("no active out tasks").add(result);
-        }
-
-        Date now = new Date();
-        int confirmedCount = 0;
-        for (WrkMast wrkMast : activeTasks) {
-            if (wrkMast == null || "Y".equalsIgnoreCase(wrkMast.getPdcType())) {
-                continue;
+        if (Objects.equals(param.getExecute(), 1)) {
+            // ERP纭绔嬪嵆鎵ц锛屼粎澶勭悊寰呬笅鍙戠殑鍑哄簱浠诲姟銆�
+            List<WrkMast> pendingTasks = activeTasks.stream()
+                    .filter(wrkMast -> wrkMast != null && Objects.equals(wrkMast.getWrkSts(), 11L))
+                    .collect(Collectors.toList());
+            Map<String, Object> result = new HashMap<>();
+            result.put("orderNo", param.getOrderId());
+            result.put("taskCount", pendingTasks.size());
+            if (pendingTasks.isEmpty()) {
+                result.put("confirmedCount", 0);
+                return R.ok("鏃犳湁鏁堝嚭搴撲换鍔�").add(result);
             }
-            wrkMast.setPdcType("Y");
+
+            Date now = new Date();
+            int confirmedCount = 0;
+            for (WrkMast wrkMast : pendingTasks) {
+                if (wrkMast == null || "Y".equalsIgnoreCase(wrkMast.getPdcType())) {
+                    continue;
+                }
+                wrkMast.setPdcType("Y");
 //            wrkMast.setUpdMk("ERP_CONFIRMED");
 //            wrkMast.setManuType("ERP_CONFIRM_OUT");
-            wrkMast.setModiTime(now);
-            wrkMast.setModiUser(9527L);
-            if (!wrkMastService.updateById(wrkMast)) {
-                throw new CoolException("confirm out task failed: " + wrkMast.getWrkNo());
+                wrkMast.setModiTime(now);
+                wrkMast.setModiUser(9527L);
+                if (!wrkMastService.updateById(wrkMast)) {
+                    throw new CoolException("纭鎵ц鍑哄簱浠诲姟澶辫触: " + wrkMast.getWrkNo());
+                }
+                confirmedCount++;
             }
-            confirmedCount++;
-        }
 
-        result.put("confirmedCount", confirmedCount);
-        return R.ok(confirmedCount == 0 ? "tasks already confirmed" : "erp confirm out success").add(result);
+            result.put("confirmedCount", confirmedCount);
+            return R.ok(confirmedCount == 0 ? "浠诲姟宸茬‘璁ゆ墽琛�" : "ERP纭鎵ц鍑哄簱鎴愬姛").add(result);
+        }
+        if (Objects.equals(param.getExecute(), 2)) {
+            // ERP璇锋眰鍙栨秷浠诲姟锛氱洿鎺ユ敹闆嗕换鍔″彿锛屾寜 taskList 鏍煎紡鍙戦�佺粰 WCS銆�
+            Map<String, Object> result = new HashMap<>();
+            result.put("orderNo", param.getOrderId());
+            result.put("execute", param.getExecute());
+            result.put("taskCount", activeTasks.size());
+            if (activeTasks.isEmpty()) {
+                return R.ok("鏃犳湁鏁堝嚭搴撲换鍔�").add(result);
+            }
+            List<HashMap<String,Object>> taskList = new ArrayList<>();
+            for (WrkMast wrkMast : activeTasks) {
+                HashMap<String,Object> hashMap = new HashMap<>();
+                hashMap.put("taskNo", wrkMast.getWrkNo());
+                if (!Cools.isEmpty(wrkMast) && wrkMast.getWrkSts() ==11L) {
+                    workService.cancelWrkMast(wrkMast.getWrkNo()+"", 9955L);
+                    continue;
+                }
+                taskList.add(hashMap);
+            }
+            wcsApiService.pauseOutTasks(taskList);
+            return R.ok("鍙栨秷浠诲姟宸插彂閫佽嚦WCS").add(result);
+        }
+        throw new CoolException("reason浠呮敮鎸�1鎴�2");
+    }
+
+    /** WCS 杩斿洖闈炴垚鍔熺爜鏃舵姏閿� */
+    private void requireWcsPauseOk(R wcsR) {
+        if (wcsR == null) {
+            throw new CoolException("WCS鍙栨秷鍑哄簱浠诲姟鏃犺繑鍥�");
+        }
+        Object codeObj = wcsR.get("code");
+        int code = codeObj instanceof Number ? ((Number) codeObj).intValue() : -1;
+        if (code != 200) {
+            Object msgObj = wcsR.get("msg");
+            throw new CoolException(msgObj == null ? "WCS鍙栨秷鍑哄簱浠诲姟澶辫触" : String.valueOf(msgObj));
+        }
     }
 
     @Override
-    @Transactional
     public R pakoutOrderExecute(OpenOrderPakoutExecuteParam param) {
-        if (param == null || Cools.isEmpty(param.getOrderId())) {
-            throw new CoolException("orderId is empty");
-        }
-        if (param.getExecute() == null) {
-            throw new CoolException("execute is empty");
-        }
-        if (Objects.equals(param.getExecute(), 1)) {
-            return createPakoutTasks(param.getOrderId());
-        }
-        if (Objects.equals(param.getExecute(), 2)) {
-            OpenOrderPakoutPauseParam pauseParam = new OpenOrderPakoutPauseParam();
-            pauseParam.setOrderId(param.getOrderId());
-//            pauseParam.setReason("OPEN_API_CONFIRM_OUT");
-            return pakoutOrderPause(pauseParam);
-        }
-        throw new CoolException("execute only supports 1 or 2");
+        return null;
     }
 
     private List<WrkMast> findActiveOutboundTasks(String orderNo) {
@@ -444,7 +500,7 @@
         List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>()
                 .in("wrk_no", wrkNos)
                 .in("io_type", Arrays.asList(101, 103, 104, 107, 108, 110))
-                .eq("wrk_sts", 11L));
+                .in("wrk_sts", Arrays.asList(11L, 12L, 13L)));
         if (wrkMasts == null || wrkMasts.isEmpty()) {
             return Collections.emptyList();
         }
@@ -555,24 +611,12 @@
         return orderDetl.getMatnr() + "|batch=" + (orderDetl.getBatch() == null ? "" : orderDetl.getBatch()) + "|lack=" + lackQty;
     }
 
-    private boolean needNotifyWcsStop(WrkMast wrkMast) {
-        return wrkMast != null
-                && wrkMast.getWrkSts() != null
-                && wrkMast.getWrkSts() >= 12L
-                && wrkMast.getWrkSts() < 14L;
-    }
-
-    private StopOutTaskParams buildStopOutTaskParams(OpenOrderPakoutPauseParam param, List<WrkMast> wrkMasts) {
+    private StopOutTaskParams buildStopOutTaskParams(List<WrkMast> wrkMasts) {
         StopOutTaskParams stopParams = new StopOutTaskParams();
-        stopParams.setOrderNo(param.getOrderId());
-        stopParams.setReason(param.getReason());
+        // WCS鍙栨秷鎺ュ彛浠呮帴鏀� taskList.taskNo銆�
         for (WrkMast wrkMast : wrkMasts) {
             StopOutTaskParams.TaskItem item = new StopOutTaskParams.TaskItem();
             item.setTaskNo(String.valueOf(wrkMast.getWrkNo()));
-            if (wrkMast.getStaNo() != null) {
-                item.setStaNo(String.valueOf(wrkMast.getStaNo()));
-            }
-            item.setLocNo(wrkMast.getSourceLocNo());
             stopParams.getTasks().add(item);
         }
         return stopParams;
@@ -637,6 +681,117 @@
     @Transactional
     public List<StockVo> queryStock() {
         return locDetlService.queryStockTotal();
+    }
+
+    @Override
+    public R reportPakinHistoryToErp(List<String> barcodes) {
+        List<String> normalizedBarcodes = normalizeBarcodes(barcodes);
+        if (normalizedBarcodes.isEmpty()) {
+            return R.error("鎵樼洏鐮侀泦鍚堜笉鑳戒负绌�");
+        }
+        if (!isErpReportEnabled()) {
+            return R.error("ERP reporting is disabled");
+        }
+        if (Cools.isEmpty(erpInAddress)) {
+            return R.error("ERP鍏ュ簱涓婃姤鍦板潃鏈厤缃�");
+        }
+
+        Map<String, WrkMastLog> latestInboundLogMap = loadLatestInboundHistoryLogMap(normalizedBarcodes);
+        Map<Integer, List<WrkDetlLog>> wrkDetlLogMap = loadWrkDetlLogMap(latestInboundLogMap.values());
+        Map<String, WaitPakinLog> waitPakinLogMap = loadWaitPakinLogMap(normalizedBarcodes);
+        String requestUrl = buildErpInboundRequestUrl();
+
+        List<Map<String, Object>> rows = new ArrayList<>();
+        int successCount = 0;
+        int failCount = 0;
+
+        for (String barcode : normalizedBarcodes) {
+            Map<String, Object> row = new LinkedHashMap<>();
+            row.put("barcode", barcode);
+
+            WrkMastLog wrkMastLog = latestInboundLogMap.get(barcode);
+            if (wrkMastLog == null) {
+                row.put("success", false);
+                row.put("message", "鏈壘鍒版渶鏂板叆搴撳巻鍙茶褰�");
+                rows.add(row);
+                failCount++;
+                continue;
+            }
+
+            WaitPakinLog waitPakinLog = waitPakinLogMap.get(barcode);
+            List<WrkDetlLog> wrkDetlLogs = wrkDetlLogMap.getOrDefault(wrkMastLog.getWrkNo(), Collections.emptyList());
+            ErpPakinReportParam param = buildInboundErpParam(barcode, wrkMastLog, wrkDetlLogs, waitPakinLog);
+            if (Cools.isEmpty(param.getPalletId())) {
+                row.put("success", false);
+                row.put("message", "鎵樼洏鐮佺己灏戜笂鎶ュ瓧娈礫palletId]");
+                rows.add(row);
+                failCount++;
+                continue;
+            }
+            if (Cools.isEmpty(param.getLocId())) {
+                row.put("success", false);
+                row.put("message", "鎵樼洏鐮佺己灏戜笂鎶ュ瓧娈礫locId]");
+                rows.add(row);
+                failCount++;
+                continue;
+            }
+
+            String request = JSON.toJSONString(param);
+            String response = "";
+            boolean success = false;
+            String errorMsg = null;
+            try {
+                response = new HttpHandler.Builder()
+                        .setUri(erpUrl)
+                        .setPath(erpInAddress)
+                        .setJson(request)
+                        .build()
+                        .doPost();
+                success = isErpCallSuccess(response);
+                if (!success) {
+                    errorMsg = extractErpCallError(response);
+                }
+            } catch (Exception e) {
+                errorMsg = e.getMessage();
+            } finally {
+                try {
+                    apiLogService.save(
+                            "Inbound ERP Report",
+                            requestUrl,
+                            null,
+                            "127.0.0.1",
+                            request,
+                            response,
+                            success,
+                            "barcode=" + barcode + ",workNo=" + wrkMastLog.getWrkNo()
+                    );
+                } catch (Exception logEx) {
+                    log.error("save inbound erp api log failed", logEx);
+                }
+            }
+
+            row.put("workNo", wrkMastLog.getWrkNo());
+            Integer sourceStaNo = resolveInboundSourceStaNo(wrkMastLog);
+            row.put("sourceStaNo", sourceStaNo);
+            row.put("weightFactor", resolveInboundWeightFactor(sourceStaNo));
+            row.put("erpWeight", param.getWeight());
+            row.put("success", success);
+            row.put("message", success ? "OK" : errorMsg);
+            rows.add(row);
+
+            if (success) {
+                successCount++;
+            } else {
+                failCount++;
+            }
+        }
+
+        Map<String, Object> result = new LinkedHashMap<>();
+        result.put("total", normalizedBarcodes.size());
+        result.put("successCount", successCount);
+        result.put("failCount", failCount);
+        result.put("rows", rows);
+        return R.ok().add(result);
     }
 
     @Override
@@ -1221,7 +1376,10 @@
                 stationParams.add(stationParam);
             }
         }
+        String requestJson = JSON.toJSONString(stationParams);
         String response = "";
+        boolean pushOk = false;
+        String pushUrl = buildMesStationRequestUrl();
         try {
             //鑾峰彇Cookie鍊�
             HashMap<String, Object> headers = new HashMap<>();
@@ -1231,21 +1389,55 @@
                     .setHeaders(headers)
                     .setUri(mesUrl)
                     .setPath(stationAddress)
-                    .setJson(JSON.toJSONString(stationParams))
+                    .setJson(requestJson)
                     .build()
                     .doPost();
             JSONObject jsonResponse = JSON.parseObject(response);
-            if (jsonResponse.getInteger("code") == 200) {
-
+            if (jsonResponse != null && Integer.valueOf(200).equals(jsonResponse.getInteger("code"))) {
+                pushOk = true;
             } else {
                 return R.error();
             }
         } catch (Exception e) {
             e.printStackTrace();
+        } finally {
+            try {
+                apiLogService.save(
+                        "鎺‥RP-绔欑偣鍚屾",
+                        pushUrl,
+                        null,
+                        "127.0.0.1",
+                        requestJson,
+                        response,
+                        pushOk,
+                        "绔欑偣鍚屾鎺ㄥ鏂�"
+                );
+            } catch (Exception logEx) {
+                log.error("save station sync api log failed", logEx);
+            }
         }
         return R.ok();
     }
 
+    private String buildMesStationRequestUrl() {
+        if (Cools.isEmpty(mesUrl)) {
+            return stationAddress;
+        }
+        if (stationAddress == null) {
+            return mesUrl;
+        }
+        if (mesUrl.endsWith("/") && stationAddress.startsWith("/")) {
+            return mesUrl + stationAddress.substring(1);
+        }
+        if (!mesUrl.endsWith("/") && !stationAddress.startsWith("/")) {
+            return mesUrl + "/" + stationAddress;
+        }
+        return mesUrl + stationAddress;
+    }
+
+    /**
+     * 7.3 缁勬墭淇℃伅涓嬪彂銆�
+     */
     @Override
     public R mesToComb(MesToCombParam param) {
         if (Cools.isEmpty(param.getPalletId())) {
@@ -1258,7 +1450,12 @@
 
             if (param.getFull() == 1) {
                 //婊℃墭鐩�
-                mat = matService.selectByMatnr("emptyPallet");
+                if(param.getBoxType1().equals("aws")){
+                    mat = matService.selectByMatnr("amazon");
+                }else {
+                    mat = matService.selectByMatnr("cloudWarehouse");
+                }
+
             } else if (param.getFull() == 0) {
                 //绌烘墭鐩�
                 mat = matService.selectByMatnr("emptyPallet");
@@ -1273,31 +1470,65 @@
 
         waitPakin.setIoStatus("N");     // 鍏ュ嚭鐘舵��
         waitPakin.setAnfme(param.getAnfme());  // 鏁伴噺
+        waitPakin.setFreqType(param.getFreqType());
         waitPakin.setStatus("Y");    // 鐘舵��
         waitPakin.setAppeUser(9995L);
         waitPakin.setAppeTime(now);
         waitPakin.setModiUser(9995L);
         waitPakin.setModiTime(now);
-        waitPakin.setOrderNo(String.valueOf(param.getOrderId()));
+        waitPakin.setOrderNo(String.valueOf(param.getBizNo()));
         waitPakin.setOrigin(String.valueOf(param.getStorageArea()));//寤鸿鍏ュ簱鍖哄煙
         waitPakin.setManu(String.valueOf(param.getLocId()));//mes鍏蜂綋搴撲綅缂栧彿
         waitPakin.setThreeCode(param.getBizNo());
+        // 7.3 鏂板瀛楁澶嶇敤鐜版湁缁勬墭妗f壙杞戒綅锛屼笉鏂板鍒椼��
+        waitPakin.setSuppCode(param.getCustomerId());
+        waitPakin.setSupp(param.getCustomerName());
+        waitPakin.setStandby3(param.getItem());
+        waitPakin.setStandby1(param.getEntryWmsCode());
+        waitPakin.setStandby2(param.getOutDoorNo());
         waitPakin.setBeBatch(param.getPackage1());//鏄惁鏁h揣锛�0 闈炴暎璐э紱1 鏁h揣锛涗负浜嗙鎺у嚭璐ч�熺巼锛屾暎璐у彲浠ュ嚭鎱㈢偣銆�
+        // ERP 鍏ュ彛榛樿鎵� erp锛孧QTT 缁勬墭浼氬湪鍙傛暟閲屾樉寮忎紶 aws銆�
+        waitPakin.setBoxType1(Cools.isEmpty(param.getBoxType1()) ? "erp" : param.getBoxType1());
         if (!waitPakinService.insert(waitPakin)) {
             throw new CoolException("淇濆瓨鍏ュ簱閫氱煡妗eけ璐�");
         }
-        return null;
+        return R.ok().add(Cools.add("palletId", param.getPalletId()).add("orderId", param.getOrderId()));
     }
 
+    /**
+     * 7.11 鍑哄簱閫氱煡鍗曪紙浼犻�掓湁搴忔棤搴忚鍒欙級鍗曟潯寤哄崟銆�
+     */
     @Override
-    public R outOrder(OutTaskParam param) {
+    public R outOrder(OutTaskParam param,int count) {
+        return outOrder(param, count, 1);
+    }
+
+    private R outOrder(OutTaskParam param, int count, int teu) {
         LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_sts", "F").eq("barcode", param.getPalletId()));
         if (locMast == null) {
-            return R.error("娌℃湁鎵惧埌鎵樼洏鐮�=" + param.getPalletId() + "瀵瑰簲鐨勫簱浣�");
+            throw new CoolException("娌℃湁鎵惧埌鎵樼洏鐮�=" + param.getPalletId() + "瀵瑰簲鐨勫簱浣�");
+        }
+        if (Cools.isEmpty(param.getStationId())) {
+            throw new CoolException("鍑哄簱鍙g紪鐮佷笉鑳戒负绌�");
+        }
+        if (Cools.isEmpty(param.getBatchSeq())) {
+            throw new CoolException("鎵规鏍囪瘑涓嶈兘涓虹┖");
+        }
+        if (param.getSeq() == null || param.getSeq() < 0) {
+            throw new CoolException("鍑哄簱椤哄簭涓嶈兘涓虹┖涓斾笉鑳藉皬浜�0");
         }
         Integer ioType = 101;
         // 鑾峰彇璺緞
-        StaDesc staDesc = staDescService.queryCrnStn(ioType, locMast.getCrnNo(), Integer.valueOf(param.getStationId()));
+        Integer stationId;
+        try {
+            stationId = Integer.valueOf(param.getStationId());
+        } catch (NumberFormatException ex) {
+            throw new CoolException("鍑哄簱鍙g紪鐮佹牸寮忛敊璇細" + param.getStationId());
+        }
+        StaDesc staDesc = staDescService.queryCrnStn(ioType, locMast.getCrnNo(), stationId);
+        if (staDesc == null) {
+            throw new CoolException("鏈壘鍒板嚭搴撳彛=" + param.getStationId() + "瀵瑰簲璺緞");
+        }
         Date now = new Date();
         // 鐢熸垚宸ヤ綔鍙�
         int workNo = commonService.getWorkNo(WorkNoType.getWorkNoType(ioType));
@@ -1319,7 +1550,10 @@
         wrkMast.setEmptyMk("N"); // 绌烘澘
         wrkMast.setLinkMis("N");
         wrkMast.setPdcType("N");
+        wrkMast.setContainerNo(param.getContainerNo());
+        // 7.11锛歰rderId 瀛� userNo锛宐atchSeq 瀛樻壒娆℃爣璇嗭紝seq 瀛樻壒娆″唴椤哄簭銆�
         wrkMast.setUserNo(param.getOrderId());//璁㈠崟鍙�
+        wrkMast.setBatchSeq(param.getBatchSeq());//璁㈠崟鍐呮壒娆℃爣璇�
         wrkMast.setPltType(param.getSeq());//鍑哄簱椤哄簭锛屼粠1寮�濮�
         wrkMast.setTakeNone("0");  //0闈炶嚜鍔�
         wrkMast.setAppeUser(9995L); // 鎿嶄綔浜哄憳鏁版嵁
@@ -1346,6 +1580,11 @@
             wrkDetl.setAppeUser(9995L);
             wrkDetl.setModiTime(now);
             wrkDetl.setModiUser(9995L);
+            wrkDetl.setTeu(teu);
+            // 7.11锛歟ntryWmsCode銆乷utDoorNo 澶嶇敤鏄庣粏澶囩敤瀛楁銆�
+            wrkDetl.setStandby1(param.getEntryWmsCode());
+            wrkDetl.setStandby2(param.getOutDoorNo());
+            wrkDetl.setSupp(param.getSeq()+"/"+count);
 
             if (!wrkDetlService.insert(wrkDetl)) {
                 throw new CoolException("淇濆瓨宸ヤ綔妗f槑缁嗗け璐�");
@@ -1364,7 +1603,534 @@
             log.error(locMast.getLocNo() + "搴撲綅涓嶆槸鍦ㄥ簱鐘舵��");
             throw new CoolException(locMast.getLocNo() + "搴撲綅涓嶆槸鍦ㄥ簱鐘舵��");
         }
+        return R.ok().add(Cools.add("wrkNo", workNo).add("orderId", param.getOrderId()));
+    }
+
+    /**
+     * 7.11 鍑哄簱閫氱煡鍗曪紙浼犻�掓湁搴忔棤搴忚鍒欙級鎵归噺寤哄崟銆�
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public R outOrderBatch(List<OutTaskParam> params) {
+        int n = params.size();
+        Map<String, Integer> batchLineCounts = new HashMap<>();
+        Map<String, Integer> batchTeuCounts = buildOutOrderBatchTeuCounts(params);
+        for (OutTaskParam outTaskParam : params) {
+            batchLineCounts.merge(buildOutOrderBatchKey(outTaskParam), 1, Integer::sum);
+        }
+        for (OutTaskParam outTaskParam : params) {
+            int count = batchLineCounts.getOrDefault(buildOutOrderBatchKey(outTaskParam), n);
+            int teu = batchTeuCounts.getOrDefault(outTaskParam.getBatchSeq(), 1);
+            R r = outOrder(outTaskParam, count, teu);
+            if (!Objects.equals(r.get("code"), 200)) {
+                throw new CoolException("鍑哄簱寤哄崟澶辫触");
+            }
+        }
         return R.ok();
     }
+
+    /**
+     * 7.9 鍑哄簱寮傚父鍙樺姩涓婃姤銆�
+     * WCS 鍙紶 palletId銆乪rrorMsg锛學MS 瑙f瀽 orderId 鍚庤浆鍙� ERP銆�
+     */
+    @Override
+    public R outOrderAbnormalReport(OutOrderAbnormalReportParam param) {
+        if (param == null || Cools.isEmpty(param.getPalletId())) {
+            return R.error("palletId涓嶈兘涓虹┖");
+        }
+        String orderId = resolveOutboundOrderId(param.getPalletId());
+        if (Cools.isEmpty(orderId)) {
+            return R.error("鏈壘鍒版墭鐩樺搴斿嚭搴撳崟鍙凤細" + param.getPalletId());
+        }
+        if (Cools.isEmpty(erpOutErrorAddress)) {
+            return R.error("ERP鍑哄簱寮傚父涓婃姤鍦板潃鏈厤缃�");
+        }
+
+        ErpOutOrderAbnormalReportParam erpParam = new ErpOutOrderAbnormalReportParam();
+        erpParam.setPalletId(param.getPalletId());
+        erpParam.setErrorMsg(param.getErrorMsg());
+        erpParam.setOrderId(orderId);
+
+        String requestJson = JSON.toJSONString(erpParam);
+        String response = "";
+        boolean pushOk = false;
+        String errorMsg = null;
+        String pushUrl = buildErpOutErrorRequestUrl();
+        try {
+            response = new HttpHandler.Builder()
+                    .setUri(erpUrl)
+                    .setPath(erpOutErrorAddress)
+                    .setJson(requestJson)
+                    .build()
+                    .doPost();
+            pushOk = isErpCallSuccess(response);
+            if (!pushOk) {
+                errorMsg = extractErpCallError(response);
+            }
+        } catch (Exception e) {
+            errorMsg = e.getMessage();
+            log.error("鎺‥RP鍑哄簱寮傚父涓婃姤澶辫触, palletId={}, orderId={}", param.getPalletId(), orderId, e);
+        } finally {
+            try {
+                apiLogService.save(
+                        "鎺‥RP-鍑哄簱寮傚父涓婃姤",
+                        pushUrl,
+                        null,
+                        "127.0.0.1",
+                        requestJson,
+                        response,
+                        pushOk,
+                        "palletId=" + param.getPalletId() + ",orderId=" + orderId
+                );
+            } catch (Exception logEx) {
+                log.error("save out abnormal api log failed", logEx);
+            }
+        }
+        if (!pushOk) {
+            return R.error(Cools.isEmpty(errorMsg) ? "ERP鍑哄簱寮傚父涓婃姤澶辫触" : errorMsg);
+        }
+        return R.ok().add(Cools.add("palletId", param.getPalletId()).add("orderId", orderId));
+    }
+
+    /**
+     * 7.10 鍑哄簱寮傚父鍙樺姩澶勭悊銆�
+     * 鏈湡鍙帴鏀躲�佹牎楠屽拰璁板綍锛屼笉鏀规湰鍦颁换鍔★紝涔熶笉杞彂 WCS銆�
+     */
+    @Override
+    public R outOrderAbnormalHandle(OutOrderAbnormalHandleParam param) {
+        if (param == null || Cools.isEmpty(param.getPalletId())) {
+            return R.error("palletId涓嶈兘涓虹┖");
+        }
+        if (param.getOperateType() == null || !(Objects.equals(param.getOperateType(), 0) || Objects.equals(param.getOperateType(), 2))) {
+            return R.error("operateType鍙厑璁�0鎴�2");
+        }
+        if (!existsOutboundPallet(param.getPalletId())) {
+            return R.error("鏈壘鍒版墭鐩橈細" + param.getPalletId());
+        }
+        log.info("鎺ユ敹鍑哄簱寮傚父澶勭悊鎸囦护, palletId={}, operateType={}", param.getPalletId(), param.getOperateType());
+        return R.ok().add(Cools.add("palletId", param.getPalletId()).add("operateType", param.getOperateType()));
+    }
+
+    private String buildOutOrderBatchKey(OutTaskParam param) {
+        return param.getOrderId() + "#" + param.getBatchSeq();
+    }
+
+    private Map<String, Integer> buildOutOrderBatchTeuCounts(List<OutTaskParam> params) {
+        Map<String, Set<String>> batchOrderIds = new HashMap<>();
+        for (OutTaskParam param : params) {
+            batchOrderIds.computeIfAbsent(param.getBatchSeq(), k -> new LinkedHashSet<>()).add(param.getOrderId());
+        }
+        Map<String, Integer> batchTeuCounts = new HashMap<>();
+        for (Map.Entry<String, Set<String>> entry : batchOrderIds.entrySet()) {
+            batchTeuCounts.put(entry.getKey(), entry.getValue().size());
+        }
+        return batchTeuCounts;
+    }
+
+    private String resolveOutboundOrderId(String palletId) {
+        List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>()
+                .eq("io_type", 101)
+                .eq("barcode", palletId)
+                .orderBy("io_time", false)
+                .orderBy("wrk_no", false));
+        if (!Cools.isEmpty(wrkMasts)) {
+            for (WrkMast wrkMast : wrkMasts) {
+                if (!Cools.isEmpty(wrkMast.getUserNo())) {
+                    return wrkMast.getUserNo();
+                }
+            }
+        }
+        List<WrkDetl> wrkDetls = wrkDetlService.selectList(new EntityWrapper<WrkDetl>()
+                .eq("zpallet", palletId)
+                .orderBy("io_time", false)
+                .orderBy("wrk_no", false));
+        if (!Cools.isEmpty(wrkDetls)) {
+            for (WrkDetl wrkDetl : wrkDetls) {
+                if (!Cools.isEmpty(wrkDetl.getOrderNo())) {
+                    return wrkDetl.getOrderNo();
+                }
+            }
+        }
+        return null;
+    }
+
+    private boolean existsOutboundPallet(String palletId) {
+        if (locDetlService.selectCount(new EntityWrapper<LocDetl>().eq("zpallet", palletId)) > 0) {
+            return true;
+        }
+        if (wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("io_type", 101).eq("barcode", palletId)) > 0) {
+            return true;
+        }
+        return wrkDetlService.selectCount(new EntityWrapper<WrkDetl>().eq("zpallet", palletId)) > 0;
+    }
+
+    private boolean isErpCallSuccess(String response) {
+        if (Cools.isEmpty(response)) {
+            return false;
+        }
+        try {
+            JSONObject jsonObject = JSON.parseObject(response);
+            if (jsonObject == null) {
+                return false;
+            }
+            Boolean success = jsonObject.getBoolean("success");
+            if (success != null) {
+                return success;
+            }
+            Integer code = jsonObject.getInteger("code");
+            if (code != null) {
+                return code == 200 || code == 0;
+            }
+            Integer status = jsonObject.getInteger("status");
+            if (status != null) {
+                return status == 200 || status == 0;
+            }
+            String result = jsonObject.getString("result");
+            if (!Cools.isEmpty(result)) {
+                return "success".equalsIgnoreCase(result) || "ok".equalsIgnoreCase(result) || "true".equalsIgnoreCase(result);
+            }
+        } catch (Exception ignore) {
+            return response.toLowerCase().contains("success") && response.toLowerCase().contains("true");
+        }
+        return false;
+    }
+
+    private String extractErpCallError(String response) {
+        if (Cools.isEmpty(response)) {
+            return "empty erp response";
+        }
+        try {
+            JSONObject jsonObject = JSON.parseObject(response);
+            if (jsonObject == null) {
+                return response;
+            }
+            String[] keys = new String[]{"msg", "message", "error", "errMsg"};
+            for (String key : keys) {
+                String value = jsonObject.getString(key);
+                if (!Cools.isEmpty(value)) {
+                    return value;
+                }
+            }
+        } catch (Exception ignore) {
+        }
+        return response;
+    }
+
+    private boolean isErpReportEnabled() {
+        if (!erpReportOld) {
+            return false;
+        }
+        String erpReport = Parameter.get().getErpReport();
+        return Cools.isEmpty(erpReport) || "true".equalsIgnoreCase(erpReport);
+    }
+
+    private List<String> normalizeBarcodes(List<String> barcodes) {
+        if (barcodes == null || barcodes.isEmpty()) {
+            return Collections.emptyList();
+        }
+        LinkedHashSet<String> normalized = new LinkedHashSet<>();
+        for (String barcode : barcodes) {
+            if (barcode == null) {
+                continue;
+            }
+            String value = barcode.trim();
+            if (!value.isEmpty()) {
+                normalized.add(value);
+            }
+        }
+        return new ArrayList<>(normalized);
+    }
+
+    private Map<String, WrkMastLog> loadLatestInboundHistoryLogMap(List<String> barcodes) {
+        Map<String, WrkMastLog> latestInboundLogMap = new LinkedHashMap<>();
+        if (barcodes == null || barcodes.isEmpty()) {
+            return latestInboundLogMap;
+        }
+        List<WrkMastLog> wrkMastLogs = wrkMastLogService.selectList(new EntityWrapper<WrkMastLog>()
+                .in("barcode", barcodes)
+                .orderBy("barcode", true)
+                .orderBy("io_time", false)
+                .orderBy("wrk_no", false));
+        if (Cools.isEmpty(wrkMastLogs)) {
+            return latestInboundLogMap;
+        }
+        for (WrkMastLog wrkMastLog : wrkMastLogs) {
+            if (wrkMastLog == null || Cools.isEmpty(wrkMastLog.getBarcode())) {
+                continue;
+            }
+            if (!isInboundHistoryLog(wrkMastLog.getIoType())) {
+                continue;
+            }
+            latestInboundLogMap.putIfAbsent(wrkMastLog.getBarcode(), wrkMastLog);
+        }
+        return latestInboundLogMap;
+    }
+
+    private boolean isInboundHistoryLog(Integer ioType) {
+        if (ioType == null) {
+            return false;
+        }
+        // 鍘嗗彶琛ㄩ噷鏃㈡湁鍏ュ簱瀹屾垚锛屼篃鏈夊簱瀛樿皟鏁达紱杩欓噷鍙彇鐪熸鐨勫叆搴撶被璁板綍銆�
+        return ioType < 19 || ioType == 53 || ioType == 54 || ioType == 57;
+    }
+
+    private Map<Integer, List<WrkDetlLog>> loadWrkDetlLogMap(Collection<WrkMastLog> wrkMastLogs) {
+        Map<Integer, List<WrkDetlLog>> wrkDetlLogMap = new HashMap<>();
+        if (wrkMastLogs == null || wrkMastLogs.isEmpty()) {
+            return wrkDetlLogMap;
+        }
+        LinkedHashSet<Integer> wrkNos = new LinkedHashSet<>();
+        for (WrkMastLog wrkMastLog : wrkMastLogs) {
+            if (wrkMastLog != null && wrkMastLog.getWrkNo() != null) {
+                wrkNos.add(wrkMastLog.getWrkNo());
+            }
+        }
+        if (wrkNos.isEmpty()) {
+            return wrkDetlLogMap;
+        }
+        List<WrkDetlLog> wrkDetlLogs = wrkDetlLogService.selectList(new EntityWrapper<WrkDetlLog>().in("wrk_no", wrkNos));
+        if (Cools.isEmpty(wrkDetlLogs)) {
+            return wrkDetlLogMap;
+        }
+        for (WrkDetlLog wrkDetlLog : wrkDetlLogs) {
+            if (wrkDetlLog == null || wrkDetlLog.getWrkNo() == null) {
+                continue;
+            }
+            wrkDetlLogMap.computeIfAbsent(wrkDetlLog.getWrkNo(), k -> new ArrayList<>()).add(wrkDetlLog);
+        }
+        return wrkDetlLogMap;
+    }
+
+    private Map<String, WaitPakinLog> loadWaitPakinLogMap(List<String> barcodes) {
+        Map<String, WaitPakinLog> waitPakinLogMap = new LinkedHashMap<>();
+        if (barcodes == null || barcodes.isEmpty()) {
+            return waitPakinLogMap;
+        }
+        List<WaitPakinLog> waitPakinLogs = waitPakinLogService.selectList(new EntityWrapper<WaitPakinLog>()
+                .in("zpallet", barcodes)
+                .orderBy("zpallet", true)
+                .orderBy("appe_time", false)
+                .orderBy("modi_time", false));
+        if (Cools.isEmpty(waitPakinLogs)) {
+            return waitPakinLogMap;
+        }
+        for (WaitPakinLog waitPakinLog : waitPakinLogs) {
+            if (waitPakinLog == null || Cools.isEmpty(waitPakinLog.getZpallet())) {
+                continue;
+            }
+            waitPakinLogMap.putIfAbsent(waitPakinLog.getZpallet(), waitPakinLog);
+        }
+        return waitPakinLogMap;
+    }
+
+    private ErpPakinReportParam buildInboundErpParam(String barcode,
+                                                     WrkMastLog wrkMastLog,
+                                                     List<WrkDetlLog> wrkDetlLogs,
+                                                     WaitPakinLog waitPakinLog) {
+        ErpPakinReportParam param = new ErpPakinReportParam();
+        param.setPalletId(resolvePalletId(barcode, wrkMastLog, wrkDetlLogs, waitPakinLog));
+        param.setAnfme(resolveInboundAnfme(wrkDetlLogs, waitPakinLog));
+        param.setLocId(transformInboundLocId(resolveInboundLocNo(wrkMastLog, waitPakinLog)));
+        param.setWeight(resolveInboundWeight(wrkMastLog, waitPakinLog));
+        param.setCreateTime(formatDate(resolveInboundCreateTime(wrkMastLog)));
+        param.setBizNo(resolveInboundBizNo(wrkDetlLogs, waitPakinLog));
+        param.setStartTime(formatDate(resolveInboundStartTime(wrkMastLog, waitPakinLog)));
+        return param;
+    }
+
+    private String resolvePalletId(String barcode,
+                                   WrkMastLog wrkMastLog,
+                                   List<WrkDetlLog> wrkDetlLogs,
+                                   WaitPakinLog waitPakinLog) {
+        if (wrkMastLog != null && !Cools.isEmpty(wrkMastLog.getBarcode())) {
+            return wrkMastLog.getBarcode();
+        }
+        if (wrkDetlLogs != null) {
+            for (WrkDetlLog wrkDetlLog : wrkDetlLogs) {
+                if (wrkDetlLog != null && !Cools.isEmpty(wrkDetlLog.getZpallet())) {
+                    return wrkDetlLog.getZpallet();
+                }
+            }
+        }
+        if (waitPakinLog != null && !Cools.isEmpty(waitPakinLog.getZpallet())) {
+            return waitPakinLog.getZpallet();
+        }
+        return barcode;
+    }
+
+    private Double resolveInboundAnfme(List<WrkDetlLog> wrkDetlLogs, WaitPakinLog waitPakinLog) {
+        double total = 0D;
+        boolean hasDetail = false;
+        if (wrkDetlLogs != null) {
+            for (WrkDetlLog wrkDetlLog : wrkDetlLogs) {
+                if (wrkDetlLog == null || wrkDetlLog.getAnfme() == null) {
+                    continue;
+                }
+                total += wrkDetlLog.getAnfme();
+                hasDetail = true;
+            }
+        }
+        if (hasDetail) {
+            return total;
+        }
+        if (waitPakinLog != null && waitPakinLog.getAnfme() != null) {
+            return waitPakinLog.getAnfme();
+        }
+        return total;
+    }
+
+    private BigDecimal resolveInboundWeight(WrkMastLog wrkMastLog, WaitPakinLog waitPakinLog) {
+        BigDecimal baseWeight = BigDecimal.ZERO;
+        if (wrkMastLog != null && wrkMastLog.getScWeight() != null) {
+            baseWeight = BigDecimal.valueOf(wrkMastLog.getScWeight());
+        } else if (waitPakinLog != null && waitPakinLog.getWeight() != null) {
+            baseWeight = BigDecimal.valueOf(waitPakinLog.getWeight());
+        }
+        Integer sourceStaNo = resolveInboundSourceStaNo(wrkMastLog);
+        return baseWeight.multiply(resolveInboundWeightFactor(sourceStaNo));
+    }
+
+    private Integer resolveInboundSourceStaNo(WrkMastLog wrkMastLog) {
+        if (wrkMastLog == null) {
+            return null;
+        }
+        if (wrkMastLog.getSourceStaNo() != null) {
+            return wrkMastLog.getSourceStaNo();
+        }
+        return wrkMastLog.getStaNo();
+    }
+
+    private BigDecimal resolveInboundWeightFactor(Integer sourceStaNo) {
+        if (sourceStaNo == null) {
+            return BigDecimal.ONE;
+        }
+        BigDecimal factor = INBOUND_WEIGHT_FACTOR_BY_SOURCE_STA.get(sourceStaNo);
+        return factor == null ? BigDecimal.ONE : factor;
+    }
+
+    private String resolveInboundLocNo(WrkMastLog wrkMastLog, WaitPakinLog waitPakinLog) {
+        if (wrkMastLog != null && !Cools.isEmpty(wrkMastLog.getLocNo())) {
+            return wrkMastLog.getLocNo();
+        }
+        if (waitPakinLog != null && !Cools.isEmpty(waitPakinLog.getLocNo())) {
+            return waitPakinLog.getLocNo();
+        }
+        return null;
+    }
+
+    private Date resolveInboundCreateTime(WrkMastLog wrkMastLog) {
+        if (wrkMastLog == null) {
+            return new Date();
+        }
+        if (wrkMastLog.getCrnEndTime() != null) {
+            return wrkMastLog.getCrnEndTime();
+        }
+        if (wrkMastLog.getModiTime() != null) {
+            return wrkMastLog.getModiTime();
+        }
+        if (wrkMastLog.getIoTime() != null) {
+            return wrkMastLog.getIoTime();
+        }
+        return new Date();
+    }
+
+    private Date resolveInboundStartTime(WrkMastLog wrkMastLog, WaitPakinLog waitPakinLog) {
+        if (waitPakinLog != null && waitPakinLog.getAppeTime() != null) {
+            return waitPakinLog.getAppeTime();
+        }
+        if (wrkMastLog == null) {
+            return new Date();
+        }
+        if (wrkMastLog.getAppeTime() != null) {
+            return wrkMastLog.getAppeTime();
+        }
+        if (wrkMastLog.getIoTime() != null) {
+            return wrkMastLog.getIoTime();
+        }
+        if (wrkMastLog.getModiTime() != null) {
+            return wrkMastLog.getModiTime();
+        }
+        return new Date();
+    }
+
+    private String resolveInboundBizNo(List<WrkDetlLog> wrkDetlLogs, WaitPakinLog waitPakinLog) {
+        if (wrkDetlLogs != null) {
+            for (WrkDetlLog wrkDetlLog : wrkDetlLogs) {
+                if (wrkDetlLog != null && !Cools.isEmpty(wrkDetlLog.getThreeCode())) {
+                    return wrkDetlLog.getThreeCode();
+                }
+            }
+        }
+        if (waitPakinLog != null && !Cools.isEmpty(waitPakinLog.getThreeCode())) {
+            return waitPakinLog.getThreeCode();
+        }
+        return null;
+    }
+
+    private String transformInboundLocId(String locId) {
+        if (Cools.isEmpty(locId)) {
+            return null;
+        }
+        String trimmed = locId.trim();
+        if (trimmed.length() < 7) {
+            return trimmed;
+        }
+        String row = trimmed.substring(0, 2);
+        String col = trimmed.substring(2, 5);
+        String lev = trimmed.substring(5, 7);
+        try {
+            int rowNo = Integer.parseInt(row);
+            if (rowNo >= 37) {
+                row = "C" + row;
+            } else if (rowNo >= 13) {
+                row = "B" + row;
+            } else {
+                row = "A" + row;
+            }
+        } catch (Exception ignore) {
+            return trimmed;
+        }
+        return row + "-" + col + "-" + lev;
+    }
+
+    private String formatDate(Date date) {
+        if (date == null) {
+            return null;
+        }
+        return new SimpleDateFormat(DATE_TIME_PATTERN).format(date);
+    }
+
+    private String buildErpInboundRequestUrl() {
+        if (Cools.isEmpty(erpUrl)) {
+            return erpInAddress;
+        }
+        if (erpInAddress == null) {
+            return erpUrl;
+        }
+        if (erpUrl.endsWith("/") && erpInAddress.startsWith("/")) {
+            return erpUrl + erpInAddress.substring(1);
+        }
+        if (!erpUrl.endsWith("/") && !erpInAddress.startsWith("/")) {
+            return erpUrl + "/" + erpInAddress;
+        }
+        return erpUrl + erpInAddress;
+    }
+
+    private String buildErpOutErrorRequestUrl() {
+        if (Cools.isEmpty(erpUrl)) {
+            return erpOutErrorAddress;
+        }
+        if (erpOutErrorAddress == null) {
+            return erpUrl;
+        }
+        if (erpUrl.endsWith("/") && erpOutErrorAddress.startsWith("/")) {
+            return erpUrl + erpOutErrorAddress.substring(1);
+        }
+        if (!erpUrl.endsWith("/") && !erpOutErrorAddress.startsWith("/")) {
+            return erpUrl + "/" + erpOutErrorAddress;
+        }
+        return erpUrl + erpOutErrorAddress;
+    }
 }
 
+

--
Gitblit v1.9.1