yanjinhui
2026-03-20 5cbf9d0d05f6f2357c71f4e585441fdb4fc51d89
´úÂë¹ÜÀí/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/HKTaskMethods.cs
@@ -10,6 +10,9 @@
using WIDESEAWCS_DTO.WMS;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO;
using WIDESEAWCS_DTO.AGV.HIKROBOT;
using System.Diagnostics.CodeAnalysis;
namespace WIDESEAWCS_TaskInfoService
{
@@ -25,10 +28,20 @@
        /// <returns></returns>
        public WebResponseContent CreateHKNewInTask(TaskDTO taskDTO, int taskType)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_HKLocationInfo? hKLocationInfo = null;
                #region ç‚¹åˆ°ç‚¹
                if (!string.IsNullOrEmpty(taskDTO.toLocationCode))
                {
                    hKLocationInfo = _hKLocationInfoService.Repository.QueryFirst(x => x.LocationCode == taskDTO.toLocationCode) ?? throw new Exception($"未找到终点货位【{taskDTO.toLocationCode}】");
                    if (hKLocationInfo.LocationStatus != (int)LocationStatusEnum.Free) throw new Exception($"终点货位【{taskDTO.toLocationCode}】货位状态不为空货位");
                }
                #endregion
                //获取货位信息
                Dt_HKLocationInfo? hKLocationInfo = _hKLocationInfoService.GetFreeLocationInfo(taskDTO.toAreaCode) ?? throw new Exception($"未找到终点库区【{taskDTO.toAreaCode}】可用空货位!"); ;
                else
                    hKLocationInfo = _hKLocationInfoService.GetFreeLocationInfo(taskDTO.toAreaCode) ?? throw new Exception($"未找到终点库区【{taskDTO.toAreaCode}】可用空货位!");
                Dt_Task dt_Task = new Dt_Task()
                {
                    TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
@@ -46,17 +59,42 @@
                    Creater = "WMS",
                };
                hKLocationInfo.LocationStatus = LocationStatusEnum.InLock.ObjToInt();
                Db.Ado.BeginTran();
                BaseDal.AddData(dt_Task);
                _hKLocationInfoService.Repository.UpdateData(hKLocationInfo);
                Db.Ado.CommitTran();
                return WebResponseContent.Instance.OK();
                #region ä¸‹å‘海康AGV任务
                content = SendHIKROBOTTask(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);
                    _hKLocationInfoService.Repository.UpdateData(hKLocationInfo);
                    Db.Ado.CommitTran();
                }
                catch (Exception ex)
                {
                    Db.Ado.RollbackTran();
                    throw new Exception(ex.Message);
                }
                content.OK(data: new
                {
                    taskCode = taskDTO.taskCode,
                    Message = "成功!"
                });
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                throw new Exception(ex.Message);
                content.Data = new
                {
                    taskCode = taskDTO.taskCode,
                    Message = $"失败!{ex.Message}"
                };
                content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// åˆ›å»ºæµ·åº·AGV出库任务
@@ -66,6 +104,7 @@
        /// <returns></returns>
        public WebResponseContent CreateHKNewOutTask(TaskDTO taskDTO, int taskType)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_HKLocationInfo? hKLocationInfo = _hKLocationInfoService.Repository.QueryFirst(x => x.LocationCode == taskDTO.fromLocationCode) ?? throw new Exception($"未找到起点库位【{taskDTO.fromLocationCode}】!");
