xiazhengtongxue
2026-03-17 4f3b0a6727ecfa555892ebea64bd76e5a07cd6fb
ÏîÄ¿´úÂë/WCSServices/WIDESEAWCS_TaskInfoService/TaskService.cs
@@ -91,7 +91,7 @@
        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,ILocationInfoRepository locationInfoRepository,IUnitOfWorkManage unitOfWorkManage, ILocationInfoService locationInfoService,ILocationStatusChangeRecordService locationStatusChangeRecordService,IErrorInfoRepository errorInfoRepository) : base(BaseDal)
        public TaskService(ITaskRepository BaseDal, IMapper mapper, ICacheService cacheService, IRouterService routerService, ITaskExecuteDetailService taskExecuteDetailService, ITaskExecuteDetailRepository taskExecuteDetailRepository, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, IApiInfoRepository apiInfoRepository, ILocationInfoRepository locationInfoRepository, IUnitOfWorkManage unitOfWorkManage, ILocationInfoService locationInfoService, ILocationStatusChangeRecordService locationStatusChangeRecordService, IErrorInfoRepository errorInfoRepository) : base(BaseDal)
        {
            _mapper = mapper;
            _cacheService = cacheService;
@@ -104,7 +104,7 @@
            _locationInfoRepository = locationInfoRepository;
            _unitOfWorkManage = unitOfWorkManage;
            _locationInfoService = locationInfoService;
            _locationStatusChangeRecordService=locationStatusChangeRecordService;
            _locationStatusChangeRecordService = locationStatusChangeRecordService;
            _errorInfoRepository = errorInfoRepository;
        }
        static object lock_taskReceive = new object();
@@ -123,18 +123,18 @@
                {
                    WriteLog.Write_Log("入库任务下发", "WMS入库任务接收参数", "接收参数", $"参数:{taskDTO.ToJson()}");
                    List<Dt_Task> tasks = new List<Dt_Task>();
                    List<Dt_Task> taskOlds = BaseDal.QueryData(x=> taskDTO.Tasks.Select(x => x.TaskDescribe.ContainerCode).Contains(x.PalletCode));
                    List<Dt_LocationInfo> locationInfos = _locationInfoRepository.GetCanOut(taskDTO.Tasks.Select(x=>x.TaskDescribe.ContainerCode).ToList());
                    List<Dt_Task> taskOlds = BaseDal.QueryData(x => taskDTO.Tasks.Select(x => x.TaskDescribe.ContainerCode).Contains(x.PalletCode));
                    List<Dt_LocationInfo> locationInfos = _locationInfoRepository.GetCanOut(taskDTO.Tasks.Select(x => x.TaskDescribe.ContainerCode).ToList());
                    List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData();
                    ////下发任务组
                    //string taskGroup= taskDTO.TaskGroupCode.IsNullOrEmpty() ? Guid.NewGuid().ToString().Replace("-","") : taskDTO.TaskGroupCode;
                    foreach (var item in taskDTO.Tasks.OrderBy(x=>x.TaskDescribe.ToStationCode))
                    foreach (var item in taskDTO.Tasks.OrderBy(x => x.TaskDescribe.ToStationCode))
                    {
                        if (item.TaskDescribe.ToStationCode.IsNullOrEmpty()) throw new Exception($"任务{item.TaskCode}出库目标操作台不能为空");
                        //获取操作台
                        Dt_StationManger? stationManger = stationMangers.FirstOrDefault(x => x.PickStationCode == item.TaskDescribe.ToStationCode);
                        if (stationManger == null) throw new Exception($"任务{item.TaskCode}出库目标操作台{item.TaskDescribe.ToStationCode}不存在");
                        Dt_Task? taskOld = taskOlds.FirstOrDefault(x=>x.PalletCode==item.TaskDescribe.ContainerCode);
                        Dt_Task? taskOld = taskOlds.FirstOrDefault(x => x.PalletCode == item.TaskDescribe.ContainerCode);
                        if (taskOld != null)
                        {
                            errorMsg += $"料箱{taskOld.PalletCode}" + (taskOld.TaskType == TaskTypeEnum.Inbound.ObjToInt() ? "入库任务已存在;" : "出库任务已存在;");
@@ -147,7 +147,7 @@
                            errorMsg += $"料箱{item.TaskDescribe.ContainerCode}不存在;";
                            content.FailData.Add(new BinCodeObj() { Bincode = item.TaskDescribe.ContainerCode });
                            continue;
                        };
                        }
                        Dt_LocationInfo? noOutLocation = locationInfos.FirstOrDefault(x => (x.LocationStatus != LocationStatusEnum.InStock.ObjToInt() || x.EnableStatus != EnableStatusEnum.Normal.ObjToInt()) && x.PalletCode == item.TaskDescribe.ContainerCode);
                        if (noOutLocation != null)
                        {
@@ -171,7 +171,7 @@
                    locationInfos.ForEach(x =>
                    {
                        x.LocationStatus=LocationStatusEnum.Lock.ObjToInt();
                        x.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                    });
                    //添加任务更新货位数据
                    _unitOfWorkManage.BeginTran();
@@ -179,11 +179,11 @@
                    _locationInfoRepository.UpdateData(locationInfos);
                    _unitOfWorkManage.CommitTran();
                    _taskExecuteDetailService.AddTaskExecuteDetail(tasks.Select(x => x.TaskNum).ToList(), "接收WMS任务");
                    if (tasks.Count>0)
                    if (tasks.Count > 0)
                    {
                        _locationStatusChangeRecordService.AddLocationStatusChangeRecord(locationInfos, LocationStatusEnum.InStock, LocationStatusEnum.Lock, LocationChangeType.OutboundAssignLocation, tasks);
                    }
                    content.OK(errorMsg.IsNullOrEmpty()? "成功": errorMsg);
                    content.OK(errorMsg.IsNullOrEmpty() ? "成功" : errorMsg);
                }
            }
            catch (Exception ex)
