肖洋
2024-11-29 663d9d2e658ab99a0c3598becd23b00b34b3e3d9
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/Partial/TaskService.cs
@@ -1,13 +1,343 @@
using System;
using HslCommunication;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Common;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core;
using WIDESEAWCS_DTO.TaskInfo;
using System.Diagnostics.CodeAnalysis;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob.Models;
namespace WIDESEAWCS_TaskInfoService
{
    public partial class TaskService
    {
        /// <summary>
        /// 根据托盘号、起始地址向WMS请求任务
        /// </summary>
        /// <param name="palletCode">托盘号</param>
        /// <param name="sourceAddress">起始地址</param>
        /// <returns></returns>
        public async Task<WebResponseContent> RequestTask(string palletCode, string EquiCodeMOM, string Roadways, string area, string CurrentChildCode)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var hasTask = await BaseDal.QueryFirstAsync(x => x.PalletCode == palletCode);
                if (hasTask != null)
                {
                    return content.Error("当前托盘存在任务");
                }
                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.RequestInTask)?.ConfigValue;
                if (wmsBase == null || requestTask == null)
                {
                    throw new InvalidOperationException("WMS IP 未配置");
                }
                var wmsIpAddrss = wmsBase + requestTask;
                var result = await HttpHelper.PostAsync(wmsIpAddrss, new { palletCode = palletCode, EquiCodeMOM = EquiCodeMOM, Roadways = Roadways, area = area, Position = CurrentChildCode }.ToJsonString());
                content = JsonConvert.DeserializeObject<WebResponseContent>(result);
                if (!content.Status)
                {
                    // wms失败返回去NG口任务
                    return content;
                }
                var task = JsonConvert.DeserializeObject<WMSTaskDTO>(content.Data.ToString());
                return ReceiveByWMSTask(task);
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// 根据托盘号、站台向WMS请求任务
        /// </summary>
        /// <param name="palletCode">托盘号</param>
        /// <param name="sourceAddress">起始地址</param>
        /// <returns></returns>
        public async Task<WebResponseContent> RequestTask(string palletCode, Dt_StationManager stationManager)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var hasTask = await BaseDal.QueryFirstAsync(x => x.PalletCode == palletCode);
                if (hasTask != null)
                {
                    return content.Error("当前托盘存在任务");
                }
                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.RequestInTask)?.ConfigValue;
                if (wmsBase == null || requestTask == null)
                {
                    throw new InvalidOperationException("WMS IP 未配置");
                }
                var wmsIpAddrss = wmsBase + requestTask;
                var result = await HttpHelper.PostAsync(wmsIpAddrss, new { palletCode = palletCode, EquiCodeMOM = stationManager.stationEquipMOM, Roadways = stationManager.Roadway, area = stationManager.stationArea, Position = stationManager.stationChildCode }.ToJsonString());
                content = JsonConvert.DeserializeObject<WebResponseContent>(result);
                if (!content.Status)
                {
                    // wms失败返回去NG口任务
                    return content;
                }
                var task = JsonConvert.DeserializeObject<WMSTaskDTO>(content.Data.ToString());
                return ReceiveByWMSTask(task);
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// 接收WMS任务信息
        /// </summary>
        /// <param name="taskDTOs">WMS任务对象集合</param>
        /// <returns>返回处理结果</returns>
        public WebResponseContent ReceiveByWMSTask([NotNull] WMSTaskDTO taskDTO)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (BaseDal.QueryFirst(x => x.TaskNum == taskDTO.TaskNum || x.PalletCode == taskDTO.PalletCode) != null)
                {
                    return content.OK();
                }
                Dt_Task task = _mapper.Map<Dt_Task>(taskDTO);
                task.Creater = "WMS";
                if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                {
                    var stationinfo = _stationManagerRepository.QueryFirst(x => x.stationLocation == taskDTO.TargetAddress && x.Roadway == taskDTO.RoadWay);
                    task.TaskState = (int)TaskOutStatusEnum.OutNew;
                    task.CurrentAddress = taskDTO.RoadWay;
                    task.NextAddress = stationinfo.stationChildCode;
                    task.SourceAddress = taskDTO.SourceAddress;
                    task.TargetAddress = taskDTO.TargetAddress;
                }
                else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
                {
                    var stationinfo = _stationManagerRepository.QueryFirst(x => x.stationLocation == taskDTO.SourceAddress && x.Roadway == taskDTO.RoadWay);
                    task.TaskState = (int)TaskInStatusEnum.Line_InFinish;
                    task.SourceAddress = taskDTO.SourceAddress;
                    task.CurrentAddress = stationinfo.stationChildCode;
                    task.NextAddress = stationinfo.stationChildCode;
                    task.TargetAddress = taskDTO.TargetAddress;
                }
                BaseDal.AddData(task);
                _taskExecuteDetailService.AddTaskExecuteDetail(task.WMSId, "接收WMS任务");
                content = WebResponseContent.Instance.OK("成功");
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error($"任务接收错误,错误信息:{ex.Message}");
            }
            return content;
        }
        /// <summary>
        /// 高温出库
        /// </summary>
        /// <param name="taskDTO"></param>
        /// <returns></returns>
        public WebResponseContent ReceiveByWMSGWTask([NotNull] WMSTaskDTO taskDTO)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (BaseDal.QueryFirst(x => x.TaskNum == taskDTO.TaskNum || x.PalletCode == taskDTO.PalletCode) != null)
                {
                    return content.OK();
                }
                Dt_Task task = _mapper.Map<Dt_Task>(taskDTO);
                task.Creater = "WMS";
                if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                {
                    task.TaskState = (int)TaskOutStatusEnum.OutNew;
                    task.CurrentAddress = taskDTO.RoadWay;
                    task.NextAddress = "002-000-002";
                    task.SourceAddress = taskDTO.SourceAddress;
                    task.TargetAddress = taskDTO.TargetAddress;
                }
                BaseDal.AddData(task);
                _taskExecuteDetailService.AddTaskExecuteDetail(task.WMSId, "接收WMS任务");
                content = WebResponseContent.Instance.OK("成功");
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error($"任务接收错误,错误信息:{ex.Message}");
            }
            return content;
        }
        /// <summary>
        /// 接收WMS任务信息
        /// </summary>
        /// <param name="taskDTOs">WMS任务对象集合</param>
        /// <returns>返回处理结果</returns>
        public async Task<WebResponseContent> RequestFlow(Dt_StationManager stationManager)
        {
            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.RequestFlow)?.ConfigValue;
                if (wmsBase == null || requestTask == null)
                {
                    throw new InvalidOperationException("WMS IP 未配置");
                }
                var wmsIpAddrss = wmsBase + requestTask;
                var result = await HttpHelper.PostAsync(wmsIpAddrss, new { EquiCodeMOM = stationManager.stationEquipMOM, Roadways = stationManager.Roadway, area = stationManager.stationArea, Position = stationManager.stationChildCode }.ToJsonString());
                content = JsonConvert.DeserializeObject<WebResponseContent>(result);
                if (!content.Status)
                {
                    return content;
                }
                Dt_StationManager dt_Station = content.Data as Dt_StationManager;
                //dt_Station.
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error($"任务接收错误,错误信息:{ex.Message}");
            }
            return content;
        }
        /// <summary>
        /// 根据托盘号和设备号查询入库任务
        /// </summary>
        /// <param name="taskNum">任务号</param>
        /// <param name="currentAddress">当前地址</param>
        /// <returns></returns>
        public Dt_Task QueryTaskByPalletCode(string palletCode, string Roadway)
        {
            return BaseDal.QueryFirst(x => x.PalletCode == palletCode && x.Roadway == Roadway, TaskOrderBy);
        }
        ///// <summary>
        ///// 根据设备编号、当前地址查询输送线未执行的任务
        ///// </summary>
        ///// <param name="deviceNo">设备编号</param>
        ///// <param name="currentAddress">当前地址</param>
        ///// <returns></returns>
        //public Dt_Task QueryConveyorLineTask(string deviceNo, string currentAddress)
        //{
        //    return BaseDal.QueryFirst(x => (TaskInboundTypes.Contains(x.TaskType) && x.TaskState == (int)TaskInStatusEnum.InNew || TaskOutboundTypes.Contains(x.TaskType) && x.TaskState == (int)TaskOutStatusEnum.SC_OutFinish) && x.CurrentAddress == currentAddress, TaskOrderBy);
        //}
        /// 任务完成
        /// </summary>
        /// <param name="taskNum">任务编号</param>
        /// <returns>返回处理结果</returns>
        public WebResponseContent StackCraneTaskCompletedByStation(int taskNum)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
                if (task == null) return WebResponseContent.Instance.Error($"未找到该任务信息,任务号:【{taskNum}】");
                //if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup && task.TaskState == (int)TaskOutStatusEnum.SC_OutExecuting)
                //{
                //    //todo
                //}
                //else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup && task.TaskState == (int)TaskInStatusEnum.SC_InExecuting)
                //{
                //    //todo 同步到WMS
                //    BaseDal.DeleteData(task);
                //}
                //else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup)
                //{
                //    //todo 调用WMS移库完成
                //}
                //else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OtherGroup)
                //{
                //}
                //else
                //{
                //    throw new Exception($"任务类型错误,未找到该任务类型,任务号:【{taskNum}】,任务类型:【{task.TaskType}】");
                //}
                #region WMS同步任务完成
                var keys = new Dictionary<string, object>()
                {
                    {"taskNum", taskNum}
                };
                // 获取WMSip地址
                var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
                var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
                var completeTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.CompleteTask)?.ConfigValue;
                if (wmsBase == null || completeTask == null)
                {
                    throw new InvalidOperationException("WMS IP 未配置");
                }
                var wmsIpAddress = wmsBase + completeTask;
                var result = HttpHelper.GetAsync(wmsIpAddress, keys).Result;
                content = JsonConvert.DeserializeObject<WebResponseContent>(result);
                #endregion
                if (content.Status)
                {
                    task.TaskState = 1;  //任务完成
                    BaseDal.DeleteData(task);
                }
                #region  更新任务状态
                //var updateTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.UpdateTask)?.ConfigValue;
                //if (wmsBase == null || updateTask == null)
                //{
                //    throw new InvalidOperationException("WMS IP 未配置");
                //}
                //wmsIpAddress = wmsBase + updateTask;
                //result = HttpHelper.PostAsync(wmsIpAddress, new { TaskNum = task.TaskNum, TaskState = task.TaskState }.ToJsonString()).Result;
                //content = JsonConvert.DeserializeObject<WebResponseContent>(result);
                #endregion
                //content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error($"任务完成异常,任务号:【{taskNum}】");
            }
            return content;
        }
    }
}