1
yangpeixing
2026-02-04 e4a391c581aaca1fa7b0239bc96375d0c0cb8de6
WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
@@ -20,20 +20,30 @@
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Database;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using Org.BouncyCastle.Asn1.Tsp;
using Spire.Pdf;
using SqlSugar;
using System;
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks;
using WIDESEA_BasicRepository;
using WIDESEA_Common;
using WIDESEA_Common.Log;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Core;
@@ -43,6 +53,7 @@
using WIDESEA_Core.Helper;
using WIDESEA_Core.TaskEnum;
using WIDESEA_DTO.Basic;
using WIDESEA_DTO.ERP;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.Stock;
using WIDESEA_DTO.Task;
@@ -50,6 +61,7 @@
using WIDESEA_IBasicService;
using WIDESEA_IInboundRepository;
using WIDESEA_IInboundService;
using WIDESEA_InboundRepository;
using WIDESEA_IOutboundRepository;
using WIDESEA_IOutboundService;
using WIDESEA_IRecordService;
@@ -59,28 +71,19 @@
using WIDESEA_ITaskInfoService;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.Inbound;
using WIDESEA_OutboundRepository;
using WIDESEA_StockRepository;
using WIDESEA_TaskInfoRepository;
using static WIDESEA_Common.Authentication;
using static WIDESEA_Common.HouseBackboundPassBack;
using static WIDESEA_Common.HouseInboundPassBack;
using static WIDESEA_Common.HouseInboundPassBack.data.data1;
using static WIDESEA_Common.HouseoutboundPassBack;
using static WIDESEA_Common.InventoryAllocate;
using static WIDESEA_Common.NewHouseInboundPassBack;
using static WIDESEA_Common.NewHouseInboundPassBack.Parame.Syncretism;
using static WIDESEA_ITaskInfoService.ITaskService;
using Parameter = WIDESEA_Common.Parameter;
using WIDESEA_DTO.ERP;
using WIDESEA_OutboundRepository;
using System.ComponentModel.DataAnnotations;
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Spire.Pdf;
using WIDESEA_InboundRepository;
using System.Drawing.Printing;
using System;
using WIDESEA_Common.Log;
using static WIDESEA_Common.HouseInboundPassBack.data.data1;
namespace WIDESEA_TaskInfoService
{
@@ -720,6 +723,7 @@
                                }
                                if (inboundOrder.OrderStatus == InboundStatusEnum.入库完成.ObjToInt() && inboundOrder.OrderType == 0 && inboundOrder.System.Equals("SMOM"))
                                {
                                    List<Dt_StockInfo> StockInfos = _stockRepository.StockInfoRepository.Db
                                        .Queryable<Dt_StockInfo>()
                                        .Includes(x => x.Details, d => d.StockDetails)
@@ -872,7 +876,79 @@
                                        }
                                    }
                                }
                                else if (inboundOrder.OrderStatus == InboundStatusEnum.入库完成.ObjToInt() && inboundOrder.OrderType == 5 && inboundOrder.System.Equals("SMOM"))
                                {
                                    List<Dt_StockInfo> StockInfos = _stockRepository.StockInfoRepository.Db
                                         .Queryable<Dt_StockInfo>()
                                         .Includes(x => x.Details, d => d.StockDetails)
                                         .Where(x => x.WarehouseId == task.WarehouseId &&
                                                     x.Details.Any(v => v.OrderNo == inboundOrder.OrderNo))
                                         .ToList();
                                    Dt_InboundOrder? dt_InboundOrder = _inboundService.InbounOrderService.Db.Queryable<Dt_InboundOrder>().Where(x => x.OrderNo == inboundOrder.OrderNo).Includes(x => x.Details).First();
                                    if (StockInfos.Count  == 0) throw new Exception("未找到库存信息");
                                    var houseSyncretism = new NewHouseInboundPassBack
                                    {
                                        ApiType = "InventoryMoveController",
                                        Method = "AsrsUnPickingDatas",
                                        Parameters = new List<NewHouseInboundPassBack.Parame>
                                        {
                                            new NewHouseInboundPassBack.Parame
                                            {
                                                Value =  new Parame.Syncretism
                                                    {
                                                        OrderNo = inboundOrder.OrderNo,
                                                        Details =   StockInfos.SelectMany(stockInfo =>
                                                    stockInfo.Details.Select(g =>
                                                    {
                                                       //var InboundOrderde1=dt_InboundOrder.Details.FirstOrDefault;
                                                       return new Parame.Syncretism.details
                                                       {
                                                        MoveType = 0,
                                                        WareHouseCode = warehouse.WarehouseCode,
                                                        ItemCode = g.MaterielCode,
                                                        MoveNumber = g.StockQuantity,
                                                        LotNo = g.BatchNo,
                                                        WipBatch = g.BatchNo,
                                                        Lpn = g.StockDetails.FirstOrDefault().LPNNO,
                                                        LocationName = g.StockDetails.FirstOrDefault().OrinalLocation,
                                                        TargetLocName = stockInfo.LocationCode, // 添加null检查
                                                        TargetLpn = stockInfo.PalletCode, // 添加null检查
                                                       };
                                                    })).ToList()
                                                    }
                                            }
                                        }
                                    };
                                    var authResult = AuthenticateWithWMS();
                                    if (authResult.IsSuccess)
                                    {
                                        houseSyncretism.Context = new Dictionary<string, string>
                                            {
                                                { "Ticket", authResult.Ticket },
                                                { "InvOrgId", authResult.InvOrgId }
                                            };
                                        var response = HttpHelper.Post<MomRequestContent>(ReceiveWMSTaskin, houseSyncretism, "立库入库数量回传WMS");
                                        if (!response.Success)
                                        {
                                            throw new Exception($"操作失败: {response.Message ?? "未提供错误信息"}");
                                        }
                                    }
                                }
                                _unitOfWorkManage.CommitTran();
                                return WebResponseContent.Instance.OK();
                            }
