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.Core;
using Myshipping.Core.Entity;
using Myshipping.Core.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application
{
///
/// 派车服务
///
[ApiDescriptionSettings("Application", Name = "BookingTruck", Order = 9)]
public class BookingTruckService : IBookingTruckService, IDynamicApiController, ITransient
{
private readonly ISysCacheService _cache;
private readonly ILogger _logger;
private readonly SqlSugarRepository _bookingTruckRepository;
private readonly SqlSugarRepository _bookingTruckContaRepository;
private readonly SqlSugarRepository _bookingOrderRepository;
private readonly SqlSugarRepository _bookingCtnRepository;
public BookingTruckService(ISysCacheService cache, ILogger logger,
SqlSugarRepository bookingTruckRepository,
SqlSugarRepository bookingTruckContaRepository,
SqlSugarRepository bookingOrderRepository,
SqlSugarRepository bookingCtnRepository)
{
_cache = cache;
_logger = logger;
_bookingTruckRepository = bookingTruckRepository;
_bookingTruckContaRepository = bookingTruckContaRepository;
_bookingOrderRepository = bookingOrderRepository;
_bookingCtnRepository = bookingCtnRepository;
}
///
/// 保存派车
///
/// 派车信息
/// 返回回执
[HttpPost("/BookingTruck/Save")]
public async Task Save(BookingTruckDto info)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
BookingTruck entity = info.Adapt();
if(entity == null)
throw Oops.Oh($"派车信息不能为空");
List entityCtnList = info.ContaList.Adapt>();
if (entity.Id == 0)
{
_bookingTruckRepository.Insert(entity);
if (entityCtnList.Count > 0)
{
entityCtnList.ForEach(async ctn =>
{
ctn.TruckId = entity.Id;
await _bookingTruckContaRepository.InsertAsync(ctn);
});
}
}
else
{
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,
}).ExecuteCommandAsync();
if (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();
});
}
}
result.succ = true;
result.msg = "保存成功";
result.ext = entity.Id;
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"保存派车异常,原因:{ex.Message}";
}
return result;
}
///
/// 获取派车详情
///
/// 派车主键
/// 返回回执
[HttpGet("/BookingTruck/GetInfo")]
public async Task 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();
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("/BookingTruck/InitFromBookingOrder")]
public async Task 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;
}
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();
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;
}
///
/// 引入订舱详情生成派车信息
///
/// 订舱主键
/// 返回派车初始信息
[HttpGet("/BookingTruck/PullInBookingOrder")]
public async Task 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;
}
///
/// 引入订舱集装箱详情生成派车信息
///
/// 订舱主键
/// 返回派车集装箱初始信息
[HttpGet("/BookingTruck/PullInBookingOrderConta")]
public async Task PullInBookingOrderConta(long bookingId)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
}
catch (Exception ex)
{
}
return result;
}
///
/// 提交派车
///
/// 派车信息
/// 返回回执
[HttpPost("/BookingTruck/Submit")]
public async Task Submit(BookingTruckDto info)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
}
catch (Exception ex)
{
}
return result;
}
///
/// 批量提交派车
///
/// 派车主键组
/// 返回回执
[HttpPost("/BookingTruck/SubmitBatch")]
public async Task SubmitBatch([FromBody] long[] ids)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
}
catch (Exception ex)
{
}
return result;
}
///
/// 撤销派车
///
/// 派车主键
/// 返回回执
[HttpGet("/BookingTruck/Cancel")]
public async Task Cancel(long id)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
}
catch (Exception ex)
{
}
return result;
}
///
/// 批量撤销派车
///
/// 派车主键组
/// 返回回执
public async Task CancelBatch([FromBody] long[] ids)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
}
catch (Exception ex)
{
}
return result;
}
///
/// 打印派车
///
/// 派车主键
/// 返回回执
[HttpGet("/BookingTruck/Print")]
public async Task Print(long id)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
}
catch (Exception ex)
{
}
return result;
}
///
/// 批量派车
///
/// 派车主键组
/// 返回回执
[HttpPost("/BookingTruck/SendDispatchBatch")]
public async Task SendDispatchBatch([FromBody] long[] ids)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
}
catch (Exception ex)
{
}
return result;
}
///
/// 取消派车
///
/// 派车主键
/// 返回回执
[HttpGet("/BookingTruck/CancelDispatch")]
public async Task CancelDispatch(long id)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
}
catch (Exception ex)
{
}
return result;
}
///
/// 批量取消派车
///
/// 派车主键组
/// 返回回执
[HttpPost("/BookingTruck/CancelDispatchBatch")]
public async Task CancelDispatchBatch([FromBody] long[] ids)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
}
catch (Exception ex)
{
}
return result;
}
}
}