wanshenmean
2026-03-30 1a8dc6279c478a1b8e4cea78fa91ee856a720e3a
Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
@@ -1,7 +1,9 @@
using SqlSugar;
using WIDESEA_Common.StockEnum;
using WIDESEA_Core;
using WIDESEA_DTO.MES;
using WIDESEA_DTO.Stock;
using WIDESEA_IBasicService;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
@@ -33,6 +35,11 @@
        public IStockInfo_HtyService StockInfo_HtyService { get; }
        /// <summary>
        /// Mes接口服务
        /// </summary>
        public IMesService _mesService { get; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="stockInfoDetailService">库存明细服务</param>
@@ -43,12 +50,14 @@
            IStockInfoDetailService stockInfoDetailService,
            IStockInfoService stockInfoService,
            IStockInfoDetail_HtyService stockInfoDetail_HtyService,
            IStockInfo_HtyService stockInfo_HtyService)
            IStockInfo_HtyService stockInfo_HtyService,
            IMesService mesService)
        {
            StockInfoDetailService = stockInfoDetailService;
            StockInfoService = stockInfoService;
            StockInfoDetail_HtyService = stockInfoDetail_HtyService;
            StockInfo_HtyService = stockInfo_HtyService;
            _mesService = mesService;
        }
        /// <summary>
@@ -120,6 +129,20 @@
                    Status = StockStatusEmun.组盘暂存.GetHashCode(),
                }).ToList();
                var bindRequest = new BindContainerRequest
                {
                    ContainerCode = stock?.TargetPalletNo,
                    EquipmentCode = "STK-GROUP-001",
                    ResourceCode = "STK-GROUP-001",
                    LocalTime = now,
                    OperationType = 0, // 0代表组盘
                    ContainerSfcList = details.Select(d => new ContainerSfcItem
                    {
                        Sfc = d.SerialNumber,
                        Location = d.InboundOrderRowNo.ToString(),
                    }).ToList()
                };
                return await ExecuteWithinTransactionAsync(async () =>
                {
                    var existingStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
@@ -140,9 +163,15 @@
                        Creater = "system",
                        Details = details
                    };
                    result = StockInfoService.Repository.AddData(entity, x => x.Details);
                    return result ? content.OK("组盘成功") : content.Error("组盘失败");
                    if (!result) return content.Error("组盘失败");
                    var mesResult = _mesService.BindContainer(bindRequest);
                    if (mesResult == null || mesResult.Data == null || !mesResult.Data.IsSuccess)
                    {
                        return content.Error($"组盘成功,但MES绑定失败: {mesResult?.Data?.Msg ?? mesResult?.ErrorMessage ?? "未知错误"}");
                    }
                    return content.OK("组盘成功");
                });
            }
            catch (Exception ex)
@@ -204,8 +233,45 @@
                        return content.Error("换盘历史记录保存失败");
                    detailEntities.ForEach(d => d.StockId = targetStock.Id);
                    // 调用MES解绑源托盘电芯
                    var unbindRequest = new UnBindContainerRequest
                    {
                        EquipmentCode = "STK-GROUP-001",
                        ResourceCode = "STK-GROUP-001",
                        LocalTime = DateTime.Now,
                        ContainCode = stock.SourcePalletNo,
                        SfcList = detailEntities.Select(d => d.SerialNumber).ToList()
                    };
                    var unbindResult = _mesService.UnBindContainer(unbindRequest);
                    if (unbindResult == null || unbindResult.Data == null || !unbindResult.Data.IsSuccess)
                    {
                        return content.Error($"换盘成功,但MES解绑失败: {unbindResult?.Data?.Msg ?? unbindResult?.ErrorMessage ?? "未知错误"}");
                    }
                    var result = await StockInfoDetailService.Repository.UpdateDataAsync(detailEntities);
                    if (!result) return content.Error("换盘失败");
                    // 调用MES绑定目标托盘电芯
                    var bindRequest = new BindContainerRequest
                    {
                        ContainerCode = stock.TargetPalletNo,
                        EquipmentCode = "STK-GROUP-001",
                        ResourceCode = "STK-GROUP-001",
                        LocalTime = DateTime.Now,
                        OperationType = 0,
                        ContainerSfcList = detailEntities.Select(d => new ContainerSfcItem
                        {
                            Sfc = d.SerialNumber,
                            Location = d.InboundOrderRowNo.ToString()
                        }).ToList()
                    };
                    var bindResult = _mesService.BindContainer(bindRequest);
                    if (bindResult == null || bindResult.Data == null || !bindResult.Data.IsSuccess)
                    {
                        return content.Error($"换盘成功,但MES绑定失败: {bindResult?.Data?.Msg ?? bindResult?.ErrorMessage ?? "未知错误"}");
                    }
                    return content.OK("换盘成功");
                });
            }