heshaofeng
2025-12-01 5f0b86328b2678a326166e84b461e9ede3dc4715
ÏîÄ¿´úÂë/WMSÎÞ²Ö´¢°æ/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
@@ -51,6 +51,8 @@
using WIDESEA_IStockService;
using WIDESEA_ITaskInfoService;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.Check;
using WIDESEA_Model.Models.Outbound;
namespace WIDESEA_TaskInfoService
{
@@ -65,6 +67,8 @@
        private readonly IInboundOrderService _inboundOrderService;
        private readonly IInboundOrderDetailService _inboundOrderDetailService;
        private readonly IRepository<Dt_ReCheckOrder> _reCheckOrderRepository;
        private readonly IRepository<Dt_OutboundBatch> _OutboundBatchRepository;
        private readonly IOutboundOrderService _outboundOrderService;
        private readonly IOutboundOrderDetailService _outboundOrderDetailService;
        private readonly IOutStockLockInfoService _outStockLockInfoService;
@@ -93,7 +97,7 @@
        public List<int> TaskOutboundTypes => typeof(TaskTypeEnum).GetEnumIndexList();
        public TaskService(IRepository<Dt_Task> BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IRepository<Dt_StockInfo> stockRepository, ILocationInfoService locationInfoService, IInboundOrderService inboundOrderService, ILocationStatusChangeRecordService locationStatusChangeRecordService, IESSApiService eSSApiService, ILogger<TaskService> logger, IStockService stockService, IRecordService recordService, IInboundOrderDetailService inboundOrderDetailService, IOutboundOrderService outboundOrderService, IOutboundOrderDetailService outboundOrderDetailService, IInvokeMESService invokeMESService, IOutStockLockInfoService outStockLockInfoService, IAllocateService allocateService) : base(BaseDal)
        public TaskService(IRepository<Dt_Task> BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IRepository<Dt_StockInfo> stockRepository, ILocationInfoService locationInfoService, IInboundOrderService inboundOrderService, ILocationStatusChangeRecordService locationStatusChangeRecordService, IESSApiService eSSApiService, ILogger<TaskService> logger, IStockService stockService, IRecordService recordService, IInboundOrderDetailService inboundOrderDetailService, IOutboundOrderService outboundOrderService, IOutboundOrderDetailService outboundOrderDetailService, IInvokeMESService invokeMESService, IOutStockLockInfoService outStockLockInfoService, IAllocateService allocateService, IRepository<Dt_OutboundBatch> outboundBatchRepository,IRepository<Dt_ReCheckOrder> reCheckOrderRepository) : base(BaseDal)
        {
            _mapper = mapper;
            _unitOfWorkManage = unitOfWorkManage;
@@ -111,6 +115,8 @@
            _invokeMESService = invokeMESService;
            _outStockLockInfoService = outStockLockInfoService;
            _allocateService = allocateService;
            _OutboundBatchRepository = outboundBatchRepository;
            _reCheckOrderRepository = reCheckOrderRepository;
        }
@@ -127,7 +133,7 @@
                if (int.TryParse(taskNum, out var newTaskNum))
                {
                    task =await BaseDal.QueryFirstAsync(x => x.TaskNum == newTaskNum);
                    task = await BaseDal.QueryFirstAsync(x => x.TaskNum == newTaskNum);
                    if (task == null)
                    {
                        return WebResponseContent.Instance.Error("未找到任务信息");
@@ -154,12 +160,12 @@
                                return responseContent;
                            }
                        }
                        catch (AggregateException ex)
                        catch (AggregateException ex)
                        {
                            _logger.LogError($"TaskService TaskCompleted  taskResult:   {ex.Message} ");
                            return WebResponseContent.Instance.Error(ex.Message);
                        }
                        catch(Exception ex)
                        catch (Exception ex)
                        {
                            _logger.LogError(ex, $"Unexpected error in {task.TaskType}");
                            return WebResponseContent.Instance.Error(ex.Message);
@@ -270,16 +276,16 @@
            {
                foreach (var inboundOrder in inboundOrders)
                {
                    if (inboundOrder.OrderType == InOrderTypeEnum.Allocat.ObjToInt())//调拨入库
                    if (inboundOrder.OrderType == InOrderTypeEnum.AllocatInbound.ObjToInt())//调拨入库
                    {
                        if (inboundOrder != null && inboundOrder.OrderStatus == InOrderStatusEnum.入库完成.ObjToInt())
                        {
                            var allocate = _allocateService.Repository.QueryData(x => x.OrderNo == inboundOrder.InboundOrderNo).First();
                            var feedmodel = new AllocateDto
                            var allocatefeedmodel = new AllocateDto
                            {
                                ReqCode = Guid.NewGuid().ToString(),
                                ReqTime = DateTime.Now.ToString(),
                                BusinessType = "2",
                                BusinessType = "3",
                                FactoryArea = inboundOrder.FactoryArea,
                                OperationType = 1,
                                Operator = inboundOrder.Operator,
@@ -308,9 +314,9 @@
                                       Unit = row.Unit
                                   }).ToList()
                               }).ToList();
                            feedmodel.Details = groupedData;
                            allocatefeedmodel.Details = groupedData;
                            var result = await _invokeMESService.FeedbackAllocate(feedmodel);
                            var result = await _invokeMESService.FeedbackAllocate(allocatefeedmodel);
                            if (result != null && result.code == 200)
                            {
                                _inboundOrderService.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 1 })
@@ -322,6 +328,13 @@
                    }
                    else if (inboundOrder.OrderType == InOrderTypeEnum.ReCheck.ObjToInt()) //重检入库
                    {
                        //不需要回传。占一个位置。
                    }
                    else if (inboundOrder.OrderType == InOrderTypeEnum.InternalAllocat.ObjToInt()) //智仓调智仓
                    {
                        _logger.LogInformation($"InboundTaskCompleted å›žå†™MES  : {inboundOrder.InboundOrderNo }  ,ordertype: {InOrderTypeEnum.InternalAllocat.ObjToInt()} "  );
                        // BusinessTypeEnum.智仓调智仓
                    }
                    else
@@ -382,6 +395,13 @@
            return WebResponseContent.Instance.OK();
        }
        public async Task<WebResponseContent> OutAllocateTaskCompleted(Dt_Task task)
        {
            _logger.LogInformation($"TaskService  OutAllocateTaskCompleted: {task.TaskNum}");
           return  await OutboundTaskCompleted(task);
        }
        public async Task<WebResponseContent> OutboundTaskCompleted(Dt_Task task)
        {
            _logger.LogInformation($"TaskService  OutboundTaskCompleted: {task.TaskNum}");
@@ -394,7 +414,7 @@
            locationInfo.LocationStatus = LocationStatusEnum.Free.ObjToInt();
            _locationInfoService.Repository.UpdateData(locationInfo);
            var outloks =await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>().Where(x => x.TaskNum == task.TaskNum).ToListAsync();
            var outloks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>().Where(x => x.TaskNum == task.TaskNum).ToListAsync();
            var stockids = outloks.Select(x => x.StockId).ToList();
@@ -489,7 +509,7 @@
            try
            {
                //查库存
                Dt_StockInfo stockInfo =await _stockRepository.Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.PalletCode == task.PalletCode).FirstAsync();
                Dt_StockInfo stockInfo = await _stockRepository.Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.PalletCode == task.PalletCode).FirstAsync();
                if (stockInfo == null)
                {
                    _logger.LogInformation($"TaskService  InPickTaskCompleted: æœªæ‰¾åˆ°æ‰˜ç›˜å¯¹åº”的组盘信息.{task.TaskNum}");
@@ -510,7 +530,7 @@
                var beforelocationStatus = locationInfo.LocationStatus;
                // èŽ·å–æ‰€æœ‰å›žåº“ä¸­çš„å‡ºåº“é”å®šè®°å½•
                var returnLocks =await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                var returnLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                    .Where(it => it.OrderNo == task.OrderNo && it.PalletCode == task.PalletCode && it.Status == (int)OutLockStockStatusEnum.回库中)
                    .ToListAsync();
                // æ›´æ–°å‡ºåº“锁定记录状态为回库完成
@@ -518,7 +538,7 @@
                {
                    lockInfo.Status = (int)OutLockStockStatusEnum.已回库;
                }
                _outStockLockInfoService.Db.Updateable(returnLocks).ExecuteCommand();
                _outStockLockInfoService.Db.Updateable(returnLocks).ExecuteCommand();
                stockInfo.LocationCode = task.TargetAddress;
                stockInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
@@ -532,7 +552,7 @@
                }
                _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                await ProcessStockDetailsForReturn(task, stockInfo.Id);
                await DeleteZeroQuantityStockDetails(stockInfo.Id);
