xiazhengtongxue
2026-01-24 a647de5665411f706beddc5e0ff7b3a3daaca4fc
ÏîÄ¿´úÂë/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/MESTaskService.cs
@@ -1,4 +1,5 @@
using MailKit.Search;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
@@ -9,6 +10,7 @@
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.LocationEnum;
@@ -21,6 +23,7 @@
using WIDESEA_Core.Helper;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.Task;
using WIDESEA_ITaskInfoService;
using WIDESEA_Model.Models;
using static WIDESEA_ITaskInfoService.ITaskService;
@@ -28,10 +31,11 @@
{
    public partial class TaskService
    {
        public static List<string> InStationareaList = new List<string>() { "1113", "1114", "1115", "1116", "1117", "1118", "1119", "1120", "1121", "1122" };
        //入库,空盘回库
        public ApiResponse AddInStoreDoc(MES_InTask mES_In)
        {
            WriteLog.GetLog("接收MES入库任务下发").Write($"参数:{mES_In.ToJson()}", $"任务接收参数");
            WriteLog.Write_Log("MES_接收入库任务下发", $"任务接收参数", "接收参数", $"参数:{mES_In.ToJson()}");
            ApiResponse apiResponse = new ApiResponse();
            try
            {
@@ -56,7 +60,7 @@
                string TargetAddress = "";
                string CurrentAddress = mES_In.startPosition;
                string NextAddress = "";
                int WarehouseId = 0;
                int WarehouseId = dt_Warehouse.WarehouseId;
                if (dt_Warehouse.WarehouseCode == mES_In.endPosition)
@@ -74,10 +78,17 @@
                    //获取对应PLC站台信息
                    Dt_roadwayinfo _Roadwayinfo = _roadWayinfoService.QbtainPlatform(Roadway);
                    NextAddress = _Roadwayinfo.InStationCode;
                    WarehouseId = dt_Warehouse.WarehouseId;
                }
                else
                {
                    if (dt_Warehouse.WarehouseType == (int)WarehouseEnum.YMYL)
                    {
                        Roadway="1";
                    }
                    else if (dt_Warehouse.WarehouseType == (int)WarehouseEnum.YMCP)
                    {
                        Roadway = "2";
                    }
                    TargetAddress = mES_In.endPosition;
                    NextAddress = mES_In.endPosition;
                }
@@ -99,25 +110,47 @@
                task.Creater = "MES";
                task.CreateDate = DateTime.Now;
                /*Dt_StockInfo dt_StockInfo = new Dt_StockInfo();
                dt_StockInfo.PalletCode = task.PalletCode;
                dt_StockInfo.PalletType = task.PalletType;
                dt_StockInfo.WarehouseId = task.WarehouseId;
                dt_StockInfo.StockStatus = (int)StockStatusEmun.入库中;
                dt_StockInfo.Creater = "MWS";
                dt_StockInfo.CreateDate = DateTime.Now;*/
                int taskid=BaseDal.AddData(task);
                _unitOfWorkManage.BeginTran();
                BaseDal.AddData(task);
                //_stockInfoService.Repository.AddData(dt_StockInfo);
                _unitOfWorkManage.CommitTran();
                WriteLog.GetLog("接收MES入库任务下发").Write($"参数:{mES_In.containerNo}", $"入库任务添加成功");
                return apiResponse.OK();
                //创建任务后,判断是否是产线回库的任务
                if (!InStationareaList.Contains(task.SourceAddress))
                {
                    WriteLog.Write_Log("MES_接收入库任务下发", $"反馈信息", "成功", $"任务添加成功,托盘条码:{task.PalletCode}");
                    return apiResponse.OK();
                }
                else
                {
                    WCSginseng result = PLC_IssueTasks(
                        task.TaskId,
                        task.WarehouseId,
                        task.TaskNum,
                        task.PalletCode,
                        int.Parse(task.PalletType),
                        task.CurrentAddress,
                        task.NextAddress,
                        "");
                    if (result.IsSuccess)
                    {
                        Dt_Task deltaskin = BaseDal.QueryData(x => x.TaskId == taskid).FirstOrDefault();
                        deltaskin.TaskStatus = (int)InTaskStatusEnum.PLC_InExecuting;
                        string Result = MesInTaskStatusEnum.Start.GetDescription();
                        BaseDal.UpdateData(deltaskin);
                        //调取上游系统反馈开始任务
                        InStoreDocCallback(task.TaskNum, Result, "操作成功", task.PalletCode, "");
                        return apiResponse.OK();
                    }
                    else
                    {
                        Dt_Task deltask= BaseDal.QueryData(x => x.TaskId == taskid).FirstOrDefault();
                        BaseDal.DeleteData(deltask);
                        return apiResponse.Error($"下发任务失败,下发WCS失败,原因:{result.Message}");
                    }
                }
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                WriteLog.GetLog("接收MES入库任务下发").Write($"原因:{ex.Message}", $"入库任务添加");
                WriteLog.Write_Log("MES_接收入库任务下发", $"反馈信息", "失败", $"任务添加失败,托盘条码:{mES_In.containerNo},系统出错原因:{ex.Message}");
                return apiResponse.Error($"WMS任务添加错误,原因:{ex.Message}");
            }
@@ -127,7 +160,8 @@
        //出库任务
        public ApiResponse AddOutStoreDoc(MES_InTask mES_In)
        {
            WriteLog.GetLog("接收MES出库任务下发").Write($"参数:{mES_In.ToJson()}", $"任务接收参数");
            WriteLog.Write_Log("MES_接收MES出库任务下发", $"任务接收参数", "接收参数", $"参数:{mES_In.ToJson()}");
            ApiResponse apiResponse = new ApiResponse();
            try
            {
@@ -177,13 +211,14 @@
                _locationInfoService.Repository.UpdateData(dt_LocationInfo);
                _unitOfWorkManage.CommitTran();
                WriteLog.GetLog("接收MES出库任务下发").Write($"添加成功,托盘条码:{dt_Stock.PalletCode}", $"出库任务添加");
                WriteLog.Write_Log("MES_接收MES出库任务下发", $"出库任务添加", "成功", $"添加成功,托盘条码:{dt_Stock.PalletCode}");
                return apiResponse.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                WriteLog.GetLog("接收MES出库任务下发").Write($"原因:{ex.Message}", $"出库任务添加");
                WriteLog.Write_Log("MES_接收MES出库任务下发", $"出库任务添加", "失败", $"任务添加失败,托盘条码:{mES_In.containerNo},系统出错原因:{ex.Message}");
                return apiResponse.Error($"WMS任务添加错误,原因:{ex.Message}");
            }
        }
@@ -191,7 +226,8 @@
        //储位查询接口
        public ApiResponse GetLocationByContainer(MES_InTask mES_In)
        {
            WriteLog.GetLog("储位查询接口").Write($"参数:{mES_In.ToJson()}", $"任务接收参数");
            WriteLog.Write_Log("MES_储位查询接口", $"储位查询接口", "参数", $"托盘条码:{mES_In.ToJson()}");
            ApiResponse apiResponse = new ApiResponse();
            try
            {
@@ -221,7 +257,7 @@
            }
            catch (Exception ex)
            {
                WriteLog.GetLog("储位查询接口").Write($"原因:{ex.Message}", $"储位查询查询失败");
                WriteLog.Write_Log("MES_储位查询接口", $"储位查询接口", "查询失败", $"托盘条码:{mES_In.ToJson()}");
                return apiResponse.Error($"WMS储位查询错误,原因:{ex.Message}");
            }
            
@@ -230,6 +266,7 @@
        //任务查询接口
        public ApiResponse GetTaskInfo(MES_InTask mES_In)
        {
            WriteLog.Write_Log("MES_任务查询接口", $"任务查询接口", "参数", $"接收的参数:{mES_In.ToJson()}");
            ApiResponse apiResponse = new ApiResponse();
            try
            {
@@ -256,7 +293,7 @@
            }
            catch (Exception ex)
            {
                WriteLog.GetLog("任务查询接口").Write($"原因:{ex.Message}", $"任务查询失败");
                WriteLog.Write_Log("MES_任务查询接口", $"任务查询接口", "失败", $"条码:{mES_In.transNo},原因:{ex.Message}");
                return apiResponse.Error($"WMS任务查询接口错误,原因:{ex.Message}");
            }
        }
@@ -272,9 +309,6 @@
                if (dt_Task == null) return apiResponse.Error($"WMS未能查找道对应单据任务,条码:{mES_In.transNo}");
                if(dt_Task.TaskStatus== (int)InTaskStatusEnum.InNew)
                {
                    Dt_StockInfo dt_StockInfo=_stockInfoService.Repository.QueryData(x=>x.PalletCode==dt_Task.PalletCode).FirstOrDefault();
                    if (dt_StockInfo == null) return apiResponse.Error("取消失败,WMS记录的托盘库存查找失败");
                    _stockInfoService.Repository.DeleteData(dt_StockInfo);
                    BaseDal.DeleteData(dt_Task);
                    return apiResponse.OK();
                }else if(dt_Task.TaskStatus == (int)OutTaskStatusEnum.OutNew)
@@ -287,17 +321,19 @@
                    _stockInfoService.Repository.UpdateData(dt_StockInfo);
                    _locationInfoService.Repository.UpdateData(dt_LocationInfo);
                    BaseDal.DeleteData(dt_Task);
                    WriteLog.Write_Log("MES_任务取消接口", $"任务取消接口", "成功", $"单据编号:{mES_In.transNo}");
                    return apiResponse.OK();
                }
                else
                {
                    WriteLog.Write_Log("MES_任务取消接口", $"任务取消接口", "失败", $"单据编号:{mES_In.transNo},任务已经在执行中,不可取消");
                    return apiResponse.Error("任务已经在执行中,不可取消");
                }
                
            }
            catch (Exception ex)
            {
                WriteLog.GetLog("任务取消接口").Write($"原因:{ex.Message}", $"任务取消失败");
                WriteLog.Write_Log("MES_任务取消接口", $"任务取消接口", "失败", $"单据编号:{mES_In.transNo},原因:{ex.Message}");
                return apiResponse.Error($"WMS任务取消接口错误,原因:{ex.Message}");
            }
        }
