yanjinhui
7 天以前 aeb971fe12b73d83d31a83b00f0a6e2876b24a00
´úÂë¹ÜÀí/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/KLSTaskMethods.cs
@@ -10,83 +10,361 @@
using WIDESEAWCS_Common.LocationEnum;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_DTO;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_QuartzJob;
using System.Reflection.Metadata;
namespace WIDESEAWCS_TaskInfoService
{
    public partial class TaskService
    {
        #region å‡¯ä¹å£«ä»»åŠ¡åˆ›å»º
        /// <summary>
        /// åˆ›å»ºå…¥åº“任务
        /// åˆ›å»ºå‡¯ä¹å£«AGV入库任务
        /// </summary>
        /// <param name="taskDTO"></param>
        /// <returns></returns>
        public WebResponseContent CreateKLSNewInTask(TaskDTO taskDTO, int taskType)
        {
            WebResponseContent content = new WebResponseContent();
            Dt_KLSLocationInfo? kLSLocationInfo = null;
            try
            {
                int containerType = taskDTO.containerCode.Contains("") ? LocationTypeEnum.LargePallet.ObjToInt() : LocationTypeEnum.SmallPallet.ObjToInt();
                int containerType = taskDTO.containerCode.Contains("LLM") ? (int)LocationTypeEnum.LargePallet : (int)LocationTypeEnum.SmallPallet;
                #region ç‚¹åˆ°ç‚¹
                if (!string.IsNullOrEmpty(taskDTO.toLocationCode))
                {
                    kLSLocationInfo = _kLSLocationInfoService.Repository.QueryFirst(x => x.LocationCode == taskDTO.toLocationCode) ?? throw new Exception($"未找到终点货位【{taskDTO.toLocationCode}】");
                    if (kLSLocationInfo.LocationStatus != (int)LocationStatusEnum.Free) throw new Exception($"终点货位【{taskDTO.toLocationCode}】货位状态不为空货位");
                }
                #endregion
                //获取货位信息
                Dt_KLSLocationInfo? kLSLocationInfo = _kLSLocationInfoService.Repository.QueryFirst(x => x.WarehouseId.ToString() == taskDTO.toAreaCode && x.EnableStatus == EnableStatusEnum.Normal.ObjToInt() && x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && x.LocationType == containerType);
                if (kLSLocationInfo == null) throw new Exception($"未找到终点库区【{taskDTO.toAreaCode}】可用空货位!");
                Dt_Task dt_Task = new Dt_Task()
                else
                    kLSLocationInfo = _kLSLocationInfoService.GetFreeLocationInfo(taskDTO.toAreaCode, containerType) ?? throw new Exception($"未找到终点库区【{taskDTO.toAreaCode}】可用空货位!");
                Dt_Task dt_Task = new()
                {
                    TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    WMSTaskNum = taskDTO.taskCode,
                    //WMSId = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    Grade = taskDTO.taskPriority,
                    PalletCode = taskDTO.containerCode,
                    TaskState = TaskInStatusEnum.InNew.ObjToInt(),
                    Roadway = kLSLocationInfo.RoadwayNo,
                    TaskState = (int)TaskStatusEnum.New,
                    TaskType = taskType,
                    SourceAddress = taskDTO.fromLocationCode,
                    CurrentAddress = taskDTO.fromLocationCode,
                    NextAddress = kLSLocationInfo.LocationCode,
                    TargetAddress = kLSLocationInfo.LocationCode,
                    Creater = "WMS",
                };
                kLSLocationInfo.LocationStatus = LocationStatusEnum.InLock.ObjToInt();
                Db.Ado.BeginTran();
                BaseDal.AddData(dt_Task);
                _kLSLocationInfoService.Repository.UpdateData(kLSLocationInfo);
                Db.Ado.CommitTran();
                return WebResponseContent.Instance.OK();
                kLSLocationInfo.LocationStatus = (int)LocationStatusEnum.InLock;
                #region ä¸‹å‘凯乐士任务
                content = SendKLSTask(dt_Task);
                if (!content.Status) throw new Exception(content.Message);
                dt_Task.TaskState = (int)TaskStatusEnum.Execut;
                dt_Task.Dispatchertime = DateTime.Now;
                #endregion
                try
                {
                    Db.Ado.BeginTran();
                    BaseDal.AddData(dt_Task);
                    _kLSLocationInfoService.Repository.UpdateData(kLSLocationInfo);
                    Db.Ado.CommitTran();
                }
                catch (Exception ex)
                {
                    Db.Ado.RollbackTran();
                    throw new Exception(ex.Message);
                }
                return content.OK(data: new
                {
                    taskCode = taskDTO.taskCode,
                    Message = "成功!"
                });
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
                content.Data = new
                {
                    taskCode = taskDTO.taskCode,
                    Message = $"失败!{ex.Message}"
                };
                return content.Error(ex.Message);
            }
        }
        /// <summary>
        /// åˆ›å»ºå‡¯ä¹å£«AGV出库任务
        /// </summary>
        /// <param name="taskDTO"></param>
        /// <param name="taskType"></param>
        /// <returns></returns>
        public WebResponseContent CreateKLSNewOutTask(TaskDTO taskDTO, int taskType)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_KLSLocationInfo? kLSLocationInfo = _kLSLocationInfoService.Repository.QueryFirst(x => x.LocationCode == taskDTO.fromLocationCode);
                if (kLSLocationInfo == null) throw new Exception($"未找到起点库位【{taskDTO.fromLocationCode}】!");
                if (kLSLocationInfo.LocationStatus != LocationStatusEnum.InStock.ObjToInt()) throw new Exception($"起点库位【{taskDTO.fromLocationCode}】当前库位状态不可出库!");
                Dt_Task dt_Task = new Dt_Task()
                Dt_KLSLocationInfo? kLSLocationInfo = _kLSLocationInfoService.Repository.QueryFirst(x => x.LocationCode == taskDTO.fromLocationCode) ?? throw new Exception($"未找到起点库位【{taskDTO.fromLocationCode}】!");
                if (kLSLocationInfo.LocationStatus != (int)LocationStatusEnum.InStock) throw new Exception($"起点库位【{taskDTO.fromLocationCode}】当前库位状态不可出库!");
                if (kLSLocationInfo.PalletCode != taskDTO.containerCode) throw new Exception($"起点库位【{taskDTO.fromLocationCode}】绑定料箱号【{kLSLocationInfo.PalletCode}】与任务料箱号【{taskDTO.containerCode}】不匹配!");
                Dt_Task dt_Task = new()
                {
                    TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    WMSTaskNum = taskDTO.taskCode,
                    //WMSId = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    Grade = taskDTO.taskPriority,
                    PalletCode = taskDTO.containerCode,
                    TaskState = TaskOutStatusEnum.OutNew.ObjToInt(),
                    Roadway = kLSLocationInfo.RoadwayNo,
                    TaskState = (int)TaskStatusEnum.New,
                    TaskType = taskType,
                    SourceAddress = taskDTO.fromLocationCode,
                    CurrentAddress = taskDTO.fromLocationCode,
                    NextAddress = taskDTO.toLocationCode,
                    TargetAddress = taskDTO.toLocationCode,
                    Creater = "WMS",
                };
                kLSLocationInfo.LocationStatus = LocationStatusEnum.OutLock.ObjToInt();
                Db.Ado.BeginTran();
                BaseDal.AddData(dt_Task);
                _kLSLocationInfoService.Repository.UpdateData(kLSLocationInfo);
                Db.Ado.CommitTran();
                return WebResponseContent.Instance.OK();
                kLSLocationInfo.LocationStatus = (int)LocationStatusEnum.OutLock;
                #region ä¸‹å‘凯乐士任务
                content = SendKLSTask(dt_Task);
                if (!content.Status) throw new Exception(content.Message);
                dt_Task.TaskState = (int)TaskStatusEnum.Execut;
                dt_Task.Dispatchertime = DateTime.Now;
                #endregion
                try
                {
                    Db.Ado.BeginTran();
                    BaseDal.AddData(dt_Task);
                    _kLSLocationInfoService.Repository.UpdateData(kLSLocationInfo);
                    Db.Ado.CommitTran();
                }
                catch (Exception ex)
                {
                    Db.Ado.RollbackTran();
                    throw new Exception(ex.Message);
                }
                return content.OK(data: new
                {
                    taskCode = taskDTO.taskCode,
                    Message = "成功!"
                });
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
                content.Data = new
                {
                    taskCode = taskDTO.taskCode,
                    Message = $"失败!{ex.Message}"
                };
                return content.Error(ex.Message);
            }
        }
        #endregion
        #region ä¸‹å‘凯乐士任务
        public WebResponseContent SendKLSTask(Dt_Task task)
        {
            WebResponseContent content = new WebResponseContent();
            GALAXISTaskInfo gALAXISTaskInfo = new();
            GALAXISReturn agvContent = new();
            try
            {
                gALAXISTaskInfo.groupId = DateTime.Now.ToString("yyMMddHHmmss") + task.TaskNum;
                gALAXISTaskInfo.msgTime = DateTime.Now.ToString();
                GALAXISTask gALAXISTask = new GALAXISTask()
                {
                    taskId = task.WMSTaskNum,
                    taskType = task.TaskType == (int)TaskTypeEnum.MLInbound ? 0 : 1,
                    barCode = task.PalletCode,
                    endNode = task.TargetAddress,
                    startNode = task.SourceAddress,
                    priorityCode = task.Grade
                };
                gALAXISTaskInfo.tasks.Add(gALAXISTask);
                Dt_ApiInfo? apiInfo = _apiInfoService.Repository.QueryFirst(x => x.ApiCode == nameof(GALAXISTaskInfo)) ?? throw new Exception("未找到凯乐士AGV任务下发接口配置信息!请检查接口配置");
                string response = HttpHelper.Post(apiInfo.ApiAddress, gALAXISTaskInfo.Serialize());
                agvContent = response.DeserializeObject<GALAXISReturn>();
                content.OK(data: agvContent);
                if (agvContent.success)
                {
                    if (agvContent.data.returnStatus != 0) throw new Exception(agvContent.data.returnInfo);
                }
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            finally
            {
                _trackloginfoService.AddTrackLog(gALAXISTaskInfo, content, "下发凯乐士AGV任务", "", agvContent.data.returnInfo);
            }
            return content;
        }
        #endregion
        #region 5 è·¨æ¥¼å±‚运输任务创建 ï¼ˆéƒ½æ˜¯ç‚¹åˆ°ç‚¹ï¼‰
        public WebResponseContent CarryTask(TaskDTO taskDTO, int taskType)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_StationManger? stationInfo = null;
                Dt_Task dt_Task = null;
                //入库 åˆ°ä¸€æ¥¼ä¸‰ä¸ªç«™å°çš„位置
                if (taskType == (int)TaskTypeEnum.Q3RK)
                {
                    dt_Task = new Dt_Task()
                    {
                        TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                        WMSTaskNum = taskDTO.taskCode,
                        Grade = taskDTO.taskPriority,
                        PalletCode = taskDTO.containerCode,
                        Roadway = "1",
                        TaskState = (int)TaskStatusEnum.New,
                        TaskType = taskType,
                        SourceAddress = taskDTO.fromLocationCode,
                        CurrentAddress = taskDTO.fromLocationCode,
                        NextAddress = taskDTO.toLocationCode,
                        TargetAddress = taskDTO.toLocationCode,
                        Creater = "WMS",
                    };
                }
                //出库
                if (taskType == (int)TaskTypeEnum.Q3CK)
                {
                    //stationInfo = _stationMangerService.Repository.QueryFirst(x => x.StationCode == taskDTO.fromLocationCode) ?? throw new Exception($"未找到起点库位【{taskDTO.fromLocationCode}】!");
                    //if (stationInfo.IsOccupied != (int)LocationStatusEnum.InStock) throw new Exception($"起点库位【{taskDTO.fromLocationCode}】当前库位状态不可出库!");
                    dt_Task = new Dt_Task()
                    {
                        TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                        WMSTaskNum = taskDTO.taskCode,
                        //WMSId = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                        Grade = taskDTO.taskPriority,
                        PalletCode = taskDTO.containerCode,
                        Roadway = "1",
                        TaskState = (int)TaskStatusEnum.New,
                        TaskType = taskType,
                        SourceAddress = taskDTO.fromLocationCode,
                        CurrentAddress = taskDTO.fromLocationCode,
                        NextAddress = taskDTO.toLocationCode,
                        TargetAddress = taskDTO.toLocationCode,
                        Creater = "WMS",
                    };
                }
                try
                {
                    Db.Ado.BeginTran();
                    BaseDal.AddData(dt_Task);
                    Db.Ado.CommitTran();
                }
                catch (Exception ex)
                {
                    Db.Ado.RollbackTran();
                    throw new Exception(ex.Message);
                }
                return content.OK(data: new
                {
                    taskCode = taskDTO.taskCode,
                    Message = "成功!"
                });
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        #endregion
        #region 6 è£å‰ªé€è´§
        public WebResponseContent CJCarryTaske(TaskDTO taskDTO, int taskType)
        {
            WebResponseContent content = new WebResponseContent();
            Dt_StationInfo? stationInfo = null;
            try
            {
                //#region æ˜¯å¦éœ€è¦åˆ¤æ–­å¹³åº“库位状态
                //if (taskType!= (int)TaskTypeEnum.CK3F)
                //{
                //    stationInfo = _stationInfo.Repository.QueryFirst(x => x.StationName == taskDTO.fromLocationCode) ?? throw new Exception($"未找到起点库位【{taskDTO.fromLocationCode}】!");
                //    if (stationInfo.StationName != LocationStatusEnum.InStock.ToString()) throw new Exception($"起点库位【{taskDTO.fromLocationCode}】当前库位状态不可出库!");
                //    if (stationInfo.PalletCode != taskDTO.containerCode) throw new Exception($"起点库位【{taskDTO.fromLocationCode}】绑定料箱号【{stationInfo.PalletCode}】与任务料箱号【{taskDTO.containerCode}】不匹配!");
                //}
                //else
                //{
                //    stationInfo = _stationInfo.Repository.QueryFirst(x => x.StationName == taskDTO.toLocationCode) ?? throw new Exception($"未找到终点库位【{taskDTO.toLocationCode}】!");
                //    if (stationInfo.StationName != LocationStatusEnum.InStock.ToString()) throw new Exception($"起点库位【{taskDTO.toLocationCode}】当前库位状态不可出库!");
                //}
                //#endregion
                Dt_Task dt_Task = new Dt_Task()
                {
                    TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    WMSTaskNum = taskDTO.taskCode,
                    //WMSId = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    Grade = taskDTO.taskPriority,
                    PalletCode = taskDTO.containerCode,
                    Roadway = "",
                    TaskState = (int)TaskStatusEnum.New,
                    TaskType = taskType,
                    SourceAddress = taskDTO.fromLocationCode,
                    CurrentAddress = taskDTO.fromLocationCode,
                    NextAddress = taskDTO.toLocationCode,
                    TargetAddress = taskDTO.toLocationCode,
                    Creater = "WMS",
                };
                content = SendHIKROBOTTask(dt_Task);
                if (!content.Status) throw new Exception(content.Message);
                dt_Task.TaskState = (int)TaskStatusEnum.Execut;
                dt_Task.Dispatchertime = DateTime.Now;
                BaseDal.AddData(dt_Task);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        #endregion
        #region 5空车转运
        public WebResponseContent HKF01Transport(TaskDTO taskDTO, int taskType)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var stationInfo = _stationInfo.Repository.QueryFirst(x => x.StationName == taskDTO.fromLocationCode) ?? throw new Exception($"未找到起点库位【{taskDTO.fromLocationCode}】!");
                var dt_Task = new Dt_Task()
                {
                    TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    WMSTaskNum = taskDTO.taskCode,
                    //WMSId = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    Grade = taskDTO.taskPriority,
                    PalletCode = taskDTO.containerCode,
                    Roadway = "1",
                    TaskState = (int)TaskStatusEnum.New,
                    TaskType = taskType,
                    SourceAddress = taskDTO.fromLocationCode,
                    CurrentAddress = taskDTO.fromLocationCode,
                    NextAddress = taskDTO.toLocationCode,
                    TargetAddress = taskDTO.toLocationCode,
                    Creater = "WMS",
                };
                content = SendHIKROBOTTask(dt_Task);
                if (!content.Status) throw new Exception(content.Message);
                dt_Task.Dispatchertime = DateTime.Now;
                BaseDal.AddData(dt_Task);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        #endregion
    }
}