@@ -572,25 +592,25 @@
        private async Task HandleOutboundOrderToMESCompletion(Dt_OutboundOrder outboundOrder, string orderNo)
        {
            try
            {
                var orderDetails = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
                    .LeftJoin<Dt_OutboundOrder>((o, item) => o.OrderId == item.Id)
                    .Where((o, item) => item.OrderNo == orderNo)
                    .Select((o, item) => o)
                    .ToListAsync();
                    .ToListAsync();
                bool allCompleted = true;
                foreach (var detail in orderDetails)
                {
                    _logger.LogInformation($"TaskService  HandleOutboundOrderToMESCompletion: {outboundOrder.OrderNo} , {detail.NeedOutQuantity}");
                    if (detail.OverOutQuantity < detail.NeedOutQuantity)
                    {
                        allCompleted = false;
                        break;
                    }
                }
                }
                _logger.LogInformation($"TaskService  HandleOutboundOrderToMESCompletion: {outboundOrder.OrderNo} , {allCompleted}");
                int newStatus = allCompleted ? (int)OutOrderStatusEnum.出库完成 : (int)OutOrderStatusEnum.出库中;
                if (outboundOrder.OrderStatus != newStatus)
@@ -599,75 +619,145 @@
                        .SetColumns(x => x.OrderStatus == newStatus)
                        .Where(x => x.OrderNo == orderNo)
                        .ExecuteCommandAsync();
                }
                //只有正常分拣完成时才向MES反馈
                if (allCompleted && newStatus == (int)OutOrderStatusEnum.出库完成)
                {
                    var feedmodel = new FeedbackOutboundRequestModel
                    if (outboundOrder.OrderType == OutOrderTypeEnum.Allocate.ObjToInt())
                    {
                        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 = outboundOrder.OrderNo,
                        status = outboundOrder.OrderStatus,
                        details = new List<FeedbackOutboundDetailsModel>()
                    };
                    foreach (var detail in orderDetails)
                    {
                        // èŽ·å–è¯¥æ˜Žç»†å¯¹åº”çš„æ¡ç ä¿¡æ¯ï¼ˆä»Žé”å®šè®°å½•ï¼‰
                        var detailLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                            .Where(x => x.OrderNo == orderNo &&
                                       x.OrderDetailId == detail.Id &&
                                       x.Status == (int)OutLockStockStatusEnum.拣选完成)
                            .ToListAsync();
                        var detailModel = new FeedbackOutboundDetailsModel
                        var allocate = _allocateService.Repository.QueryData(x => x.UpperOrderNo == outboundOrder.UpperOrderNo).First();
                        var allocatefeedmodel = new AllocateDto
                        {
                            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
                            ReqCode = Guid.NewGuid().ToString(),
                            ReqTime = DateTime.Now.ToString(),
                            BusinessType = "2",
                            FactoryArea = outboundOrder.FactoryArea,
                            OperationType = 1,
                            Operator = outboundOrder.Operator,
                            OrderNo = outboundOrder.UpperOrderNo,
                            // documentsNO = outboundOrder.OrderNo,
                            // status = outboundOrder.OrderStatus,
                            fromWarehouse = allocate?.FromWarehouse ?? "",
                            toWarehouse = allocate?.ToWarehouse ?? "",
                            Details = new List<AllocateDtoDetail>()
                        };
                        foreach (var detail in orderDetails)
                        {
                            // èŽ·å–è¯¥æ˜Žç»†å¯¹åº”çš„æ¡ç ä¿¡æ¯ï¼ˆä»Žé”å®šè®°å½•ï¼‰
                            var detailLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                                .Where(x => x.OrderNo == orderNo &&
                                           x.OrderDetailId == detail.Id &&
                                           x.Status == (int)OutLockStockStatusEnum.拣选完成)
                                .ToListAsync();
                            var detailModel = new AllocateDtoDetail
                            {
                                barcode = lockInfo.CurrentBarcode,
                                supplyCode = lockInfo.SupplyCode,
                                batchNo = lockInfo.BatchNo,
                                unit = lockInfo.Unit,
                                qty = lockInfo.PickedQty // æ¡ç çº§åˆ«çš„æ•°é‡ä»ç”¨é”å®šè®°å½•
                            }).ToList()
                                MaterialCode = detail.MaterielCode,
                                LineNo = detail.lineNo, // æ³¨æ„ï¼šè¿™é‡Œå¯èƒ½éœ€è¦è°ƒæ•´å­—段名
                                WarehouseCode = detail.WarehouseCode,
                                Qty = detail.OverOutQuantity, // ä½¿ç”¨è®¢å•明细的已出库数量
                                //currentDeliveryQty = detail.OverOutQuantity,
                                Unit = detail.Unit,
                                Barcodes = detailLocks.Select(lockInfo => new BarcodeInfo
                                {
                                    Barcode = lockInfo.CurrentBarcode,
                                    SupplyCode = lockInfo.SupplyCode,
                                    BatchNo = lockInfo.BatchNo,
                                    Unit = lockInfo.BarcodeUnit,
                                    Qty = lockInfo.BarcodeQty // æ¡ç çº§åˆ«çš„æ•°é‡ä»ç”¨é”å®šè®°å½•
                                }).ToList()
                            };
                            allocatefeedmodel.Details.Add(detailModel);
                        }
                        var result = await _invokeMESService.FeedbackAllocate(allocatefeedmodel);
                        if (result != null && result.code == 200)
                        {
                            await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
                                   .SetColumns(x => x.ReturnToMESStatus == 1)
                                   .Where(x => x.OrderId == outboundOrder.Id).ExecuteCommandAsync();
                            await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
                                  .SetColumns(x => new Dt_OutboundOrder
                                  {
                                      ReturnToMESStatus = 1,
                                      Operator = App.User.UserName,
                                  }).Where(x => x.OrderNo == orderNo).ExecuteCommandAsync();
                        }
                    }
                    else if (outboundOrder.OrderType == OutOrderTypeEnum.ReCheck.ObjToInt())
                    {
                        //不用回传
                    }
                    else
                    {
                        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 = outboundOrder.OrderNo,
                            status = outboundOrder.OrderStatus,
                            details = new List<FeedbackOutboundDetailsModel>()
                        };
                        feedmodel.details.Add(detailModel);
                    }
                    var result = await _invokeMESService.FeedbackOutbound(feedmodel);
                    if (result != null && result.code == 200)
                    {
                        await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
                            .SetColumns(x => x.ReturnToMESStatus == 1)
                            .Where(x => x.OrderId == outboundOrder.Id)
                            .ExecuteCommandAsync();
                        foreach (var detail in orderDetails)
                        {
                            // èŽ·å–è¯¥æ˜Žç»†å¯¹åº”çš„æ¡ç ä¿¡æ¯ï¼ˆä»Žé”å®šè®°å½•ï¼‰
                            var detailLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                                .Where(x => x.OrderNo == orderNo &&
                                           x.OrderDetailId == detail.Id &&
                                           x.Status == (int)OutLockStockStatusEnum.拣选完成)
                                .ToListAsync();
                        await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
                            .SetColumns(x => x.ReturnToMESStatus == 1)
                            .Where(x => x.OrderNo == orderNo)
                            .ExecuteCommandAsync();
                            var detailModel = new FeedbackOutboundDetailsModel
                            {
                                materialCode = detail.MaterielCode,
                                lineNo = detail.lineNo, // æ³¨æ„ï¼šè¿™é‡Œå¯èƒ½éœ€è¦è°ƒæ•´å­—段名
                                warehouseCode = detail.WarehouseCode,
                                qty = detail.BarcodeQty,
                                currentDeliveryQty = detail.BarcodeQty,
                                unit = detail.Unit,
                                barcodes = detailLocks.Select(lockInfo => new WIDESEA_DTO.Outbound.BarcodesModel
                                {
                                    barcode = lockInfo.CurrentBarcode,
                                    supplyCode = lockInfo.SupplyCode,
                                    batchNo = lockInfo.BatchNo,
                                    unit = lockInfo.BarcodeUnit,
                                    qty = lockInfo.BarcodeQty // æ¡ç çº§åˆ«çš„æ•°é‡ä»ç”¨é”å®šè®°å½•
                                }).ToList()
                            };
                            feedmodel.details.Add(detailModel);
                        }
                        var result = await _invokeMESService.FeedbackOutbound(feedmodel);
                        if (result != null && result.code == 200)
                        {
                            await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
                                .SetColumns(x => x.ReturnToMESStatus == 1)
                                .Where(x => x.OrderId == outboundOrder.Id)
                                .ExecuteCommandAsync();
                            await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
                                .SetColumns(x => x.ReturnToMESStatus == 1)
                                .Where(x => x.OrderNo == orderNo)
                                .ExecuteCommandAsync();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"CheckAndUpdateOrderStatus失败 - OrderNo: {orderNo}, Error: {ex.Message}");
            }
            }
        }
@@ -753,8 +843,8 @@
                task.TaskStatus = TaskStatusEnum.Finish.ObjToInt();
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateTypeEnum.自动完成 : OperateTypeEnum.人工完成);
                _stockService.StockInfoService.Repository.DeleteAndMoveIntoHty(stockInfo, App.User.UserId == 0 ? OperateTypeEnum.自动完成 : OperateTypeEnum.人工完成);
                _stockRepository.Db.Deleteable(stockInfo).ExecuteCommand();
                //_stockRepository.Db.Deleteable(stockInfo).ExecuteCommand();
                _stockService.StockInfoService.DeleteData(stockInfo);
                _locationStatusChangeRecordService.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Outbound.ObjToInt(), stockInfo.Details.FirstOrDefault()?.OrderNo ?? "", task.TaskNum);
                return await Task.FromResult(WebResponseContent.Instance.OK());