wangxinhui
2025-11-21 5336bfc54525253a30f1f8238806d3a67f388e14
ÏîÄ¿´úÂë/WCS/WCSServices/WIDESEAWCS_TaskInfoService/TaskService.cs
@@ -18,6 +18,7 @@
using Newtonsoft.Json;
using SqlSugar;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http.Headers;
using WIDESEA_DTO.Agv;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.APIEnum;
@@ -27,6 +28,7 @@
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.LogHelper;
using WIDESEAWCS_DTO;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_ITaskInfoRepository;
@@ -100,7 +102,9 @@
                }
            }
        }
        /// <summary>
        /// ä»»åŠ¡å®Œæˆ
        /// </summary>
        public WebResponseContent TaskCompleted(int taskNum)
        {
            try
@@ -179,7 +183,7 @@
                            return WebResponseContent.Instance.Error($"未找到站台配置信息");
                        }
                        //获取路由信息
                        List<Dt_Router> routers = _routerRepository.QueryData(x => x.InOutType == item.TaskType && (item.SourceAddress == x.StartPosi || item.RoadWay == x.StartPosi));
                        List<Dt_Router> routers = _routerRepository.QueryData(x => x.InOutType == item.TaskType && (item.SourceAddress == x.StartPosi || item.RoadWay == x.StartPosi) && x.Depth==1);
                        router = routers.FirstOrDefault();
                        if (router == null)
                        {
@@ -320,6 +324,10 @@
                        content = ReceiveWMSTask(new List<WMSTaskDTO> { taskDTO });
                    }
                }
                else
                {
                    content = responseContent ?? content.Error("结果错误");
                }
            }
            catch (Exception ex)
