From c99808261b1b1460d22a7f7db5955a65080c3b4b Mon Sep 17 00:00:00 2001
From: Admin <Admin@ADMIN>
Date: 星期一, 27 四月 2026 08:58:57 +0800
Subject: [PATCH] 优化货位查找

---
 项目代码/WMS/WIDESEA_WMSServer/WIDESEA.Services/Services/Inbound/Partial/Dt_boxing_headService.cs |  162 +++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 142 insertions(+), 20 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA.Services/Services/Inbound/Partial/Dt_boxing_headService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA.Services/Services/Inbound/Partial/Dt_boxing_headService.cs"
index d969f1a..e23f5cf 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA.Services/Services/Inbound/Partial/Dt_boxing_headService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA.Services/Services/Inbound/Partial/Dt_boxing_headService.cs"
@@ -6,31 +6,32 @@
 *鐢ㄦ埛淇℃伅銆佹潈闄愩�佽鑹茬瓑浣跨敤UserContext.Current鎿嶄綔
 *Dt_boxing_headService瀵瑰銆佸垹銆佹敼鏌ャ�佸鍏ャ�佸鍑恒�佸鏍镐笟鍔′唬鐮佹墿灞曞弬鐓erviceFunFilter
 */
-using WIDESEA.Core.BaseProvider;
-using WIDESEA.Core.Extensions.AutofacManager;
-using WIDESEA.Entity.DomainModels;
-using System.Linq;
-using WIDESEA.Core.Utilities;
-using System.Linq.Expressions;
-using WIDESEA.Core.Extensions;
+using Microsoft.AspNetCore.Http;
 using Microsoft.EntityFrameworkCore;
 using Microsoft.Extensions.DependencyInjection;
-using Microsoft.AspNetCore.Http;
-using WIDESEA.Services.IRepositories;
 using Newtonsoft.Json;
-using System.Text;
-using System.Net;
-using System.IO;
 using System;
-using WIDESEA.Services.Repositories;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Net;
+using System.Text;
 using System.Threading;
 using WIDESEA.Common;
-using WIDESEA_Services;
-using WIDESEA.Core.Services;
+using WIDESEA.Core.BaseProvider;
 using WIDESEA.Core.Enums;
+using WIDESEA.Core.Extensions;
+using WIDESEA.Core.Extensions.AutofacManager;
 using WIDESEA.Core.ManageUser;
-using System.Collections.Generic;
+using WIDESEA.Core.Services;
+using WIDESEA.Core.Utilities;
+using WIDESEA.Entity.DomainModels;
+using WIDESEA.Services.IRepositories;
+using WIDESEA.Services.Repositories;
 using WIDESEA.Services.Services.ToMes;
+using WIDESEA_Common.LogEnum;
+using WIDESEA_Services;
 
 namespace WIDESEA.Services.Services
 {
@@ -144,11 +145,51 @@
 
                     //newBoxDetail.boxdtl_text1 = 
 
+
+                    //鑾峰彇绌鸿揣浣�
+                    Dt_locationinfo emptyLocation;
+
+                    int maxRetries = 20;
+                    int retryCount = 0;
+                    bool needRetry;
+                    do
+                    {
+                        needRetry = false;  // 榛樿涓嶉渶瑕侀噸璇�
+                        retryCount++;
+
+                        //鑾峰彇绌鸿揣浣�
+                        emptyLocation = CommonFunction.GetEmptyLocationAction(1);
+                        if (emptyLocation == null)
+                        {
+                            return content.Error("鏃犳硶鑾峰彇搴撲綅");
+                        }
+
+                        // 2. 妫�鏌ュ簱瀛�
+                        if (IsEmptyPalletLocation(emptyLocation.location_id))
+                        {
+                            needRetry = true;
+                            continue;  // 鐩存帴杩涘叆涓嬩竴娆″惊鐜紙閲嶈瘯锛�
+                        }
+
+                        // 3. 妫�鏌ヤ换鍔�
+                        if (Dt_taskinfoRepository.Instance.Find(x => x.task_endstation == emptyLocation.location_id).FirstOrDefault() != null)
+                        {
+                            needRetry = true;
+                            continue;
+                        }
+
+                    } while (needRetry && retryCount < maxRetries);
+                    // 妫�鏌ユ槸鍚﹁秴杩囨渶澶ч噸璇曟鏁�
+                    if (retryCount >= maxRetries)
+                    {
+                        return content.Error($"鏃犳硶鎵惧埌鍙敤搴撲綅锛堝凡灏濊瘯 {retryCount} 娆★級");
+                    }
+                    LogRecord.WriteLog((int)LogEnum.InBound, $"鎵樼洏鏉$爜锛歿rfid},鍒嗛厤鐨勮揣浣嶅彿{emptyLocation.location_id}锛岃揣浣嶇姸鎬侊細{emptyLocation.location_state}");
+
+
                     Dt_boxing_headRepository.Instance.Add(newBoxHead, true);
                     Dt_boxing_detailRepository.Instance.Add(newBoxDetail, true);
 
-                    //鑾峰彇绌鸿揣浣�
-                    Dt_locationinfo emptyLocation = CommonFunction.GetEmptyLocationAction();
                     //鑾峰彇浠诲姟
                     Dt_taskinfo tmpTaskInfo = CommonFunction.AddWMSTask_BoxPalletIn(emptyLocation,
                                    rfid, mesInfo.mesInfo_carType, mesInfo.mesInfo_qrCode,"");
@@ -173,6 +214,34 @@
             return content;
         }
 
