From 314076388f664f8ce1a1d19c4c9717fe54634e05 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期六, 18 四月 2026 15:51:51 +0800
Subject: [PATCH] Merge branch 'dev' of http://115.159.85.185:8098/r/SuZhouGuanHong/ShanMeiXinNengYuan into dev

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockInfoService.cs |  170 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 168 insertions(+), 2 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockInfoService.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockInfoService.cs
index 81f3390..9a84608 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockInfoService.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockInfoService.cs
@@ -1,8 +1,10 @@
 using WIDESEA_Common.StockEnum;
+using WIDESEA_Core;
 using WIDESEA_Core.BaseRepository;
 using WIDESEA_Core.BaseServices;
 using WIDESEA_DTO.Stock;
 using WIDESEA_IBasicService;
+using WIDESEA_IRecordService;
 using WIDESEA_IStockService;
 using WIDESEA_Model.Models;
 
@@ -27,15 +29,27 @@
         /// 浠撳簱淇℃伅鏈嶅姟鎺ュ彛锛堢敤浜庤幏鍙栦粨搴撳熀鏈俊鎭級
         /// </summary>
         private readonly IWarehouseService _warehouseService;
+        private readonly IRecordService _recordService;
+        private readonly IUnitOfWorkManage _unitOfWorkManage;
+        private readonly IStockInfoDetailService _stockInfoDetailService;
 
         /// <summary>
         /// 鏋勯�犲嚱鏁�
         /// </summary>
         /// <param name="baseDal">鍩虹鏁版嵁璁块棶瀵硅薄</param>
-        public StockInfoService(IRepository<Dt_StockInfo> baseDal, ILocationInfoService locationInfoService, IWarehouseService warehouseService) : base(baseDal)
+        public StockInfoService(
+            IRepository<Dt_StockInfo> baseDal,
+            ILocationInfoService locationInfoService,
+            IWarehouseService warehouseService,
+            IRecordService recordService,
+            IUnitOfWorkManage unitOfWorkManage,
+            IStockInfoDetailService stockInfoDetailService) : base(baseDal)
         {
             _locationInfoService = locationInfoService;
             _warehouseService = warehouseService;
+            _recordService = recordService;
+            _unitOfWorkManage = unitOfWorkManage;
+            _stockInfoDetailService = stockInfoDetailService;
         }
 
         /// <summary>
@@ -79,7 +93,100 @@
         /// <returns>鏇存柊鏄惁鎴愬姛</returns>
         public async Task<bool> UpdateStockAsync(Dt_StockInfo stockInfo)
         {
-            return await BaseDal.UpdateDataAsync(stockInfo);
+            var beforeStock = await BaseDal.QueryDataNavFirstAsync(x => x.Id == stockInfo.Id);
+            var result = await BaseDal.UpdateDataAsync(stockInfo);
+            if (!result)
+                return false;
+
+            var afterStock = await BaseDal.QueryDataNavFirstAsync(x => x.Id == stockInfo.Id) ?? stockInfo;
+            var changeType = ResolveChangeType(beforeStock, afterStock);
+            return await _recordService.AddStockChangeRecordAsync(beforeStock, afterStock, changeType, remark: "搴撳瓨鏇存柊");
+        }
+
+        public override WebResponseContent AddData(Dt_StockInfo entity)
+        {
+            var result = base.AddData(entity);
+            if (!result.Status)
+                return result;
+
+            var saveRecordResult = _recordService.AddStockChangeRecordAsync(null, entity, StockChangeTypeEnum.Inbound, remark: "搴撳瓨鏂板").GetAwaiter().GetResult();
+            return saveRecordResult ? result : WebResponseContent.Instance.Error("搴撳瓨鍙樻洿璁板綍淇濆瓨澶辫触");
+        }
+
+        public override WebResponseContent UpdateData(Dt_StockInfo entity)
+        {
+            var beforeStock = BaseDal.QueryFirst(x => x.Id == entity.Id);
+            var result = base.UpdateData(entity);
+            if (!result.Status)
+                return result;
+
+            var changeType = ResolveChangeType(beforeStock, entity);
+            var saveRecordResult = _recordService.AddStockChangeRecordAsync(beforeStock, entity, changeType, remark: "搴撳瓨鏇存柊").GetAwaiter().GetResult();
+            return saveRecordResult ? result : WebResponseContent.Instance.Error("搴撳瓨鍙樻洿璁板綍淇濆瓨澶辫触");
+        }
+
+        public override WebResponseContent DeleteData(Dt_StockInfo entity)
+        {
+            var beforeStock = CloneStockSnapshot(entity);
+            var result = base.DeleteData(entity);
+            if (!result.Status)
+                return result;
+
+            var saveRecordResult = _recordService.AddStockChangeRecordAsync(beforeStock, null, StockChangeTypeEnum.Outbound, remark: "搴撳瓨鍒犻櫎").GetAwaiter().GetResult();
+            return saveRecordResult ? result : WebResponseContent.Instance.Error("搴撳瓨鍙樻洿璁板綍淇濆瓨澶辫触");
+        }
+
+        private static StockChangeTypeEnum ResolveChangeType(Dt_StockInfo? beforeStock, Dt_StockInfo? afterStock)
+        {
+            if (beforeStock == null)
+                return StockChangeTypeEnum.Inbound;
+
+            if (afterStock == null)
+                return StockChangeTypeEnum.Outbound;
+
+            if (!string.Equals(beforeStock.LocationCode, afterStock.LocationCode, StringComparison.OrdinalIgnoreCase))
+                return StockChangeTypeEnum.Relocation;
+
+            if (beforeStock.StockStatus != afterStock.StockStatus)
+                return StockChangeTypeEnum.StockLock;
+
+            var beforeQuantity = beforeStock.Details?.Sum(x => x.StockQuantity) ?? 0;
+            var afterQuantity = afterStock.Details?.Sum(x => x.StockQuantity) ?? 0;
+            return afterQuantity >= beforeQuantity ? StockChangeTypeEnum.Inbound : StockChangeTypeEnum.Outbound;
+        }
+
+        private static Dt_StockInfo CloneStockSnapshot(Dt_StockInfo stockInfo)
+        {
+            return new Dt_StockInfo
+            {
+                Id = stockInfo.Id,
+                PalletCode = stockInfo.PalletCode,
+                PalletType = stockInfo.PalletType,
+                LocationId = stockInfo.LocationId,
+                LocationCode = stockInfo.LocationCode,
+                WarehouseId = stockInfo.WarehouseId,
+                StockStatus = stockInfo.StockStatus,
+                Remark = stockInfo.Remark,
+                OutboundDate = stockInfo.OutboundDate,
+                Details = stockInfo.Details?.Select(detail => new Dt_StockInfoDetail
+                {
+                    Id = detail.Id,
+                    StockId = detail.StockId,
+                    MaterielCode = detail.MaterielCode,
+                    MaterielName = detail.MaterielName,
+                    OrderNo = detail.OrderNo,
+                    BatchNo = detail.BatchNo,
+                    ProductionDate = detail.ProductionDate,
+                    EffectiveDate = detail.EffectiveDate,
+                    SerialNumber = detail.SerialNumber,
+                    StockQuantity = detail.StockQuantity,
+                    OutboundQuantity = detail.OutboundQuantity,
+                    Status = detail.Status,
+                    Unit = detail.Unit,
+                    InboundOrderRowNo = detail.InboundOrderRowNo,
+                    Remark = detail.Remark
+                }).ToList()
+            };
         }
 
         /// <summary>
