You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Application/Service/BookingTruck/BookingTruckService.cs

1129 lines
42 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Furion;
using Furion.DependencyInjection;
using Furion.DistributedIDGenerator;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Furion.JsonSerialization;
using Furion.RemoteRequest.Extensions;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Myshipping.Application.Entity;
using Myshipping.Application.Helper;
using Myshipping.Core;
using Myshipping.Core.Entity;
using Myshipping.Core.Service;
using MySqlX.XDevAPI.Common;
using NetTaste;
using NPOI.OpenXmlFormats.Wordprocessing;
using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Crypto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Myshipping.Application
{
/// <summary>
/// 派车服务
/// </summary>
[ApiDescriptionSettings("Application", Name = "BookingTruck", Order = 9)]
public class BookingTruckService : IBookingTruckService, IDynamicApiController, ITransient
{
private readonly ISysCacheService _cache;
private readonly ILogger<BookingTruckService> _logger;
private readonly SqlSugarRepository<BookingTruck> _bookingTruckRepository;
private readonly SqlSugarRepository<BookingTruckCtn> _bookingTruckContaRepository;
private readonly SqlSugarRepository<BookingOrder> _bookingOrderRepository;
private readonly SqlSugarRepository<BookingCtn> _bookingCtnRepository;
private readonly ITaskManageExternalService _taskManageExternalService;
public BookingTruckService(ISysCacheService cache, ILogger<BookingTruckService> logger,
SqlSugarRepository<BookingTruck> bookingTruckRepository,
SqlSugarRepository<BookingTruckCtn> bookingTruckContaRepository,
SqlSugarRepository<BookingOrder> bookingOrderRepository,
SqlSugarRepository<BookingCtn> bookingCtnRepository,
ITaskManageExternalService taskManageExternalService)
{
_cache = cache;
_logger = logger;
_bookingTruckRepository = bookingTruckRepository;
_bookingTruckContaRepository = bookingTruckContaRepository;
_bookingOrderRepository = bookingOrderRepository;
_bookingCtnRepository = bookingCtnRepository;
_taskManageExternalService = taskManageExternalService;
}
/// <summary>
/// 保存派车
/// </summary>
/// <param name="info">派车信息</param>
/// <returns>返回回执</returns>
[HttpPost("/BookingTruck/Save")]
public async Task<TaskManageOrderResultDto> Save(BookingTruckDto 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;
}
/// <summary>
/// 保存内部方法
/// </summary>
/// <param name="info">派车信息</param>
/// <returns>返回派车Id</returns>
[SqlSugarUnitOfWork]
private async Task<long> InnerSave(BookingTruckDto info)
{
BookingTruck entity = info.Adapt<BookingTruck>();
if (entity == null)
throw Oops.Oh($"派车信息不能为空");
List<BookingTruckCtn> entityCtnList = info.ContaList.Adapt<List<BookingTruckCtn>>();
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 (entity.Id == 0)
{
entity.Status = BookingTruckStatus.TEMP.ToString();
_bookingTruckRepository.Insert(entity);
if (entityCtnList != null && entityCtnList.Count > 0)
{
entityCtnList.ForEach(async ctn =>
{
ctn.TruckId = entity.Id;
await _bookingTruckContaRepository.InsertAsync(ctn);
});
}
}
else
{
var model = _bookingTruckRepository.AsQueryable().First(a => a.Id == entity.Id);
if(model == null)
throw Oops.Oh($"派车信息获取失败,派车信息不存在或已作废");
//校验
ValidateTruck(OperateTypeEnum.Save, new BookingTruck[] { model });
entity.UpdatedTime = DateTime.Now;
entity.UpdatedUserId = UserManager.UserId;
entity.UpdatedUserName = UserManager.Name;
await _bookingTruckRepository.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 _bookingTruckContaRepository.DeleteAsync(x => x.TruckId == model.Id);
if (entityCtnList != null && entityCtnList.Count > 0)
{
entityCtnList.ForEach(async ctn =>
{
ctn.TruckId = entity.Id;
//await _bookingTruckContaRepository.AsUpdateable(ctn).IgnoreColumns(it => new
//{
// it.TenantId,
// it.CreatedTime,
// it.CreatedUserId,
// it.CreatedUserName,
// it.IsDeleted,
//}).ExecuteCommandAsync();
await _bookingTruckContaRepository.InsertAsync(ctn);
});
}
}
return entity.Id;
}
/// <summary>
/// 获取派车详情
/// </summary>
/// <param name="id">派车主键</param>
/// <returns>返回回执</returns>
[HttpGet("/BookingTruck/GetInfo")]
public async Task<TaskManageOrderResultDto> GetInfo(long id)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
var truckOrder = _bookingTruckRepository.AsQueryable().First(a => a.Id == id);
if (truckOrder == null)
throw Oops.Oh($"派车主键{id}无法获取业务信息");
var truckCtnList = _bookingTruckContaRepository.AsQueryable().Where(a => a.TruckId == id).ToList();
BookingTruckShowDto model = truckOrder.Adapt<BookingTruckShowDto>();
if (truckCtnList.Count > 0)
model.ContaList = truckCtnList.Adapt<List<BookingTruckCtnDto>>();
result.succ = true;
result.ext = model;
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"获取派车详情异常,原因:{ex.Message}";
}
return result;
}
/// <summary>
/// 订舱生成派车初始信息
/// </summary>
/// <param name="bookingId">订舱主键</param>
/// <returns>返回派车初始信息</returns>
[HttpGet("/BookingTruck/InitFromBookingOrder")]
public async Task<TaskManageOrderResultDto> InitFromBookingOrder(long bookingId)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
var model = InnerCreateTruckFromBookingOrder(bookingId);
result.succ = true;
result.ext = model;
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"订舱生成派车初始信息异常,原因:{ex.Message}";
}
return result;
}
/// <summary>
/// 派车台账查询
/// </summary>
/// <param name="QuerySearch">派车台账查询请求</param>
/// <returns>返回结果</returns>
[HttpPost("/BookingTruck/GetPage")]
public async Task<SqlSugarPagedList<BookingTruckPageDto>> GetPageAsync([FromBody] QueryBookingTruckDto QuerySearch)
{
List<string> contaList = new List<string>();
if (!string.IsNullOrWhiteSpace(QuerySearch.ContaNo))
{
if (Regex.IsMatch(QuerySearch.ContaNo, "(\\t|\\n\\r|\\n)"))
{
contaList = Regex.Replace(QuerySearch.ContaNo, "(\\t |\\n\\r |\\n)", "#").Split(new char[] { '#' }).Select(t => t?.Trim()).ToList();
}
else
{
contaList.Add(QuerySearch.ContaNo.Trim());
}
}
//制单日期
DateTime createBegin = DateTime.MinValue;
DateTime createEnd = DateTime.MinValue;
//派车日期
DateTime truckDateBegin = DateTime.MinValue;
DateTime truckDateEnd = DateTime.MinValue;
//截单日期
DateTime closeDateBegin = DateTime.MinValue;
DateTime closeDateEnd = DateTime.MinValue;
//提货日期
DateTime pickUpTimeBegin = DateTime.MinValue;
DateTime pickUpTimeEnd = DateTime.MinValue;
//返场时间
DateTime returnTimeBegin = DateTime.MinValue;
DateTime returnTimeEnd = DateTime.MinValue;
//要求到达时间
DateTime needArriveTimeBegin = DateTime.MinValue;
DateTime needArriveTimeEnd = DateTime.MinValue;
#region 查询条件
//制单日期
if (!string.IsNullOrWhiteSpace(QuerySearch.CreateBegin))
{
if (!DateTime.TryParse(QuerySearch.CreateBegin, out createBegin))
throw Oops.Oh($"创建起始日期格式错误,{QuerySearch.CreateBegin}");
}
if (!string.IsNullOrWhiteSpace(QuerySearch.CreateEnd))
{
if (!DateTime.TryParse(QuerySearch.CreateEnd, out createEnd))
throw Oops.Oh($"创建结束日期格式错误,{QuerySearch.CreateEnd}");
createEnd = createEnd.AddDays(1);
}
//派车日期
if (!string.IsNullOrWhiteSpace(QuerySearch.TruckTimeBegin))
{
if (!DateTime.TryParse(QuerySearch.TruckTimeBegin, out truckDateBegin))
throw Oops.Oh($"派车起始日期格式错误,{QuerySearch.TruckTimeBegin}");
}
if (!string.IsNullOrWhiteSpace(QuerySearch.TruckTimeEnd))
{
if (!DateTime.TryParse(QuerySearch.TruckTimeEnd, out truckDateEnd))
throw Oops.Oh($"派车结束日期格式错误,{QuerySearch.TruckTimeEnd}");
truckDateEnd = truckDateEnd.AddDays(1);
}
//截单日期
if (!string.IsNullOrWhiteSpace(QuerySearch.ClosingTimeBegin))
{
if (!DateTime.TryParse(QuerySearch.ClosingTimeBegin, out closeDateBegin))
throw Oops.Oh($"截单起始日期格式错误,{QuerySearch.ClosingTimeBegin}");
}
if (!string.IsNullOrWhiteSpace(QuerySearch.ClosingTimeEnd))
{
if (!DateTime.TryParse(QuerySearch.ClosingTimeEnd, out closeDateEnd))
throw Oops.Oh($"截单结束日期格式错误,{QuerySearch.ClosingTimeEnd}");
closeDateEnd = closeDateEnd.AddDays(1);
}
//提货日期
if (!string.IsNullOrWhiteSpace(QuerySearch.PickUpTimeBegin))
{
if (!DateTime.TryParse(QuerySearch.PickUpTimeBegin, out pickUpTimeBegin))
throw Oops.Oh($"提货起始日期格式错误,{QuerySearch.PickUpTimeBegin}");
}
if (!string.IsNullOrWhiteSpace(QuerySearch.PickUpTimeEnd))
{
if (!DateTime.TryParse(QuerySearch.ClosingTimeEnd, out pickUpTimeEnd))
throw Oops.Oh($"提货结束日期格式错误,{QuerySearch.PickUpTimeEnd}");
pickUpTimeEnd = pickUpTimeEnd.AddDays(1);
}
//返场时间
if (!string.IsNullOrWhiteSpace(QuerySearch.ReturnTimeBegin))
{
if (!DateTime.TryParse(QuerySearch.ReturnTimeBegin, out returnTimeBegin))
throw Oops.Oh($"返场起始日期格式错误,{QuerySearch.ReturnTimeBegin}");
}
if (!string.IsNullOrWhiteSpace(QuerySearch.ReturnTimeEnd))
{
if (!DateTime.TryParse(QuerySearch.ReturnTimeEnd, out returnTimeEnd))
throw Oops.Oh($"返场结束日期格式错误,{QuerySearch.ReturnTimeEnd}");
returnTimeEnd = returnTimeEnd.AddDays(1);
}
//要求到达时间
if (!string.IsNullOrWhiteSpace(QuerySearch.NeedArriveTimeBegin))
{
if (!DateTime.TryParse(QuerySearch.NeedArriveTimeBegin, out needArriveTimeBegin))
throw Oops.Oh($"返场起始日期格式错误,{QuerySearch.NeedArriveTimeBegin}");
}
if (!string.IsNullOrWhiteSpace(QuerySearch.NeedArriveTimeEnd))
{
if (!DateTime.TryParse(QuerySearch.NeedArriveTimeEnd, out needArriveTimeEnd))
throw Oops.Oh($"返场结束日期格式错误,{QuerySearch.NeedArriveTimeEnd}");
needArriveTimeEnd = needArriveTimeEnd.AddDays(1);
}
#endregion
string entityOrderCol = "CreatedTime";
//这里因为返回给前端的台账数据是DTO所以这里排序时候需要转换成Entity对应的字段
if (!string.IsNullOrWhiteSpace(QuerySearch.SortField))
entityOrderCol = MapsterExtHelper.GetAdaptProperty<BookingTruckDto, BookingTruck>(QuerySearch.SortField);
var entities = await _bookingTruckRepository.AsQueryable()
.WhereIF(createBegin != DateTime.MinValue, t => t.CreatedTime.HasValue && t.CreatedTime.Value >= createBegin)
.WhereIF(createEnd != DateTime.MinValue, t => t.CreatedTime.HasValue && t.CreatedTime.Value < createEnd)
.WhereIF(truckDateBegin != DateTime.MinValue, t => t.TruckTime.HasValue && t.TruckTime.Value >= truckDateBegin)
.WhereIF(truckDateEnd != DateTime.MinValue, t => t.TruckTime.HasValue && t.TruckTime.Value < truckDateEnd)
.WhereIF(closeDateBegin != DateTime.MinValue, t => t.ClosingTime.HasValue && t.ClosingTime.Value >= closeDateBegin)
.WhereIF(closeDateEnd != DateTime.MinValue, t => t.ClosingTime.HasValue && t.ClosingTime.Value < closeDateEnd)
.WhereIF(pickUpTimeBegin != DateTime.MinValue, t => t.PickUpTime.HasValue && t.PickUpTime.Value >= pickUpTimeBegin)
.WhereIF(pickUpTimeEnd != DateTime.MinValue, t => t.PickUpTime.HasValue && t.PickUpTime.Value < pickUpTimeEnd)
.WhereIF(returnTimeBegin != DateTime.MinValue, t => t.ReturnTime.HasValue && t.ReturnTime.Value >= returnTimeBegin)
.WhereIF(returnTimeEnd != DateTime.MinValue, t => t.ReturnTime.HasValue && t.ReturnTime.Value < returnTimeEnd)
.WhereIF(needArriveTimeBegin != DateTime.MinValue, t => t.NeedArriveTime.HasValue && t.NeedArriveTime.Value >= needArriveTimeBegin)
.WhereIF(needArriveTimeEnd != DateTime.MinValue, t => t.NeedArriveTime.HasValue && t.NeedArriveTime.Value < needArriveTimeEnd)
.WhereIF(!string.IsNullOrWhiteSpace(QuerySearch.Yard), t => t.YARDID.Contains(QuerySearch.Yard) || t.YARD.Contains(QuerySearch.Yard))
.WhereIF(!string.IsNullOrWhiteSpace(QuerySearch.InYard), t => t.InYardID.Contains(QuerySearch.Yard) || t.InYard.Contains(QuerySearch.Yard))
.WhereIF(!string.IsNullOrWhiteSpace(QuerySearch.Status), t => t.Status == QuerySearch.Status)
.OrderBy(entityOrderCol + (QuerySearch.descSort ? " asc " : " desc "))
.ToPagedListAsync(QuerySearch.PageNo, QuerySearch.PageSize);
return entities.Adapt<SqlSugarPagedList<BookingTruckPageDto>>();
}
/// <summary>
/// 订舱主键获取相关派车列表
/// </summary>
/// <param name="bookingId">订舱主键</param>
/// <returns>返回派车列表</returns>
[HttpGet("/BookingTruck/GetTruckListByBooking")]
public async Task<List<BookingTruckShowDto>> GetTruckListByBookingAsync(long bookingId)
{
var list = _bookingTruckRepository.AsQueryable().Where(a => a.BookingId == bookingId).ToList();
return list.Adapt<List<BookingTruckShowDto>>();
}
private BookingTruckDto InnerCreateTruckFromBookingOrder(long bookingId)
{
BookingTruckDto model = null;
//取订舱主信息
var orderInfo = _bookingOrderRepository.AsQueryable()
.First(a => a.Id == bookingId);
if (orderInfo == null)
throw Oops.Oh($"订舱主键{bookingId}无法获取业务信息");
model = new BookingTruckDto();
model.YARDID = orderInfo.YARDID;
model.YARD = orderInfo.YARD;
model.ClosingTime = orderInfo.CLOSINGDATE;
model.InYardID = orderInfo.YARDID;
model.InYard = orderInfo.YARD;
model.FromName = UserManager.Name;
model.FromTel = UserManager.TEl;
model.FromMail = UserManager.Email;
if (orderInfo.KGS.HasValue)
{
//计算总吨数
model.KGS = Math.Round(orderInfo.KGS.Value / 1000m, 3);
}
//取订舱箱列表
var ctnList = _bookingCtnRepository.AsQueryable()
.Where(a => a.BILLID == bookingId).ToList();
if (ctnList.Count > 0)
{
model.ContaList = new List<BookingTruckCtnDto>();
ctnList.ForEach(b =>
{
if (b.CTNNUM == 1)
{
model.ContaList.Add(new BookingTruckCtnDto
{
CTNCODE = b.CTNCODE,
CTNALL = b.CTNALL,
KGS = b.KGS,
PKGS = b.PKGS,
CBM = b.CBM,
TAREWEIGHT = b.TAREWEIGHT,
CNTRNO = b.CNTRNO,
KINDPKGS = b.KINDPKGS,
SEALNO = b.SEALNO,
TEU = b.TEU,
CTNNUM = b.CTNNUM,
});
}
else
{
for (int i = 0; i < b.CTNNUM; i++)
{
model.ContaList.Add(new BookingTruckCtnDto
{
CTNCODE = b.CTNCODE,
CTNALL = b.CTNALL,
KGS = b.KGS,
PKGS = b.PKGS,
CBM = b.CBM,
TAREWEIGHT = b.TAREWEIGHT,
CNTRNO = b.CNTRNO,
KINDPKGS = b.KINDPKGS,
SEALNO = b.SEALNO,
TEU = b.TEU,
CTNNUM = b.CTNNUM,
});
}
}
});
}
return model;
}
/// <summary>
/// 引入订舱详情生成派车信息
/// </summary>
/// <param name="bookingId">订舱主键</param>
/// <returns>返回派车初始信息</returns>
[HttpGet("/BookingTruck/PullInBookingOrder")]
public async Task<TaskManageOrderResultDto> PullInBookingOrder(long bookingId)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
var model = InnerCreateTruckFromBookingOrder(bookingId);
result.succ = true;
result.ext = model;
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"引入订舱详情生成派车信息异常,原因:{ex.Message}";
}
return result;
}
/// <summary>
/// 引入订舱集装箱详情生成派车信息
/// </summary>
/// <param name="bookingId">订舱主键</param>
/// <returns>返回派车集装箱初始信息</returns>
[HttpGet("/BookingTruck/PullInBookingOrderConta")]
public async Task<TaskManageOrderResultDto> PullInBookingOrderConta(long bookingId)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
var model = InnerCreateTruckFromBookingOrder(bookingId);
result.succ = true;
if (model != null && model.ContaList != null && model.ContaList.Count > 0)
{
result.ext = model.ContaList;
}
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"引入订舱详情生成派车信息异常,原因:{ex.Message}";
}
return result;
}
/// <summary>
/// 提交派车
/// </summary>
/// <param name="info">派车信息</param>
/// <returns>返回回执</returns>
[HttpPost("/BookingTruck/Submit")]
public async Task<TaskManageOrderResultDto> Submit(BookingTruckDto info)
{
string batchNo = IDGen.NextID().ToString();
_logger.LogInformation("批次={no}获取提交派车请求 {id}", batchNo, info.Id);
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
result = await InnerSubmit(info, batchNo);
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"提交异常,原因:{ex.Message}";
}
return result;
}
#region 提交内部方法
/// <summary>
/// 提交内部方法
/// </summary>
/// <param name="info">派车信息</param>
/// <param name="batchNo">批次号</param>
/// <returns>返回回执</returns>
private async Task<TaskManageOrderResultDto> InnerSubmit(BookingTruckDto info, string batchNo, bool isDefaultSave = true)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
/*
提交派车流程
1、先保存一遍派车信息。需校验当前派车信息未提交只有暂存的才能保存
2、调取派车任务接口推送派车任务。
3、派车任务推送成功后更新状态、任务ID。
4、返回结果。
*/
try
{
//先保存
var id = info.Id;
if (isDefaultSave)
id = await InnerSave(info);
//校验
var model = _bookingTruckRepository.AsQueryable().First(a => a.Id == id);
if (model == null)
throw Oops.Oh($"派车信息获取失败,派车信息不存在或已作废");
//校验
ValidateTruck(OperateTypeEnum.Submit, new BookingTruck[] { model });
var contaList = _bookingTruckContaRepository.AsQueryable().Where(a => a.TruckId == id).ToList();
DateTime bDate = DateTime.Now;
TaskManageOrderMessageInfo messageInfo = new TaskManageOrderMessageInfo
{
Head = new TaskManageOrderMessageHeadInfo
{
GID = id.ToString(),//直接用派车的主键
MessageType = "TASK",
SenderId = "BookingOrder",
SenderName = "订舱派车",
ReceiverId = "TaskManage",
ReceiverName = "派车任务",
RequestDate = bDate.ToString("yyyy-MM-dd HH:mm:ss.fff"),
Version = "1.0",
RequestAction = "Add"
},
Main = new TaskManageOrderMessageMainInfo
{
TaskType = TaskBaseTypeEnum.TRUCK_DISPATCH,
TruckInfo = model.Adapt<TaskManageOrderTruckInfo>(),
}
};
if (contaList.Count > 0)
messageInfo.Main.TruckInfo.ContaList = contaList.Adapt<List<TaskManageOrderTruckContaInfo>>();
messageInfo.Main.TaskUserId = UserManager.UserId.ToString();
messageInfo.Main.TaskUserName = UserManager.Name;
messageInfo.Main.TaskTitle = $"{model.MBLNo}_派车任务";
messageInfo.Main.TaskDesp = $"{model.MBLNo}_派车任务";
messageInfo.Main.TaskSource = TaskSourceEnum.CUSTOMER_SUBMIT;
_logger.LogInformation("批次={no} 请求报文msg={msg}", batchNo, JSON.Serialize(messageInfo));
//推送新增派车任务接口
var taskRlt = await _taskManageExternalService.SubmitTruckDispatchAsync(messageInfo);
DateTime eDate = DateTime.Now;
TimeSpan ts = eDate.Subtract(bDate);
var timeDiff = ts.TotalMilliseconds;
_logger.LogInformation("批次={no} 请求完成,耗时:{timeDiff}ms. 结果{msg}", batchNo, timeDiff, JSON.Serialize(taskRlt));
if (!taskRlt.succ)
{
throw Oops.Oh($"请求派车调度失败,原因={taskRlt.msg}");
}
//更新派车订单为已提交
model.Status = BookingTruckStatus.SUBMITED.ToString();
model.UpdatedTime = DateTime.Now;
model.UpdatedUserId = UserManager.UserId;
model.UpdatedUserName = UserManager.Name;
//提取任务流水号
if (taskRlt.ext != null)
model.TaskNo = taskRlt.ext.ToString();
await _bookingTruckRepository.AsUpdateable(model).UpdateColumns(it => new
{
it.Status,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
result.succ = true;
result.msg = "提交成功";
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"提交异常,原因:{ex.Message}";
}
return result;
}
#endregion
#region 批量提交派车
/// <summary>
/// 批量提交派车
/// </summary>
/// <param name="ids">派车主键组</param>
/// <returns>返回回执</returns>
[HttpPost("/BookingTruck/SubmitBatch")]
public async Task<TaskManageOrderResultDto> SubmitBatch([FromBody] long[] ids)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
string batchNo = IDGen.NextID().ToString();
_logger.LogInformation("批次={no}获取提交派车请求 {ids}", batchNo, ids);
try
{
var list = _bookingTruckRepository.AsQueryable()
.Where(a => ids.Contains(a.Id)).ToList();
if(list.Count != ids.Length)
throw Oops.Oh($"部分派车信息获取失败,不能提交,派车信息不存在或已作废");
//校验
ValidateTruck(OperateTypeEnum.Submit, list.ToArray());
List<Task<TaskManageOrderResultDto>> taskList = new List<Task<TaskManageOrderResultDto>>();
list.ForEach(async tk => {
taskList.Add(InnerSubmit(new BookingTruckDto { Id = tk.Id }, batchNo, false));
});
//等待所有结果
Task.WaitAll(taskList.ToArray(),20000);
result.succ = true;
result.msg = "提交成功";
List<string> msgList = new List<string>();
taskList.ForEach(async a => {
var rlt = a.Result;
if (!rlt.succ)
{
result.succ = false;
msgList.Add($"派车单号:{rlt.ext.ToString()}{rlt.msg}");
result.msg = "提交失败";
}
});
result.ext = msgList;
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"批量提交派车异常,原因:{ex.Message}";
}
return result;
}
#endregion
/// <summary>
/// 撤销派车
/// </summary>
/// <param name="id">派车主键</param>
/// <returns>返回回执</returns>
[HttpGet("/BookingTruck/Cancel")]
public async Task<TaskManageOrderResultDto> Cancel(long id)
{
/*
撤销派车流程
1、确定当前派车已提交。
2、调取取消派车任务接口申请撤销派车。
3、取消派车推送成功后更新状态。
4、返回结果。
*/
string batchNo = IDGen.NextID().ToString();
_logger.LogInformation("批次={no}获取撤销派车请求 {id}", batchNo, id);
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
result = await InnerCancel(id, batchNo);
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"撤销异常,原因:{ex.Message}";
}
return result;
}
/// <summary>
/// 批量撤销派车
/// </summary>
/// <param name="ids">派车主键组</param>
/// <returns>返回回执</returns>
public async Task<TaskManageOrderResultDto> CancelBatch([FromBody] long[] ids)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
/*
批量撤销派车流程
1、确定当前派车已提交。
2、调取取消派车任务接口申请撤销派车。
3、取消派车推送成功后更新状态。
4、返回结果。
*/
string batchNo = IDGen.NextID().ToString();
_logger.LogInformation("批次={no}获取撤销派车请求 {ids}", batchNo, ids);
try
{
var list = _bookingTruckRepository.AsQueryable()
.Where(a => ids.Contains(a.Id)).ToList();
if (list.Count != ids.Length)
throw Oops.Oh($"部分派车信息获取失败,不能撤销,派车信息不存在或已作废");
//校验
ValidateTruck(OperateTypeEnum.Submit, list.ToArray());
List<Task<TaskManageOrderResultDto>> taskList = new List<Task<TaskManageOrderResultDto>>();
list.ForEach(async tk => {
taskList.Add(InnerCancel(tk.Id, batchNo));
});
//等待所有结果
Task.WaitAll(taskList.ToArray(), 20000);
result.succ = true;
result.msg = "撤销成功";
List<string> msgList = new List<string>();
taskList.ForEach(async a => {
var rlt = a.Result;
if (!rlt.succ)
{
result.succ = false;
msgList.Add($"派车单号:{rlt.ext.ToString()}{rlt.msg}");
result.msg = "撤销失败";
}
});
result.ext = msgList;
}
catch (Exception ex)
{
}
return result;
}
#region 内部撤销派车
/// <summary>
/// 内部撤销派车
/// </summary>
/// <param name="id">派车主键</param>
/// <param name="batchNo">批次号</param>
/// <returns>返回回执</returns>
private async Task<TaskManageOrderResultDto> InnerCancel(long id,string batchNo)
{
/*
撤销派车流程
1、确定当前派车已提交。
2、调取取消派车任务接口申请撤销派车。
3、取消派车推送成功后更新状态。
4、返回结果。
*/
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
//校验
var model = _bookingTruckRepository.AsQueryable().First(a => a.Id == id);
if (model == null)
throw Oops.Oh($"派车信息获取失败,派车信息不存在或已作废");
//校验
ValidateTruck(OperateTypeEnum.Cancel, new BookingTruck[] { model });
DateTime bDate = DateTime.Now;
TaskManageOrderMessageInfo messageInfo = new TaskManageOrderMessageInfo
{
Head = new TaskManageOrderMessageHeadInfo
{
GID = id.ToString(),//直接用派车的主键
MessageType = "TASK",
SenderId = "BookingOrder",
SenderName = "订舱派车",
ReceiverId = "TaskManage",
ReceiverName = "派车任务",
RequestDate = bDate.ToString("yyyy-MM-dd HH:mm:ss.fff"),
Version = "1.0",
RequestAction = "Add"
},
Main = new TaskManageOrderMessageMainInfo
{
TaskType = TaskBaseTypeEnum.TRUCK_DISPATCH,
TruckInfo = model.Adapt<TaskManageOrderTruckInfo>(),
}
};
messageInfo.Main.TaskUserId = UserManager.UserId.ToString();
messageInfo.Main.TaskUserName = UserManager.Name;
messageInfo.Main.TaskTitle = $"{model.MBLNo}_派车任务";
messageInfo.Main.TaskDesp = $"{model.MBLNo}_派车任务";
messageInfo.Main.TaskSource = TaskSourceEnum.CUSTOMER_SUBMIT;
_logger.LogInformation("批次={no} 请求报文msg={msg}", batchNo, JSON.Serialize(messageInfo));
//推送新增派车任务接口
var taskRlt = await _taskManageExternalService.CancelTruckDispatchAsync(messageInfo);
DateTime eDate = DateTime.Now;
TimeSpan ts = eDate.Subtract(bDate);
var timeDiff = ts.TotalMilliseconds;
_logger.LogInformation("批次={no} 请求完成,耗时:{timeDiff}ms. 结果{msg}", batchNo, timeDiff, JSON.Serialize(taskRlt));
if (!taskRlt.succ)
{
throw Oops.Oh($"请求撤销派车调度失败,原因={taskRlt.msg}");
}
//更新派车订单为已提交
model.Status = BookingTruckStatus.CANCELED.ToString();
model.UpdatedTime = DateTime.Now;
model.UpdatedUserId = UserManager.UserId;
model.UpdatedUserName = UserManager.Name;
model.TaskNo = null;
await _bookingTruckRepository.AsUpdateable(model).UpdateColumns(it => new
{
it.Status,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName,
it.TaskNo
}).ExecuteCommandAsync();
result.succ = true;
result.msg = "撤销成功";
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"撤销异常,原因:{ex.Message}";
}
return result;
}
#endregion
/// <summary>
/// 删除派车
/// </summary>
/// <param name="id">派车主键</param>
/// <returns>返回回执</returns>
public async Task<TaskManageOrderResultDto> Delete(long id)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
//检索
var truckOrder = _bookingTruckRepository.AsQueryable().First(a => a.Id == id);
if(truckOrder == null)
throw Oops.Oh($"派车信息获取失败,派车信息不存在或已作废");
//先校验
ValidateTruck(OperateTypeEnum.Delete, new BookingTruck[] { truckOrder });
await _bookingTruckRepository.UpdateAsync(x => x.Id == id,
x => new BookingTruck { IsDeleted = true });
result.succ = true;
result.msg = "删除成功";
_logger.LogInformation("删除派车成功 id={id} user={usr}", id, UserManager.UserId);
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"删除派车异常,原因:{ex.Message}";
}
return result;
}
/// <summary>
/// 批量删除派车
/// </summary>
/// <param name="ids">派车主键组</param>
/// <returns>返回回执</returns>
public async Task<TaskManageOrderResultDto> DeleteBatch(long[] ids)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
//检索
var truckOrderList = _bookingTruckRepository.AsQueryable()
.Where(a => ids.Contains(a.Id)).ToList();
if (truckOrderList.Count != ids.Length)
throw Oops.Oh($"部分派车信息获取失败,派车信息不存在或已作废");
//先校验
ValidateTruck(OperateTypeEnum.Delete, truckOrderList.ToArray());
truckOrderList.ForEach(async tk => {
await _bookingTruckRepository.UpdateAsync(x => x.Id == tk.Id,
x => new BookingTruck { IsDeleted = true });
});
result.succ = true;
result.msg = "删除成功";
_logger.LogInformation("删除派车成功 ids={id} user={usr}", ids, UserManager.UserId);
}
catch (Exception ex)
{
}
return result;
}
/// <summary>
/// 打印派车
/// </summary>
/// <param name="id">派车主键</param>
/// <returns>返回回执</returns>
[HttpGet("/BookingTruck/Print")]
public async Task<TaskManageOrderResultDto> Print(long id)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
}
catch (Exception ex)
{
}
return result;
}
#region 校验派车
/// <summary>
/// 校验派车
/// </summary>
/// <param name="operateType">操作类型</param>
/// <param name="entityArg">派车信息列表</param>
private void ValidateTruck(OperateTypeEnum operateType, BookingTruck[] 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
}
}