+        /// <summary>
+        /// 妫�鏌ユ寚瀹氳揣浣嶆槸鍚︿负绌烘墭鐩樿揣浣�
+        /// </summary>
+        /// <param name="locationId">璐т綅ID</param>
+        /// <returns>true: 鏄┖鎵樼洏璐т綅, false: 涓嶆槸绌烘墭鐩樿揣浣�</returns>
+        public bool IsEmptyPalletLocation(string locationId)
+        {
+            try
+            {
+                // 鏌ヨ绌烘墭鐩樿揣浣嶄俊鎭�
+                var emptyPalletLocation = VV_ContainerInfo_EmptyPalletRepository.Instance
+                    .Find(x => x.location_id == locationId).FirstOrDefault();
+
+                // 鏌ヨ璐т綅涓婄殑瀹瑰櫒淇℃伅
+                var containerInfo = VV_ContainerInfoRepository.Instance
+                    .Find(x => x.location_id == locationId).FirstOrDefault();
+
+                bool isEmptyPallet = emptyPalletLocation != null || containerInfo != null;
+
+                return isEmptyPallet;
+            }
+            catch (Exception ex)
+            {
+                LogRecord.WriteLog((int)LogEnum.Errer, $"妫�鏌ョ┖鎵樼洏璐т綅澶辫触锛岃揣浣岻D锛歿locationId}锛岄敊璇細{ex.Message}");
+                return false;
+            }
+        }
+
 
         /// <summary>
         /// 鍒涘缓绌烘墭鍏ュ簱浠诲姟
@@ -185,6 +254,10 @@
             try
             {
                 string barcode = saveModel.MainData["barcode"].ToString();
+                lock (barcode)
+                {
+
+                }
 
                 Dt_taskinfo oldTask = Dt_taskinfoRepository.Instance.FindFirst(r => r.task_barcode == barcode);
                 if (null != oldTask)
@@ -193,9 +266,56 @@
                 VV_ContainerInfo_EmptyPallet emptyCon = VV_ContainerInfo_EmptyPalletRepository.Instance.FindFirst(x => x.containerhead_barcode == barcode);
                 if (emptyCon != null)
                     throw new Exception($"搴撳瓨涓瓨鍦ㄦ墭鐩樺彿锛氥�恵barcode}銆戠殑銆愮┖鎵樸�戝簱瀛橈紝璇蜂汉宸ユ牳瀹烇紝鎵�鍦ㄨ揣浣嶏細{emptyCon.location_id}");
+                //鑾峰彇绌鸿揣浣�
+                Dt_locationinfo emptyLocation;
 
-                //璇存槑鏄┖鎵樺叆搴�
-                Dt_locationinfo emptyLocation = CommonFunction.GetEmptyLocationAction();
+                int maxRetries = 20;
+                int retryCount = 0;
+                bool needRetry;
+                do
+                {
+                    needRetry = false;  // 榛樿涓嶉渶瑕侀噸璇�
+                    retryCount++;
+
+                    //鑾峰彇绌鸿揣浣�
+                    emptyLocation = CommonFunction.GetEmptyLocationAction(1);
+                    if (emptyLocation == null)
+                    {
+                        return content.Error("鏃犳硶鑾峰彇搴撲綅");
+                    }
+
+                    // 2. 妫�鏌ュ簱瀛�
+                    if (IsEmptyPalletLocation(emptyLocation.location_id))
+                    {
+                        needRetry = true;
+                        continue;  // 鐩存帴杩涘叆涓嬩竴娆″惊鐜紙閲嶈瘯锛�
+                    }
+
+                    // 3. 妫�鏌ヤ换鍔�
+                    if (Dt_taskinfoRepository.Instance.Find(x => x.task_endstation == emptyLocation.location_id).FirstOrDefault() != null)
+                    {
+                        needRetry = true;
+                        continue;
+                    }
+
+                } while (needRetry && retryCount < maxRetries);
+                // 妫�鏌ユ槸鍚﹁秴杩囨渶澶ч噸璇曟鏁�
+                if (retryCount >= maxRetries)
+                {
+                    return content.Error($"鏃犳硶鎵惧埌鍙敤搴撲綅锛堝凡灏濊瘯 {retryCount} 娆★級");
+                }
+
+                LogRecord.WriteLog((int)LogEnum.InBound, $"鎵樼洏鏉$爜锛歿barcode},鍒嗛厤鐨勮揣浣嶅彿{emptyLocation.location_id}锛岃揣浣嶇姸鎬侊細{emptyLocation.location_state}");
+
+
+                VV_ContainerInfo_EmptyPallet SelectLocation = VV_ContainerInfo_EmptyPalletRepository.Instance.FindFirst(x => x.location_id == emptyLocation.location_id);
+                VV_ContainerInfo conInfohw = VV_ContainerInfoRepository.Instance.FindFirst(x => x.location_id == emptyLocation.location_id);
+                if (SelectLocation != null || conInfohw != null)
+                {
+                    LogRecord.WriteLog((int)LogEnum.InBound, $"鍒嗛厤鐨勮揣浣嶅凡鏈夎揣,璐т綅鍙穥emptyLocation.location_id}");
+                    throw new Exception($"鍒嗛厤鐨勮揣浣嶅凡鏈夎揣,璐т綅鍙穥SelectLocation.location_id}");
+                }
+
                 //鍦ㄦ澶勭敓鎴怶MS浠诲姟鍜屼笅鍙慦CS浠诲姟---------绌烘墭鍏ュ簱
                 Dt_taskinfo tmpTaskInfo = null;
                 content = Dt_taskinfoRepository.Instance.DbContextBeginTransaction(() =>
@@ -222,6 +342,8 @@
         }
 
 
+
+
         /// <summary>
         /// 鏌ヨ涓�涓墭鐩樺彿瀵瑰簲鐨勭粍鐩樹俊鎭�
         /// </summary>

--
Gitblit v1.9.3