1
刘磊
2026-03-05 f45293a0c3c9838567d6ff01eae53157a9ed5284
ÏîÄ¿´úÂë/WCS/WCSServer/WIDESEAWCS_TaskInfoService/Partial/TaskService.cs
@@ -2,6 +2,7 @@
using Mapster;
using MoYu.Localization;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using System.Diagnostics.CodeAnalysis;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
@@ -74,66 +75,6 @@
            }
        }
        /// <summary>
        /// æ ¹æ®æ‰˜ç›˜å·ã€èµ·å§‹åœ°å€å‘WMS请求任务(入库版本)
        /// </summary>
        /// <param name="palletCode">托盘号</param>
        /// <param name="sourceAddress">起始地址</param>
        /// <returns></returns>
        public Dt_Task? InBeforeCheck(int taskNum)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
                var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
                var requestTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.InTransferCheck)?.ConfigValue;
                if (wmsBase == null || requestTask == null)
                {
                    throw new InvalidOperationException("WMS IP æœªé…ç½®");
                }
                var wmsIpAddrss = wmsBase + requestTask;
                var result = HttpHelper.Post(wmsIpAddrss, new { taskNum = taskNum }.ToJsonString());
                content = JsonConvert.DeserializeObject<WebResponseContent>(result);
                if (!content.Status)
                {
                    // wms失败
                    throw new Exception();
                }
                //todo ä¼˜åŒ– å¦‚未查询到任务则处理
                //if (content.Data != null)
                //{
                var task = JsonConvert.DeserializeObject<Dt_Task>(content.Data.ToString());
                Dt_Task? Relocationtask = null;
                if (task.TaskNum != taskNum)
                {
                    BaseDal.AddData(task);
                    Relocationtask = BaseDal.QueryFirst(x => x.TaskNum == task.TaskNum);
                }
                //}
                if (Relocationtask != null)
                {
                    return task;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"请求异常:{ex.Message}");
            }
        }
        #region æ ¹æ®æ‰˜ç›˜å·ã€ç«™å°å‘WMS请求任务
        /// <summary>
        /// æ ¹æ®æ‰˜ç›˜å·ã€ç«™å°å‘WMS请求任务