@@ -315,13 +351,15 @@
                mesInResult.ContainerNo = ContainerNo;
                mesInResult.LocationCode = LocationCode;
                mES_Parame = HttpHelper.Post<MES_parameter>(MES_InReporttask, mesInResult, "入库任务汇报");
                WriteLog.GetLog("入库任务反馈接口").Write($"订单编号:{TransNo},执行结果:{Result},执行结果描述:{ResultMsg},托盘条码:{ContainerNo},库位编号:{LocationCode},接收到的回参:{mesInResult.ToJson()}", $"任务上报信息");
                WriteLog.Write_Log("MES_入库任务反馈接口", $"入库任务反馈接口", "成功", $"订单编号:{TransNo},执行结果:{Result},执行结果描述:{ResultMsg},托盘条码:{ContainerNo},库位编号:{LocationCode},上传的参数:{mesInResult.ToJson()},回传的参数:{mES_Parame.ToJson()}");
                return mES_Parame;
            }
            catch (Exception ex)
            {
                mES_Parame.Result = "N";
                mES_Parame.ResultMsg= $"入库:WMS系统上传失败,原因:{ex.Message}";
                WriteLog.Write_Log("MES_入库任务反馈接口", $"入库任务反馈接口", "失败", $"订单编号:{TransNo},执行结果:{Result},执行结果描述:{ResultMsg},托盘条码:{ContainerNo},库位编号:{LocationCode},上传失败,原因:{ex.Message}");
                return mES_Parame;
            }
