wanshenmean
2026-03-27 c1aabd3aaa92b072591fc368d81ab2cc37a0aa14
Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
@@ -102,46 +102,53 @@
        public async Task<WebResponseContent> GroupPalletAsync(StockDTO stock)
        {
            WebResponseContent content = new WebResponseContent();
            var now = DateTime.Now;
            var details = stock.Details.Select(item => new Dt_StockInfoDetail
            try
            {
                MaterielCode = "电芯",
                MaterielName = "电芯",
                StockQuantity = item.Quantity,
                Unit = "PCS",
                Creater = "system",
                OrderNo = "111",
                ProductionDate = now.ToString(),
                EffectiveDate = now.AddYears(1).ToString(),
                SerialNumber = item.CellBarcode,
                InboundOrderRowNo = item.Channel,
                Status = StockStatusEmun.组盘暂存.GetHashCode(),
            }).ToList();
            return await ExecuteWithinTransactionAsync(async () =>
            {
                var existingStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
                var result = false;
                if (existingStock != null)
                var now = DateTime.Now;
                var details = stock.Details.Select(item => new Dt_StockInfoDetail
                {
                    details.ForEach(d => d.StockId = existingStock.Id);
                    result = await StockInfoDetailService.Repository.AddDataAsync(details) > 0;
                    return result ? content.OK("组盘成功") : content.Error("组盘失败");
                }
                var entity = new Dt_StockInfo
                {
                    PalletCode = stock.TargetPalletNo,
                    //WarehouseId = stock.WarehouseId > 0 ? stock.WarehouseId : 1,
                    WarehouseId = 1,
                    StockStatus = StockStatusEmun.组盘暂存.GetHashCode(),
                    MaterielCode = "电芯",
                    MaterielName = "电芯",
                    StockQuantity = item.Quantity,
                    Unit = "PCS",
                    Creater = "system",
                    Details = details
                };
                    OrderNo = "111",
                    ProductionDate = now.ToString(),
                    EffectiveDate = now.AddYears(1).ToString(),
                    SerialNumber = item.CellBarcode,
                    InboundOrderRowNo = item.Channel,
                    Status = StockStatusEmun.组盘暂存.GetHashCode(),
                }).ToList();
                result = StockInfoService.Repository.AddData(entity, x => x.Details);
                return result ? content.OK("组盘成功") : content.Error("组盘失败");
            });
                return await ExecuteWithinTransactionAsync(async () =>
                {
                    var existingStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
                    var result = false;
                    if (existingStock != null)
                    {
                        details.ForEach(d => d.StockId = existingStock.Id);
                        result = await StockInfoDetailService.Repository.AddDataAsync(details) > 0;
                        return result ? content.OK("组盘成功") : content.Error("组盘失败");
                    }
                    var entity = new Dt_StockInfo
                    {
                        PalletCode = stock.TargetPalletNo,
                        //WarehouseId = stock.WarehouseId > 0 ? stock.WarehouseId : 1,
                        WarehouseId = 1,
                        StockStatus = StockStatusEmun.组盘暂存.GetHashCode(),
                        Creater = "system",
                        Details = details
                    };
                    result = StockInfoService.Repository.AddData(entity, x => x.Details);
                    return result ? content.OK("组盘成功") : content.Error("组盘失败");
                });
            }
            catch (Exception ex)
            {
                return content.Error($"组盘失败: {ex.Message}");
            }
        }
        /// <summary>
