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/Event/BookingSyncSubscriber.cs

112 lines
5.2 KiB
C#

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
{
/// <summary>
/// 订舱同步给客户订舱系统
/// </summary>
public class BookingSyncSubscriber : IEventSubscriber
{
private IServiceProvider _services { get; }
private readonly ILogger<BookingSyncSubscriber> _logger;
public BookingSyncSubscriber(IServiceProvider services, ILogger<BookingSyncSubscriber> 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<SqlSugarRepository<BookingCustomerOrder>>();
var repoOrder = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingOrder>>();
var repoCtn = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingCtn>>();
var repoFile = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingFile>>();
var repoStaLog = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingStatusLog>>();
var repoStaLogDetail = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingStatusLogDetail>>();
var repoGoodsSta = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingGoodsStatus>>();
var repoGoodsStaCfg = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingGoodsStatusConfig>>();
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<List<BookingStatusLogSyncCustomerDto>>();
staLogSendList.ForEach(x =>
{
x.Details = staLogDetails.Where(y => y.PId == x.Id).Adapt<List<BookingStatusLogDetailSyncCustomerDto>>();
});
//发送对象
var sendObj = new BookingCustomerRecDataFeedbackDto()
{
Order = order.Adapt<BookingOrderSyncCustomerDto>(),
Ctns = ctns.Adapt<List<BookingCtnSyncCustomerDto>>(),
Files = files.Adapt<List<BookingFileSyncCustomerDto>>(),
StatusLogs = staLogSendList
};
//文件内容
var opt = App.GetOptions<BookingAttachOptions>();
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()}");
}
}
}