@@ -215,5 +322,64 @@
                 Locations = locationItems
             };
         }
+
+        /// <summary>
+        /// 浣跨敤浜嬪姟鍒犻櫎搴撳瓨鍜屾槑缁嗕俊鎭紙鍏堟煡璇㈠啀鍒犻櫎锛�
+        /// </summary>
+        /// <param name="stockId">搴撳瓨ID</param>
+        /// <returns>鍒犻櫎缁撴灉</returns>
+        public async Task<WebResponseContent> DeleteStockWithDetailsAsync(int stockId)
+        {
+            if (stockId <= 0)
+                return WebResponseContent.Instance.Error("搴撳瓨ID鏃犳晥");
+
+            _unitOfWorkManage.BeginTran();
+            try
+            {
+                // 鍏堟煡璇㈠簱瀛樹俊鎭紙鍖呭惈鏄庣粏锛�
+                var stockInfo = await BaseDal.QueryDataNavFirstAsync(x => x.Id == stockId);
+                if (stockInfo == null)
+                {
+                    _unitOfWorkManage.RollbackTran();
+                    return WebResponseContent.Instance.Error("搴撳瓨璁板綍涓嶅瓨鍦�");
+                }
+
+                // 鏌ヨ骞跺垹闄ゅ簱瀛樻槑缁嗚褰�
+                var existingDetails = await _stockInfoDetailService.Repository.QueryDataAsync(x => x.StockId == stockId);
+                if (existingDetails != null && existingDetails.Any())
+                {
+                    var deleteDetailResult = await _stockInfoDetailService.Repository.DeleteDataAsync(existingDetails);
+                    if (!deleteDetailResult)
+                    {
+                        _unitOfWorkManage.RollbackTran();
+                        return WebResponseContent.Instance.Error("鍒犻櫎搴撳瓨鏄庣粏璁板綍澶辫触");
+                    }
+                }
+
+                // 鍒犻櫎搴撳瓨涓昏褰�
+                var deleteStockResult = await BaseDal.DeleteDataAsync(stockInfo);
+                if (!deleteStockResult)
+                {
+                    _unitOfWorkManage.RollbackTran();
+                    return WebResponseContent.Instance.Error("鍒犻櫎搴撳瓨涓昏褰曞け璐�");
+                }
+
+                _unitOfWorkManage.CommitTran();
+
+                // 璁板綍搴撳瓨鍙樻洿鏃ュ織
+                var saveRecordResult = await _recordService.AddStockChangeRecordAsync(stockInfo, null, StockChangeTypeEnum.Outbound, remark: "搴撳瓨鍒犻櫎");
+                if (!saveRecordResult)
+                {
+                    return WebResponseContent.Instance.Error("搴撳瓨鍙樻洿璁板綍淇濆瓨澶辫触");
+                }
+
+                return WebResponseContent.Instance.OK("搴撳瓨鍒犻櫎鎴愬姛");
+            }
+            catch (Exception ex)
+            {
+                _unitOfWorkManage.RollbackTran();
+                return WebResponseContent.Instance.Error($"鍒犻櫎搴撳瓨鍜屾槑缁嗘椂鍙戠敓寮傚父: {ex.Message}");
+            }
+        }
     }
 }

--
Gitblit v1.9.3