@@ -1686,10 +1762,13 @@
                                       x.LocationName == stockInfo.LocationCode &&
                                       x.LPNNo == stockInfo.PalletCode)
                                .ToList();
                            List<Dt_NewOutboundOrderDetail> outboundOrderDetails1 = _outboundService.NewOutboundOrderDetailService.Db.Queryable<Dt_NewOutboundOrderDetail>()
                            List<Dt_NewOutboundOrderDetail> outboundOrderDetails1 = new List<Dt_NewOutboundOrderDetail>();
                            if (outboundOrderDetails == null || outboundOrderDetails.Count == 0)
                            {
                                outboundOrderDetails1 = _outboundService.NewOutboundOrderDetailService.Db.Queryable<Dt_NewOutboundOrderDetail>()
                                .Where(x => x.OrderId == outboundOrder.Id)                                 
                                .ToList();
                            }
                            if ((outboundOrderDetails == null && outboundOrderDetails1 == null) || (outboundOrderDetails.Count == 0  && outboundOrderDetails1.Count == 0))
                            {
                                throw new Exception($"未找到托盘 {stockInfo.PalletCode} 在货位 {stockInfo.LocationCode} 上的出库单明细");
@@ -1698,6 +1777,7 @@
                            var stockInfoDetails = stockInfo.Details.ToList();
                            int overCount = outboundOrder.Details.Count(x => x.OrderDetailStatus == OrderDetailStatusEnum.Over.ObjToInt());
                            if (outboundOrderDetails == null || outboundOrderDetails.Count == 0)
                            {
                                foreach (var stockDetail in stockInfoDetails)
@@ -1858,7 +1938,7 @@
                                        }
                                        // 调拨出库
                                        if (outboundOrder.OrderType == 2)
                                        if (outboundOrder.OrderType == 4)
                                        {
                                            var allocate = new InventoryAllocate
                                            {
@@ -2090,7 +2170,7 @@
                                        }
                                        // 调拨出库
                                        if (outboundOrder.OrderType == 2)
                                        if (outboundOrder.OrderType == 4)
                                        {
                                            var allocate = new InventoryAllocate
                                            {
@@ -2954,9 +3034,9 @@
                            }
                        }
                        // 只减去单据明细中的数量
                        matchedStockDetail.OutboundQuantity = 0;
                        matchedStockDetail.StockQuantity = outDetail.OrderQuantity;
                        matchedStockDetail.OrderNo = returnOrder.OrderNo;
                        //matchedStockDetail.OutboundQuantity = 0;
                        //matchedStockDetail.StockQuantity = outDetail.OrderQuantity;
                        //matchedStockDetail.OrderNo = returnOrder.OrderNo;
                        // 更新匹配的明细
                        _stockService.StockInfoDetailService.Repository.UpdateData(matchedStockDetail);
                    }
@@ -2979,9 +3059,14 @@
                    decimal totalOutboundQuantity = outboundOrderDetails.Sum(x => x.OrderQuantity);
                    decimal totalStockQuantity = stockInfoDetails.Sum(x => x.StockQuantity);
                    foreach (var detail in stockInfo.Details)
                    {
                    foreach (var groupeDetail in groupedDetails)
                    {
                        var outboundDetail = groupeDetail.First();
                            if (outboundDetail.MaterielCode == detail.MaterielCode)
                            {
                        decimal totalOrderQuantity = groupeDetail.Sum(x => x.OrderQuantity);
                        var matchedStockDetail = stockInfoDetails.FirstOrDefault(x => x.MaterielCode == outboundDetail.MaterielCode && x.BatchNo == outboundDetail.BatchNo);
                        if (matchedStockDetail == null)
@@ -3023,38 +3108,21 @@
                            // 添加返库单
                            returnOrders.Add(returnOrder);
                            // 更新当前批次明细:只保留出库数量,其余返库
                            matchedStockDetail.StockQuantity = remainingQuantity;
                            matchedStockDetail.OutboundQuantity = outboundDetail.OrderQuantity;
                            //matchedStockDetail.OrderNo = returnOrder.OrderNo;
                        }
                        else if (remainingQuantity == 0)
                        {
                            // 当前批次正好出完
                            matchedStockDetail.OutboundQuantity = matchedStockDetail.StockQuantity;
                            if (matchedStockDetail.StockQuantity == totalOrderQuantity)
                            {
                                _stockService.StockInfoDetailService.Repository.DeleteAndMoveIntoHty(
                                    matchedStockDetail, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                            }
                            else
                            {
                                matchedStockDetail.StockQuantity = 0;
                                _stockService.StockInfoDetailService.Repository.UpdateData(matchedStockDetail);
                            }
                        }
                        else
                                if (remainingQuantity < 0 && matchedStockDetail.StockQuantity < matchedStockDetail.OutboundQuantity)
                        {
                            throw new Exception($"出库数量 {outboundDetail.OrderQuantity} 大于库存数量 {matchedStockDetail.StockQuantity}");
                        }
                        processedStockDetails.Add(matchedStockDetail);
                    }
                    // 处理其他批次的库存明细(全部返库)
                    var otherBatchDetails = stockInfoDetails
                        .Where(x => !processedStockDetails.Contains(x) && x.StockQuantity > 0)
                                    .Where(x => !processedStockDetails.Contains(x) && x.StockQuantity > 0 && x.OutboundQuantity == 0)
                        .ToList();
                                if (returnOrders == null || returnOrders.Count == 0)
                                {
                    if (otherBatchDetails.Count > 0)
                    {
                        decimal totalOtherQuantity = otherBatchDetails.Sum(x => x.StockQuantity);
@@ -3082,9 +3150,19 @@
                        {
                            otherReturnOrder.LocationCode = stockInfo.LocationCode;
                        }
                        returnOrders.Add(otherReturnOrder);
                                    }
                                }
                                else
                                {
                                    decimal totalOtherQuantity = otherBatchDetails.Sum(x => x.StockQuantity);
                                    foreach (var item in returnOrders)
                                    {
                                        item.OrderQuantity = totalOrderQuantity;
                                        item.ReceiptQuantity = totalOrderQuantity;
                                        item.Remark = $"多批次返库,共{otherBatchDetails.Count}个批次";
                                    }
                    }
                    // 批量更新已处理的库存明细
@@ -3102,7 +3180,7 @@
                    // 更新库存主表状态
                    decimal returnQuantity = totalStockQuantity - totalOutboundQuantity;
                    if (returnQuantity == 0)
                                if (returnQuantity == 0 || stockInfo.Details.All(x => x.StockQuantity == x.OutboundQuantity))
                    {
                        if (outboundOrder.System == "SMOM" && stockInfo.LocationCode != null)
                        {
@@ -3111,11 +3189,11 @@
                            _basicRepository.LocationInfoRepository.UpdateData(dt_LocationInfo);
                        }
                        // 全部出库,删除库存
                        DeleteAndMoveIntoHtStockStatus(stockInfo);
                        foreach (var item in stockInfo.Details)
                        {
                            BaseDal.Db.Deleteable(item.StockDetails).ExecuteCommand();
                        }
                                    //DeleteAndMoveIntoHtStockStatus(stockInfo);
                                    //foreach (var item in stockInfo.Details)
                                    //{
                                    //    BaseDal.Db.Deleteable(item.StockDetails).ExecuteCommand();
                                    //}
                    }
                    else if (returnQuantity > 0)
                    {
@@ -3125,10 +3203,14 @@
                        stockInfo.Remark = $"余料退库,共{returnOrders.Count}个返库单";
                        _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                    }
                            }
                        }
                }
                _unitOfWorkManage.CommitTran();
            }
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();