using Furion; using Furion.EventBus; using Furion.FriendlyException; using Furion.RemoteRequest.Extensions; 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 Myshipping.Core.Service; using Newtonsoft.Json.Linq; 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 cacheService = 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); } } //回推回执 var recFeedbackConfig = cacheService.GetAllSysConfig().Result.FirstOrDefault(x => x.Code == "DjyBookingCustomerRecFeedbackURL"); if (recFeedbackConfig == null || string.IsNullOrEmpty(recFeedbackConfig.Value)) { throw Oops.Bah("回推订舱数据的URL地址未配置,请联系管理员"); } var recFeedbackUserKey = cacheService.GetAllSysConfig().Result.FirstOrDefault(x => x.Code == "DjyBookingCustomerRecFeedbackUserKey"); if (recFeedbackUserKey == null || string.IsNullOrEmpty(recFeedbackUserKey.Value)) { throw Oops.Bah("回推订舱数据的用户KEY未配置,请联系管理员"); } var recFeedbackUserSecret = cacheService.GetAllSysConfig().Result.FirstOrDefault(x => x.Code == "DjyBookingCustomerRecFeedbackUserSecret"); if (recFeedbackUserSecret == null || string.IsNullOrEmpty(recFeedbackUserSecret.Value)) { throw Oops.Bah("回推订舱数据的用户秘钥未配置,请联系管理员"); } var feedbackUrl = recFeedbackConfig.Value; if (!feedbackUrl.EndsWith("/")) { feedbackUrl += "/"; } feedbackUrl += "BookingCustomerOrder/RecBookingDataFeedback"; _logger.LogInformation($"准备发送客户订舱数据同步:{sendObj.ToJsonString()},URL:{feedbackUrl}"); var rtn = await feedbackUrl .SetHeaders(new Dictionary { { CommonConst.API_USER_HEADER_KEY, recFeedbackUserKey.Value}, { CommonConst.API_USER_HEADER_SECRET, recFeedbackUserSecret.Value} }) .SetBody(sendObj) .PostAsStringAsync(); _logger.LogInformation($"回推审核返回:{rtn}"); var jobjRtn = JObject.Parse(rtn); if (jobjRtn.GetIntValue("code") != 200) { throw Oops.Bah(jobjRtn.GetStringValue("message")); } } } }