using log4net.Core; using Masuit.Tools; using System.Collections.Generic; using WIDESEA_Comm.AGVTask; using WIDESEA_Comm.WCSInterface.Requst; using WIDESEA_Common; using WIDESEA_Core.Const; using WIDESEA_DTO.WMS; using WIDESEA_IStorageBasicRepository; using WIDESEA_StorageBasicRepository; using WIDESEA_StorageTaskRepository; using WIDESEA_StoragIntegrationServices; using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob.Models; namespace WIDESEA_StorageTaskServices; public partial class Dt_TaskService : ServiceBase, IDt_TaskService { #region 请求任务入库 /// /// 请求入库 /// /// 请求模型 /// 包含任务信息的响应内容 public async Task RequestInTask(SaveModel saveModel) { // 创建一个WebResponseContent对象 WebResponseContent content = new WebResponseContent(); try { string palletCode = saveModel.MainData["palletCode"].ToString(); string station = saveModel.MainData["station"].ToString(); // 调用BaseDal.QueryFirstAsync方法,查询任务 var task = await BaseDal.QueryFirstAsync(x => x.PalletCode == palletCode); if (task != null) { throw new Exception($"托盘{palletCode}已存在任务"); } var stationInfo = _stationManagerRepository.QueryFirst(x => x.stationChildCode == station); if (stationInfo == null) { throw new Exception($"站台{station}不存在"); } var boxingInfo = _boxingInfoRepository.QueryFirst(x => x.PalletCode == palletCode); if (boxingInfo == null) { throw new Exception($"托盘{palletCode}组盘信息不存在"); } if (boxingInfo.CurrentStatue != 1) { throw new Exception($"托盘{palletCode}当前状态不允许入库"); } // 获取库位 var location = RequestLocation(stationInfo.Roadway); if (location == null) { return content.Error("无法获取货位信息或库位已满"); } string agvId = Guid.NewGuid().ToString().Replace("-", "").Take(16); var newtask = new Dt_Task { CurrentAddress = station, Grade = 1, Roadway = stationInfo.Roadway, TargetAddress = location.LocationCode, Dispatchertime = DateTime.Now, MaterialNo = "", NextAddress = stationInfo.Roadway, OrderNo = null, PalletCode = palletCode, SourceAddress = stationInfo.stationLocation, TaskState = (int)TaskInStatusEnum.InNew, TaskType = (int)TaskInboundTypeEnum.Inbound, TaskNum = await BaseDal.GetTaskNo(), Creater = "Systeam", AGVtaskId = agvId, }; // 尝试添加新任务 addtask schedulingTask = new addtask { task_id = newtask.AGVtaskId, task_type = newtask.TaskType == (int)TaskTypeEnum.Inbound ? "push" : "pop", work_begin = newtask.SourceAddress, work_end = newtask.TargetAddress }; string address = AGV_Interface + "add_task"; string result = HttpsClient.PostAsync(address, schedulingTask.ToDictionary()).Result; content = JsonConvert.DeserializeObject(result); if (content.ack != 0) { Console.WriteLine($"请求RCS异常:{content.msg}"); LogFactory.GetLog("下发AGV任务").Info(true, $"\r\r--------------------------------------"); LogFactory.GetLog("下发AGV任务").Info(true, $"请求参数:{schedulingTask.ToJsonString()}"); LogFactory.GetLog("下发AGV任务").Info(true, $"响应参数:{content.ToJsonString()}"); throw new Exception(content.msg); } _unitOfWorkManage.BeginTran(); BaseDal.AddData(newtask); location.LocationStatus = (int)LocationEnum.InStockDisable; _locationRepository.UpdateData(location); boxingInfo.CurrentStatue = 3; _boxingInfoRepository.UpdateData(boxingInfo); _unitOfWorkManage.CommitTran(); content.OK("申请入库成功:请等待AGV取料"); } catch (Exception er) { _unitOfWorkManage.RollbackTran(); // 如果发生异常,则调用content.Error方法,记录错误信息,并输出错误信息 content.Error($"入库申请失败:{er.Message}"); Console.WriteLine(er.Message); } // 返回content return content; } #endregion 请求任务入库 #region 库位分配 #region 获取货位 object objLOCK = new object(); /// /// 双升库位分配 /// /// /// /// public DtLocationInfo RequestLocation(string roadwayNo) { lock (objLOCK) { try { List locations = new List(); locations = _locationRepository.QueryData(x => x.RoadwayNo == roadwayNo && x.LocationStatus == (int)LocationEnum.Free && x.EnalbeStatus == 1); //&& x.LocationType == 1 var location = GetEmptyLocation(locations); if (location == null) { throw new Exception("库位已满"); } return location; } catch (Exception err) { Console.WriteLine(err.Message.ToString()); return null; } } } private DtLocationInfo GetEmptyLocation(List dtLocationInfos) { var locationinfo = dtLocationInfos.Where(x => x.LocationStatus == (int)LocationEnum.Free && x.EnalbeStatus == 1).OrderBy(x => x.Layer).ThenByDescending(x => x.Depth).ThenBy(x => x.Row).ThenBy(x => x.Column).FirstOrDefault(); return locationinfo; } #endregion 获取货位 #endregion 库位分配 public WebResponseContent confirmTask(int taskNum) { WebResponseContent content = new WebResponseContent(); try { var taskInfo = BaseDal.QueryFirst(x => x.TaskNum == taskNum); if (taskInfo != null) { BaseDal.DeleteData(taskInfo); } content.OK("确认完成"); } catch (Exception ex) { content.Error($"确认异常:{ex.Message}"); } return content; } }