wanshenmean
2026-02-11 e6b190354191122069b1a0518f050d6504f7ec5e
Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
@@ -11,20 +11,15 @@
    public class StockSerivce : IStockService
    {
        public IStockInfoDetailService StockInfoDetailService { get; }
        public IStockInfoService StockInfoService { get; }
        public IStockInfoDetail_HtyService StockInfoDetail_HtyService { get; }
        public IStockInfo_HtyService StockInfo_HtyService { get; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="stockInfoDetailService">库存明细服务</param>
        /// <param name="stockInfoService">库存服务</param>
        /// <param name="stockInfoDetail_HtyService">库存明细历史服务</param>
        /// <param name="stockInfo_HtyService">库存历史服务</param>
        public StockSerivce(IStockInfoDetailService stockInfoDetailService, IStockInfoService stockInfoService, IStockInfoDetail_HtyService stockInfoDetail_HtyService, IStockInfo_HtyService stockInfo_HtyService)
        public StockSerivce(
            IStockInfoDetailService stockInfoDetailService,
            IStockInfoService stockInfoService,
            IStockInfoDetail_HtyService stockInfoDetail_HtyService,
            IStockInfo_HtyService stockInfo_HtyService)
        {
            StockInfoDetailService = stockInfoDetailService;
            StockInfoService = stockInfoService;
@@ -35,15 +30,13 @@
        /// <summary>
        /// 组盘
        /// </summary>
        /// <param name="stock">组盘数据</param>
        /// <returns>是否成功</returns>
        public async Task<bool> GroupPallet(StockDTO stock)
        {
            // 组装明细数据
            var now = DateTime.Now;
            var details = stock.Details.Select(item => new Dt_StockInfoDetail()
            var details = stock.Details.Select(item => new Dt_StockInfoDetail
            {
                MaterielCode = "电芯",
                MaterielName = "电芯",
                StockQuantity = item.Quantity,
                Unit = "PCS",
                Creater = "system",
@@ -52,20 +45,17 @@
                EffectiveDate = now.AddYears(1).ToString(),
                SerialNumber = item.CellBarcode,
                InboundOrderRowNo = item.Channel,
                MaterielName = "电芯",
                Status = StockStatusEmun.组盘暂存.GetHashCode(),
            }).ToList();
            // 如果托盘已存在,直接新增明细
            var existingStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
            if (existingStock != null)
            {
                details.ForEach(detail => detail.StockId = existingStock.Id);
                details.ForEach(d => d.StockId = existingStock.Id);
                return await StockInfoDetailService.Repository.AddDataAsync(details) > 0;
            }
            // 托盘不存在则创建并写入明细
            var entity = new Dt_StockInfo()
            var entity = new Dt_StockInfo
            {
                PalletCode = stock.TargetPalletNo,
                WarehouseId = 1,
@@ -80,34 +70,23 @@
        /// <summary>
        /// 换盘
        /// </summary>
        /// <param name="stock">需要换盘的明细数据</param>
        /// <returns>是否成功</returns>
        public async Task<bool> ChangePallet(StockDTO stock)
        {
            // 参数校验
            if (stock == null || string.IsNullOrWhiteSpace(stock.TargetPalletNo) || string.IsNullOrWhiteSpace(stock.SourcePalletNo))
            if (stock == null ||
                string.IsNullOrWhiteSpace(stock.TargetPalletNo) ||
                string.IsNullOrWhiteSpace(stock.SourcePalletNo) ||
                string.Equals(stock.SourcePalletNo, stock.TargetPalletNo, StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
            // 新旧托盘一致不处理
            if (string.Equals(stock.SourcePalletNo, stock.TargetPalletNo, StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
            // 查询源托盘
            var sourceStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.SourcePalletNo);
            if (sourceStock == null)
            {
                return false;
            }
            if (sourceStock == null) return false;
            // 查询或创建目标托盘
            var targetStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
            if (targetStock == null)
            {
                var newStock = new Dt_StockInfo()
                var newStock = new Dt_StockInfo
                {
                    PalletCode = stock.TargetPalletNo,
                    WarehouseId = sourceStock.WarehouseId,
@@ -116,146 +95,103 @@
                };
                var newId = StockInfoService.Repository.AddData(newStock);
                if (newId <= 0)
                {
                    return false;
                }
                if (newId <= 0) return false;
                targetStock = newStock;
                targetStock.Id = newId;
            }
            // 获取需要转移的条码
            var serialNumbers = stock.Details.Select(d => d.CellBarcode).Distinct().ToList();
            if (!serialNumbers.Any())
            {
            if (!serialNumbers.Any()) return false;
            var detailEntities = StockInfoDetailService.Repository.QueryData(
                d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.SerialNumber));
            if (!detailEntities.Any()) return false;
            if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "换盘")) <= 0)
                return false;
            }
            // 查询源托盘对应的明细
            var detailEntities = StockInfoDetailService.Repository.QueryData(d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.SerialNumber));
            if (!detailEntities.Any())
            {
            if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock, targetStock }, "换盘")) <= 0)
                return false;
            }
            // 写入历史记录
            var detailHistory = CreateDetailHistory(detailEntities, "换盘");
            if (await StockInfoDetail_HtyService.Repository.AddDataAsync(detailHistory) <= 0)
            {
                return false;
            }
            var stockHistory = CreateStockHistory(new List<Dt_StockInfo> { sourceStock, targetStock }, "换盘");
            if (await StockInfo_HtyService.Repository.AddDataAsync(stockHistory) <= 0)
            {
                return false;
            }
            // 更新明细的托盘主键
            detailEntities.ForEach(detail => detail.StockId = targetStock.Id);
            detailEntities.ForEach(d => d.StockId = targetStock.Id);
            return await StockInfoDetailService.Repository.UpdateDataAsync(detailEntities);
        }
        /// <summary>
        /// 拆盘
        /// </summary>
        /// <param name="stock">需要拆盘的明细数据</param>
        /// <returns>是否成功</returns>
        public async Task<bool> SplitPallet(StockDTO stock)
        {
            // 参数校验
            if (stock == null || string.IsNullOrWhiteSpace(stock.SourcePalletNo))
            {
                return false;
            }
            // 查询源托盘
            var sourceStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.SourcePalletNo);
            if (sourceStock == null)
            {
                return false;
            }
            if (sourceStock == null) return false;
            // 获取需要拆除的条码
            var serialNumbers = stock.Details.Select(d => d.CellBarcode).Distinct().ToList();
            if (!serialNumbers.Any())
            {
                return false;
            }
            if (!serialNumbers.Any()) return false;
            // 查询源托盘对应的明细
            var detailEntities = StockInfoDetailService.Repository.QueryData(d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.SerialNumber));
            if (!detailEntities.Any())
            {
                return false;
            }
            var detailEntities = StockInfoDetailService.Repository.QueryData(
                d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.SerialNumber));
            if (!detailEntities.Any()) return false;
            // 写入历史记录
            var detailHistory = CreateDetailHistory(detailEntities, "拆盘");
            if (await StockInfoDetail_HtyService.Repository.AddDataAsync(detailHistory) <= 0)
            {
            if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "拆盘")) <= 0)
                return false;
            }
            var stockHistory = CreateStockHistory(new List<Dt_StockInfo> { sourceStock }, "拆盘");
            if (await StockInfo_HtyService.Repository.AddDataAsync(stockHistory) <= 0)
            {
            if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock }, "拆盘")) <= 0)
                return false;
            }
            // 删除明细
            return await StockInfoDetailService.Repository.DeleteDataAsync(detailEntities);
        }
        private List<Dt_StockInfoDetail_Hty> CreateDetailHistory(IEnumerable<Dt_StockInfoDetail> details, string operateType)
        private static List<Dt_StockInfoDetail_Hty> CreateDetailHistory(IEnumerable<Dt_StockInfoDetail> details, string operateType)
        {
            var now = DateTime.Now;
            return details.Select(detail => new Dt_StockInfoDetail_Hty
            return details.Select(d => new Dt_StockInfoDetail_Hty
            {
                SourceId = detail.Id,
                SourceId = d.Id,
                OperateType = operateType,
                InsertTime = now,
                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,
                Creater = detail.Creater,
                CreateDate = detail.CreateDate,
                Modifier = detail.Modifier,
                ModifyDate = detail.ModifyDate
                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 List<Dt_StockInfo_Hty> CreateStockHistory(IEnumerable<Dt_StockInfo> stocks, string operateType)
        private static List<Dt_StockInfo_Hty> CreateStockHistory(IEnumerable<Dt_StockInfo> stocks, string operateType)
        {
            var now = DateTime.Now;
            return stocks.Select(stock => new Dt_StockInfo_Hty
            return stocks.Select(s => new Dt_StockInfo_Hty
            {
                SourceId = stock.Id,
                SourceId = s.Id,
                OperateType = operateType,
                InsertTime = now,
                PalletCode = stock.PalletCode,
                PalletType = stock.PalletType,
                LocationCode = stock.LocationCode,
                WarehouseId = stock.WarehouseId,
                StockStatus = stock.StockStatus,
                Remark = stock.Remark,
                Creater = stock.Creater,
                CreateDate = stock.CreateDate,
                Modifier = stock.Modifier,
                ModifyDate = stock.ModifyDate
                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
            }).ToList();
        }
    }