@ -13,6 +13,10 @@ using Myshipping.Application.Helper;
using Myshipping.Core ;
using Myshipping.Core.Entity ;
using Myshipping.Core.Service ;
using MySqlX.XDevAPI.Common ;
using NPOI.OpenXmlFormats.Wordprocessing ;
using NPOI.SS.Formula.Functions ;
using Org.BouncyCastle.Crypto ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
@ -36,13 +40,14 @@ namespace Myshipping.Application
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 )
SqlSugarRepository < BookingCtn > bookingCtnRepository ,
ITaskManageExternalService taskManageExternalService )
{
_cache = cache ;
_logger = logger ;
@ -51,6 +56,8 @@ namespace Myshipping.Application
_bookingTruckContaRepository = bookingTruckContaRepository ;
_bookingOrderRepository = bookingOrderRepository ;
_bookingCtnRepository = bookingCtnRepository ;
_taskManageExternalService = taskManageExternalService ;
}
/// <summary>
@ -65,7 +72,7 @@ namespace Myshipping.Application
try
{
var id = InnerSave ( info ) ;
var id = await InnerSave ( info ) ;
result . succ = true ;
result . msg = "保存成功" ;
@ -86,6 +93,7 @@ namespace Myshipping.Application
/// </summary>
/// <param name="info">派车信息</param>
/// <returns>返回派车Id</returns>
[SqlSugarUnitOfWork]
private async Task < long > InnerSave ( BookingTruckDto info )
{
BookingTruck entity = info . Adapt < BookingTruck > ( ) ;
@ -97,6 +105,8 @@ namespace Myshipping.Application
if ( entity . Id = = 0 )
{
entity . Status = BookingTruckStatus . TEMP . ToString ( ) ;
_bookingTruckRepository . Insert ( entity ) ;
if ( entityCtnList ! = null & & entityCtnList . Count > 0 )
@ -111,6 +121,18 @@ namespace Myshipping.Application
}
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 ,
@ -122,22 +144,27 @@ namespace Myshipping.Application
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.AsUpdateable(ctn).IgnoreColumns(it => new
//{
// it.TenantId,
// it.CreatedTime,
// it.CreatedUserId,
// it.CreatedUserName,
// it.IsDeleted,
//}).ExecuteCommandAsync();
await _bookingTruckContaRepository . InsertAsync ( ctn ) ;
} ) ;
}
}
@ -365,6 +392,21 @@ namespace Myshipping.Application
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 ;
@ -510,6 +552,33 @@ namespace Myshipping.Application
/// <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 ;
}
/// <summary>
/// 提交内部方法
/// </summary>
/// <param name="info">派车信息</param>
/// <param name="batchNo">批次号</param>
/// <returns>返回回执</returns>
private async Task < TaskManageOrderResultDto > InnerSubmit ( BookingTruckDto info , string batchNo )
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto ( ) ;
@ -522,17 +591,56 @@ namespace Myshipping.Application
* /
try
{
//先保存
var id = InnerSave ( info ) ;
var 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 } ) ;
DateTime bDate = DateTime . Now ;
//推送新增派车任务接口
var msgModel = model . Adapt < TaskManageOrderMessageInfo > ( ) ;
var taskRlt = await _taskManageExternalService . SubmitTruckDispatchAsync ( msgModel ) ;
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}" ) ;
}
catch ( Exception ex )
//更新派车订单为已提交
model . Status = BookingTruckStatus . SUBMITED . ToString ( ) ;
model . UpdatedTime = DateTime . Now ;
model . UpdatedUserId = UserManager . UserId ;
model . UpdatedUserName = UserManager . Name ;
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 ;
@ -665,6 +773,82 @@ namespace Myshipping.Application
return result ;
}
/// <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>