@@ -141,7 +82,109 @@
        /// <param name="palletCode">托盘号</param>
        /// <param name="stationManager">站台</param>
        /// <returns></returns>
        public async Task<WebResponseContent> RequestTask(string palletCode, Dt_StationManager stationManager)
        public async Task<WebResponseContent> RequestTask(string palletCode, string PVI, Dt_StationManager stationManager)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                StaticVariable.isLineRun = false;
                // æ£€æŸ¥å½“前托盘是否存在任务
                if (await HasTask(PVI))
                {
                    return content.Error("当前托盘存在任务");
                }
                //palletCode = "QT101025"; // æ¨¡æ‹Ÿæ‰˜ç›˜è¯·æ±‚任务
                // å‘起请求获取入库任务
                if (stationManager.stationType == 1)
                {
                    content = await RequestInTask(palletCode, PVI, stationManager);
                }
                else if (stationManager.stationType == 6)
                {
                    content = await RequestInEmptyTask(palletCode, PVI, stationManager);
                }
                ConsoleHelper.WriteErrorLine($"{stationManager.stationChildCode}站台请求响应信息:{content.ToJsonString()}");
                if (!content.Status)
                {
                    return content;
                }
                var task = JsonConvert.DeserializeObject<WMSTaskDTO>(content.Data.ToString());
                ConsoleHelper.WriteSuccessLine(content.Data.ToString());
                return ReceiveByWMSTask(task);
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
                StaticVariable.isLineRun = true;
            }
        }
        // æ£€æŸ¥å½“前托盘是否存在任务
        private async Task<bool> HasTask(string PVI)
        {
            var hasTask = await BaseDal.QueryFirstAsync(x => x.PVI == PVI);
            return hasTask != null;
        }
        // èŽ·å– WMS è¯·æ±‚地址
        private string GetWmsIpAddress(string configKey)
        {
            var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
            var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
            var requestTask = config.FirstOrDefault(x => x.ConfigKey == configKey)?.ConfigValue;
            if (wmsBase == null || requestTask == null)
            {
                throw new InvalidOperationException("WMS IP æœªé…ç½®");
            }
            return wmsBase + requestTask;
        }
        // å‘起请求获取入库任务
        private async Task<WebResponseContent> RequestInTask(string palletCode, string PVI, Dt_StationManager stationManager)
        {
            var wmsIpAddrss = GetWmsIpAddress(SysConfigKeyConst.RequestInTask);
            var result = await HttpHelper.PostAsync(wmsIpAddrss, new { palletCode = palletCode, Roadways = stationManager.Roadway, area = stationManager.stationArea, Position = stationManager.stationChildCode, PVI = PVI }.ToJsonString());
            return JsonConvert.DeserializeObject<WebResponseContent>(result);
        }
        private async Task<WebResponseContent> RequestInEmptyTask(string palletCode, string PVI, Dt_StationManager stationManager)
        {
            var wmsIpAddrss = GetWmsIpAddress(SysConfigKeyConst.RequestInTask);
            var result = await HttpHelper.PostAsync(wmsIpAddrss, new { palletCode = palletCode, Roadways = stationManager.Roadway, area = stationManager.stationArea, Position = stationManager.stationChildCode, PVI = PVI }.ToJsonString());
            return JsonConvert.DeserializeObject<WebResponseContent>(result);
        }
        #endregion æ ¹æ®æ‰˜ç›˜å·ã€ç«™å°å‘WMS请求任务
        public async Task<WebResponseContent> RequestWhiteBody(string palletCode, string PVI, Dt_StationManager stationManager)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var wmsIpAddrss = GetWmsIpAddress(SysConfigKeyConst.RequestWhiteBody);
                var result = await HttpHelper.PostAsync(wmsIpAddrss, new { palletCode = palletCode, Roadways = stationManager.Roadway, area = stationManager.stationArea, Position = stationManager.stationChildCode, PVI = PVI }.ToJsonString());
                return JsonConvert.DeserializeObject<WebResponseContent>(result);
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public async Task<WebResponseContent> RequestRoadWayTask(string palletCode, string PVI, Dt_StationManager stationManager)
        {
            WebResponseContent content = new WebResponseContent();
            try
@@ -154,10 +197,11 @@
                    return content.Error("当前托盘存在任务");
                }
                palletCode = "QT101025"; // æ¨¡æ‹Ÿæ‰˜ç›˜è¯·æ±‚任务
                //PVI = "J26000456TF12"; // æ¨¡æ‹ŸPVI请求任务
                // å‘起请求获取入库任务
                content = await RequestInTask(palletCode, stationManager);
                content = await RequestInRoadWayTask(palletCode, PVI, stationManager);
                ConsoleHelper.WriteErrorLine($"{stationManager.stationChildCode}站台请求响应信息:{content.ToJsonString()}");
                if (!content.Status)
