pan
2025-11-30 dcdb87f1cb6cfd66d3fc01bc2248e4876c37f223
ÏîÄ¿´úÂë/WMSÎÞ²Ö´¢°æ/WIDESEA_WMSServer/WIDESEA_BasicService/InvokeMESService.cs
@@ -14,6 +14,7 @@
using WIDESEA_Common.StockEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.Allocate;
using WIDESEA_DTO.Basic;
using WIDESEA_DTO.Inbound;
@@ -115,9 +116,11 @@
        public async Task<ResponseModel> FeedbackAllocate(AllocateDto model)
        {
            _logger.LogInformation($"InvokeMESService  FeedbackAllocate  åºåˆ—化前: {JsonConvert.SerializeObject(model)}");
            string json = JsonConvert.SerializeObject(model, new JsonSerializerSettings
            {
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
                NullValueHandling = NullValueHandling.Include
            });
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            var _client = _httpClientFactory.CreateClient("MESUrl");
@@ -246,42 +249,89 @@
                                var inboundOrder = _inboundOrderRepository.Db.Queryable<Dt_InboundOrder>().First(x => x.InboundOrderNo == lists.FirstOrDefault().OrderNo);
                                if (inboundOrder != null)
                                {
                                    var feedmodel = new FeedbackInboundRequestModel
                                    if (inboundOrder.OrderType == (int)InOrderTypeEnum.AllocatInbound)//调拨入库
                                    {
                                        reqCode = Guid.NewGuid().ToString(),
                                        reqTime = DateTime.Now.ToString(),
                                        business_type = inboundOrder.BusinessType,
                                        factoryArea = inboundOrder.FactoryArea,
                                        operationType = 1,
                                        Operator = inboundOrder.Operator,
                                        orderNo = inboundOrder.UpperOrderNo,
                                        status = inboundOrder.OrderStatus,
                                        details = new List<FeedbackInboundDetailsModel>()
                                        var allocate = SqlSugarHelper.DbWMS.Queryable<Dt_AllocateOrder>().Where(x => x.OrderNo == inboundOrder.InboundOrderNo).First();
                                        var allocatefeedmodel = new AllocateDto
                                        {
                                            ReqCode = Guid.NewGuid().ToString(),
                                            ReqTime = DateTime.Now.ToString(),
                                            BusinessType = "2",
                                            FactoryArea = inboundOrder.FactoryArea,
                                            OperationType = 1,
                                            Operator = inboundOrder.Operator,
                                            OrderNo = inboundOrder.UpperOrderNo,
                                            fromWarehouse = allocate?.FromWarehouse ?? "",
                                            toWarehouse = allocate?.ToWarehouse ?? "",
                                            Details = new List<AllocateDtoDetail>()
                                    };
                                        };
                                    var groupedData = lists.GroupBy(item => new { item.MaterielCode, item.SupplyCode, item.BatchNo, item.InboundOrderRowNo, item.BarcodeUnit, item.WarehouseCode })
                                       .Select(group => new FeedbackInboundDetailsModel
                                       {
                                           materialCode = group.Key.MaterielCode,
                                           supplyCode = group.Key.SupplyCode,
                                           batchNo = group.Key.BatchNo,
                                           lineNo = group.Key.InboundOrderRowNo,
                                           qty = group.Sum(x => x.BarcodeQty),
                                           // warehouseCode = group.Key.WarehouseCode=="0"?"1072": group.Key.WarehouseCode,
                                           warehouseCode = group.Key.WarehouseCode,
                                           unit = group.Key.BarcodeUnit,
                                           barcodes = group.Select(row => new FeedbackBarcodesModel
                                        var groupedData = lists.GroupBy(item => new { item.MaterielCode, item.SupplyCode, item.BatchNo, item.InboundOrderRowNo, item.BarcodeUnit, item.WarehouseCode })
                                           .Select(group => new AllocateDtoDetail
                                           {
                                               barcode = row.Barcode,
                                               qty = row.BarcodeQty
                                           }).ToList()
                                       }).ToList();
                                    feedmodel.details = groupedData;
                                    var result = await FeedbackInbound(feedmodel);
                                    if (result != null && result.code == 200)
                                               MaterialCode = group.Key.MaterielCode,
                                               LineNo = group.Key.InboundOrderRowNo,
                                               WarehouseCode = group.Key.WarehouseCode,
                                               Qty = group.Sum(x => x.BarcodeQty),
                                               // warehouseCode= "1072",
                                               Unit = group.Key.BarcodeUnit,
                                               Barcodes = group.Select(row => new BarcodeInfo
                                               {
                                                   Barcode = row.Barcode,
                                                   Qty = row.BarcodeQty,
                                                   BatchNo = row.BatchNo,
                                                   SupplyCode = row.SupplyCode,
                                                   Unit = row.Unit
                                               }).ToList()
                                           }).ToList();
                                        allocatefeedmodel.Details = groupedData;
                                        var result = await FeedbackAllocate(allocatefeedmodel);
                                        if (result != null && result.code == 200)
                                        {
                                            _feedbacktomesRepository.Db.Insertable(new Dt_FeedbackToMes { OrderNo = orderNo, PalletCode = item.PalletCode, ReportStatus = 1 }).ExecuteCommand();
                                        }
                                    }
                                    else
                                    {
                                        _feedbacktomesRepository.Db.Insertable(new Dt_FeedbackToMes { OrderNo = orderNo, PalletCode = item.PalletCode, ReportStatus = 1 }).ExecuteCommand();
                                        var feedmodel = new FeedbackInboundRequestModel
                                        {
                                            reqCode = Guid.NewGuid().ToString(),
                                            reqTime = DateTime.Now.ToString(),
                                            business_type = inboundOrder.BusinessType,
                                            factoryArea = inboundOrder.FactoryArea,
                                            operationType = 1,
                                            Operator = inboundOrder.Operator,
                                            orderNo = inboundOrder.UpperOrderNo,
                                            status = inboundOrder.OrderStatus,
                                            details = new List<FeedbackInboundDetailsModel>()
                                        };
                                        var groupedData = lists.GroupBy(item => new { item.MaterielCode, item.SupplyCode, item.BatchNo, item.InboundOrderRowNo, item.BarcodeUnit, item.WarehouseCode })
                                           .Select(group => new FeedbackInboundDetailsModel
                                           {
                                               materialCode = group.Key.MaterielCode,
                                               supplyCode = group.Key.SupplyCode,
                                               batchNo = group.Key.BatchNo,
                                               lineNo = group.Key.InboundOrderRowNo,
                                               qty = group.Sum(x => x.BarcodeQty),
                                               // warehouseCode = group.Key.WarehouseCode=="0"?"1072": group.Key.WarehouseCode,
                                               warehouseCode = group.Key.WarehouseCode,
                                               unit = group.Key.BarcodeUnit,
                                               barcodes = group.Select(row => new FeedbackBarcodesModel
                                               {
                                                   barcode = row.Barcode,
                                                   qty = row.BarcodeQty
                                               }).ToList()
                                           }).ToList();
                                        feedmodel.details = groupedData;
                                        var result = await FeedbackInbound(feedmodel);
                                        if (result != null && result.code == 200)
                                        {
                                            _feedbacktomesRepository.Db.Insertable(new Dt_FeedbackToMes { OrderNo = orderNo, PalletCode = item.PalletCode, ReportStatus = 1 }).ExecuteCommand();
                                        }
                                    }
                                }
                            }
