wangxinhui
2 天以前 37051424de7c4a97132fbb06e45df594790aabf9
ÏîÄ¿´úÂë/WCSServices/WIDESEAWCS_TaskInfoService/TaskService.cs
@@ -24,8 +24,10 @@
using System.Linq;
using System.Net.Http.Headers;
using System.Security.Policy;
using System.Threading.Tasks;
using WIDESEA_DTO.Agv;
using WIDESEA_External.Model;
using WIDESEAWCS_BasicInfoService;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.APIEnum;
using WIDESEAWCS_Common.TaskEnum;
@@ -63,6 +65,7 @@
        private readonly IApiInfoRepository _apiInfoRepository;
        private readonly ILocationInfoRepository _locationInfoRepository;
        private readonly ILocationInfoService _locationInfoService;
        private readonly ILocationStatusChangeRecordService _locationStatusChangeRecordService;
        private Dictionary<string, OrderByType> _taskOrderBy = new()
            {
@@ -80,7 +83,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) : 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) : base(BaseDal)
        {
            _mapper = mapper;
            _cacheService = cacheService;
@@ -93,6 +96,7 @@
            _locationInfoRepository = locationInfoRepository;
            _unitOfWorkManage = unitOfWorkManage;
            _locationInfoService = locationInfoService;
            _locationStatusChangeRecordService=locationStatusChangeRecordService;
        }
        static object lock_taskReceive = new object();
        /// <summary>
@@ -100,22 +104,17 @@
        /// </summary>
        /// <param name="taskDTOs">WMS任务对象集合</param>
        /// <returns>返回处理结果</returns>
        public WebResponseContent ReceiveWMSTask([NotNull] WMSTaskDTO taskDTO)
        public WMSReceiveTaskContent ReceiveWMSTask([NotNull] WMSTaskDTO taskDTO)
        {
            WebResponseContent content = new WebResponseContent();
            WMSReceiveTaskContent content = new WMSReceiveTaskContent();
            string errorMsg = "";
            try
            {
                lock (lock_taskReceive)
                {
                    List<Dt_Task> tasks = new List<Dt_Task>();
                    Dt_Task taskOld = BaseDal.QueryFirst(x=> taskDTO.Tasks.Select(x => x.TaskDescribe.ContainerCode).Contains(x.PalletCode));
                    if (taskOld != null) throw new Exception($"料箱{taskOld.PalletCode}"+(taskOld.TaskType == TaskTypeEnum.Inbound.ObjToInt() ? "入库任务已存在" : "出库任务已存在"));
                    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());
                    Dt_LocationInfo? noOutLocation = locationInfos.FirstOrDefault(x=>x.LocationStatus != LocationStatusEnum.InStock.ObjToInt() || x.EnableStatus != EnableStatusEnum.Normal.ObjToInt());
                    if (noOutLocation != null) throw new Exception($"料箱{noOutLocation.PalletCode}货位{noOutLocation.LocationCode}状态不可出库");
                    List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData();
                    //下发任务组
                    string taskGroup= taskDTO.TaskGroupCode.IsNullOrEmpty() ? Guid.NewGuid().ToString().Replace("-","") : taskDTO.TaskGroupCode;
@@ -125,16 +124,28 @@
                        //获取操作台
                        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);
                        if (taskOld != null)
                        {
                            errorMsg += $"料箱{taskOld.PalletCode}" + (taskOld.TaskType == TaskTypeEnum.Inbound.ObjToInt() ? "入库任务已存在;" : "出库任务已存在;");
                            content.FailData.Add(new BinCodeObj() { Bincode = item.TaskDescribe.ContainerCode });
                            continue;
                        }
                        Dt_LocationInfo? locationInfo = locationInfos.FirstOrDefault(x => x.PalletCode == item.TaskDescribe.ContainerCode);
                        if (locationInfo == null)
                        {
                            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)
                        {
                            errorMsg += $"料箱{noOutLocation.PalletCode}货位{noOutLocation.LocationCode}状态不可出库";
                            content.FailData.Add(new BinCodeObj() { Bincode = noOutLocation.PalletCode });
                            continue;
                        }
                        Dt_Task task = _mapper.Map<Dt_Task>(item);
                        task.SourceAddress = locationInfo.LocationCode;
                        task.CurrentAddress = locationInfo.LocationCode;
                        task.NextAddress = stationManger.PickStationCode;
@@ -145,6 +156,7 @@
                        task.DeviceCode = stationManger.CraneCode;
                        task.TaskState = TaskStatusEnum.AGV_Execute.ObjToInt();
                        tasks.Add(task);
                        content.SucessData.Add(new BinCodeObj() { Bincode = item.TaskDescribe.ContainerCode });
                    }
                    locationInfos.ForEach(x =>
@@ -156,10 +168,12 @@
                    BaseDal.AddData(tasks);
                    _locationInfoRepository.UpdateData(locationInfos);
                    _unitOfWorkManage.CommitTran();
                    _taskExecuteDetailService.AddTaskExecuteDetail(tasks.Select(x => x.TaskNum).ToList(), "接收WMS任务");
                    content = tasks.Count > 0 ? content.OK("成功!"+(errorMsg.IsNullOrEmpty()? "": errorMsg)) : content.Error("失败");
                    if (tasks.Count>0)
                    {
                        _locationStatusChangeRecordService.AddLocationStatusChangeRecord(locationInfos, LocationStatusEnum.InStock, LocationStatusEnum.Lock, LocationChangeType.OutboundAssignLocation, tasks);
                    }
                    content.OK(errorMsg.IsNullOrEmpty()? "成功": errorMsg);
                }
            }
            catch (Exception ex)
