1
xby-y
2026-01-12 c1eac732fc3ef407d51b76d288f311d2fa43e618
ÏîÄ¿´úÂë/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
@@ -546,7 +546,7 @@
            ApiResponse<object> apiResponse = new ApiResponse<object>();
            apiResponse.Message = Message;
            apiResponse.Success = Success;
            apiResponse.Code = 1;
            apiResponse.Code = Code;
            return apiResponse;
        }
@@ -555,16 +555,16 @@
        /// </summary>
        /// <param name="taskNum">任务号</param>
        /// <returns></returns>
        public WebResponseContent Cancelinventory(int taskNum)
        public ApiResponse<object> Cancelinventory(string palletTaskDTO)
        {
            WebResponseContent webResponse = new WebResponseContent();
            try
            {
                // æŸ¥æ‰¾ä»»åŠ¡
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum && x.TaskStatus == TaskOutStatusEnum.OutFinish.ObjToInt());
                Dt_Task task = BaseDal.QueryFirst(x => x.PalletCode == palletTaskDTO && (x.TaskStatus == TaskOutStatusEnum.OutNew.ObjToInt()|| x.TaskStatus == TaskRelocationStatusEnum.RelocationNew.ObjToInt()|| x.TaskStatus == TaskInStatusEnum.Line_InExecuting.ObjToInt()));
                if (task == null)
                {
                    return webResponse.Error($"未找到任务号为【{taskNum}】的任务信息");
                    return MESresponse($"未找到货位信息,条码:{palletTaskDTO}", false, 0);
                }
                bool res;
@@ -583,7 +583,7 @@
                        res = false;
                        break;
                }
                if (!res) return WebResponseContent.Instance.Error("任务状态异常");
                if (!res) return MESresponse($"任务状态异常", false, 0);
                Dt_StockInfo stockInfo = _stockRepository.QueryFirst(x => x.PalletCode == task.PalletCode);
@@ -598,10 +598,18 @@
                  .SetColumns(x => x.StockStatus == StockStatusEmun.入库完成.ObjToInt())
                  .Where(x => x.PalletCode == task.PalletCode)
                  .ExecuteCommand();
                    if (stockmsseg<=0)
                    {
                        return MESresponse($"无库存信息", false, 0);
                    }
                    locationmsseg = _locationInfoRepository.Db.Updateable<Dt_LocationInfo>()
          .SetColumns(x => x.LocationStatus == LocationStatusEnum.InStock.ObjToInt())
          .Where(x => x.LocationCode == stockInfo.LocationCode)
          .ExecuteCommand();
                    if (locationmsseg <= 0)
                    {
                        return MESresponse($"未找到货位信息,条码:{palletTaskDTO}", false, 0);
                    }
                    taskmsseg = BaseDal.DeleteData(task);
                }
                else
