using Furion.DependencyInjection;
using Furion.EventBus;
using Furion.RemoteRequest.Extensions;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Myshipping.Application.Entity;
using Myshipping.Application.MQ;
using Myshipping.Core;
using Myshipping.Core.Entity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application.Event
{
///
/// 订舱自动化事件处理
///
public class BookingAutoSubscriber : IEventSubscriber
{
private IServiceProvider _services { get; }
private readonly ILogger _logger;
private readonly INamedServiceProvider _namedBookingSlotServiceProvider;
public BookingAutoSubscriber(IServiceProvider services, ILogger logger)
{
_services = services;
_logger = logger;
}
//收到BC
[EventSubscribe("BookingAuto:BC")]
public async Task BookingAutoBC(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到订舱自动化BC消息:{context.Source.Payload}");
var msgBC = context.Source.Payload as BookingAutoMessageBC;
var scope = _services.CreateScope();
var repoBooking = scope.ServiceProvider.GetRequiredService>();
var repoStatuslog = scope.ServiceProvider.GetRequiredService>();
var bookingfile = scope.ServiceProvider.GetRequiredService>();
var booking = await repoBooking.AsQueryable().Filter(null, true).FirstAsync(x => x.CUSTNO == msgBC.BookingNO && x.TenantId == msgBC.TenantId);
if (booking != null)
{
booking.MBLNO = msgBC.MBLNO; //回写提单号
await repoBooking.AsUpdateable(booking).UpdateColumns(x => new { x.MBLNO }).ExecuteCommandAsync();
//货运动态
var bsl = new BookingStatusLog();
bsl.BookingId = booking.Id;
bsl.Status = $"收到BC";
bsl.OpTime = DateTime.Now;
bsl.Category = "ship";
bsl.MBLNO = booking.MBLNO;
await repoStatuslog.InsertAsync(bsl);
//挂载附件
var newFile = new BookingFile
{
FileName = Path.GetFileName(msgBC.AttachUrl),
FilePath = msgBC.AttachUrl,
TypeCode = "BC",
TypeName = ".pdf",
BookingId = booking.Id,
};
await bookingfile.InsertAsync(newFile);
}
}
//收到LARA提单号
[EventSubscribe("BookingAuto:LaraBlno")]
public async Task BookingAutoLaraBlno(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到订舱自动化LARA提单号消息:{context.Source.Payload}");
var msgBC = context.Source.Payload as BookingAutoMessageLaraBlno;
var scope = _services.CreateScope();
var repoBooking = scope.ServiceProvider.GetRequiredService>();
var repoStatuslog = scope.ServiceProvider.GetRequiredService>();
var bookingfile = scope.ServiceProvider.GetRequiredService>();
var booking = await repoBooking.AsQueryable().Filter(null, true).FirstAsync(x => x.CUSTNO == msgBC.CsrCode && x.TenantId == msgBC.TenantId);
if (booking != null)
{
booking.MBLNO = msgBC.MBLNO; //回写提单号
await repoBooking.AsUpdateable(booking).UpdateColumns(x => new { x.MBLNO }).ExecuteCommandAsync();
}
}
//收到INTTRA BC
[EventSubscribe("BookingAuto:InttraBc")]
public async Task BookingAutoInttraBc(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到订舱自动化InttraBc消息:{context.Source.Payload}");
var msgBC = context.Source.Payload as BookingAutoMessageInttraBC;
var scope = _services.CreateScope();
var repoBooking = scope.ServiceProvider.GetRequiredService>();
var booking = await repoBooking.AsQueryable().Filter(null, true).FirstAsync(x => x.CUSTNO == msgBC.BookingNO && x.TenantId == msgBC.TenantId);
if (booking != null)
{
booking.MBLNO = msgBC.MBLNO; //回写提单号
await repoBooking.AsUpdateable(booking).UpdateColumns(x => new { x.MBLNO }).ExecuteCommandAsync();
}
}
//收到CMA VGM Missing
[EventSubscribe("BookingAuto:CmaVgmMissing")]
public async Task BookingAutoCmaVgmMissing(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到订舱自动化CMA VGM Missing消息:{context.Source.Payload}");
var msgVgmMissing = context.Source.Payload as BookingAutoMessageCmaVgmMissing;
var scope = _services.CreateScope();
var repoBooking = scope.ServiceProvider.GetRequiredService>();
var repoCtn = scope.ServiceProvider.GetRequiredService>();
var mblnoList = msgVgmMissing.DetailData.Select(x => x.MBLNO).Distinct().ToList();
foreach (var mblno in mblnoList)
{
var booking = await repoBooking.AsQueryable().Filter(null, true).FirstAsync(x => x.MBLNO == mblno && x.TenantId == msgVgmMissing.TenantId);
if (booking != null)
{
var ctns = await repoCtn.AsQueryable().Filter(null, true).Where(x => x.IsDeleted == false && x.BILLID == booking.Id).ToListAsync();
foreach (var ctnMiss in msgVgmMissing.DetailData.Where(x => x.MBLNO == mblno))
{
var ctn = ctns.FirstOrDefault(x => x.CNTRNO == ctnMiss.CtnNO);
if (ctn != null)
{
ctn.CTNSTATUS = "Missing";
await repoCtn.AsUpdateable(ctn).UpdateColumns(x => new { x.CTNSTATUS }).ExecuteCommandAsync();
}
}
}
}
}
}
}