@@ -168,46 +182,156 @@
            }
            return content;
        }
        static object lock_containerFlow = new object();
        /// <summary>
        /// å®¹å™¨å…¥åº“创建任务
        /// </summary>
        /// <returns></returns>
        public WebResponseContent ContainerFlow(ContainerFlowDTO containerFlowDTO, string deviceCode)
        public WebResponseContent ContainerFlow(ContainerFlowDTO containerFlowDTO, string deviceCode, string stationCode, int type = 0)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                lock (lock_containerFlow)
                List<Dt_LocationInfo> locationInfos = _locationInfoRepository.QueryData();
                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)
                {
                    List<Dt_LocationInfo> locationInfos = _locationInfoRepository.QueryData();
                    Dt_LocationInfo? locationInfo = locationInfos.FirstOrDefault(x=>x.PalletCode== containerFlowDTO.ContainerCode);
                    if (locationInfo != null) throw new Exception($"库位料箱号{containerFlowDTO.ContainerCode}已存在");
                    if (BaseDal.QueryFirst(x=>x.PalletCode==containerFlowDTO.ContainerCode)!=null) throw new Exception($"料箱号{containerFlowDTO.ContainerCode}任务已存在");
                    Dt_LocationInfo? noInLocation = locationInfos.FirstOrDefault(x => x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && x.EnableStatus == EnableStatusEnum.Normal.ObjToInt());
                    if (noInLocation == null) throw new Exception($"可用货位不足!");
                    Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.StationType == StationTypeEnum.StationType_OnlyInbound.ObjToInt() && x.StationDeviceCode == deviceCode);
                    //创建任务
                    Dt_Task task = new Dt_Task();
                    task.PalletCode = containerFlowDTO.ContainerCode;
                    task.SourceAddress = containerFlowDTO.SlotCode;
                    task.CurrentAddress = containerFlowDTO.SlotCode;
                    task.NextAddress = stationManger.StationCode;
                    task.TargetAddress = "";
                    task.WMSId = "";
                    task.TaskType = TaskTypeEnum.Inbound.ObjToInt();
                    task.Roadway = noInLocation.RoadwayNo;
                    task.DeviceCode = stationManger.StationDeviceCode;
                    task.TaskState = TaskStatusEnum.CL_Executing.ObjToInt();
                    //添加任务
                    BaseDal.AddData(task);
                    _taskExecuteDetailService.AddTaskExecuteDetail(new List<int>() { task.TaskNum }, "创建入库任务");
                    content.OK("成功");
                    return content.OK();
                }
                if (taskOld != null && taskOld.PalletCode == containerFlowDTO.ContainerCode && taskOld.TaskType == TaskTypeEnum.Inbound.ObjToInt() && taskOld.TaskState == TaskStatusEnum.CL_Executing.ObjToInt() && taskOld.DeviceCode != deviceCode)
                {
                    Dt_StationManger stationOld = _stationMangerRepository.QueryFirst(x => x.StationType == StationTypeEnum.StationType_OnlyInbound.ObjToInt() && x.StationDeviceCode == deviceCode);
                    string oldSlotCode = taskOld.SourceAddress;
                    taskOld.SourceAddress = containerFlowDTO.SlotCode;
                    taskOld.CurrentAddress = containerFlowDTO.SlotCode;
                    taskOld.NextAddress = stationOld.StationCode;
                    taskOld.DeviceCode = stationOld.StationDeviceCode;
                    //更新任务
                    BaseDal.UpdateData(taskOld);
                    _taskExecuteDetailService.AddTaskExecuteDetail(new List<int>() { taskOld.TaskNum }, $"{oldSlotCode}换至{containerFlowDTO.SlotCode}入库");
                    return content.OK();
                }
                else if(taskOld != null)
                {
                    throw new Exception($"料箱号{containerFlowDTO.ContainerCode}" + (taskOld.TaskType == TaskTypeEnum.Inbound.ObjToInt() ? "入库AGV执行中": "出库AGV执行中"));
                }
                Dt_LocationInfo? noInLocation = locationInfos.FirstOrDefault(x => x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && x.EnableStatus == EnableStatusEnum.Normal.ObjToInt());
                if (noInLocation == null) throw new Exception($"可用货位不足!");
                Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.StationType == StationTypeEnum.StationType_OnlyInbound.ObjToInt() && x.StationDeviceCode == deviceCode);
                //创建任务
                Dt_Task task = new Dt_Task();
                task.PalletCode = containerFlowDTO.ContainerCode;
                task.SourceAddress = containerFlowDTO.SlotCode;
                task.CurrentAddress = containerFlowDTO.SlotCode;
                task.NextAddress = stationManger.StationCode;
                task.TargetAddress = "";
                task.WMSId = "";
                task.TaskType = TaskTypeEnum.Inbound.ObjToInt();
                task.Roadway = noInLocation.RoadwayNo;
                task.DeviceCode = stationManger.StationDeviceCode;
                task.TaskState = TaskStatusEnum.CL_Executing.ObjToInt();
                //添加任务
                BaseDal.AddData(task);
                if (type > 0)
                {
                    _taskExecuteDetailService.AddTaskExecuteDetail(new List<int>() { task.TaskNum }, "手动按钮入库");
                }
                else
                {
                    _taskExecuteDetailService.AddTaskExecuteDetail(new List<int>() { task.TaskNum }, "创建入库任务");
                }
                content.OK("成功");
            }
            catch (Exception ex)
            {
                content.Error($"错误信息:{ex.Message}");
            }
            return content;
        }
        /// <summary>
        /// æ’­ç§å¢™åˆå§‹åŒ–
        /// </summary>
        /// <returns></returns>
        public WebResponseContent InitLight()
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                EPLightContent lightContent = INITIALIZATION();
                if (lightContent.Result != "0") throw new Exception($"{lightContent.Msg}");
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// ç»“束作业
        /// </summary>
        /// <returns></returns>
        public WebResponseContent EndLight()
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                EPLightContent lightContent = ENDWORK();
                if (lightContent.Result != "0") throw new Exception($"{lightContent.Msg}");
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// æ’­ç§å¢™äº®ç¯
        /// </summary>
        /// <returns></returns>
        public WebResponseContent SendLight(TaskSendLight taskSendLight)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (taskSendLight.TagNo=="B2")
                {
                    List<EPLightSendDTO> lightSendDTOs = new List<EPLightSendDTO>()
                    {
                        new EPLightSendDTO
                        {
                            DOCNO = taskSendLight.DocNo,
                            TASKNO= taskSendLight.TaskNo,
                            LOCATION=taskSendLight.TagCode,
                            QUANTITY=taskSendLight.TagQunity,
                            LIGHTCOLOR=taskSendLight.Color switch
                            {
                                "Blue" => "1",
                                "Green" => "2",
                                "Red" => "4",
                                _ => throw new Exception($"未找到颜色定义")
                            },
                            ORDERTYPE=taskSendLight.Mode.ToString(),
                            LIGHTTYPE="1",
                        }
                    };
                    EPLightContent pLightContent = PickOrderInfoRequest(lightSendDTOs);
                    if (pLightContent.Result != "0") throw new Exception($"{pLightContent.Msg}");
                    content.OK();
                }
                else
                {
                    content.OK();
                }
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
@@ -239,7 +363,7 @@
                    _locationInfoRepository.UpdateData(locationInfo);
                    _unitOfWorkManage.CommitTran();
                    _taskExecuteDetailService.AddTaskExecuteDetail(new List<int>() { task.TaskNum }, $"分配货位{locationInfo.LocationCode}");
                    _locationStatusChangeRecordService.AddLocationStatusChangeRecord(locationInfo, LocationStatusEnum.Free, LocationStatusEnum.Lock, LocationChangeType.InboundAssignLocation, task.TaskNum);
                    content.OK("成功");
                }
            }