@@ -304,24 +354,150 @@
                    {
                        await HandleOutboundOrderToMESCompletion(outboundOrder, orderNo);
                    }
                    else if (outboundOrder != null && outboundOrder.IsBatch ==1)
                    else if (outboundOrder != null && outboundOrder.IsBatch == 1)
                    {
                        await HandleOutboundOrderBatchToMESCompletion(outboundOrder, orderNo);
                    }
                }
            }
            return WebResponseContent.Instance.OK();
        }
        private async Task HandleOutboundOrderBatchToMESCompletion(Dt_OutboundOrder outboundOrder, string orderNo)
        {
            try
            {
                if (outboundOrder.ReturnToMESStatus == 1)
                {
                    return;
                }
                var orderDetails = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
                    .LeftJoin<Dt_OutboundOrder>((o, item) => o.OrderId == item.Id)
                    .Where((o, item) => item.OrderNo == orderNo && item.ReturnToMESStatus != 1)
                    .Select((o, item) => o)
                    .ToListAsync();
                var detailids = new List<int>();
                var allCompleted = true;
                foreach (var detail in orderDetails.Where(x => x.ReturnToMESStatus == 0).ToList())
                {
                    if (detail.OverOutQuantity >= detail.NeedOutQuantity)
                    {
                        detailids.Add(detail.Id);
                    }
                    else
                    {
                        allCompleted = false;
                    }
                }
                if (orderDetails.Any(x => x.ReturnToMESStatus == 2))
                {
                    allCompleted = false;
                }
                int newStatus = allCompleted ? (int)OutOrderStatusEnum.出库完成 : (int)OutOrderStatusEnum.出库中;
                if (outboundOrder.OrderStatus != newStatus)
                {
                    await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
                        .SetColumns(x => x.OrderStatus == newStatus)
                        .Where(x => x.OrderNo == orderNo)
                        .ExecuteCommandAsync();
                }
                var documentno = UniqueValueGenerator.Generate();
                var feedmodel = new FeedbackOutboundRequestModel
                {
                    reqCode = Guid.NewGuid().ToString(),
                    reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    business_type = outboundOrder.BusinessType,
                    factoryArea = outboundOrder.FactoryArea,
                    operationType = 1,
                    Operator = outboundOrder.Operator,
                    orderNo = outboundOrder.UpperOrderNo,
                    documentsNO = documentno,
                    status = outboundOrder.OrderStatus,
                    details = new List<FeedbackOutboundDetailsModel>()
                };
                foreach (var detail in orderDetails.Where(x => detailids.Contains(x.Id)).ToList())
                {
                    // èŽ·å–è¯¥æ˜Žç»†å¯¹åº”çš„æ¡ç ä¿¡æ¯ï¼ˆä»Žé”å®šè®°å½•ï¼‰
                    var detailLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                        .Where(x => x.OrderNo == orderNo && detailids.Contains(x.OrderDetailId) &&
                                   x.Status == (int)OutLockStockStatusEnum.拣选完成)
                        .ToListAsync();
                    var detailModel = new FeedbackOutboundDetailsModel
                    {
                        materialCode = detail.MaterielCode,
                        lineNo = detail.lineNo, // æ³¨æ„ï¼šè¿™é‡Œå¯èƒ½éœ€è¦è°ƒæ•´å­—段名
                        warehouseCode = detail.WarehouseCode,
                        qty = detail.OverOutQuantity, // ä½¿ç”¨è®¢å•明细的已出库数量
                        currentDeliveryQty = detail.OverOutQuantity,
                        unit = detail.Unit,
                        barcodes = detailLocks.Select(lockInfo => new WIDESEA_DTO.Outbound.BarcodesModel
                        {
                            barcode = lockInfo.CurrentBarcode,
                            supplyCode = lockInfo.SupplyCode,
                            batchNo = lockInfo.BatchNo,
                            unit = lockInfo.Unit,
                            qty = lockInfo.PickedQty // æ¡ç çº§åˆ«çš„æ•°é‡ä»ç”¨é”å®šè®°å½•
                        }).ToList()
                    };
                    feedmodel.details.Add(detailModel);
                }
                var result = await FeedbackOutbound(feedmodel);
                if (result != null && result.code == 200)
                {
                    await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
                           .SetColumns(it => new Dt_OutboundOrderDetail
                           {
                               ReturnToMESStatus = 1,
                               documentsNO = documentno,
                           })
                        .Where(x => detailids.Contains(x.Id))
                        .ExecuteCommandAsync();
                    if (allCompleted && newStatus == (int)OutOrderStatusEnum.出库完成)
                    {
                        await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
                          .SetColumns(x => x.ReturnToMESStatus == 1)
                          .Where(x => x.OrderNo == orderNo)
                          .ExecuteCommandAsync();
                    }
                }
                else
                {
                    await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
                         .SetColumns(it => new Dt_OutboundOrderDetail
                         {
                             ReturnToMESStatus = 2,
                             documentsNO = documentno,
                         })
                      .Where(x => detailids.Contains(x.Id))
                      .ExecuteCommandAsync();
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"CheckAndUpdateOrderStatus失败 - OrderNo: {orderNo}, Error: {ex.Message}");
            }
        }
        private async Task HandleOutboundOrderToMESCompletion(Dt_OutboundOrder outboundOrder, string orderNo)
        {
            try
            {
                if (outboundOrder.ReturnToMESStatus == 1 || outboundOrder.IsBatch == 1)
                if (outboundOrder.ReturnToMESStatus == 1)
                {
                    return;
                }