using Furion;
using Furion.EventBus;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Myshipping.Application.ConfigOption;
using Myshipping.Application.Entity;
using Myshipping.Application.Service.BookingOrder.Dto;
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 BookingSyncSubscriber : IEventSubscriber
{
private IServiceProvider _services { get; }
private readonly ILogger _logger;
public BookingSyncSubscriber(IServiceProvider services, ILogger logger)
{
_services = services;
_logger = logger;
}
//发送订舱同步数据给客户订舱系统
[EventSubscribe("SendToCustomer:Book")]
public async Task CreateOpLog(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到订舱同步客户订舱系统请求:{context.Source.Payload}");
var bookId = (long)context.Source.Payload;
using var scope = _services.CreateScope();
var repoCutomerOrder = scope.ServiceProvider.GetRequiredService>();
var repoOrder = scope.ServiceProvider.GetRequiredService>();
var repoCtn = scope.ServiceProvider.GetRequiredService>();
var repoFile = scope.ServiceProvider.GetRequiredService>();
var repoStaLog = scope.ServiceProvider.GetRequiredService>();
var repoStaLogDetail = scope.ServiceProvider.GetRequiredService>();
var repoGoodsSta = scope.ServiceProvider.GetRequiredService>();
var repoGoodsStaCfg = scope.ServiceProvider.GetRequiredService>();
var custOrder = await repoCutomerOrder.AsQueryable().Filter(null, true).FirstAsync(x => x.BookingId == bookId);
if (custOrder == null) //非客户订舱系统过来的数据
{
_logger.LogInformation($"ID为 {bookId} 的数据并非来自客户订舱系统,不继续处理数据回推");
return;
}
var order = await repoOrder.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == bookId);
var ctns = await repoCtn.AsQueryable().Filter(null, true).Where(x => x.BILLID == bookId).ToListAsync();
var files = await repoFile.AsQueryable().Filter(null, true).Where(x => x.BookingId == bookId).ToListAsync();
var staLogs = await repoStaLog.AsQueryable().Filter(null, true).Where(x => x.BookingId == bookId).ToListAsync();
var staLogDetails = await repoStaLogDetail.AsQueryable().Filter(null, true).Where(x => staLogs.Select(y => y.Id).Contains(x.PId)).ToListAsync();
var goodsLogs = await repoGoodsSta.AsQueryable().Filter(null, true).Where(x => x.bookingId == bookId).ToListAsync();
var goodsLogConfigs = await repoGoodsStaCfg.AsQueryable().Filter(null, true).Where(x => goodsLogs.Select(y => y.ConfigId).Contains(x.Id)).ToListAsync();
//把ID还原为客户订舱系统中的ID
order.Id = Convert.ToInt64(order.BSNO);
order.BSNO = null;
ctns.ForEach(x => x.BILLID = order.Id);
files.ForEach(x => x.BookingId = order.Id);
staLogs.ForEach(x => x.BookingId = order.Id);
goodsLogs.ForEach(x => x.bookingId = order.Id);
var staLogSendList = staLogs.Adapt>();
staLogSendList.ForEach(x =>
{
x.Details = staLogDetails.Where(y => y.PId == x.Id).Adapt>();
});
//发送对象
var sendObj = new BookingCustomerRecDataFeedbackDto()
{
Order = order.Adapt(),
Ctns = ctns.Adapt>(),
Files = files.Adapt>(),
StatusLogs = staLogSendList
};
//文件内容
var opt = App.GetOptions();
var dirAbs = opt.basePath;
if (string.IsNullOrEmpty(dirAbs))
{
dirAbs = App.WebHostEnvironment.WebRootPath;
}
foreach (var file in files)
{
var fileFullPath = Path.Combine(dirAbs, file.FilePath);
if (File.Exists(fileFullPath))
{
sendObj.Files.First(x => x.Id == file.Id).FileContent = File.ReadAllBytes(fileFullPath);
}
}
_logger.LogInformation($"准备发送客户订舱数据同步:{sendObj.ToJsonString()}");
}
}
}