wanshenmean
2026-03-26 e25dc0d8fad5a2362bf75cf5ca9f26a0fe6c674c
Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
@@ -1,4 +1,5 @@
using WIDESEA_Common.StockEnum;
using SqlSugar;
using WIDESEA_Common.StockEnum;
using WIDESEA_Core;
using WIDESEA_DTO.Stock;
using WIDESEA_IStockService;
@@ -51,6 +52,51 @@
        }
        /// <summary>
        /// 在事务中执行操作
        /// </summary>
        private async Task<WebResponseContent> ExecuteWithinTransactionAsync(Func<Task<WebResponseContent>> operation)
        {
            var db = StockInfoService.Repository.Db as SqlSugarClient;
            if (db == null)
            {
                return WebResponseContent.Instance.Error("Database context does not support transactions");
            }
            var ownsTransaction = db.Ado.Transaction == null;
            try
            {
                if (ownsTransaction)
                {
                    db.BeginTran();
                }
                var result = await operation();
                if (result?.Status == true)
                {
                    if (ownsTransaction)
                    {
                        db.CommitTran();
                    }
                    return result;
                }
                if (ownsTransaction)
                {
                    db.RollbackTran();
                }
                return result ?? WebResponseContent.Instance.Error("Transaction failed");
            }
            catch
            {
                if (ownsTransaction)
                {
                    db.RollbackTran();
                }
                throw;
            }
        }
        /// <summary>
        /// 组盘
        /// </summary>
        public async Task<WebResponseContent> GroupPalletAsync(StockDTO stock)
@@ -72,28 +118,29 @@
                Status = StockStatusEmun.组盘暂存.GetHashCode(),
            }).ToList();
            var existingStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
            var result = false;
            if (existingStock != null)
            return await ExecuteWithinTransactionAsync(async () =>
            {
                details.ForEach(d => d.StockId = existingStock.Id);
                result = await StockInfoDetailService.Repository.AddDataAsync(details) > 0;
                if (result) return content.OK("组盘成功");
                return content.Error("组盘失败");
            }
                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 = 1,
                StockStatus = 1,
                Creater = "system",
                Details = details
            };
                var entity = new Dt_StockInfo
                {
                    PalletCode = stock.TargetPalletNo,
                    WarehouseId = 1,
                    StockStatus = 1,
                    Creater = "system",
                    Details = details
                };
            result = StockInfoService.Repository.AddData(entity, x => x.Details);
            if (result) return content.OK("组盘成功");
            return content.Error("组盘失败");
                result = StockInfoService.Repository.AddData(entity, x => x.Details);
                return result ? content.OK("组盘成功") : content.Error("组盘失败");
            });
        }
        /// <summary>
@@ -110,44 +157,47 @@
                return content.Error("源托盘号与目标托盘号相同");
            }
            var sourceStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.SourcePalletNo);
            if (sourceStock == null) return content.Error("源托盘不存在");
            var targetStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
            if (targetStock == null)
            return await ExecuteWithinTransactionAsync(async () =>
            {
                var newStock = new Dt_StockInfo
                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)
                {
                    PalletCode = stock.TargetPalletNo,
                    WarehouseId = sourceStock.WarehouseId,
                    StockStatus = StockStatusEmun.组盘暂存.GetHashCode(),
                    Creater = "system",
                };
                    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("换盘失败");
                    var newId = StockInfoService.Repository.AddData(newStock);
                    if (newId <= 0) return content.Error("换盘失败");
                targetStock = newStock;
                targetStock.Id = newId;
            }
                    targetStock = newStock;
                    targetStock.Id = newId;
                }
            var serialNumbers = stock.Details.Select(d => d.CellBarcode).Distinct().ToList();
            if (!serialNumbers.Any()) return content.Error("未找到有效的序列号");
                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.SerialNumber));
            if (!detailEntities.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 StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "换盘")) <= 0)
                    return content.Error("换盘历史记录保存失败");
            if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock, targetStock }, "换盘")) <= 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("换盘成功");
                detailEntities.ForEach(d => d.StockId = targetStock.Id);
                var result = await StockInfoDetailService.Repository.UpdateDataAsync(detailEntities);
                if (!result) return content.Error("换盘失败");
                return content.OK("换盘成功");
            });
        }
        /// <summary>
@@ -159,31 +209,34 @@
            if (stock == null || string.IsNullOrWhiteSpace(stock.SourcePalletNo))
                return content.Error("源托盘号不能为空");
            var sourceStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.SourcePalletNo);
            if (sourceStock == null) 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("拆盘成功");
            });
        }
        /// <summary>
@@ -253,7 +306,9 @@
                Creater = s.Creater,
                CreateDate = s.CreateDate,
                Modifier = s.Modifier,
                ModifyDate = s.ModifyDate
                ModifyDate = s.ModifyDate,
                LocationId = s.LocationId,
                OutboundDate = s.OutboundDate
            }).ToList();
        }
    }