1
HuBingJie
2025-11-22 51af2794f47d5b68496edbc09d0ccb168347f02b
ÏîÄ¿´úÂë/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
@@ -1,4 +1,4 @@
#region << ç‰ˆ æœ¬ æ³¨ é‡Š >>
#region << ç‰ˆ æœ¬ æ³¨ é‡Š >>
/*----------------------------------------------------------------
 * å‘½åç©ºé—´ï¼šWIDESEA_TaskInfoService
 * åˆ›å»ºè€…:胡童庆
@@ -21,6 +21,7 @@
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SqlSugar;
using System.Diagnostics;
using System.DirectoryServices.Protocols;
using System.Net;
using System.Reflection.Emit;
@@ -167,9 +168,6 @@
                    Dt_StockInfo dt_Stock = _stockRepository.QueryFirst(x => x.LocationCode == ShallowLocation.LocationCode);
                    if (dt_Stock == null) return content.Error($"货位编号:{Locatask}的浅货位库存异常,请检查!!!");
                    Dt_Task _Task = BaseDal.QueryFirst(x => x.SourceAddress == ShallowLocation.LocationCode || x.TargetAddress == ShallowLocation.LocationCode);
                    if (_Task != null) return content.Error($"货位编号:{Locatask}的浅货位库已有任务,不可进行移库");
                    //进行获取新的库位
                    Dt_LocationInfo? Nextlocation = _basicService.LocationInfoService.GetLocation(DeepLocation.RoadwayNo);//获取到新库位
                    if (Nextlocation == null)
@@ -196,11 +194,11 @@
                        NextAddress = Nextlocation.LocationCode,
                        Grade = 1,
                        Creater = "WMS",
                        Depth = ShallowLocation.Depth,
                        Depth = Nextlocation.Depth,
                        CreateDate = DateTime.Now,
                    };
                    _locationInfoRepository.AddData(loca);
                    _locationInfoRepository.UpdateData(loca);
                    BaseDal.AddData(dt_Task);   //新增任务
                    _unitOfWorkManage.CommitTran();
@@ -269,13 +267,15 @@
                BaseDal.DeleteData(task);
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateTypeEnum.自动完成 : OperateTypeEnum.人工完成);
                WebResponseContent content=MES_TransferCompletionFeedback(task);
                WebResponseContent content = MES_TransferCompletionFeedback(task);
                //content.Status = true;
                if (!content.Status)
                {
                    _unitOfWorkManage.RollbackTran();
                    return content;
                }
                _unitOfWorkManage.CommitTran();
                return webResponse.OK();
            }
@@ -320,16 +320,20 @@
                _locationInfoRepository.UpdateData(dt_LocationInfo);
                //修改库存状态
                _stockRepository.UpdateData(stockInfo);
                task.TaskStatus = (int)TaskInStatusEnum.InFinish;
                //删除任务添加历史
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId > 0 ? OperateTypeEnum.人工完成 : OperateTypeEnum.自动完成);
                /*WebResponseContent content = TaskCompletionFeedback(task);
                WebResponseContent content = TaskCompletionFeedback(task);
                //content.Status = true;
                if (!content.Status)
                {
                    _unitOfWorkManage.RollbackTran();
                    return content;
                }*/
                }
                //Thread.Sleep(5000);
                _unitOfWorkManage.CommitTran();
            }
@@ -370,13 +374,15 @@
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateTypeEnum.自动完成 : OperateTypeEnum.人工完成);
                _locationStatusChangeRecordService.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockStatusEmun.出库完成.ObjToInt(), "1", task.TaskNum);
                /*WebResponseContent content = TaskCompletionFeedback(task);
                WebResponseContent content = TaskCompletionFeedback(task);
                //content.Status = true;
                if (!content.Status)
                {
                    _unitOfWorkManage.RollbackTran();
                    return content;
                }*/
                }
                //Thread.Sleep(5000);
                _unitOfWorkManage.CommitTran();
                return webResponse.OK();
            }
