using Furion.DistributedIDGenerator; using Furion.DynamicApiController; using Furion.FriendlyException; using Mapster; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Myshipping.Application.Entity; using Myshipping.Core; using Myshipping.Core.Service; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Myshipping.Application { /// /// 派车任务 /// [ApiDescriptionSettings("Application", Name = "TaskManageTruck", Order = 10)] public class TaskManageTruckService : ITaskManageTruckService, IDynamicApiController { private readonly ISysCacheService _cache; private readonly ILogger _logger; private readonly SqlSugarRepository _taskTruckRepository; private readonly SqlSugarRepository _taskTruckContaRepository; private readonly SqlSugarRepository _taskBaseRepository; public TaskManageTruckService(SqlSugarRepository taskTruckRepository, SqlSugarRepository taskTruckContaRepository, SqlSugarRepository taskBaseRepository, ISysCacheService cache, ILogger logger) { _cache = cache; _logger = logger; _taskTruckRepository = taskTruckRepository; _taskTruckContaRepository = taskTruckContaRepository; _taskBaseRepository = taskBaseRepository; } /// /// 保存派车 /// /// 派车信息 /// 返回回执 [HttpPost("/TaskManageTruck/Save")] public async Task Save(TaskTruckDto info) { TaskManageOrderResultDto result = new TaskManageOrderResultDto(); try { var id = await InnerSave(info); result.succ = true; result.msg = "保存成功"; result.ext = id; } catch (Exception ex) { result.succ = false; result.msg = $"保存派车异常,原因:{ex.Message}"; } return result; } /// /// 保存内部方法 /// /// 派车信息 /// 返回派车Id [SqlSugarUnitOfWork] private async Task InnerSave(TaskTruckDto info) { TaskTruckInfo entity = info.Adapt(); if (entity == null) throw Oops.Oh($"派车信息不能为空"); List entityCtnList = info.ContaList.Adapt>(); if (entityCtnList != null && entityCtnList.Count > 0) { //保存时默认统计箱型箱量 entity.CntrTotal = string.Join(";", entityCtnList.GroupBy(a => a.CTNALL) .Select(a => { return $"{a.Key}*{a.ToList().Sum(b => b.CTNNUM.HasValue ? b.CTNNUM.Value : 1)}"; }).ToArray()); } if (string.IsNullOrWhiteSpace(entity.PK_ID)) { entity.Status = BookingTruckStatus.TEMP.ToString(); entity.PK_ID = IDGen.NextID().ToString(); _taskTruckRepository.Insert(entity); if (entityCtnList != null && entityCtnList.Count > 0) { entityCtnList.ForEach(async ctn => { ctn.P_ID = entity.PK_ID; ctn.PK_ID = IDGen.NextID().ToString(); await _taskTruckContaRepository.InsertAsync(ctn); }); } } else { var model = _taskTruckRepository.AsQueryable().First(a => a.PK_ID == entity.PK_ID); if (model == null) throw Oops.Oh($"派车信息获取失败,派车信息不存在或已作废"); //校验 ValidateTruck(OperateTypeEnum.Save, new TaskTruckInfo[] { model }); entity.UpdatedTime = DateTime.Now; entity.UpdatedUserId = UserManager.UserId; entity.UpdatedUserName = UserManager.Name; await _taskTruckRepository.AsUpdateable(entity).IgnoreColumns(it => new { it.TenantId, it.CreatedTime, it.CreatedUserId, it.CreatedUserName, it.IsDeleted, it.BookingId, it.TruckId, it.TruckName, it.TruckCode, it.Status, }).ExecuteCommandAsync(); await _taskTruckContaRepository.DeleteAsync(x => x.P_ID == model.PK_ID); if (entityCtnList != null && entityCtnList.Count > 0) { entityCtnList.ForEach(async ctn => { ctn.P_ID = entity.PK_ID; ctn.PK_ID = IDGen.NextID().ToString(); //await _bookingTruckContaRepository.AsUpdateable(ctn).IgnoreColumns(it => new //{ // it.TenantId, // it.CreatedTime, // it.CreatedUserId, // it.CreatedUserName, // it.IsDeleted, //}).ExecuteCommandAsync(); await _taskTruckContaRepository.InsertAsync(ctn); }); } } return entity.PK_ID; } /// /// 获取派车详情 /// /// 派车主键 /// 返回回执 [HttpGet("/TaskManageTruck/GetInfo")] public async Task GetInfo(string pkId) { TaskManageOrderResultDto result = new TaskManageOrderResultDto(); try { var truckOrder = _taskTruckRepository.AsQueryable().First(a => a.PK_ID == pkId); if (truckOrder == null) throw Oops.Oh($"派车主键{pkId}无法获取业务信息"); var truckCtnList = _taskTruckContaRepository.AsQueryable().Where(a => a.P_ID == pkId).ToList(); TaskTruckShowDto model = truckOrder.Adapt(); if (truckCtnList.Count > 0) model.ContaList = truckCtnList.Adapt>(); result.succ = true; result.ext = model; } catch (Exception ex) { result.succ = false; result.msg = $"获取派车详情异常,原因:{ex.Message}"; } return result; } /// /// 通过任务主键获取派车详情 /// /// 派车主键 /// 返回回执 [HttpGet("/TaskManageTruck/GetInfoByTaskId")] public async Task GetInfoByTaskId(string taskPkId) { TaskManageOrderResultDto result = new TaskManageOrderResultDto(); try { var taskBase =_taskBaseRepository.AsQueryable().First(a => a.PK_ID == taskPkId); if(taskBase == null) throw Oops.Oh($"任务主键{taskPkId}无法获取业务信息"); var truckOrder = _taskTruckRepository.AsQueryable().First(a => a.TASK_ID == taskBase.PK_ID); if (truckOrder == null) throw Oops.Oh($"任务主键{taskPkId}无法获取派车业务信息"); var truckCtnList = _taskTruckContaRepository.AsQueryable().Where(a => a.P_ID == truckOrder.PK_ID).ToList(); TaskTruckShowDto model = truckOrder.Adapt(); if (truckCtnList.Count > 0) model.ContaList = truckCtnList.Adapt>(); result.succ = true; result.ext = model; } catch (Exception ex) { result.succ = false; result.msg = $"获取派车详情异常,原因:{ex.Message}"; } return result; } /// /// 打印派车 /// /// 派车主键 /// 返回回执 [HttpGet("/TaskManageTruck/Print")] public async Task Print(string pkId) { TaskManageOrderResultDto result = new TaskManageOrderResultDto(); try { } catch (Exception ex) { } return result; } /// /// 批量派车 /// /// 派车主键组 /// 返回回执 [HttpPost("/TaskManageTruck/SendDispatchBatch")] public async Task SendDispatchBatch([FromBody] string[] pkIds) { TaskManageOrderResultDto result = new TaskManageOrderResultDto(); try { } catch (Exception ex) { } return result; } /// /// 取消派车 /// /// 派车主键 /// 返回回执 [HttpGet("/TaskManageTruck/CancelDispatch")] public async Task CancelDispatch(string pkId) { TaskManageOrderResultDto result = new TaskManageOrderResultDto(); try { } catch (Exception ex) { } return result; } /// /// 批量取消派车 /// /// 派车主键组 /// 返回回执 [HttpPost("/TaskManageTruck/CancelDispatchBatch")] public async Task CancelDispatchBatch([FromBody] string[] pkIds) { TaskManageOrderResultDto result = new TaskManageOrderResultDto(); try { } catch (Exception ex) { } return result; } #region 校验派车 /// /// 校验派车 /// /// 操作类型 /// 派车信息列表 private void ValidateTruck(OperateTypeEnum operateType, TaskTruckInfo[] entityArg) { if (operateType == OperateTypeEnum.Save) { if (entityArg.Any(a => a.Status != BookingTruckStatus.TEMP.ToString() && a.Status != BookingTruckStatus.CANCELED.ToString())) { throw Oops.Oh($"派车状态只有暂存、已撤销才能保存"); } } else if (operateType == OperateTypeEnum.Submit) { if (entityArg.Any(a => a.Status != BookingTruckStatus.TEMP.ToString() && a.Status != BookingTruckStatus.CANCELED.ToString())) { throw Oops.Oh($"派车状态只有暂存、已撤销才能提交"); } } else if (operateType == OperateTypeEnum.Cancel) { if (entityArg.Any(a => a.Status != BookingTruckStatus.SUBMITED.ToString())) { throw Oops.Oh($"派车状态只有已提交才能撤销派车"); } } else if (operateType == OperateTypeEnum.Delete) { if (entityArg.Any(a => a.Status != BookingTruckStatus.TEMP.ToString() && a.Status != BookingTruckStatus.CANCELED.ToString())) { throw Oops.Oh($"派车状态只有暂存、已撤销才能作废"); } } } #endregion } }