using WIDESEA_Common.StockEnum; using WIDESEA_Core.BaseRepository; using WIDESEA_Core.BaseServices; using WIDESEA_IRecordService; using WIDESEA_Model.Models; namespace WIDESEA_RecordService { /// /// 库存数量变更记录服务实现类 /// public partial class StockQuantityChangeRecordService : ServiceBase>, IStockQuantityChangeRecordService { /// /// 构造函数 /// public StockQuantityChangeRecordService(IRepository baseDal) : base(baseDal) { } /// /// 获取库存数量变更记录仓储接口 /// public IRepository Repository => BaseDal; /// /// 记录库存变更。 /// public async Task 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? values) { return values?.FirstOrDefault() ?? string.Empty; } } }