wanshenmean
2026-03-31 9b77acb859f0866f3a854d2a2842072b2fe9cca8
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,21 @@
        /// 仓库信息服务接口(用于获取仓库基本信息)
        /// </summary>
        private readonly IWarehouseService _warehouseService;
        private readonly IRecordService _recordService;
        /// <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) : base(baseDal)
        {
            _locationInfoService = locationInfoService;
            _warehouseService = warehouseService;
            _recordService = recordService;
        }
        /// <summary>
@@ -79,7 +87,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>