@@ -610,50 +618,83 @@
                    .SetColumns(x => x.LocationStatus == LocationStatusEnum.Free.ObjToInt())
                    .Where(x => x.LocationCode == stockInfo.LocationCode)
                    .ExecuteCommand();
                    if (locationmsseg <= 0)
                    {
                        return MESresponse($"未找到货位信息,条码:{palletTaskDTO}", false, 0);
                    }
                    stockmsseg = _stockRepository.DeleteData(stockInfo).ObjToInt();
                    taskmsseg = BaseDal.DeleteData(task);
                }
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId > 0 ? OperateTypeEnum.人工完成 : OperateTypeEnum.自动完成);
                BaseDal.DeleteAndMoveIntoHty(task,OperateTypeEnum.人工删除);
                //获取接口进行调取
                var ConfigsAPIInfo = _dt_ApiInfoService.GetConfigsByAPIInfo(CateGoryConst.CONFIG_MES_IPAddress, SysConfigConst.WCSCancelinventory);      //获取到wcs全部类型的接口
                var ConfigsAPIInfo = _dt_ApiInfoService.GetConfigsByAPIInfo(CateGoryConst.CONFIG_WCS_IPAddress, SysConfigConst.WCSCancelinventory);      //获取到wcs全部类型的接口
                string WCSReceiveTaskAPI = ConfigsAPIInfo.ApiAddress + ConfigsAPIInfo.ApiName;
                if (WCSReceiveTaskAPI == null)
                {
                    return webResponse.Error($"任务反馈MES失败,未配置MES任务完成反馈接口");
                }
                var respon = HttpHelper.Get(WCSReceiveTaskAPI, JsonConvert.SerializeObject(task.TaskNum));
                if (respon != null)
                {
                    var response = JsonConvert.DeserializeObject<ApiResponse<object>>(respon);
                    //    if (response.Success)
                    //    {
                    //        return webResponse.OK();
                    //    }
                    //    else
                    //    {
                    //        return webResponse.Error($"调取接口失败,反馈参数原因:{response.Message}");
                    //    }
                    //}
                    //else
                    //{
                    //    return webResponse.Error($"调取接口失败,反馈参数为空");
                    //}
                    if (stockmsseg > 0 && locationmsseg > 0 && taskmsseg)
                    {
                        _unitOfWorkManage.CommitTran();
                        return webResponse.OK("任务取消成功");
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"手动取消失败未找到wcs的配置接口", false, 0);
                }
                string apiUrl = $"{WCSReceiveTaskAPI}?taskNum={task.TaskNum}";
                var respon = HttpHelper.Get(apiUrl);
                if (respon == null)
                {
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"wcs调取接口失败返回参数为null", false, 0);
                }
                var response = JsonConvert.DeserializeObject<ApiResponse<object>>(respon);
                if (!response.status)
                {
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"WCS调取接口失败,反馈参数原因:{response.Message}", false, 0);
                }
                if (stockmsseg <= 0 && locationmsseg <= 0 && taskmsseg)
                    {
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"任务取消失败", false, 0);
                    }
                }
                ////获取接口进行调取
                //var MESConfigsAPIInfo = _dt_ApiInfoService.GetConfigsByAPIInfo(CateGoryConst.CONFIG_MES_IPAddress, SysConfigConst.MEScancelTask);      //获取到MES全部类型的接口
                //string MEScancelTask = MESConfigsAPIInfo.ApiAddress + MESConfigsAPIInfo.ApiName;
                //if (MEScancelTask == null)
                //{
                //    _unitOfWorkManage.RollbackTran();
                //    return webResponse.Error($"任务手动取消失败,未配置MES任务手动取消反馈接口");
                //}
                //var MES = new
                //{
                //    businessId = task.MESbusinessId,
                //    taskId = task.MEStaskId,
                //    palletCode = task.PalletCode
                //};
                //var MESrespon = HttpHelper.Post(MEScancelTask, JsonConvert.SerializeObject(MES));
                //if (MESrespon == null)
                //{
                //    _unitOfWorkManage.RollbackTran();
                //    return webResponse.Error($"MES调取接口失败,反馈参数为空");
                //}
                //var MESresponse = JsonConvert.DeserializeObject<ApiResponse<object>>(MESrespon);
                //if (!MESresponse.Success)
                //{
                //    _unitOfWorkManage.RollbackTran();
                //    return webResponse.Error($"MES调取接口失败,反馈参数原因:{response.Message}");
                //}
                _unitOfWorkManage.CommitTran();
                return MESresponse($"任务取消成功", true, 1);
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return webResponse.Error($"取消任务失败,原因:{ex.Message}");
                return  MESresponse($"取消任务失败,原因:{ex.Message}", false, 0);
            }
            _unitOfWorkManage.RollbackTran();
            return webResponse.Error("任务取消失败");
        }
        /// <summary>