@@ -505,14 +629,16 @@
                        CompleteType = 1
                    };
                    string request = JsonConvert.SerializeObject(containerInFinishDTO, settings);
                    //调用接口
                    string response = HttpHelper.Post(url, request);
                    WMSResponseContent wMSResponse = JsonConvert.DeserializeObject<WMSResponseContent>(response) ?? throw new Exception($"{taskNum},未接收到WMS出库上报返回值");
                    if (wMSResponse.Code != "0") throw new Exception($"出库任务{task.TaskNum}WMS出库上报错误,信息:{wMSResponse.Msg}");
                    _unitOfWorkManage.BeginTran();
                    _locationInfoRepository.UpdateData(locationInfo);
                    BaseDal.DeleteAndMoveIntoHty(task, App.User?.UserId == 0 ? OperateTypeEnum.自动完成 : OperateTypeEnum.人工完成);
                    _unitOfWorkManage.CommitTran();
                    _locationStatusChangeRecordService.AddLocationStatusChangeRecord(locationInfo, LocationStatusEnum.Lock, LocationStatusEnum.Free, LocationChangeType.OutboundCompleted, task.TaskNum);
                    //调用接口
                    string response = HttpHelper.Post(url, request);
                    WMSResponseContent wMSResponse = JsonConvert.DeserializeObject<WMSResponseContent>(response) ?? throw new Exception($"{taskNum},未接收到WMS出库上报返回值");
                    if (wMSResponse.Code != "0") content.Message=$"出库任务{task.TaskNum}WMS出库上报错误,信息:{wMSResponse.Msg}";
                }
                else if(task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//入库任务完成逻辑
                {
@@ -532,10 +658,7 @@
                        CompleteType = 2
                    };
                    string request = JsonConvert.SerializeObject(containerInFinishDTO, settings);
                    //调用接口
                    string response = HttpHelper.Post(url, request);
                    WMSResponseContent wMSResponse = JsonConvert.DeserializeObject<WMSResponseContent>(response) ?? throw new Exception($"{taskNum},未接收到WMS入库上报返回值");
                    if (wMSResponse.Code != "0") throw new Exception($"入库任务{task.TaskNum}WMS入库上报错误,信息:{wMSResponse.Msg}");
                    Dt_LocationInfo locationInfo = _locationInfoRepository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                    if (locationInfo.LocationStatus != LocationStatusEnum.Lock.ObjToInt())
                    {
@@ -548,6 +671,11 @@
                    _locationInfoRepository.UpdateData(locationInfo);
                    BaseDal.DeleteAndMoveIntoHty(task, App.User?.UserId == 0 ? OperateTypeEnum.自动完成 : OperateTypeEnum.人工完成);
                    _unitOfWorkManage.CommitTran();
                    _locationStatusChangeRecordService.AddLocationStatusChangeRecord(locationInfo, LocationStatusEnum.Lock, LocationStatusEnum.InStock, LocationChangeType.InboundCompleted, task.TaskNum);
                    //调用接口
                    string response = HttpHelper.Post(url, request);
                    WMSResponseContent wMSResponse = JsonConvert.DeserializeObject<WMSResponseContent>(response) ?? throw new Exception($"{taskNum},未接收到WMS入库上报返回值");
                    if (wMSResponse.Code != "0") content.Message = $"入库任务{task.TaskNum}WMS入库上报错误,信息:{wMSResponse.Msg}";
                }
                content.OK("任务完成");
            }