@@ -337,13 +375,14 @@
                mesInResult.Result = Result;
                mesInResult.ResultMsg = ResultMsg;
                mES_Parame = HttpHelper.Post<MES_parameter>(MES_OutReporttask, mesInResult, "出库任务汇报");
                WriteLog.GetLog("出库任务反馈接口").Write($"订单编号:{TransNo},执行结果:{Result},执行结果描述:{ResultMsg},接收到的回参:{mesInResult.ToJson()}", $"任务上报信息");
                WriteLog.Write_Log("MES_出库任务反馈接口", $"出库任务反馈接口", "成功", $"订单编号:{TransNo},执行结果:{Result},执行结果描述:{ResultMsg},上传的参数:{mesInResult.ToJson()},回传的参数:{mES_Parame.ToJson()}");
                return mES_Parame;
            }
            catch (Exception ex)
            {
                mES_Parame.Result = "N";
                mES_Parame.ResultMsg = $"出库:WMS系统上传失败,原因:{ex.Message}";
                WriteLog.Write_Log("MES_出库任务反馈接口", $"出库任务反馈接口", "失败", $"订单编号:{TransNo},执行结果:{Result},执行结果描述:{ResultMsg},上传失败,原因:{ex.Message}");
                return mES_Parame;
            }
        }