@@ -192,7 +192,7 @@
            }
            return content;
        }
        /// <summary>
        /// å®¹å™¨å…¥åº“创建任务
        /// </summary>
@@ -206,7 +206,7 @@
                Dt_LocationInfo? locationInfo = locationInfos.FirstOrDefault(x => x.PalletCode == containerFlowDTO.ContainerCode);
                if (locationInfo != null) throw new Exception($"库位料箱号{containerFlowDTO.ContainerCode}已存在");
                Dt_Task taskOld = BaseDal.QueryFirst(x => x.PalletCode == containerFlowDTO.ContainerCode);
                if (taskOld !=null && taskOld.PalletCode == containerFlowDTO.ContainerCode && taskOld.TaskType == TaskTypeEnum.Inbound.ObjToInt() && taskOld.TaskState == TaskStatusEnum.CL_Executing.ObjToInt() && taskOld.DeviceCode == deviceCode)
                if (taskOld != null && taskOld.PalletCode == containerFlowDTO.ContainerCode && taskOld.TaskType == TaskTypeEnum.Inbound.ObjToInt() && taskOld.TaskState == TaskStatusEnum.CL_Executing.ObjToInt() && taskOld.DeviceCode == deviceCode)
                {
                    Thread.Sleep(500);
                    return content.OK();
@@ -243,7 +243,7 @@
                task.Roadway = noInLocation.RoadwayNo;
                task.DeviceCode = stationManger.StationDeviceCode;
                task.TaskState = TaskStatusEnum.CL_Executing.ObjToInt();
                //添加任务
                int taskId = BaseDal.AddData(task);
                if (type > 0)
@@ -466,14 +466,14 @@
        /// ç”³è¯·å…¥åº“
        /// </summary>
        /// <returns></returns>
        public WebResponseContent RequestInTask(string stationCode,string barCode)
        public WebResponseContent RequestInTask(string stationCode, string barCode)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                lock (lock_requestInTask)
                {
                    Dt_Task task = BaseDal.QueryFirst(x => x.PalletCode == barCode && x.TaskType==TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress == stationCode && x.TaskState == TaskStatusEnum.CL_Executing.ObjToInt());
                    Dt_Task task = BaseDal.QueryFirst(x => x.PalletCode == barCode && x.TaskType == TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress == stationCode && x.TaskState == TaskStatusEnum.CL_Executing.ObjToInt());
                    if (task == null) throw new Exception($"{barCode}料箱未找到任务!");
                    Dt_LocationInfo? locationInfo = _locationInfoService.AssignLocation();
                    if (locationInfo == null) throw new Exception($"可用货位不足!");
@@ -639,7 +639,7 @@
            return content;
        }
        /// <summary>
        /// æŽ¥å—WMS手动完成任务
        /// </summary>
@@ -669,7 +669,7 @@
        /// <returns></returns>
        public WebResponseContent AgvTaskFlow(string code)
        {
            WebResponseContent content=new WebResponseContent();
            WebResponseContent content = new WebResponseContent();
            try
            {
                WriteLog.Write_Log("AGV任务放行", "AGV任务放行接口", "料箱号", $"料箱:{code}");
@@ -697,7 +697,7 @@
        /// WMS料箱到达拣选位上报
        /// </summary>
        /// <returns></returns>
        public WebResponseContent WMSPickUp(string stationCode,string pickCode)
        public WebResponseContent WMSPickUp(string stationCode, string pickCode)
        {
            WebResponseContent content = new WebResponseContent();
            try
@@ -737,7 +737,7 @@
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
                if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)//出库任务完成逻辑
                {
                    Dt_LocationInfo locationInfo = _locationInfoRepository.QueryFirst(x=>x.PalletCode==task.PalletCode);
                    Dt_LocationInfo locationInfo = _locationInfoRepository.QueryFirst(x => x.PalletCode == task.PalletCode);
                    if (locationInfo.LocationStatus != LocationStatusEnum.Lock.ObjToInt())
                    {
                        return content.Error($"{locationInfo.LocationCode}货位状态不正确");
@@ -772,9 +772,9 @@
                    string response = HttpHelper.Post(url, request);
                    WriteLog.Write_Log("WMS出库任务完成回传", "任务完成接口", "任务信息", $"请求:{request},回传:{response}");
                    WMSResponseContent wMSResponse = JsonConvert.DeserializeObject<WMSResponseContent>(response) ?? throw new Exception($"{taskNum},未接收到WMS出库上报返回值");
                    if (wMSResponse.Code != "0") content.Message=$"出库任务{task.TaskNum}WMS出库上报错误,信息:{wMSResponse.Msg}";
                    if (wMSResponse.Code != "0") content.Message = $"出库任务{task.TaskNum}WMS出库上报错误,信息:{wMSResponse.Msg}";
                }
                else if(task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//入库任务完成逻辑
                else if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//入库任务完成逻辑
                {
                    string? url = _apiInfoRepository.QueryFirst(x => x.ApiCode == APIEnum.WMSInOutBoundBack.ToString())?.ApiAddress;
                    if (string.IsNullOrEmpty(url))
@@ -834,14 +834,14 @@
                WriteLog.Write_Log("任务取消接收", "人工手动取消指定任务", "任务号", $"任务:{taskNum}");
                // 1. èŽ·å–ä»»åŠ¡ä¿¡æ¯
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
                if (task == null)return content.Error($"任务{taskNum}不存在");
                if (task == null) return content.Error($"任务{taskNum}不存在");
                TaskTypeGroup group = task.TaskType.GetTaskTypeGroup();
                if (group == TaskTypeGroup.InboundGroup)// å…¥åº“任务取消
                {
                    // èŽ·å–ç›®æ ‡è´§ä½
                    Dt_LocationInfo locationInfo = _locationInfoRepository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                    if (locationInfo == null)return content.Error($"目标货位{task.TargetAddress}不存在");
                    if (locationInfo == null) return content.Error($"目标货位{task.TargetAddress}不存在");
                    // éªŒè¯è´§ä½çŠ¶æ€
                    if (locationInfo.LocationStatus != LocationStatusEnum.Lock.ObjToInt())
                        return content.Error($"{task.TargetAddress}货位状态异常,无法取消");
@@ -1067,12 +1067,12 @@
                {
                    AgvTaskCancelDTO agvTaskCancel = new AgvTaskCancelDTO()
                    {
                        RequestId= DateTime.Now.ToString("yyMMddHHmmssfff"),
                        MissionCode=item.GroupId,
                        ContainerCode=item.PalletCode,
                        Position="",
                        CancelMode= "CTU_REDIRECT_START",
                        Reason=""
                        RequestId = DateTime.Now.ToString("yyMMddHHmmssfff"),
                        MissionCode = item.GroupId,
                        ContainerCode = item.PalletCode,
                        Position = "",
                        CancelMode = "CTU_REDIRECT_START",
                        Reason = ""
                    };
                    AgvCancelTask(agvTaskCancel);
                }
@@ -1243,5 +1243,46 @@
            return content;
        }
        public WebResponseContent WmsRecovery()
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                // åˆ é™¤ç¬¬ä¸€ä¸ªå¼‚常
                // æŒ‰åˆ›å»ºæ—¶é—´å‡åºèŽ·å–ç¬¬ä¸€æ¡è®°å½•
                var orderBy = new Dictionary<string, OrderByType>
                {
                    { "CreateDate", OrderByType.Asc }
                };
                Dt_ErrorInfo errorInfo = _errorInfoRepository.QueryFirst(x => true, orderBy);
                if (errorInfo == null)
                {
                    return content.Error("当前没有异常任务");
                }
                // èŽ·å–ç¬¬ä¸€ä¸ªå¼‚å¸¸ï¼ˆæŒ‰åˆ›å»ºæ—¶é—´æŽ’åºï¼‰
                _unitOfWorkManage.BeginTran();
                _errorInfoRepository.DeleteData(errorInfo);
                _unitOfWorkManage.CommitTran();
                // ä¸ŠæŠ¥æ¢å¤
                string? apiErrorBack = _apiInfoRepository.QueryFirst(x => x.ApiCode == APIEnum.WMSErrorBack.ToString())?.ApiAddress;
                if (string.IsNullOrEmpty(apiErrorBack)) throw new Exception($"未找到WMS故障上报,请检查接口配置");
                TaskError taskError = new TaskError()
                {
                    MsgID = errorInfo.Id,
                    StationCode = errorInfo.StationCode,
                    MsgCode = 0,
                    Msg = "恢复"
                };
                string reqErrorBack = JsonConvert.SerializeObject(taskError, settings);
                HttpHelper.Post(apiErrorBack, reqErrorBack);
                return content.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return content.Error(ex.Message);
                throw new Exception(ex.Message);
            }
        }
    }
}