@@ -407,17 +413,26 @@
            try
            {
                string locationCode = null;
                if (dt_Task.TaskType == (int)TaskOutboundTypeEnum.Outbound)
                {
                    locationCode = dt_Task.SourceAddress;
                }
                else
                {
                    locationCode = dt_Task.TargetAddress;
                }
                TransferRequest transferRequest = new TransferRequest()
                {
                    //业务id,任务id后续进行更改
                    businessId = "1",
                    TaskId = "1",
                    SourceLocationCode = dt_Task.SourceAddress,
                    LocationCode = dt_Task.TargetAddress,
                    PalletCode = dt_Task.PalletCode,
                    Status = "FINISH",
                    ErrorStatus = "NORMAL",
                    ErrorInfo = "",
                    taskId = dt_Task.MEStaskId,   //任务id
                    businessId = dt_Task.MESbusinessId,   //业务id
                    sourceLocationCode = dt_Task.SourceAddress,
                    locationCode = locationCode,
                    palletCode = dt_Task.PalletCode,
                    status = "FINISH",
                    errorStatus = "NORMAL",
                    errorInfo = "",
                };
                //获取接口进行调取
@@ -425,25 +440,27 @@
                string WCSReceiveTaskAPI = ConfigsAPIInfo.ApiAddress + ConfigsAPIInfo.ApiName;
                if (WCSReceiveTaskAPI == null)
                {
                    return webResponse.Error($"出库失败,未配置MES移库完成反馈接口");
                   return webResponse.Error($"出库失败,未配置MES移库完成反馈接口");
                }
                var respon = HttpHelper.Post(WCSReceiveTaskAPI, JsonConvert.SerializeObject(transferRequest));
                if (respon != null)
                {
                    var response = JsonConvert.DeserializeObject<ApiResponse<object>>(respon);
                    if (response.Success)
                    {
                        return webResponse.OK();
                    }
                    else
                    {
                        return webResponse.Error($"调取接口失败,反馈参数原因:{response.Message}");
                    }
                   var response = JsonConvert.DeserializeObject<ApiResponse<object>>(respon);
                   if (response.Success)
                   {
                       return webResponse.OK();
                   }
                   else
                   {
                       return webResponse.Error($"调取接口失败,反馈参数原因:{response.Message}");
                   }
                }
                else
                {
                    return webResponse.Error($"调取接口失败,反馈参数为空");
                   return webResponse.Error($"调取接口失败,反馈参数为空");
                }
                return webResponse.OK();
            }
            catch (Exception ex)
            {
@@ -464,17 +481,26 @@
            try
            {
                TaskNotification transferRequest = new TaskNotification()
                string locationCode = null;
                if (dt_Task.TaskType == (int)TaskOutboundTypeEnum.Outbound)
                {
                    //业务id,任务id后续进行更改
                    TaskId = "1",   //任务id
                    BusinessId = "1",   //业务id
                    PalletCode = dt_Task.PalletCode,
                    SubPalletCode = "",//子托盘编码
                    LocationCode = dt_Task.SourceAddress,
                    ErrorStatus = "NORMAL",
                    ErrorInfo = "",
                };
                    locationCode = dt_Task.SourceAddress;
                }
                else
                {
                    locationCode = dt_Task.TargetAddress;
                }
                TaskNotification transferRequest = new TaskNotification()
                    {
                        //业务id,任务id后续进行更改
                        taskId = dt_Task.MEStaskId,   //任务id
                        businessId = dt_Task.MESbusinessId,   //业务id
                        palletCode = dt_Task.PalletCode,
                        subPalletCode = dt_Task.MESsubPalletCode,//子托盘编码
                        locationCode = locationCode,
                        errorStatus = "NORMAL",
                        errorInfo = "",
                    };
                //获取接口进行调取
                var ConfigsAPIInfo = _dt_ApiInfoService.GetConfigsByAPIInfo(CateGoryConst.CONFIG_MES_IPAddress, SysConfigConst.MESTaskFeedback);      //获取到wcs全部类型的接口
@@ -483,7 +509,14 @@
                {
                    return webResponse.Error($"任务反馈MES失败,未配置MES任务完成反馈接口");
                }
                var respon = HttpHelper.Post(WCSReceiveTaskAPI, JsonConvert.SerializeObject(transferRequest));
                // æ·»åŠ è°ƒè¯•ä¿¡æ¯
                string requestJson = JsonConvert.SerializeObject(transferRequest);
                //Console.WriteLine($"MES反馈接口URL: {WCSReceiveTaskAPI}");
                //Console.WriteLine($"MES反馈请求数据: {requestJson}");
                // ä½¿ç”¨ä¿®å¤åŽçš„PostSync方法(已在PostSync中设置默认请求头)
                var respon = HttpHelper.Post(WCSReceiveTaskAPI, requestJson);
                if (respon != null)
                {
                    var response = JsonConvert.DeserializeObject<ApiResponse<object>>(respon);
@@ -508,7 +541,7 @@
            }
        }
        public ApiResponse<object> MESresponse(string Message, bool Success)
        public ApiResponse<object> MESresponse(string Message, bool Success,int  Code)
        {
            ApiResponse<object> apiResponse = new ApiResponse<object>();
            apiResponse.Message = Message;
@@ -517,8 +550,111 @@
            return apiResponse;
        }
        /// <summary>
        /// å–消任务接口
        /// </summary>
        /// <param name="taskNum">任务号</param>
        /// <returns></returns>
        public WebResponseContent Cancelinventory(int taskNum)
        {
            WebResponseContent webResponse = new WebResponseContent();
            try
            {
                // æŸ¥æ‰¾ä»»åŠ¡
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum && x.TaskStatus == TaskOutStatusEnum.OutFinish.ObjToInt());
                if (task == null)
                {
                    return webResponse.Error($"未找到任务号为【{taskNum}】的任务信息");
                }
                bool res;
                switch (task.TaskType)
                {
                    case (int)TaskInboundTypeEnum.Inbound:
                        res = task.TaskStatus == TaskInStatusEnum.Line_InExecuting.ObjToInt();
                        break;
                    case (int)TaskOutboundTypeEnum.Outbound:
                        res = task.TaskStatus == TaskOutStatusEnum.OutNew.ObjToInt();
                        break;
                    case (int)TaskRelocationTypeEnum.Relocation:
                        res = task.TaskStatus == TaskRelocationStatusEnum.RelocationNew.ObjToInt();
                        break;
                    default:
                        res = false;
                        break;
                }
                if (!res) return WebResponseContent.Instance.Error("任务状态异常");
                Dt_StockInfo stockInfo = _stockRepository.QueryFirst(x => x.PalletCode == task.PalletCode);
                _unitOfWorkManage.BeginTran();
                int stockmsseg;
                int locationmsseg;
                bool taskmsseg;
                if (task.TaskType == TaskOutboundTypeEnum.Outbound.ObjToInt() || task.TaskType == TaskRelocationTypeEnum.Relocation.ObjToInt())
                {
                    stockmsseg = _stockRepository.Db.Updateable<Dt_StockInfo>()
                  .SetColumns(x => x.StockStatus == StockStatusEmun.入库完成.ObjToInt())
                  .Where(x => x.PalletCode == task.PalletCode)
                  .ExecuteCommand();
                    locationmsseg = _locationInfoRepository.Db.Updateable<Dt_LocationInfo>()
          .SetColumns(x => x.LocationStatus == LocationStatusEnum.InStock.ObjToInt())
          .Where(x => x.LocationCode == stockInfo.LocationCode)
          .ExecuteCommand();
                    taskmsseg = BaseDal.DeleteData(task);
                }
                else
                {
                    locationmsseg = _locationInfoRepository.Db.Updateable<Dt_LocationInfo>()
                    .SetColumns(x => x.LocationStatus == LocationStatusEnum.Free.ObjToInt())
                    .Where(x => x.LocationCode == stockInfo.LocationCode)
                    .ExecuteCommand();
                    stockmsseg = _stockRepository.DeleteData(stockInfo).ObjToInt();
                    taskmsseg = BaseDal.DeleteData(task);
                }
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId > 0 ? OperateTypeEnum.人工完成 : OperateTypeEnum.自动完成);
                //获取接口进行调取
                var ConfigsAPIInfo = _dt_ApiInfoService.GetConfigsByAPIInfo(CateGoryConst.CONFIG_MES_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("任务取消成功");
                    }
                }
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return webResponse.Error($"取消任务失败,原因:{ex.Message}");
            }
            _unitOfWorkManage.RollbackTran();
            return webResponse.Error("任务取消失败");
        }
        /// <summary>
        /// åˆ›å»ºè´§ä½
