From 4b22bcec7226c8a05c27f3c839630a31e1f38ef8 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期五, 27 三月 2026 14:00:31 +0800
Subject: [PATCH] docs: 添加 RouterService 逻辑修复与新方法设计文档

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs |  305 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 297 insertions(+), 8 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
index 5747dac..1bf9205 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
@@ -1,27 +1,316 @@
-锘縰sing System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+锘縰sing SqlSugar;
+using WIDESEA_Common.StockEnum;
+using WIDESEA_Core;
+using WIDESEA_DTO.Stock;
 using WIDESEA_IStockService;
+using WIDESEA_Model.Models;
 
 namespace WIDESEA_StockService
 {
-    public class StockSerivce:IStockService
+    /// <summary>
+    /// 搴撳瓨鏈嶅姟鑱氬悎瀹炵幇绫�
+    /// </summary>
+    public class StockService : IStockService
     {
+        /// <summary>
+        /// 搴撳瓨鏄庣粏鏈嶅姟
+        /// </summary>
         public IStockInfoDetailService StockInfoDetailService { get; }
 
+        /// <summary>
+        /// 搴撳瓨淇℃伅鏈嶅姟
+        /// </summary>
         public IStockInfoService StockInfoService { get; }
+
+        /// <summary>
+        /// 搴撳瓨鏄庣粏鍘嗗彶鏈嶅姟
+        /// </summary>
         public IStockInfoDetail_HtyService StockInfoDetail_HtyService { get; }
 
+        /// <summary>
+        /// 搴撳瓨鍘嗗彶鏈嶅姟
+        /// </summary>
         public IStockInfo_HtyService StockInfo_HtyService { get; }
 
-        public StockSerivce(IStockInfoDetailService stockInfoDetailService, IStockInfoService stockInfoService, IStockInfoDetail_HtyService stockInfoDetail_HtyService, IStockInfo_HtyService stockInfo_HtyService)
+        /// <summary>
+        /// 鏋勯�犲嚱鏁�
+        /// </summary>
+        /// <param name="stockInfoDetailService">搴撳瓨鏄庣粏鏈嶅姟</param>
+        /// <param name="stockInfoService">搴撳瓨淇℃伅鏈嶅姟</param>
+        /// <param name="stockInfoDetail_HtyService">搴撳瓨鏄庣粏鍘嗗彶鏈嶅姟</param>
+        /// <param name="stockInfo_HtyService">搴撳瓨鍘嗗彶鏈嶅姟</param>
+        public StockService(
+            IStockInfoDetailService stockInfoDetailService,
+            IStockInfoService stockInfoService,
+            IStockInfoDetail_HtyService stockInfoDetail_HtyService,
+            IStockInfo_HtyService stockInfo_HtyService)
         {
             StockInfoDetailService = stockInfoDetailService;
             StockInfoService = stockInfoService;
             StockInfoDetail_HtyService = stockInfoDetail_HtyService;
             StockInfo_HtyService = stockInfo_HtyService;
         }
+
+        /// <summary>
+        /// 鍦ㄤ簨鍔′腑鎵ц鎿嶄綔
+        /// </summary>
+        private async Task<WebResponseContent> ExecuteWithinTransactionAsync(Func<Task<WebResponseContent>> operation)
+        {
+            var db = StockInfoService.Repository.Db as SqlSugarClient;
+            if (db == null)
+            {
+                return WebResponseContent.Instance.Error("Database context does not support transactions");
+            }
+
+            var ownsTransaction = db.Ado.Transaction == null;
+            try
+            {
+                if (ownsTransaction)
+                {
+                    db.BeginTran();
+                }
+
+                var result = await operation();
+                if (result?.Status == true)
+                {
+                    if (ownsTransaction)
+                    {
+                        db.CommitTran();
+                    }
+                    return result;
+                }
+
+                if (ownsTransaction)
+                {
+                    db.RollbackTran();
+                }
+                return result ?? WebResponseContent.Instance.Error("Transaction failed");
+            }
+            catch
+            {
+                if (ownsTransaction)
+                {
+                    db.RollbackTran();
+                }
+                throw;
+            }
+        }
+
+        /// <summary>
+        /// 缁勭洏
+        /// </summary>
+        public async Task<WebResponseContent> GroupPalletAsync(StockDTO stock)
+        {
+            WebResponseContent content = new WebResponseContent();
+            var now = DateTime.Now;
+            var details = stock.Details.Select(item => new Dt_StockInfoDetail
+            {
+                MaterielCode = "鐢佃姱",
+                MaterielName = "鐢佃姱",
+                StockQuantity = item.Quantity,
+                Unit = "PCS",
+                Creater = "system",
+                OrderNo = "111",
+                ProductionDate = now.ToString(),
+                EffectiveDate = now.AddYears(1).ToString(),
+                SerialNumber = item.CellBarcode,
+                InboundOrderRowNo = item.Channel,
+                Status = StockStatusEmun.缁勭洏鏆傚瓨.GetHashCode(),
+            }).ToList();
+
+            return await ExecuteWithinTransactionAsync(async () =>
+            {
+                var existingStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
+                var result = false;
+                if (existingStock != null)
+                {
+                    details.ForEach(d => d.StockId = existingStock.Id);
+                    result = await StockInfoDetailService.Repository.AddDataAsync(details) > 0;
+                    return result ? content.OK("缁勭洏鎴愬姛") : content.Error("缁勭洏澶辫触");
+                }
+
+                var entity = new Dt_StockInfo
+                {
+                    PalletCode = stock.TargetPalletNo,
+                    //WarehouseId = stock.WarehouseId > 0 ? stock.WarehouseId : 1,
+                    WarehouseId = 1,
+                    StockStatus = StockStatusEmun.缁勭洏鏆傚瓨.GetHashCode(),
+                    Creater = "system",
+                    Details = details
+                };
+
+                result = StockInfoService.Repository.AddData(entity, x => x.Details);
+                return result ? content.OK("缁勭洏鎴愬姛") : content.Error("缁勭洏澶辫触");
+            });
+        }
+
+        /// <summary>
+        /// 鎹㈢洏
+        /// </summary>
+        public async Task<WebResponseContent> ChangePalletAsync(StockDTO stock)
+        {
+            WebResponseContent content = new WebResponseContent();
+            if (stock == null ||
+                string.IsNullOrWhiteSpace(stock.TargetPalletNo) ||
+                string.IsNullOrWhiteSpace(stock.SourcePalletNo) ||
+                string.Equals(stock.SourcePalletNo, stock.TargetPalletNo, StringComparison.OrdinalIgnoreCase))
+            {
+                return content.Error("婧愭墭鐩樺彿涓庣洰鏍囨墭鐩樺彿鐩稿悓");
+            }
+
+            return await ExecuteWithinTransactionAsync(async () =>
+            {
+                var sourceStock = await StockInfoService.Repository.QueryDataNavFirstAsync(s => s.PalletCode == stock.SourcePalletNo);
+                if (sourceStock == null) return content.Error("婧愭墭鐩樹笉瀛樺湪");
+
+                var targetStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
+                if (targetStock == null)
+                {
+                    var newStock = new Dt_StockInfo
+                    {
+                        PalletCode = stock.TargetPalletNo,
+                        WarehouseId = sourceStock.WarehouseId,
+                        StockStatus = StockStatusEmun.缁勭洏鏆傚瓨.GetHashCode(),
+                        Creater = "system",
+                    };
+
+                    var newId = StockInfoService.Repository.AddData(newStock);
+                    if (newId <= 0) return content.Error("鎹㈢洏澶辫触");
+
+                    targetStock = newStock;
+                    targetStock.Id = newId;
+                }
+
+                var serialNumbers = stock.Details.Select(d => d.Channel).Distinct().ToList();
+                if (!serialNumbers.Any()) return content.Error("鏈壘鍒版湁鏁堢殑搴忓垪鍙�");
+
+                var detailEntities = StockInfoDetailService.Repository.QueryData(
+                    d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.InboundOrderRowNo));
+                if (!detailEntities.Any()) return content.Error("鏈壘鍒版湁鏁堢殑搴撳瓨鏄庣粏");
+
+                if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "鎹㈢洏")) <= 0)
+                    return content.Error("鎹㈢洏鍘嗗彶璁板綍淇濆瓨澶辫触");
+
+                if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock, targetStock }, "鎹㈢洏")) <= 0)
+                    return content.Error("鎹㈢洏鍘嗗彶璁板綍淇濆瓨澶辫触");
+
+                detailEntities.ForEach(d => d.StockId = targetStock.Id);
+                var result = await StockInfoDetailService.Repository.UpdateDataAsync(detailEntities);
+                if (!result) return content.Error("鎹㈢洏澶辫触");
+                return content.OK("鎹㈢洏鎴愬姛");
+            });
+        }
+
+        /// <summary>
+        /// 鎷嗙洏
+        /// </summary>
+        public async Task<WebResponseContent> SplitPalletAsync(StockDTO stock)
+        {
+            WebResponseContent content = new WebResponseContent();
+            if (stock == null || string.IsNullOrWhiteSpace(stock.SourcePalletNo))
+                return content.Error("婧愭墭鐩樺彿涓嶈兘涓虹┖");
+
+            return await ExecuteWithinTransactionAsync(async () =>
+            {
+                var sourceStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.SourcePalletNo);
+                if (sourceStock == null) return content.Error("婧愭墭鐩樹笉瀛樺湪");
+
+                var serialNumbers = stock.Details.Select(d => d.CellBarcode).Distinct().ToList();
+                if (!serialNumbers.Any())
+                {
+                    serialNumbers = sourceStock.Details
+                                                .Where(x => stock.Details.Any(d => d.Channel == x.InboundOrderRowNo))
+                                                .Select(x => x.SerialNumber)
+                                                .ToList();
+                }
+
+                var detailEntities = StockInfoDetailService.Repository.QueryData(
+                    d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.SerialNumber));
+                if (!detailEntities.Any()) return content.Error("鏈壘鍒版湁鏁堢殑搴撳瓨鏄庣粏");
+
+                if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "鎷嗙洏")) <= 0)
+                    return content.Error("鎷嗙洏鍘嗗彶璁板綍淇濆瓨澶辫触");
+
+                if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock }, "鎷嗙洏")) <= 0)
+                    return content.Error("鎷嗙洏鍘嗗彶璁板綍淇濆瓨澶辫触");
+
+                var result = await StockInfoDetailService.Repository.DeleteDataAsync(detailEntities);
+                if (!result) return content.Error("鎷嗙洏澶辫触");
+                return content.OK("鎷嗙洏鎴愬姛");
+            });
+        }
+
+        /// <summary>
+        /// 鍫嗗灈鏈烘崲鐩樺悗鏇存柊搴撳瓨淇℃伅锛堟竻绌哄簱浣嶄俊鎭級
+        /// </summary>
+        /// <param name="stock"></param>
+        /// <returns></returns>
+        public async Task<WebResponseContent> UpdateStockInfoAsync(StockInfoDTO stock)
+        {
+            WebResponseContent content = new WebResponseContent();
+            if (string.IsNullOrWhiteSpace(stock.PalletCode)) return content.Error("鎵樼洏鍙蜂笉鑳戒负绌�");
+
+            var existingStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.PalletCode);
+            if (existingStock == null) return content.Error("鎵樼洏淇℃伅涓嶅瓨鍦�");
+
+            existingStock.LocationCode = "";
+            existingStock.LocationId = 0;
+
+            var result = await StockInfoService.Repository.UpdateDataAsync(existingStock);
+            if (!result) return content.Error("鏇存柊搴撳瓨淇℃伅澶辫触");
+            return content.OK("鏇存柊搴撳瓨淇℃伅鎴愬姛");
+        }
+
+        private static List<Dt_StockInfoDetail_Hty> CreateDetailHistory(IEnumerable<Dt_StockInfoDetail> details, string operateType)
+        {
+            var now = DateTime.Now;
+            return details.Select(d => new Dt_StockInfoDetail_Hty
+            {
+                SourceId = d.Id,
+                OperateType = operateType,
+                InsertTime = now,
+                StockId = d.StockId,
+                MaterielCode = d.MaterielCode,
+                MaterielName = d.MaterielName,
+                OrderNo = d.OrderNo,
+                BatchNo = d.BatchNo,
+                ProductionDate = d.ProductionDate,
+                EffectiveDate = d.EffectiveDate,
+                SerialNumber = d.SerialNumber,
+                StockQuantity = d.StockQuantity,
+                OutboundQuantity = d.OutboundQuantity,
+                Status = d.Status,
+                Unit = d.Unit,
+                InboundOrderRowNo = d.InboundOrderRowNo,
+                Remark = d.Remark,
+                Creater = d.Creater,
+                CreateDate = d.CreateDate,
+                Modifier = d.Modifier,
+                ModifyDate = d.ModifyDate
+            }).ToList();
+        }
+
+        private static List<Dt_StockInfo_Hty> CreateStockHistory(IEnumerable<Dt_StockInfo> stocks, string operateType)
+        {
+            var now = DateTime.Now;
+            return stocks.Select(s => new Dt_StockInfo_Hty
+            {
+                SourceId = s.Id,
+                OperateType = operateType,
+                InsertTime = now,
+                PalletCode = s.PalletCode,
+                PalletType = s.PalletType,
+                LocationCode = s.LocationCode,
+                WarehouseId = s.WarehouseId,
+                StockStatus = s.StockStatus,
+                Remark = s.Remark,
+                Creater = s.Creater,
+                CreateDate = s.CreateDate,
+                Modifier = s.Modifier,
+                ModifyDate = s.ModifyDate,
+                LocationId = s.LocationId,
+                OutboundDate = s.OutboundDate
+            }).ToList();
+        }
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3