wanshenmean
8 天以前 eb399b544b4055c1b58a1746f8c453ba41e4010b
Code/WMS/WIDESEA_WMSServer/WIDESEA_RecordService/StockQuantityChangeRecordService.cs
@@ -1,4 +1,4 @@
using MapsterMapper;
using WIDESEA_Common.StockEnum;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_IRecordService;
@@ -11,21 +11,87 @@
    /// </summary>
    public partial class StockQuantityChangeRecordService : ServiceBase<Dt_StockQuantityChangeRecord, IRepository<Dt_StockQuantityChangeRecord>>, IStockQuantityChangeRecordService
    {
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseDal">基础数据访问对象</param>
        /// <param name="mapper">对象映射器</param>
        public StockQuantityChangeRecordService(IRepository<Dt_StockQuantityChangeRecord> baseDal, IMapper mapper) : base(baseDal)
        public StockQuantityChangeRecordService(IRepository<Dt_StockQuantityChangeRecord> baseDal) : base(baseDal)
        {
            _mapper = mapper;
        }
        /// <summary>
        /// 获取库存数量变更记录仓储接口
        /// </summary>
        public IRepository<Dt_StockQuantityChangeRecord> Repository => BaseDal;
        /// <summary>
        /// 记录库存变更。
        /// </summary>
        public async Task<bool> AddChangeRecordAsync(
            Dt_StockInfo? beforeStock,
            Dt_StockInfo? afterStock,
            StockChangeTypeEnum changeType,
            int? taskNum = null,
            string? orderNo = null,
            string? remark = null)
        {
            if (beforeStock == null && afterStock == null)
                return false;
            var beforeQuantity = GetStockQuantity(beforeStock);
            var afterQuantity = GetStockQuantity(afterStock);
            var beforeStatus = beforeStock?.StockStatus ?? 0;
            var afterStatus = afterStock?.StockStatus ?? 0;
            int? beforeLocationId = beforeStock?.LocationId > 0 ? beforeStock.LocationId : (int?)null;
            int? afterLocationId = afterStock?.LocationId > 0 ? afterStock.LocationId : (int?)null;
            var beforeLocationCode = beforeStock?.LocationCode;
            var afterLocationCode = afterStock?.LocationCode;
            if (beforeQuantity == afterQuantity &&
                beforeStatus == afterStatus &&
                beforeLocationId == afterLocationId &&
                beforeLocationCode == afterLocationCode)
            {
                return true;
            }
            var currentStock = afterStock ?? beforeStock!;
            Dt_StockQuantityChangeRecord record = new Dt_StockQuantityChangeRecord
            {
                StockDetailId = currentStock.Id,
                PalleCode = currentStock.PalletCode,
                MaterielCode = GetFirstValue(currentStock.Details?.Select(x => x.MaterielCode)),
                MaterielName = GetFirstValue(currentStock.Details?.Select(x => x.MaterielName)),
                BatchNo = GetFirstValue(currentStock.Details?.Select(x => x.BatchNo)),
                SerilNumber = GetFirstValue(currentStock.Details?.Select(x => x.SerialNumber)),
                OrderNo = orderNo,
                TaskNum = taskNum,
                ChangeType = (int)changeType,
                ChangeQuantity = afterQuantity - beforeQuantity,
                BeforeQuantity = beforeQuantity,
                AfterQuantity = afterQuantity,
                BeforeStatus = beforeStatus,
                AfterStatus = afterStatus,
                BeforeLocationId = beforeLocationId,
                AfterLocationId = afterLocationId,
                BeforeLocationCode = beforeLocationCode,
                AfterLocationCode = afterLocationCode,
                Remark = remark
            };
            return await BaseDal.AddDataAsync(record) > 0;
        }
        private static float GetStockQuantity(Dt_StockInfo? stockInfo)
        {
            if (stockInfo?.Details == null || !stockInfo.Details.Any())
                return 0;
            return stockInfo.Details.Sum(x => x.StockQuantity);
        }
        private static string GetFirstValue(IEnumerable<string>? values)
        {
            return values?.FirstOrDefault() ?? string.Empty;
        }
    }
}