huangxiaoqiang
2025-08-27 5a8b16b77cbbbe8137603ba9381f81f95f58135f
ÏîÄ¿´úÂë/WCS/WCSServices/WIDESEAWCS_TaskInfoService/TaskService.cs
@@ -14,21 +14,10 @@
 * 
 *----------------------------------------------------------------*/
#endregion << ç‰ˆ æœ¬ æ³¨ é‡Š >>
using AutoMapper;
using HslCommunication.Enthernet;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_DTO.Agv;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.APIEnum;
@@ -37,18 +26,15 @@
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.BasicInfo;
using WIDESEAWCS_DTO.System;
using WIDESEAWCS_Core.LogHelper;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Repository;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_TaskInfoRepository;
using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
namespace WIDESEAWCS_TaskInfoService
@@ -79,6 +65,8 @@
        public List<int> TaskInboundTypes => typeof(TaskTypeEnum).GetEnumIndexList().Where(x => x >= 500 && x < 900).ToList();
        public List<int> TaskOutboundTypes => typeof(TaskTypeEnum).GetEnumIndexList().Where(x => x >= 100 && x < 500).ToList();
        public List<int> TaskRelocationTypes => typeof(TaskTypeEnum).GetEnumIndexList().Where(x => x >= 900 && x < 1000).ToList();
        public TaskService(ITaskRepository BaseDal, IMapper mapper, ICacheService cacheService, IRouterService routerService, ITaskExecuteDetailService taskExecuteDetailService, ITaskExecuteDetailRepository taskExecuteDetailRepository, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, IApiInfoRepository apiInfoRepository) : base(BaseDal)
        {
@@ -139,53 +127,6 @@
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent AgvCarrying(int row,int startColumn, int endColumn, int startlayer,int targetLayer)
        {
            try
            {
                List<Dt_Task> tasks = new List<Dt_Task>();
                for (int i = startColumn; i <= endColumn; i++)
                {
                    string column = i.ToString();
                    if (i<10)
                    {
                        column = "0" + i.ToString();
                    }
                    string layer = targetLayer.ToString();
                    if (targetLayer<10)
                    {
                        layer = "0"+targetLayer.ToString();
                    }
                    Dt_Task task = new Dt_Task()
                    {
                        WarehouseId = 7,
                        TaskNum = DateTime.Now.ToString("yyMMddHHmm").ObjToInt() + i,
                        PalletCode= DateTime.Now.ToString("yyMMddHHmm") +i,
                        PalletType=0,
                        DeviceCode="AGV_CP",
                        Roadway= "AGV_CP",
                        TaskType =999,
                        SourceAddress=string.Format($"AGV_CP-00{row}-0{column}-00{startlayer}-01"),
                        CurrentAddress=string.Format($"AGV_CP-00{row}-0{column}-00{startlayer}-01"),
                        TargetAddress= string.Format($"AGV_CP-00{row}-0{column}-00{layer}-01"),
                        NextAddress= string.Format($"AGV_CP-00{row}-0{column}-00{layer}-01"),
                        Grade=0,
                        WMSId=0,
                        AgvTaskNum = "CP" + DateTime.Now.ToString("yyMMddHHmm").ObjToInt() + i,
                        TaskState = TaskStatusEnum.New.ObjToInt(),
                        Remark= "测试空框"
                    };
                    tasks.Add(task);
                }
                BaseDal.AddData(tasks);
                return WebResponseContent.Instance.OK("");
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// æŽ¥æ”¶WMS任务信息
        /// </summary>
@@ -211,10 +152,18 @@
                    task.CurrentAddress = item.SourceAddress;
                    Dt_Router? router;
                    if (task.TaskType==TaskTypeEnum.Relocation.ObjToInt())
                    {
                        task.DeviceCode = task.Roadway;
                        task.NextAddress = task.TargetAddress;
                        task.TaskState = TaskStatusEnum.SC_Execute.ObjToInt();
                        tasks.Add(task);
                        break;
                    }
                    TaskTypeGroup taskTypeGroup = item.TaskType.GetTaskTypeGroup();
                    if (string.IsNullOrEmpty(item.AGVArea))
                    {
                        Dt_StationManger stationManger;
                        //获取站台信息
                        if (taskTypeGroup == TaskTypeGroup.InboundGroup)
@@ -239,27 +188,34 @@
                        if (routers.Count == 1)
                        {
                            //入库
                            if (taskTypeGroup == TaskTypeGroup.InboundGroup)
                            {
                                task.CurrentAddress = router.StartPosi;
                                task.NextAddress = router.NextPosi;
                                task.DeviceCode = router.ChildPosiDeviceCode;
                                if (task.TaskType == TaskTypeEnum.ProductMD.ObjToInt())
                                {
                                    task.TaskState = TaskStatusEnum.Line_Executing.ObjToInt();
                                }
                            }
                            else
                            else //出库
                            {
                                task.DeviceCode = stationManger.StackerCraneCode;
                                task.NextAddress = router.NextPosi;
                                task.TaskState = (int)TaskStatusEnum.SC_Execute;
                            }
                        }
                        else
                        {
                            //成品码垛任务生成
                            if (taskTypeGroup == TaskTypeGroup.InboundGroup && !task.Roadway.Contains("YL"))
                            {
                                task.CurrentAddress = router.StartPosi;
                                task.NextAddress = "";
                                task.DeviceCode = router.ChildPosiDeviceCode;
                            }
                            else
                            else if(taskTypeGroup == TaskTypeGroup.InboundGroup && task.Roadway.Contains("YL")) //原料入库
                            {
                                router = routers.FirstOrDefault(x=>x.ChildPosiDeviceCode==task.Roadway);
                                task.CurrentAddress = router.StartPosi;
@@ -270,6 +226,7 @@
                    }
                    else
                    {
                        //成品入库
                        List<Dt_Router> routers = _routerRepository.QueryData(x => x.InOutType == item.TaskType && x.StartPosi=="AGV" && x.ChildPosiDeviceCode==item.RoadWay);
                        router = routers.FirstOrDefault();
                        if (routers.Count == 1)
@@ -347,7 +304,7 @@
        /// <param name="sourceAddress">起始地址</param>
        /// /// <param name="roadWay">巷道</param>
        /// <returns></returns>
        public WebResponseContent RequestYLWMSTaskSimple(string palletCode, string sourceAddress)
        public WebResponseContent RequestYLWMSTaskSimple(string palletCode, string sourceAddress,int taskNum=0)
        {
            WebResponseContent content = new WebResponseContent();
            try
@@ -357,7 +314,7 @@
                {
                    return WebResponseContent.Instance.Error($"未找到WMSApi地址");
                }
                string responseStr = HttpHelper.Get($"{address}/api/Task/RequestYLWMSTaskSimple?stationCode={sourceAddress}&palletCode={palletCode}");
                string responseStr = HttpHelper.Get($"{address}/api/Task/RequestYLWMSTaskSimple?stationCode={sourceAddress}&palletCode={palletCode}&taskNum{taskNum}");
                WebResponseContent? responseContent = JsonConvert.DeserializeObject<WebResponseContent>(responseStr);
                if (responseContent != null && responseContent.Status && responseContent.Data != null)
                {
@@ -375,6 +332,64 @@
            }
            return content;
        }
        // <summary>
        /// è¯·æ±‚成品入库巷道
        /// </summary>
        /// <returns></returns>
        public WebResponseContent AssignCPRoadwayNo()
        {
            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/AssignCPRoadwayNo");
                if (!responseStr.IsNullOrEmpty())
                {
                    content.OK("成功", responseStr);
                }
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            return content;
        }
        // <summary>
        /// è¯·æ±‚原料入库巷道
        /// </summary>
        /// <returns></returns>
        public WebResponseContent AssignYLRoadwayNo(string palletCode)
        {
            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/AssignYLRoadwayNo?palletCode={palletCode}");
                WebResponseContent? responseContent = JsonConvert.DeserializeObject<WebResponseContent>(responseStr);
                if (responseContent != null && responseContent.Status && responseContent.Data != null)
                {
                    content = responseContent;
                }
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            return content;
        }
        // <summary>
        /// è¯·æ±‚入库任务
        /// </summary>
@@ -382,7 +397,7 @@
        /// <param name="sourceAddress">起始地址</param>
        /// /// <param name="roadWay">巷道</param>
        /// <returns></returns>
        public WebResponseContent RequestWMSTaskSimple(string palletCode, string sourceAddress, string roadWay="", int taskType=630)
        public WebResponseContent RequestWMSTaskSimple(string palletCode, string sourceAddress,string roadWay="", int taskType=630, int taskNum = 0, string targetAddress = "")
        {
            WebResponseContent content = new WebResponseContent();
            try
@@ -401,16 +416,15 @@
                //    {
                WMSTaskDTO? taskDTO = new WMSTaskDTO()
                {
                    Id = 1,
                    TaskNum = DateTime.Now.ToString("mmss").ObjToInt(),
                    TaskNum = taskNum ==0 ? DateTime.Now.ToString("mmss").ObjToInt(): taskNum,
                    PalletCode= palletCode,
                    RoadWay= roadWay ?? "",
                    RoadWay= roadWay,
                    TaskType= taskType,
                    PalletType=1,
                    TaskStatus=TaskStatusEnum.New.ObjToInt(),
                    TaskStatus= TaskStatusEnum.New.ObjToInt(),
                    SourceAddress= sourceAddress,
                    TargetAddress="",
                    WarehouseId=1,
                    TargetAddress= targetAddress,
                    WarehouseId=2,
                    Grade=0,
                };
                content = ReceiveWMSTask(new List<WMSTaskDTO> { taskDTO });
@@ -461,42 +475,6 @@
        //    return content;
        //}
        /// <summary>
        ///
        /// </summary>
        /// <param name="sourceAddress"></param>
        /// <param name="palletCode"></param>
        /// <param name="heightType"></param>
        /// <param name="roadways"></param>
        /// <returns></returns>
        public WebResponseContent RequestWMSTask(string palletCode, string sourceAddress,  int heightType, List<string> roadways)
        {
            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/DeviceRequestInboundTaskByRoadways?stationCode={sourceAddress}&palletCode={palletCode}&heightType={heightType}", roadways.Serialize());
                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>
        /// åˆ†é…å··é“
@@ -645,29 +623,6 @@
        }
        /// <summary>
        /// å‘WMS申请分配货位
        /// </summary>
        /// <param name="taskNum"></param>
        /// <param name="roadwayNo"></param>
        /// <param name="heightType"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public string? RequestAssignLocationByHeight(int taskNum, string roadwayNo, int heightType)
        {
            string address = AppSettings.Get("WMSApiAddress");
            if (string.IsNullOrEmpty(address)) throw new Exception("未找到WMSApi地址");
            string responseStr = HttpHelper.Get($"{address}/api/Task/AssignInboundTaskLocationByHeight?taskNum={taskNum}&roadwayNo={roadwayNo}&heightType={heightType}");
            WebResponseContent? responseContent = JsonConvert.DeserializeObject<WebResponseContent>(responseStr);
            if (responseContent != null && responseContent.Status && responseContent.Data != null)
            {
                return responseContent.Data.ToString();
            }
            return "";
        }
        /// <summary>
        /// æ ¹æ®è®¾å¤‡ç¼–号、任务类型分组(可选)按照优先级以及创建时间排序查询任务池新增的任务
        /// </summary>
        /// <param name="deviceNo">设备编号</param>
@@ -681,6 +636,8 @@
                return BaseDal.QueryFirst(x => x.DeviceCode == deviceNo && TaskInboundTypes.Contains(x.TaskType) && x.TaskState == (int)TaskStatusEnum.SC_Execute, TaskOrderBy);
            if (taskTypeGroup.Value == TaskTypeGroup.OutbondGroup)
                return BaseDal.QueryFirst(x => x.DeviceCode == deviceNo && TaskOutboundTypes.Contains(x.TaskType) && x.TaskState == (int)TaskStatusEnum.SC_Execute, TaskOrderBy);
            if (taskTypeGroup.Value == TaskTypeGroup.RelocationGroup)
                return BaseDal.QueryFirst(x => x.DeviceCode == deviceNo && TaskRelocationTypes.Contains(x.TaskType) && x.TaskState == (int)TaskStatusEnum.SC_Execute, TaskOrderBy);
            return null;
        }
@@ -907,5 +864,114 @@
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        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);
        }
        public Dt_Task QueryExecutingTaskByBarcode(int TaskNum, string nextAddress)
        {
            return BaseDal.QueryFirst(x => x.TaskNum == TaskNum && x.NextAddress == nextAddress && x.TaskState == (int)TaskStatusEnum.Line_Executing, TaskOrderBy);
        }
        public Dt_Task QueryLineExecuteTaskByBarcode(int TaskNum, string nextAddress)
        {
            return BaseDal.QueryFirst(x => x.TaskNum == TaskNum && x.CurrentAddress == nextAddress && x.TaskState == (int)TaskStatusEnum.Line_Execute, TaskOrderBy);
        }
        public WebResponseContent UpdateTaskStatusToNext([NotNull] Dt_Task task, Dt_StationManger stationManger = null)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                TaskStatusEnum nextStatus = new TaskStatusEnum();
                if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                {
                    if (task.TaskState == (int)TaskStatusEnum.SC_Executing)
                    {
                        task.CurrentAddress = task.NextAddress;
                        task.NextAddress = task.TargetAddress;
                        task.TaskState = (int)TaskStatusEnum.Line_Executing;
                        nextStatus = TaskStatusEnum.Line_Executing;
                    }
                    else if (task.TaskState == (int)TaskStatusEnum.SC_Execute)
                    {
                        task.TaskState = (int)TaskStatusEnum.SC_Executing;
                        nextStatus = TaskStatusEnum.SC_Executing;
                    }
                }
                else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
                {
                    if (task.TaskState == (int)TaskStatusEnum.Line_Executing)
                    {
                        #region å…¥åº“调用接口获取货位地址
                        //string? local = RequestAssignLocation(task.TaskNum, task.Roadway);
                        string? local = "SC05_YLDual-002-090-009-01";
                        if (!string.IsNullOrEmpty(local))
                        {
                            task.CurrentAddress = stationManger.StackerCraneStationCode;
                            task.TargetAddress = local;
                            task.NextAddress = local;
                            task.DeviceCode = stationManger.StackerCraneCode;
                            task.TaskState = (int)TaskStatusEnum.SC_Execute;
                            nextStatus = TaskStatusEnum.SC_Execute;
                            content.OK($"获取货位号{local}");
                        }
                        else
                        {
                            return content.Error($"请求入库货位失败");
                        }
                        #endregion å…¥åº“调用接口获取货位地址
                    }
                    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 = content.Data.ToString();
                        List<Dt_Router> routers = _routerRepository.QueryData(x => x.InOutType == task.TaskType && task.CurrentAddress == x.StartPosi && x.ChildPosiDeviceCode == roadWay);
                        Dt_Router router = routers.FirstOrDefault();
                        if (router == null)
                        {
                            return content.Error($"任务号:{task.TaskNum}未找到路由配置信息");
                        }
                        task.Roadway = roadWay;
                        task.NextAddress = router.NextPosi;
                        task.TaskState = (int)TaskStatusEnum.Line_Execute;
                        nextStatus = TaskStatusEnum.Line_Execute;
                    }
                    else if (task.TaskState == (int)TaskStatusEnum.Line_Execute)
                    {
                        task.TaskState = (int)TaskStatusEnum.Line_Executing;
                        nextStatus = TaskStatusEnum.Line_Executing;
                    }
                    else if (task.TaskState == (int)TaskStatusEnum.SC_Execute)
                    {
                        task.TaskState = (int)TaskStatusEnum.SC_Executing;
                        nextStatus = TaskStatusEnum.SC_Executing;
                    }
                }
                else
                {
                    throw new Exception($"任务类型错误,未找到该任务类型,任务号:【{task.TaskNum}】,任务类型:【{task.TaskType}】");
                }
                task.ModifyDate = DateTime.Now;
                task.Modifier = "System";
                BaseDal.UpdateData(task);
                //UpdateTask(task, nextStatus);
                content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            return content;
        }
    }
}