@@ -606,5 +742,95 @@
            }
            return MESresponse($"", true);
        }
        public static string GetLastPart(string locationCode)
        {
            string[] parts = locationCode.Split('-');
            return parts[parts.Length - 1];
        }
        /// <summary>
        /// å‡ºåº“
        /// </summary>
        /// <param name="locationCode"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public WebResponseContent chuku(string locationCode)
        {
            try
            {
                Dt_StockInfo dt_StockInfo =  _stockRepository.QueryFirst(x => x.LocationCode.StartsWith("SC01") && x.StockStatus == 6);
                int result = GetLastPart(dt_StockInfo.LocationCode).ObjToInt(); // è¿”回 "C03"
                List<Dt_Task> dt_s = new List<Dt_Task>();
                //生成移动任务
                Dt_Task dt_Task = new()
                {
                    PalletCode = dt_StockInfo.PalletCode,
                    TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    Roadway = "SC01",
                    TaskType = TaskOutboundTypeEnum.Outbound.ObjToInt(),
                    TaskStatus = TaskOutStatusEnum.OutNew.ObjToInt(),
                    SourceAddress = dt_StockInfo.LocationCode,
                    TargetAddress = "1006",
                    CurrentAddress = dt_StockInfo.LocationCode,
                    NextAddress = "1006",
                    Grade = 1,
                    Creater = "MES",
                    Depth = result,
                    CreateDate = DateTime.Now,
                    MEStaskId = "EXTASK20251020002",
                    MESbusinessId = "EXBUS20251020002",
                    MESsubPalletCode = dt_StockInfo.MESsubPalletCode,
                    OutboundPlatform = "1005"
                };
                ////创建组盘信息
                //var dt_Stock = new Dt_StockInfo
                //{
                //    PalletCode = dt_Task.PalletCode,
                //    PalletType = 1,
                //    LocationCode = locationCode,
                //    StockStatus = 6,
                //    Creater = "WMS",
                //    CreateDate = DateTime.Now,
                //    MESsubPalletCode = dt_Task.MESsubPalletCode,
                //};
                Dt_LocationInfo dt_LocationInfo = _locationInfoRepository.QueryFirst(x => x.LocationCode == dt_StockInfo.LocationCode);
                dt_LocationInfo.LocationStatus = (int)LocationStatusEnum.InStockLock;
                dt_StockInfo.StockStatus = 7;
                bool l = _locationInfoRepository.UpdateData(dt_LocationInfo);
                bool s = _stockRepository.UpdateData(dt_StockInfo);
                int t = BaseDal.AddData(dt_Task);
                bool ss = s.ObjToBool();
                bool tt = t.ObjToBool();
                if (!l && !ss && tt)
                {
                    _unitOfWorkManage.RollbackTran();
                }
                dt_s.Add(dt_Task);
                var respon = PushTasksToWCS(dt_s, "");
                if (respon.Status)
                {
                    _unitOfWorkManage.CommitTran();  //提交事务
                    return WebResponseContent.Instance.OK();
                }
                return WebResponseContent.Instance.Error();
            }
            catch
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error();
            }
        }
    }
}