@@ -88,18 +127,200 @@
                    Creater = "WMS",
                };
                hKLocationInfo.LocationStatus = LocationStatusEnum.OutLock.ObjToInt();
                Db.Ado.BeginTran();
                BaseDal.AddData(dt_Task);
                _hKLocationInfoService.Repository.UpdateData(hKLocationInfo);
                Db.Ado.CommitTran();
                return WebResponseContent.Instance.OK();
                #region ä¸‹å‘海康AGV任务
                content = SendHIKROBOTTask(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);
                    _hKLocationInfoService.Repository.UpdateData(hKLocationInfo);
                    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();
                throw new Exception(ex.Message);
                content.Data = new
                {
                    taskCode = taskDTO.taskCode,
                    Message = $"失败!{ex.Message}"
                };
                return content.Error(ex.Message);
            }
        }
        #endregion
        #region ä¸‹å‘海康AGV任务
        public WebResponseContent SendHIKROBOTTask([NotNull] Dt_Task task)
        {
            WebResponseContent content = new WebResponseContent();
            HIKROBOTReturn hIKROBOTReturn = null;
            HIKROBOTTaskSubmit hIKROBOTTaskSubmit = null;
            try
            {
                Dt_ApiInfo? apiInfo = _apiInfoService.Repository.QueryFirst(x => x.ApiCode == nameof(HIKROBOTTaskSubmit)) ?? throw new Exception("未找到海康AGV任务下发接口配置信息!请检查接口配置");
                #region å®žä½“类转换
                hIKROBOTTaskSubmit = HIKROBOTTask(task);
                if (hIKROBOTTaskSubmit == null) throw new Exception("海康AGV任务实体转换失败");
                var json = hIKROBOTTaskSubmit.Serialize();
                #endregion
                // åˆ›å»ºHeaders字典
                var headers = new Dictionary<string, string>
                {
                    { "X-lr-request-id", DateTimeOffset.Now.ToUnixTimeSeconds().ToString() }
                    // å¦‚果需要其他Header,可以继续添加
                    // { "Content-Type", "application/json" }
                };
                // ä¼ é€’Headers参数
                string response = HttpHelper.Post(apiInfo.ApiAddress, hIKROBOTTaskSubmit.Serialize(), headers: headers);
                //string response = HttpHelper.Post(apiInfo.ApiAddress, hIKROBOTTaskSubmit.Serialize());
                hIKROBOTReturn = response.DeserializeObject<HIKROBOTReturn>();
                if (hIKROBOTReturn.code == "SUCCESS")
                {
                    var data = hIKROBOTReturn.data.ToString().DeserializeObject<HIKROBOTReturnData>();
                    content.OK(data: data);
                }
                else
                {
                    throw new Exception(hIKROBOTReturn.message);
                }
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            finally
            {
                _trackloginfoService.AddTrackLog(hIKROBOTTaskSubmit, content, "下发海康AGV任务", "", hIKROBOTReturn.message);
            }
            return content;
        }
        #endregion
        /// <summary>
        /// æµ·åº·ä»»åŠ¡å®žä½“ç±»è½¬æ¢
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        public HIKROBOTTaskSubmit HIKROBOTTask(Dt_Task task)
        {
            HIKROBOTTaskSubmit hIKROBOTTaskSubmit = new HIKROBOTTaskSubmit();
            try
            {
                var tasktype = (TaskTypeEnum)Enum.GetValues(typeof(TaskTypeEnum)).GetValue(task.TaskType-1);
                hIKROBOTTaskSubmit.initPriority = task.Grade;
                hIKROBOTTaskSubmit.robotTaskCode = task.WMSTaskNum;
                hIKROBOTTaskSubmit.taskType = tasktype.ToString(); ExtraDto extraDto = new ExtraDto();
                CarrierInfoDto carrierInfoDto = new CarrierInfoDto()
                {
                    carrierCode = task.PalletCode,
                    carrierType = task.PalletCode.Contains("LXM") ? "DX" : "SX"
                };
                extraDto.carrierInfo.Add(carrierInfoDto);
                hIKROBOTTaskSubmit.extra = extraDto;
                switch (tasktype)
                {
                    case TaskTypeEnum.CPInbound:
                    case TaskTypeEnum.CPOutbound:
                    case TaskTypeEnum.MLInbound:
                    case TaskTypeEnum.MLOutbound:
                        break;
                    case TaskTypeEnum.F01:
                        break;
                    case TaskTypeEnum.RK3F:
                    case TaskTypeEnum.CK3F:
                    case TaskTypeEnum.F02:
                        {
                            TargetRouteDto target = new TargetRouteDto()
                            {
                                code = task.CurrentAddress,
                                operation = "DELIVERY",//取货
                                seq = 0,
                                type = "SITE",
                            };
                            TargetRouteDto targetRoute = new TargetRouteDto()
                            {
                                code = task.NextAddress,
                                operation = "DELIVERY",//送货
                                seq = 1,
                                type = "SITE"
                            };
                            hIKROBOTTaskSubmit.targetRoute.Add(target);
                            hIKROBOTTaskSubmit.targetRoute.Add(targetRoute);
                        }
                        break;
                    case TaskTypeEnum.Q1TSJ4:
                        {
                            TargetRouteDto target = new TargetRouteDto()
                            {
                                code = task.CurrentAddress,
                                operation = "DELIVERY",//取货
                                seq = 0,
                                type = "SITE",
                            };
                            TargetRouteDto targetRoute = new TargetRouteDto()
                            {
                                code = task.NextAddress,
                                operation = "DELIVERY",//送货
                                seq = 1,
                                type = "STORAGE"
                            };
                            hIKROBOTTaskSubmit.targetRoute.Add(target);
                            hIKROBOTTaskSubmit.targetRoute.Add(targetRoute);
                        }
                        break;
                    case TaskTypeEnum.STU0003:
                    case TaskTypeEnum.CHUKU1:
                        {
                            TargetRouteDto target = new TargetRouteDto()
                            {
                                code = task.CurrentAddress,
                                operation = "DELIVERY",//取货
                                seq = 0,
                                type = "STORAGE",
                            };
                            TargetRouteDto targetRoute = new TargetRouteDto()
                            {
                                code = task.NextAddress,
                                operation = "DELIVERY",//送货
                                seq = 1,
                                type = "SITE",
                            };
                            hIKROBOTTaskSubmit.targetRoute.Add(target);
                            hIKROBOTTaskSubmit.targetRoute.Add(targetRoute);
                        }
                        break;
                    case TaskTypeEnum.Q3RK:
                        break;
                    case TaskTypeEnum.Q3CK:
                        break;
                    case TaskTypeEnum.Move:
                        break;
                    case TaskTypeEnum.CPMoveInventory:
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
            }
            return hIKROBOTTaskSubmit;
        }
    }
}