@@ -365,12 +404,14 @@
                Storagelocation.OriginalLocationCode = OriginalLocationCode;
                Storagelocation.NewLocationCode = NewLocationCode;
                mES_Parame = HttpHelper.Post<MES_parameter>(MES_InventoryUpdateApply, Storagelocation, "储位异动申请");
                WriteLog.Write_Log("MES_储位异动申请", $"储位异动申请", "成功", $"托盘条码:{ContainerNo},原库位:{OriginalLocationCode},新库位:{NewLocationCode},上传的参数:{Storagelocation.ToJson()},回传的参数:{mES_Parame.ToJson()}");
                return mES_Parame;
            }
            catch (Exception ex)
            {
                mES_Parame.Result = "N";
                mES_Parame.ResultMsg = $"储位异动申请:WMS系统调取失败,原因:{ex.Message}";
                WriteLog.Write_Log("MES_储位异动申请", $"储位异动申请", "失败", $"托盘条码:{ContainerNo},原库位:{OriginalLocationCode},新库位:{NewLocationCode},上传失败,原因:{ex.Message}");
                return mES_Parame;
            }
        }
@@ -392,12 +433,14 @@
                Storagelocation.OriginalLocationCode = OriginalLocationCode;
                Storagelocation.NewLocationCode = NewLocationCode;
                mES_Parame = HttpHelper.Post<MES_parameter>(MES_InventoryUpdateCallback, Storagelocation, "储位异动");
                WriteLog.Write_Log("MES_储位异动上传", $"储位异动上传", "成功", $"托盘条码:{ContainerNo},原库位:{OriginalLocationCode},新库位:{NewLocationCode},上传的参数:{Storagelocation.ToJson()},回传的参数:{mES_Parame.ToJson()}");
                return mES_Parame;
            }
            catch (Exception ex)
            {
                mES_Parame.Result = "N";
                mES_Parame.ResultMsg = $"储位异动:WMS系统调取失败,原因:{ex.Message}";
                WriteLog.Write_Log("MES_储位异动上传", $"储位异动上传", "失败", $"托盘条码:{ContainerNo},原库位:{OriginalLocationCode},新库位:{NewLocationCode},上传失败,原因:{ex.Message}");
                return mES_Parame;
            }
        }
@@ -420,12 +463,14 @@
                Storagelocation.EquipmentNumber = EquipmentNumber;
                Storagelocation.ErrorInfo = ErrorInfo;
                mES_Parame = HttpHelper.Post<MES_parameter>(MES_TaskErrorInfoCallback, Storagelocation, "任务异常");
                WriteLog.Write_Log("MES_任务异常", $"任务异常", "成功", $"单据编号:{TransNo},设备类型:{EquipmentType},设备编码:{EquipmentNumber},异常报文{ErrorInfo},,上传的参数:{Storagelocation.ToJson()},回传的参数:{mES_Parame.ToJson()}");
                return mES_Parame;
            }
            catch (Exception ex)
            {
                mES_Parame.Result = "N";
                mES_Parame.ResultMsg = $"任务异常:WMS系统调取失败,原因:{ex.Message}";
                WriteLog.Write_Log("MES_任务异常", $"任务异常", "失败", $"单据编号:{TransNo},设备类型:{EquipmentType},设备编码:{EquipmentNumber},异常报文{ErrorInfo},上传失败,原因:{ex.Message}");
                return mES_Parame;
            }
        }