| | |
| | | namespace WIDESEA_StorageOutTaskServices; |
| | | using Mapster; |
| | | namespace WIDESEA_StorageOutTaskServices; |
| | | |
| | | public class Dt_TaskService : ServiceBase<Dt_Task, IDt_TaskRepository>, IDt_TaskService |
| | | { |
| | |
| | | private readonly IPointStackerRelationRepository _pointStackerRelationRepository; |
| | | private readonly ITaskExecuteDetailRepository _taskExecuteDetailRepository; |
| | | private readonly ILocationStatusChangeRecordRepository _locationStatusChangeRecordRepository; |
| | | private readonly IBoxingInfoRepository _boxingInfoRepository; |
| | | |
| | | public Dt_TaskService(IDt_TaskRepository BaseDal, |
| | | IUnitOfWorkManage unitOfWorkManage, |
| | |
| | | IDt_WareAreaInfoRepository wareAreaInfoRepository, |
| | | IPointStackerRelationRepository pointStackerRelationRepository, |
| | | ITaskExecuteDetailRepository taskExecuteDetailRepository, |
| | | ILocationStatusChangeRecordRepository locationStatusChangeRecordRepository) : base(BaseDal) |
| | | ILocationStatusChangeRecordRepository locationStatusChangeRecordRepository, |
| | | IBoxingInfoRepository boxingInfoRepository) : base(BaseDal) |
| | | { |
| | | _unitOfWorkManage = unitOfWorkManage; |
| | | _outOrderRepository = outOrderRepository; |
| | |
| | | _pointStackerRelationRepository = pointStackerRelationRepository; |
| | | _taskExecuteDetailRepository = taskExecuteDetailRepository; |
| | | _locationStatusChangeRecordRepository = locationStatusChangeRecordRepository; |
| | | _boxingInfoRepository = boxingInfoRepository; |
| | | } |
| | | |
| | | #region 外部接口方法 |
| | | |
| | | #region 堆垛机出库任务完成 |
| | | |
| | | /// <summary> |
| | | /// 堆垛机出库任务完成 |
| | |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 总出库任务完成 |
| | | /// </summary> |
| | | /// <param name="saveModel">任务数据合集</param> |
| | | /// <returns>返回结果集</returns> |
| | | public async Task<WebResponseContent> CompleteTaskAsync(Dt_Task task, DtStockInfo stock) |
| | | { |
| | | WebResponseContent content = new WebResponseContent(); |
| | | #endregion 堆垛机出库任务完成 |
| | | |
| | | try |
| | | { |
| | | // 更新出库订单和库存货位信息 |
| | | var orderStock = await UpdateOrderAndStockAsync(stock, task.PalletCode); |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, "总任务出库完成", $"更新出库订单和库存货位信息:{JsonConvert.SerializeObject(orderStock)}"); |
| | | |
| | | // 检查订单是否完成并处理后续操作 |
| | | if (IsOrderComplete(orderStock)) |
| | | { |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, "总任务出库完成", $"订单出库完成"); |
| | | // 处理已完成订单 |
| | | await ProcessCompletedOrderAsync(orderStock, task); |
| | | } |
| | | else |
| | | { |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, "总任务出库完成", $"订单暂未出库完成"); |
| | | // 处理未完成订单 |
| | | await ProcessUpdateOrderAsync(orderStock, task); |
| | | } |
| | | |
| | | content.OK("任务完成"); |
| | | } |
| | | catch (Exception err) |
| | | { |
| | | // 记录异常信息 |
| | | Console.WriteLine(err.Message.ToString()); |
| | | content.Error("任务完成失败"); |
| | | } |
| | | |
| | | return content; |
| | | } |
| | | #region 移库任务完成 |
| | | |
| | | /// <summary> |
| | | /// 移库任务完成 |
| | |
| | | return content; |
| | | } |
| | | |
| | | #endregion 移库任务完成 |
| | | |
| | | #region 入库任务完成 |
| | | |
| | | /// <summary> |
| | | /// 入库任务完成 |
| | | /// </summary> |
| | |
| | | WebResponseContent content = new WebResponseContent(); |
| | | try |
| | | { |
| | | var boxing = await _boxingInfoRepository.QueryFirstNavAsync(x => x.PalletCode == task.PalletCode); |
| | | |
| | | var boxDetail = boxing.BoxingInfoDetails.Adapt<List<DtStockInfoDetail>>(); |
| | | // 创建库存实例模型 |
| | | DtStockInfo stock = new DtStockInfo() |
| | | { |
| | | |
| | | PalletCode = task.PalletCode, |
| | | LocationCode = task.TargetAddress, |
| | | CreateDate = DateTime.Now, |
| | | Creater = "system", |
| | | IsFull = boxing.IsFull, |
| | | StockInfoDetails = boxDetail, |
| | | LocationInfo = new DtLocationInfo() |
| | | }; |
| | | |
| | | // 创建历史任务实例模型 |
| | |
| | | } |
| | | return content; |
| | | } |
| | | |
| | | #endregion 入库任务完成 |
| | | |
| | | #region 任务完成 |
| | | |
| | | /// <summary> |
| | | /// 任务完成 |
| | |
| | | } |
| | | else |
| | | { |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, "总任务出库完成", ""); |
| | | return await CompleteTaskAsync(task, stock); |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, "库存不存在也不是入库任务", ""); |
| | | return content.Error("库存不存在"); |
| | | } |
| | | } |
| | | else if (task.TaskType == (int)TaskTypeEnum.RelocationIn) |
| | |
| | | return content.Error("任务状态异常"); |
| | | } |
| | | } |
| | | |
| | | #endregion 任务完成 |
| | | |
| | | #region 请求任务 |
| | | |
| | | /// <summary> |
| | | /// 请求任务 |
| | |
| | | |
| | | return content; |
| | | } |
| | | |
| | | #endregion 请求任务 |
| | | |
| | | #endregion 外部接口方法 |
| | | |
| | |
| | | /// <returns>更新后的订单和库存信息</returns> |
| | | private async Task<Dt_OutOrderAndStock> UpdateOrderAndStockAsync(DtStockInfo stock, string barCode) |
| | | { |
| | | //根据PalletCode获取订单和库存信息 |
| | | var orderStock = await _outOrderAndStockRepository.GetOrderAndStock(palletCode: barCode); |
| | | |
| | | //完成数量增加 |
| | | orderStock.CompletedQuantity += stock.StockInfoDetails.Sum(x => x.StockQuantity); |
| | | |
| | | //订单详情完成数量增加 |
| | | orderStock.OrderList.OrderDetailList.CompletedQuantity += stock.StockInfoDetails.Sum(x => x.StockQuantity); |
| | | |
| | | //返回更新后的订单和库存信息 |
| | | return orderStock; |
| | | } |
| | | |
| | |
| | | /// </summary> |
| | | /// <param name="orderStock">订单和库存信息</param> |
| | | /// <returns>是否完成</returns> |
| | | // 判断订单是否完成 |
| | | private bool IsOrderComplete(Dt_OutOrderAndStock orderStock) |
| | | { |
| | | // 如果出库数量等于完成出库数量,则订单完成 |
| | | return orderStock.CompletedQuantity == orderStock.OutboundQuantity; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 处理已完成的订单 |
| | | /// </summary> |
| | | /// <param name="orderStock">订单和库存信息</param> |
| | | /// <param name="task">任务数据</param> |
| | | /// <returns>异步任务</returns> |
| | | private async Task ProcessCompletedOrderAsync(Dt_OutOrderAndStock orderStock, Dt_Task task) |
| | | { |
| | | // 创建历史订单记录 |
| | | var stockHty = CreateHistoricalOrder(orderStock); |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, "总任务出库完成,订单已完成", $"创建历史订单记录:{JsonConvert.SerializeObject(stockHty)}"); |
| | | |
| | | // 创建获取历史任务 |
| | | var taskHty = CreateHistoricalTask(task); |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, "总任务出库完成,订单已完成", $"创建历史任务记录:{JsonConvert.SerializeObject(taskHty)}"); |
| | | |
| | | _unitOfWorkManage.BeginTran(); |
| | | |
| | | try |
| | | { |
| | | // 添加历史任务 |
| | | var isTaskHtyAdd = await _task_HtyRepository.AddDataAsync(taskHty) > 0; |
| | | |
| | | // 删除任务数据 |
| | | var isTaskDelete = await Delete(task.TaskId); |
| | | |
| | | // 插入历史订单 |
| | | var isStockInsert = _outOrderAndStock_HtyRepository.InsertNav(stockHty); |
| | | |
| | | // 删除原订单 |
| | | var isOrderStockDelete = _outOrderAndStockRepository.DeleteNavOrderStock(orderStock); |
| | | |
| | | if (isStockInsert && isOrderStockDelete && isTaskHtyAdd && isTaskDelete) |
| | | { |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, "总任务出库完成,订单已完成", $"事务处理完成,提交事务。添加历史任务:{isTaskHtyAdd},删除任务数据:{isTaskDelete},插入历史订单:{isStockInsert},删除原订单:{isOrderStockDelete}"); |
| | | _unitOfWorkManage.CommitTran(); |
| | | } |
| | | else |
| | | { |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, "总任务出库完成,订单已完成", $"添加数据库失败,请检查数据是否正确,数据回滚。添加历史任务:{isTaskHtyAdd},删除任务数据:{isTaskDelete},插入历史订单:{isStockInsert},删除原订单:{isOrderStockDelete}"); |
| | | _unitOfWorkManage.RollbackTran(); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, $"总任务出库完成,订单已完成,系统异常,异常信息:{ex.Message}", "无参数"); |
| | | _unitOfWorkManage.RollbackTran(); |
| | | throw; // 重新抛出异常以供外部捕获 |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 处理未完成订单 |
| | | /// </summary> |
| | | /// <param name="orderStock">订单和库存信息</param> |
| | | /// <param name="task">任务数据</param> |
| | | /// <returns>异步任务</returns> |
| | | private async Task ProcessUpdateOrderAsync(Dt_OutOrderAndStock orderStock, Dt_Task task) |
| | | { |
| | | // 创建获取历史任务 |
| | | var taskHty = CreateHistoricalTask(task); |
| | | |
| | | _unitOfWorkManage.BeginTran(); |
| | | try |
| | | { |
| | | // 添加历史任务 |
| | | var isTaskHtyAdd = await _task_HtyRepository.AddDataAsync(taskHty) > 0; |
| | | |
| | | // 删除任务数据 |
| | | var isTaskDelete = await Delete(task.TaskId); |
| | | |
| | | // 更新订单与库存 |
| | | var isOrderUpdated = _outOrderAndStockRepository.UpdateNavOrderStock(orderStock); |
| | | if (isTaskDelete && isTaskHtyAdd && isOrderUpdated) |
| | | { |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, "总任务出库完成,订单暂未完成", $"事务处理完成,提交事务。添加历史任务:{isTaskHtyAdd},删除任务数据:{isTaskDelete},更新订单与库存:{isOrderUpdated}"); |
| | | _unitOfWorkManage.CommitTran(); |
| | | } |
| | | else |
| | | { |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, "总任务出库完成,订单暂未完成", $"添加数据库失败,请检查数据是否正确,数据回滚。添加历史任务:{isTaskHtyAdd},删除任务数据:{isTaskDelete},更新订单与库存:{isOrderUpdated}"); |
| | | _unitOfWorkManage.RollbackTran(); |
| | | } |
| | | } |
| | | catch (Exception err) |
| | | { |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, $"总任务出库完成,订单暂未完成,系统异常,异常信息:{err.Message}", "无参数"); |
| | | Console.WriteLine(err.Message.ToString()); |
| | | _unitOfWorkManage.RollbackTran(); |
| | | throw; // 重新抛出异常以供外部捕获 |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 创建历史订单记录 |
| | | /// </summary> |
| | | /// <param name="orderStock">订单和库存信息</param> |
| | | /// <returns>历史订单记录</returns> |
| | | private Dt_OutOrderAndStock_Hty CreateHistoricalOrder(Dt_OutOrderAndStock orderStock) |
| | | { |
| | | var stockHty = _mapper.Map<Dt_OutOrderAndStock_Hty>(orderStock); |
| | | |
| | | //stockHty.StockList.FinishTime = DateTime.Now; |
| | | //stockHty.StockList.OperateType = (int)OperateTypeEnum.自动完成; |
| | | //stockHty.StockList.SourceId = orderStock.StockList.GroupId; |
| | | //stockHty.StockList.GroupId = 0; |
| | | |
| | | //stockHty.StockList.StockDetailList.ForEach(x => |
| | | //{ |
| | | // x.FinishTime = DateTime.Now; |
| | | // x.OperateType = (int)OperateTypeEnum.自动完成; |
| | | // x.GroupId = 0; |
| | | //}); |
| | | |
| | | stockHty.OrderList.FinishTime = DateTime.Now; |
| | | stockHty.OrderList.OperateType = (int)OperateTypeEnum.自动完成; |
| | | stockHty.OrderList.SourceId = orderStock.OrderList.Id; |
| | | stockHty.OrderList.Id = 0; |
| | | |
| | | stockHty.OrderList.orderDetailList.FinishTime = DateTime.Now; |
| | | stockHty.OrderList.orderDetailList.OperateType = (int)OperateTypeEnum.自动完成; |
| | | stockHty.OrderList.orderDetailList.Id = 0; |
| | | |
| | | return stockHty; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// </summary> |
| | | /// <param name="stock">库存对象</param> |
| | | /// <param name="toLocation">目标位置</param> |
| | | // 更新库存和位置信息 |
| | | private (DtStockInfo, DtLocationInfo) UpdateStockLocation(DtStockInfo stock, string toLocation) |
| | | { |
| | | // 获取库存信息 |
| | | var locationInfo = _locationRepository.QueryFirst(x => x.LocationCode == stock.LocationCode); |
| | | // 将库存状态设置为在库 |
| | | locationInfo.LocationStatus = LocationEnum.InStock.ObjToInt(); |
| | | |
| | | // 将库存位置设置为目标位置 |
| | | stock.LocationCode = toLocation; |
| | | // 将库存状态设置为在库 |
| | | stock.LocationInfo.LocationStatus = LocationEnum.InStock.ObjToInt(); |
| | | |
| | | // 返回更新后的库存和位置信息 |
| | | return (stock, locationInfo); |
| | | } |
| | | |
| | |
| | | else |
| | | { |
| | | // 添加库存 |
| | | isUpdateStock = await _stockInfoRepository.AddDataAsync(stock) > 0; |
| | | isUpdateStock = await _stockInfoRepository.AddDataNavAsync(stock); |
| | | } |
| | | |
| | | // 添加历史任务 |
| | |
| | | // 提交或回滚事务 |
| | | if (isUpdateStock && isTaskHtyAdd && isTaskDelete && isUpdateLoc) |
| | | { |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, "移库任务完成", $"事务处理完成,提交事务。添加历史任务:{isTaskHtyAdd},删除任务数据:{isTaskDelete},更新或添加库存:{isUpdateStock},修改移库前货位状态:{isUpdateLoc}"); |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, "任务完成", $"事务处理完成,提交事务。添加历史任务:{isTaskHtyAdd},删除任务数据:{isTaskDelete},更新或添加库存:{isUpdateStock},修改移库前货位状态:{isUpdateLoc}"); |
| | | _unitOfWorkManage.CommitTran(); |
| | | return true; |
| | | } |
| | | else |
| | | { |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, "移库任务完成", $"数据处理失败,请检查数据是否正确,数据回滚。添加历史任务:{isTaskHtyAdd},删除任务数据:{isTaskDelete},更新库存:{isUpdateStock},修改移库前货位状态:{isUpdateLoc}"); |
| | | LogFactory.GetLog("任务完成").InfoFormat(true, "任务完成", $"数据处理失败,请检查数据是否正确,数据回滚。添加历史任务:{isTaskHtyAdd},删除任务数据:{isTaskDelete},更新库存:{isUpdateStock},修改移库前货位状态:{isUpdateLoc}"); |
| | | _unitOfWorkManage.RollbackTran(); |
| | | return false; |
| | | } |
| | |
| | | /// <returns></returns> |
| | | private async Task<WebResponseContent> UpdateExistingTask(RequestTaskDto input, int areaId, Dt_Task task) |
| | | { |
| | | // 创建WebResponseContent对象 |
| | | WebResponseContent content = new WebResponseContent(); |
| | | // 定义变量 |
| | | string toAddress; |
| | | int taskState; |
| | | string original = task.CurrentAddress; |
| | | DtLocationInfo location = new DtLocationInfo(); |
| | | int beforeStatus = 0; |
| | | |
| | | // 根据任务类型判断是出库任务还是入库任务 |
| | | if (input.Type == (int)TaskTypeEnum.Outbound) |
| | | { |
| | | // 处理出库任务 |
| | |
| | | task.NextAddress = toAddress; |
| | | task.TaskState = taskState; |
| | | |
| | | // 开始事务 |
| | | _unitOfWorkManage.BeginTran(); |
| | | // 定义变量 |
| | | bool isUpdateLo = true; |
| | | bool isUpdateChange = true; |
| | | |
| | |
| | | bool isTaskDetail = await _taskExecuteDetailRepository.AddDetailAsync(task, false, TaskDescription.GetTaskUpdateDescription(task.PalletCode, original, input.Position, InTaskStatusEnum.Line_InFinish.GetIntegralRuleTypeEnumDesc())); |
| | | if (input.Type != (int)TaskTypeEnum.Outbound) |
| | | { |
| | | // 创建LocationChangeRecordDto对象 |
| | | LocationChangeRecordDto changeRecordDto = new LocationChangeRecordDto() |
| | | { |
| | | // 设置变量 |
| | | AfterStatus = location.LocationStatus, |
| | | BeforeStatus = beforeStatus, |
| | | TaskNum = task.TaskNum.Value, |
| | |
| | | LocationCode = location.LocationCode, |
| | | ChangeType = (int)StatusChangeTypeEnum.AutomaticStorage, |
| | | }; |
| | | // 更新位置状态 |
| | | isUpdateChange = _locationStatusChangeRecordRepository.AddStatusChangeRecord(changeRecordDto); |
| | | isUpdateLo = await _locationRepository.UpdateDataAsync(location); |
| | | } |
| | | |
| | | if (isResult && isUpdateLo && isTaskDetail) |
| | | { |
| | | // 提交事务 |
| | | _unitOfWorkManage.CommitTran(); |
| | | content.OK(data: task); |
| | | } |
| | | else |
| | | { |
| | | // 回滚事务 |
| | | _unitOfWorkManage.RollbackTran(); |
| | | content.Error("更新任务失败"); |
| | | } |