@@ -179,90 +223,12 @@
            }
        }
        public async Task<WebResponseContent> UpdateCheckTask(string palletCode, Dt_StationManager stationManager)
        private async Task<WebResponseContent> RequestInRoadWayTask(string palletCode, string PVI, Dt_StationManager stationManager)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var wmsIpAddrss = GetWmsIpAddress(SysConfigKeyConst.UpdateCheckTask);
                var result = await HttpHelper.PostAsync(wmsIpAddrss, new { palletCode = palletCode, Roadways = stationManager.Roadway, Position = stationManager.stationChildCode }.ToJsonString());
                return JsonConvert.DeserializeObject<WebResponseContent>(result);
            }
            catch (Exception ex)
            {
                return content.Error($"{ex.Message}");
            }
        }
        /// <summary>
        /// è½¦è½®æµå‘分配
        /// </summary>
        /// <param name="czCode"></param>
        /// <param name="stationManager"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> RequestWheelsFlow(string czCode)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var wmsIpAddrss = GetWmsIpAddress(SysConfigKeyConst.RequestWheelsFlow);
                var result = await HttpHelper.PostAsync(wmsIpAddrss, new { palletCode = czCode }.ToJsonString());
                return JsonConvert.DeserializeObject<WebResponseContent>(result);
            }
            catch (Exception ex)
            {
                return content.Error($"{ex.Message}");
            }
        }
        /// <summary>
        /// è½¦è½®æµå‘分配查询历史请求
        /// </summary>
        /// <param name="czCode"></param>
        /// <param name="stationManager"></param>
        /// <returns></returns>
        public string RequestWheletHty(string czCode)
        {
            WebResponseContent content = new WebResponseContent();
            Dt_Task_Hty dt_Task_Hty = _taskHtyRepository.QueryFirst(x => x.task_CZInfo == czCode && x.CreateDate >= DateTime.Now.AddMinutes(-30));
            if (dt_Task_Hty == null) return null;
            if (dt_Task_Hty.Towhereabouts == null && dt_Task_Hty.Towhereabouts == "") return null;
            return dt_Task_Hty.Towhereabouts;
        }
        // æ£€æŸ¥å½“前托盘是否存在任务
        private async Task<bool> HasTask(string palletCode)
        {
            var hasTask = await BaseDal.QueryFirstAsync(x => x.PalletCode == palletCode);
            return hasTask != null;
        }
        // èŽ·å– WMS è¯·æ±‚地址
        private string GetWmsIpAddress(string configKey)
        {
            var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
            var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
            var requestTask = config.FirstOrDefault(x => x.ConfigKey == configKey)?.ConfigValue;
            if (wmsBase == null || requestTask == null)
            {
                throw new InvalidOperationException("WMS IP æœªé…ç½®");
            }
            return wmsBase + requestTask;
        }
        // å‘起请求获取入库任务
        private async Task<WebResponseContent> RequestInTask(string palletCode, Dt_StationManager stationManager)
        {
            var wmsIpAddrss = GetWmsIpAddress(SysConfigKeyConst.RequestInTask);
            var result = await HttpHelper.PostAsync(wmsIpAddrss, new { palletCode = palletCode, Roadways = stationManager.Roadway, area = stationManager.stationArea, Position = stationManager.stationChildCode }.ToJsonString());
            var wmsIpAddrss = GetWmsIpAddress(SysConfigKeyConst.RequestInboundRoadWayNo);
            var result = await HttpHelper.PostAsync(wmsIpAddrss, new { palletCode = palletCode, Roadways = stationManager.Roadway, area = stationManager.stationArea, Position = stationManager.stationChildCode, PVI = PVI }.ToJsonString());
            return JsonConvert.DeserializeObject<WebResponseContent>(result);
        }
        #endregion æ ¹æ®æ‰˜ç›˜å·ã€ç«™å°å‘WMS请求任务
        /// <summary>
@@ -282,17 +248,13 @@
                Dt_Task task = _mapper.Map<Dt_Task>(taskDTO);
                task.Creater = "WMS";
                task.CarType = taskDTO.CarType;
                task.PVI = taskDTO.PVI;
                if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                {
                    var stationinfo = _stationManagerRepository.QueryFirst(x => x.stationLocation == taskDTO.TargetAddress && x.Roadway == taskDTO.RoadWay);
                    task.TaskState = (int)TaskOutStatusEnum.OutNew;
                    if (task.SourceAddress == "2009")
                    {
                        task.TaskState = (int)TaskOutStatusEnum.SC_OutFinish;
                    }
                    task.CurrentAddress = taskDTO.RoadWay;
                    task.NextAddress = stationinfo.stationChildCode;
                    task.SourceAddress = taskDTO.SourceAddress;