@@ -150,55 +157,62 @@
        public async Task<WebResponseContent> ChangePalletAsync(StockDTO stock)
        {
            WebResponseContent content = new WebResponseContent();
            if (stock == null ||
                string.IsNullOrWhiteSpace(stock.TargetPalletNo) ||
                string.IsNullOrWhiteSpace(stock.SourcePalletNo) ||
                string.Equals(stock.SourcePalletNo, stock.TargetPalletNo, StringComparison.OrdinalIgnoreCase))
            try
            {
                return content.Error("源托盘号与目标托盘号相同");
            }
            return await ExecuteWithinTransactionAsync(async () =>
            {
                var sourceStock = await StockInfoService.Repository.QueryDataNavFirstAsync(s => s.PalletCode == stock.SourcePalletNo);
                if (sourceStock == null) return content.Error("源托盘不存在");
                var targetStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
                if (targetStock == null)
                if (stock == null ||
                    string.IsNullOrWhiteSpace(stock.TargetPalletNo) ||
                    string.IsNullOrWhiteSpace(stock.SourcePalletNo) ||
                    string.Equals(stock.SourcePalletNo, stock.TargetPalletNo, StringComparison.OrdinalIgnoreCase))
                {
                    var newStock = new Dt_StockInfo
                    {
                        PalletCode = stock.TargetPalletNo,
                        WarehouseId = sourceStock.WarehouseId,
                        StockStatus = StockStatusEmun.组盘暂存.GetHashCode(),
                        Creater = "system",
                    };
                    var newId = StockInfoService.Repository.AddData(newStock);
                    if (newId <= 0) return content.Error("换盘失败");
                    targetStock = newStock;
                    targetStock.Id = newId;
                    return content.Error("源托盘号与目标托盘号相同");
                }
                var serialNumbers = stock.Details.Select(d => d.Channel).Distinct().ToList();
                if (!serialNumbers.Any()) return content.Error("未找到有效的序列号");
                return await ExecuteWithinTransactionAsync(async () =>
                {
                    var sourceStock = await StockInfoService.Repository.QueryDataNavFirstAsync(s => s.PalletCode == stock.SourcePalletNo);
                    if (sourceStock == null) return content.Error("源托盘不存在");
                var detailEntities = StockInfoDetailService.Repository.QueryData(
                    d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.InboundOrderRowNo));
                if (!detailEntities.Any()) return content.Error("未找到有效的库存明细");
                    var targetStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
                    if (targetStock == null)
                    {
                        var newStock = new Dt_StockInfo
                        {
                            PalletCode = stock.TargetPalletNo,
                            WarehouseId = sourceStock.WarehouseId,
                            StockStatus = StockStatusEmun.组盘暂存.GetHashCode(),
                            Creater = "system",
                        };
                if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "换盘")) <= 0)
                    return content.Error("换盘历史记录保存失败");
                        var newId = StockInfoService.Repository.AddData(newStock);
                        if (newId <= 0) return content.Error("换盘失败");
                if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock, targetStock }, "换盘")) <= 0)
                    return content.Error("换盘历史记录保存失败");
                        targetStock = newStock;
                        targetStock.Id = newId;
                    }
                detailEntities.ForEach(d => d.StockId = targetStock.Id);
                var result = await StockInfoDetailService.Repository.UpdateDataAsync(detailEntities);
                if (!result) return content.Error("换盘失败");
                return content.OK("换盘成功");
            });
                    var serialNumbers = stock.Details.Select(d => d.Channel).Distinct().ToList();
                    if (!serialNumbers.Any()) return content.Error("未找到有效的序列号");
                    var detailEntities = StockInfoDetailService.Repository.QueryData(
                        d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.InboundOrderRowNo));
                    if (!detailEntities.Any()) return content.Error("未找到有效的库存明细");
                    if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "换盘")) <= 0)
                        return content.Error("换盘历史记录保存失败");
                    if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock, targetStock }, "换盘")) <= 0)
                        return content.Error("换盘历史记录保存失败");
                    detailEntities.ForEach(d => d.StockId = targetStock.Id);
                    var result = await StockInfoDetailService.Repository.UpdateDataAsync(detailEntities);
                    if (!result) return content.Error("换盘失败");
                    return content.OK("换盘成功");
                });
            }
            catch (Exception ex)
            {
                return content.Error($"换盘失败: {ex.Message}");
            }
        }
        /// <summary>
@@ -207,37 +221,44 @@
        public async Task<WebResponseContent> SplitPalletAsync(StockDTO stock)
        {
            WebResponseContent content = new WebResponseContent();
            if (stock == null || string.IsNullOrWhiteSpace(stock.SourcePalletNo))
                return content.Error("源托盘号不能为空");
            return await ExecuteWithinTransactionAsync(async () =>
            try
            {
                var sourceStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.SourcePalletNo);
                if (sourceStock == null) return content.Error("源托盘不存在");
                if (stock == null || string.IsNullOrWhiteSpace(stock.SourcePalletNo))
                    return content.Error("源托盘号不能为空");
                var serialNumbers = stock.Details.Select(d => d.CellBarcode).Distinct().ToList();
                if (!serialNumbers.Any())
                return await ExecuteWithinTransactionAsync(async () =>
                {
                    serialNumbers = sourceStock.Details
                                                .Where(x => stock.Details.Any(d => d.Channel == x.InboundOrderRowNo))
                                                .Select(x => x.SerialNumber)
                                                .ToList();
                }
                    var sourceStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.SourcePalletNo);
                    if (sourceStock == null) return content.Error("源托盘不存在");
                var detailEntities = StockInfoDetailService.Repository.QueryData(
                    d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.SerialNumber));
                if (!detailEntities.Any()) return content.Error("未找到有效的库存明细");
                    var serialNumbers = stock.Details.Select(d => d.CellBarcode).Distinct().ToList();
                    if (!serialNumbers.Any())
                    {
                        serialNumbers = sourceStock.Details
                                                    .Where(x => stock.Details.Any(d => d.Channel == x.InboundOrderRowNo))
                                                    .Select(x => x.SerialNumber)
                                                    .ToList();
                    }
                if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "拆盘")) <= 0)
                    return content.Error("拆盘历史记录保存失败");
                    var detailEntities = StockInfoDetailService.Repository.QueryData(
                        d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.SerialNumber));
                    if (!detailEntities.Any()) return content.Error("未找到有效的库存明细");
                if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock }, "拆盘")) <= 0)
                    return content.Error("拆盘历史记录保存失败");
                    if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "拆盘")) <= 0)
                        return content.Error("拆盘历史记录保存失败");
                var result = await StockInfoDetailService.Repository.DeleteDataAsync(detailEntities);
                if (!result) return content.Error("拆盘失败");
                return content.OK("拆盘成功");
            });
                    if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock }, "拆盘")) <= 0)
                        return content.Error("拆盘历史记录保存失败");
                    var result = await StockInfoDetailService.Repository.DeleteDataAsync(detailEntities);
                    if (!result) return content.Error("拆盘失败");
                    return content.OK("拆盘成功");
                });
            }
            catch (Exception ex)
            {
                return content.Error($"拆盘失败: {ex.Message}");
            }
        }
        /// <summary>