@@ -832,5 +873,234 @@
            }
        }
        /// <summary>
        /// æ‰‹åŠ¨å®ŒæˆæŽ¥å£
        /// </summary>
        /// <param name="taskNum"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResponse<object> ManualTaskCompleted(string palletTaskDTO)
        {
            WebResponseContent webResponse = new WebResponseContent();
            try
            {
                // æŸ¥æ‰¾ä»»åŠ¡
                Dt_Task task = BaseDal.QueryFirst(x => x.PalletCode == palletTaskDTO);
                if (task == null)
                {
                    return MESresponse($"未找到托盘编码为【{palletTaskDTO}】的任务信息", false, 0);
                }
                Dt_StockInfo stockInfo = _stockRepository.QueryFirst(x => x.PalletCode == task.PalletCode);
                _unitOfWorkManage.BeginTran();
                Dt_StockInfo stockmsseg;
                Dt_LocationInfo locationmsseg1 = null;
                Dt_LocationInfo locationmsseg2 = null;
                bool taskmsseg;
                bool task_htymsseg;
                bool location1;
                bool location2 = false;
                bool resstock;
                if (task.TaskType == TaskInboundTypeEnum.Inbound.ObjToInt() || task.TaskType == TaskRelocationTypeEnum.Relocation.ObjToInt())
                {
                    stockmsseg = _stockRepository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                    if (stockmsseg == null) { return MESresponse($"未找到库存信息", false, 0); }
                    ;
                    stockmsseg.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                     resstock = _stockRepository.UpdateData(stockmsseg);
                    if (task.TaskType == TaskRelocationTypeEnum.Relocation.ObjToInt())
                    {
                        locationmsseg1 = _locationInfoRepository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                        locationmsseg2 = _locationInfoRepository.QueryFirst(x => x.LocationCode == task.CurrentAddress);
                        if (locationmsseg1 == null || locationmsseg2 == null) { return MESresponse($"未找到货位信息", false, 0); }
                        locationmsseg1.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
                        locationmsseg2.LocationStatus = LocationStatusEnum.Free.ObjToInt();
                        location1 = _locationInfoRepository.UpdateData(locationmsseg1);
                        location2 = _locationInfoRepository.UpdateData(locationmsseg2);
                    }
                    else
                    {
                        locationmsseg1 = _locationInfoRepository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                        if (locationmsseg1 == null) { return MESresponse($"未找到货位信息", false, 0); }
                        locationmsseg1.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
                        location1 = _locationInfoRepository.UpdateData(locationmsseg1);
                    }
                }
                else
                {
                    stockmsseg = _stockRepository.QueryFirst(x => x.LocationCode == task.CurrentAddress);
                    if (stockmsseg == null) { return MESresponse($"未找到库存信息", false, 0); }
                    resstock = _stockRepository.DeleteData(stockmsseg);
                    locationmsseg1 = _locationInfoRepository.QueryFirst(x => x.LocationCode == task.CurrentAddress);
                    if (locationmsseg1 == null) { return MESresponse($"未找到货位信息", false, 0); }
                    locationmsseg1.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
                    location1 = _locationInfoRepository.UpdateData(locationmsseg1);
                }
                taskmsseg = BaseDal.DeleteData(task);
                task_htymsseg = BaseDal.DeleteAndMoveIntoHty(task, OperateTypeEnum.人工完成);
                //获取接口进行调取
                var ConfigsAPIInfo = _dt_ApiInfoService.GetConfigsByAPIInfo(CateGoryConst.CONFIG_WCS_IPAddress, SysConfigConst.WCSManualTaskCompleted);      //获取到wcs全部类型的接口
                string WCSReceiveTaskAPI = ConfigsAPIInfo.ApiAddress + ConfigsAPIInfo.ApiName;
                if (WCSReceiveTaskAPI == null)
                {
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"任务手动完成失败,未配置WCS任务手动完成反馈接口", false, 0);
                }
                string apiUrl = $"{WCSReceiveTaskAPI}?taskNum={task.TaskNum}";
                var respon = HttpHelper.Get(apiUrl);
                if (respon == null)
                {
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"WCS调取接口失败,反馈参数为空", false, 0);
                }
                var response = JsonConvert.DeserializeObject<ApiResponse<object>>(respon);
                if (!response.status)
                {
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"WCS调取接口失败,反馈参数原因:{response.Message}", false, 0);
                }
                if (!(resstock && task_htymsseg && taskmsseg && task.TaskType == TaskRelocationTypeEnum.Relocation.ObjToInt() ? (location1 && location2) : location1))
                    {
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"任务手动完成失败", false, 0);
                }
                ////获取接口进行调取
                //var MESConfigsAPIInfo = _dt_ApiInfoService.GetConfigsByAPIInfo(CateGoryConst.CONFIG_MES_IPAddress, SysConfigConst.MESmockComplete);      //获取到wcs全部类型的接口
                //string MESmockComplete = MESConfigsAPIInfo.ApiAddress + MESConfigsAPIInfo.ApiName;
                //if (MESConfigsAPIInfo == null)
                //{
                //    _unitOfWorkManage.RollbackTran();
                //    return webResponse.Error($"任务手动完成失败,未配置MES任务手动完成反馈接口");
                //}
                //var MES = new
                //{
                //    businessId = task.MESbusinessId,
                //    taskId = task.MEStaskId,
                //    palletCode = task.PalletCode
                //};
                //var MESrespon = HttpHelper.Post(MESmockComplete, JsonConvert.SerializeObject(MES));
                //if (respon == null)
                //{
                //    _unitOfWorkManage.RollbackTran();
                //    return webResponse.Error($"MES调取接口失败,反馈参数为空");
                //}
                //var MESresponse = JsonConvert.DeserializeObject<ApiResponse<object>>(MESrespon);
                //if (!MESresponse.Success)
                //{
                //    _unitOfWorkManage.RollbackTran();
                //    return webResponse.Error($"MES调取接口失败,反馈参数原因:{response.Message}");
                //}
                WebResponseContent content;
                switch (task.TaskType)
                {
                    case (int)TaskRelocationTypeEnum.Relocation:
                        content = MES_TransferCompletionFeedback(task);
                        break;
                    case (int)TaskInboundTypeEnum.Inbound:
                        content = TaskCompletionFeedback(task);
                        break;
                    case (int)TaskOutboundTypeEnum.Outbound:
                        content = TaskCompletionFeedback(task);
                        break;
                    default: return MESresponse($"任务类型错误,任务号:{task.TaskNum}", false, 0);
                }
                if (!content.Status)
                {
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"手动向MES接口报完成失败{task.TaskNum}", false, 0);
                }
                _unitOfWorkManage.CommitTran();
                return MESresponse($"任务手动完成成功{task.TaskNum}", true, 1);
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return MESresponse($"任务手动完成失败,原因:{ex.Message}", false, 0);
            }
        }
        /// <summary>
        /// èŽ·å–å®žæ—¶ä»»åŠ¡åˆ—è¡¨
        /// </summary>
        /// <returns></returns>
        public WebResponseContent GetRealTimeTasks()
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                // æŸ¥è¯¢æœ€æ–°çš„10条任务记录
                var tasks = Repository.Db.Queryable<Dt_Task>()
                    .OrderByDescending(x => x.CreateDate)
                    .Take(10)
                    .ToList();
                // è½¬æ¢ä»»åŠ¡æ•°æ®ä¸ºå‰ç«¯éœ€è¦çš„æ ¼å¼
                var result = tasks.Select(task => {
                    // è½¬æ¢ä»»åŠ¡ç±»åž‹
                    string taskTypeText = "未知";
                    switch ((TaskInboundTypeEnum)task.TaskType)
                    {
                        case TaskInboundTypeEnum.Inbound:
                            taskTypeText = "入库";
                            break;
                        case (TaskInboundTypeEnum)TaskOutboundTypeEnum.Outbound:
                            taskTypeText = "出库";
                            break;
                        case (TaskInboundTypeEnum)TaskRelocationTypeEnum.Relocation:
                            taskTypeText = "移库";
                            break;
                    }
                    // è½¬æ¢ä»»åŠ¡çŠ¶æ€
                        string taskStatusText = "未知";
                        if ((TaskOutStatusEnum)task.TaskStatus == TaskOutStatusEnum.OutNew) {
                            taskStatusText = "待处理";
                        } else if (
                            (TaskInStatusEnum)task.TaskStatus == TaskInStatusEnum.Line_InExecuting ||
                            (TaskInStatusEnum)task.TaskStatus == TaskInStatusEnum.SC_InExecuting ||
                            (TaskOutStatusEnum)task.TaskStatus == TaskOutStatusEnum.SC_OutExecuting ||
                            (TaskOutStatusEnum)task.TaskStatus == TaskOutStatusEnum.Line_OutExecuting
                        ) {
                            taskStatusText = "运行中";
                        } else if (
                            (TaskInStatusEnum)task.TaskStatus == TaskInStatusEnum.InFinish ||
                            (TaskOutStatusEnum)task.TaskStatus == TaskOutStatusEnum.OutFinish
                        ) {
                            taskStatusText = "已完成";
                        }
                        return new {
                            id = task.TaskId,
                            taskNum = task.TaskNum,
                            taskType = taskTypeText,
                            sourceAddress = task.SourceAddress,
                            targetAddress = task.TargetAddress,
                            taskStatus = taskStatusText
                        };
                }).ToList();
                return content.OK(data: result);
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
    }
}