@@ -331,7 +339,7 @@
        // <summary>
        /// å…¥åº“申请
        /// </summary>
        public WebResponseContent YLPurchaseBoxing(string palletCode)
        public WebResponseContent YLPurchaseBoxing(string palletCode, decimal weight = 0, decimal thickness = 0, decimal wide = 0,string stationCode="")
        {
            WebResponseContent? content = new WebResponseContent();
            try
@@ -341,7 +349,7 @@
                {
                    return WebResponseContent.Instance.Error($"未找到WMSApi地址");
                }
                string responseStr = HttpHelper.Get($"{address}/api/Task/YLPurchaseBoxing?palletCode={palletCode}");
                string responseStr = HttpHelper.Get($"{address}/api/Task/YLPurchaseBoxing?palletCode={palletCode}&weight={weight}&thickness={thickness}&wide={wide}&stationCode={stationCode}");
                WebResponseContent? responseContent = JsonConvert.DeserializeObject<WebResponseContent>(responseStr);
                content = responseContent;
@@ -458,51 +466,7 @@
            }
            return content;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="palletCode"></param>
        /// <param name="sourceAddress"></param>
        /// <param name="roadway"></param>
        /// <returns></returns>
        //public WebResponseContent RequestWMSTask(string palletCode, string sourceAddress, string roadway)
        //{
        //    WebResponseContent content = new WebResponseContent();
        //    try
        //    {
        //        string address = AppSettings.Get("WMSApiAddress");
        //        if (string.IsNullOrEmpty(address))
        //        {
        //            return WebResponseContent.Instance.Error($"未找到WMSApi地址");
        //        }
        //        string responseStr = HttpHelper.Get($"{address}/api/Task/DeviceRequestInboundTask?stationCode={sourceAddress}&roadwayNo={roadway}&palletCode={palletCode}");
        //        WebResponseContent? responseContent = JsonConvert.DeserializeObject<WebResponseContent>(responseStr);
        //        if (responseContent != null && responseContent.Status && responseContent.Data != null)
        //        {
        //            WMSTaskDTO? taskDTO = JsonConvert.DeserializeObject<WMSTaskDTO>(responseContent.Data.ToString());
        //            if (taskDTO != null)
        //            {
        //                content = ReceiveWMSTask(new List<WMSTaskDTO> { taskDTO });
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        content = WebResponseContent.Instance.Error(ex.Message);
        //    }
        //    return content;
        //}
        /// <summary>
        /// åˆ†é…å··é“
        /// </summary>
        /// <param name="taskNum"></param>
        /// <param name="roadwayNos"></param>
        /// <returns></returns>
        public WebResponseContent RequestWMSAssignRoadway(int taskNum, List<string> roadwayNos)
        public WebResponseContent MESBoxCodeNotice(string boxCode)
        {
            WebResponseContent content = new WebResponseContent();
            try
@@ -512,13 +476,8 @@
                {
                    return WebResponseContent.Instance.Error($"未找到WMSApi地址");
                }
                string responseStr = HttpHelper.Post($"{address}/api/Task/AssignRoadway?taskNum={taskNum}", roadwayNos.Serialize());
                WebResponseContent? responseContent = JsonConvert.DeserializeObject<WebResponseContent>(responseStr);
                if (responseContent != null && responseContent.Status && responseContent.Data != null)
                {
                    return responseContent;
                }
                string responseStr = HttpHelper.Get($"{address}/api/Mes/MESBoxCodeNotice?boxCode={boxCode}");
                return content.OK(responseStr);
            }
            catch (Exception ex)
            {
@@ -526,77 +485,56 @@
            }
            return content;
        }
        /// <summary>
        ///
        /// åˆ¤æ–­å…è®¸æ”¾è´§
        /// </summary>
        /// <param name="roadwayNos"></param>
        /// <param name="palletCode"></param>
        /// <param name="heightType"></param>
        /// <returns></returns>
        public WebResponseContent RequestWMSAssignRoadway(List<string> roadwayNos, string palletCode, int heightType)
        public string MESAvgArriveNotice(RequestAGVArriveDTO requestAGVArriveDTO)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                string address = AppSettings.Get("WMSApiAddress");
                if (string.IsNullOrEmpty(address))
                {
                    return WebResponseContent.Instance.Error($"未找到WMSApi地址");
                }
                string responseStr = HttpHelper.Post($"{address}/api/Task/AssignRoadwayByHeightAndCode?palletCode={palletCode}&heightType={heightType}", roadwayNos.Serialize());
                WebResponseContent? responseContent = JsonConvert.DeserializeObject<WebResponseContent>(responseStr);
                if (responseContent != null && responseContent.Status && responseContent.Data != null)
                {
                    return responseContent;
                }
            Dt_ApiInfo apiInfo = _apiInfoRepository.QueryFirst(x => x.ApiCode == APIEnum.MESAvgArriveNotice.ToString());
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            return content;
            string response = Post(apiInfo.ApiAddress, JsonConvert.SerializeObject(requestAGVArriveDTO));
            return response;
        }
        /// <summary>
        /// åˆ†é…å··é“
        /// </summary>
        /// <param name="roadwayNos"></param>
        /// <param name="taskNum"></param>
        /// <param name="heightType"></param>
        /// <returns></returns>
        public WebResponseContent RequestWMSAssignRoadway(List<string> roadwayNos, int taskNum, int heightType)
        public static string Post(string serviceAddress, string requestJson = "", string contentType = "application/json", Dictionary<string, string>? headers = null)
        {
            WebResponseContent content = new WebResponseContent();
            string result = string.Empty;
            DateTime beginDate = DateTime.Now;
            try
            {
                string address = AppSettings.Get("WMSApiAddress");
                if (string.IsNullOrEmpty(address))
                using (HttpContent httpContent = new StringContent(requestJson))
                {
                    return WebResponseContent.Instance.Error($"未找到WMSApi地址");
                }
                string responseStr = HttpHelper.Post($"{address}/api/Task/AssignRoadway?taskNum={taskNum}&heightType={heightType}", roadwayNos.Serialize());
                WebResponseContent? responseContent = JsonConvert.DeserializeObject<WebResponseContent>(responseStr);
                if (responseContent != null && responseContent.Status && responseContent.Data != null)
                {
                    return responseContent;
                }
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    using HttpClient httpClient = new HttpClient();
                    httpClient.Timeout = new TimeSpan(0, 0, 30);
                    string LoginToken = AppSettings.Get("MESLoginToken");
                    headers = new Dictionary<string, string>
                    {
                        //正式
                        { "LoginToken", LoginToken }
                    };
                    if (headers != null)
                    {
                        foreach (var header in headers)
                            httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
                    }
                    HttpResponseMessage responseMessage = httpClient.PostAsync(serviceAddress, httpContent).Result;
                    result = responseMessage.Content.ReadAsStringAsync().Result;
                }
                return result;
            }
            catch (Exception ex)
            catch (Exception e)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
                throw new Exception(e.Message);
            }
            return content;
            finally
            {
                Logger.Add(serviceAddress, requestJson == null ? "" : requestJson, result, beginDate);
            }
        }
        /// <summary>
        /// åˆ†é…å··é“
        /// </summary>
        /// <param name="roadwayNos"></param>
        /// <returns></returns>
        public WebResponseContent RequestWMSAssignRoadway(List<string> roadwayNos)
        public WebResponseContent CPEmptyInbound(string palletCode,string SourceAddress)
        {
            WebResponseContent content = new WebResponseContent();
            try
@@ -606,13 +544,9 @@
                {
                    return WebResponseContent.Instance.Error($"未找到WMSApi地址");
                }
                string responseStr = HttpHelper.Post($"{address}/api/Task/SingleAssignRoadway", roadwayNos.Serialize());
                WebResponseContent? responseContent = JsonConvert.DeserializeObject<WebResponseContent>(responseStr);
                if (responseContent != null && responseContent.Status && responseContent.Data != null)
                {
                    return responseContent;
                }
                var result = HttpHelper.Get($"{address}/api/Task/EmptyBackTask?barCode={palletCode}&startPoint={SourceAddress}");
                content = JsonConvert.DeserializeObject<WebResponseContent>(result);
            }
            catch (Exception ex)
            {
@@ -632,6 +566,26 @@
            string address = AppSettings.Get("WMSApiAddress");
            if (string.IsNullOrEmpty(address)) throw new Exception("未找到WMSApi地址");
            string responseStr = HttpHelper.Get($"{address}/api/Task/AssignInboundTaskLocation?taskNum={taskNum}&roadwayNo={roadwayNo}");
            WebResponseContent? responseContent = JsonConvert.DeserializeObject<WebResponseContent>(responseStr);
            if (responseContent != null && responseContent.Status && responseContent.Data != null)
            {
                return responseContent.Data.ToString();
            }
            return "";
        }
        /// <summary>
        /// å‘WMS申请出库AGV终点
        /// </summary>
        /// <param name="taskNum">任务号</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public string? RequestTargetAddress(int taskNum)
        {
            string address = AppSettings.Get("WMSApiAddress");
            if (string.IsNullOrEmpty(address)) throw new Exception("未找到WMSApi地址");
            string responseStr = HttpHelper.Get($"{address}/api/Task/AssignOutTargetAddress?taskNum={taskNum}");
            WebResponseContent? responseContent = JsonConvert.DeserializeObject<WebResponseContent>(responseStr);
            if (responseContent != null && responseContent.Status && responseContent.Data != null)
@@ -732,7 +686,7 @@
                task.ModifyDate = DateTime.Now;
                BaseDal.UpdateData(task);
                _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, task.ExceptionMessage);
                _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, task.ExceptionMessage);
                content = WebResponseContent.Instance.OK();
            }