@@ -558,11 +686,35 @@
            }
            return content;
        }
        /// <summary>
        /// äºŒæœŸæ’­ç§å¢™å›žä¼ 
        /// </summary>
        /// <returns></returns>
        public EPLightContent WMSLightBack(List<TaskBackLight> taskBackLights)
        {
            EPLightContent content = new EPLightContent();
            try
            {
                string? url = _apiInfoRepository.QueryFirst(x => x.ApiCode == APIEnum.WMSLightBack.ToString())?.ApiAddress;
                if (string.IsNullOrEmpty(url))
                {
                    return content.Error($"未找到播种墙上报,请检查接口配置");
                }
                string request = JsonConvert.SerializeObject(taskBackLights, settings);
                string response = HttpHelper.Post(url, request);
                WMSResponseContent wMSResponse = JsonConvert.DeserializeObject<WMSResponseContent>(response) ?? throw new Exception($"未接收到播种墙上报返回值");
                if (wMSResponse.Code != "0") throw new Exception($"播种墙上报错误,信息:{wMSResponse.Msg}");
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// äºŒæœŸæ’­ç§å¢™ä¸‹å‘
        /// </summary>
        /// <param name="taskNum"></param>
        /// <returns></returns>
        public EPLightContent PickOrderInfoRequest(List<EPLightSendDTO> lightSendDTOs)
        {
@@ -574,7 +726,7 @@
                {
                    return content.Error($"未找到播种墙下发接口,请检查接口配置");
                }
                string request = JsonConvert.SerializeObject(lightSendDTOs, settings);
                string request = JsonConvert.SerializeObject(lightSendDTOs, settings).ToUpper();
                //调用接口
                string response = HttpHelper.Post(url, request);
                EPLightContent lightContent = JsonConvert.DeserializeObject<EPLightContent>(response) ?? throw new Exception($"未接收到播种墙下发上报返回值");