@@ -774,7 +728,7 @@
                BaseDal.UpdateData(task);
                _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, $"人工恢复挂起任务,恢复挂起时任务状态【{task.TaskState}】");
                _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, $"人工恢复挂起任务,恢复挂起时任务状态【{task.TaskState}】");
                content = WebResponseContent.Instance.OK();
            }
@@ -815,7 +769,7 @@
                BaseDal.UpdateData(task);
                _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, $"人工将任务状态从【{oldState}】回滚到【{task.TaskState}】");
                _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, $"人工将任务状态从【{oldState}】回滚到【{task.TaskState}】");
                content = WebResponseContent.Instance.OK();
            }
@@ -826,35 +780,7 @@
            return content;
        }
        public WebResponseContent SendSecureReplyToAgv(int taskNum)
        {
            try
            {
                Dt_Task WaitToTask = BaseDal.QueryFirst(x => x.TaskState == TaskStatusEnum.AGV_WaitToExecute.ObjToInt() && x.TaskNum == taskNum);
                AgvSecureReplyDTO replyDTO = new AgvSecureReplyDTO()
                {
                    ReqCode = Guid.NewGuid().ToString().Replace("-", ""), //WaitToTask.TaskNum.ToString(),
                    taskCode = WaitToTask.AgvTaskNum,
                };
                WebResponseContent content = AgvSecureReply(replyDTO);
                if (content.Status)
                {
                    WaitToTask.TaskState = TaskStatusEnum.AGV_Executing.ObjToInt();
                }
                else
                {
                    WaitToTask.TaskState = TaskStatusEnum.Exception.ObjToInt();
                    WaitToTask.ExceptionMessage = content.Message;
                }
                UpdateData(WaitToTask);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// //判断移库货位任务是否已存在,如存在先执行
        /// </summary>
@@ -887,7 +813,7 @@
        public Dt_Task QueryBarCodeAGVFinishTask(int TaskNum, string currentAddress)
        {
            return BaseDal.QueryFirst(x => TaskInboundTypes.Contains(x.TaskType) && x.TaskState == (int)TaskStatusEnum.AGV_Finish && x.NextAddress == currentAddress && x.TaskNum == TaskNum, TaskOrderBy);
            return BaseDal.QueryFirst(x => TaskInboundTypes.Contains(x.TaskType) && x.TaskState == (int)TaskStatusEnum.AGV_Finish && x.CurrentAddress == currentAddress && x.TaskNum == TaskNum, TaskOrderBy);
        }
        public Dt_Task QueryExecutingTaskByBarcode(int TaskNum, string nextAddress)
@@ -910,8 +836,14 @@
                {
                    if (task.TaskState == (int)TaskStatusEnum.SC_Executing)
                    {
                        Dt_Router router = _routerRepository.QueryFirst(x => x.InOutType == task.TaskType && x.StartPosi == stationManger.StationCode && x.ChildPosi == stationManger.StationDeviceCode);
                        if (router == null)
                        {
                            UpdateTaskExceptionMessage(task.TaskNum, $"未找到路由信息,{task.NextAddress}");
                            return WebResponseContent.Instance.Error($"未找到路由信息,{task.NextAddress}");
                        }
                        task.CurrentAddress = task.NextAddress;
                        task.NextAddress = task.TargetAddress;
                        task.NextAddress = router.NextPosi;
                        task.TaskState = (int)TaskStatusEnum.Line_Executing;
                        nextStatus = TaskStatusEnum.Line_Executing;
                    }
@@ -927,7 +859,7 @@
                    {
                        #region å…¥åº“调用接口获取货位地址
                        string? local = RequestAssignLocation(task.TaskNum, task.Roadway);
                        //string? local = "SC05_YLDual-002-090-009-01";
                        //string? local = "SC03_YLDual-002-090-001-01";
                        if (!string.IsNullOrEmpty(local))
                        {
                            task.CurrentAddress = stationManger.StackerCraneStationCode;
@@ -947,11 +879,11 @@
                    else if (task.TaskState == (int)TaskStatusEnum.AGV_Finish)
                    {
                        content = AssignYLRoadwayNo(task.PalletCode);
                        //content.OK(data: "SC05_YLDual");
                        if (!content.Status)
                        {
                            return content.Error($"请求入库失败:{content.Message}");
                        }
                        //string roadWay = "SC03_YLDual";
                        string roadWay = content.Data.ToString();
                        List<Dt_Router> routers = _routerRepository.QueryData(x => x.InOutType == task.TaskType && task.CurrentAddress == x.StartPosi && x.ChildPosiDeviceCode == roadWay);