using Furion.EventBus; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Myshipping.Core.Entity; using Myshipping.Core; using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Newtonsoft.Json.Linq; using Newtonsoft.Json; namespace Myshipping.Application.MQ { public class BookingAutoService : BackgroundService { private readonly IServiceScopeFactory _scopeFactory; private readonly IServiceScope _serviceScope; private readonly ILogger _logger; private readonly IEventPublisher _publisher; private IConnection mqConn; private IModel model; public BookingAutoService(IServiceScopeFactory scopeFactory) { _scopeFactory = scopeFactory; //通过这个注入DBContext _serviceScope = _scopeFactory.CreateScope(); _logger = _serviceScope.ServiceProvider.GetService>(); _publisher = _serviceScope.ServiceProvider.GetService(); } protected override Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("BookingAutoService ExecuteAsync"); return Task.Run(() => { _logger.LogInformation("BookingAutoService ExecuteAsync RunTask"); //绑定队列 BindMQ(); }); } private void BindMQ() { string ExchangeName = "djy.booking.auto"; string QueueName = $"djy.booking.auto.{DateTime.Now.Ticks}"; ConnectionFactory factory = new ConnectionFactory(); var repoSysCfg = _serviceScope.ServiceProvider.GetService>(); var mqUrl = repoSysCfg.FirstOrDefault(x => x.Code == "DjyBookingAutoMQUrl")?.Value; if (string.IsNullOrEmpty(mqUrl)) { _logger.LogError($"接收订舱自动化消息推送所需MQUrl未配置"); } else { _logger.LogInformation($"准备连接订舱自动化消息队列:{mqUrl}"); factory.Uri = new Uri(mqUrl); mqConn = factory.CreateConnection("大简云订舱平台"); model = mqConn.CreateModel(); model.ExchangeDeclare(ExchangeName, ExchangeType.Topic); model.QueueDeclare(QueueName, false, false, true, null); model.QueueBind(QueueName, ExchangeName, "*", null); var consumer = new EventingBasicConsumer(model); consumer.Received += (obj, arg) => { var body = arg.Body; var strBody = Encoding.UTF8.GetString(body.ToArray()); _logger.LogInformation($"收到订舱自动化消息队列:{strBody}"); if (arg.RoutingKey == "BC") //收到BC { var dto = JsonConvert.DeserializeObject(strBody); _publisher.PublishAsync(new ChannelEventSource("BookingAuto:BC", dto)); } else if (arg.RoutingKey == "DraftDownloadOk") //DRAFT下载完成 { } else if (arg.RoutingKey == "LaraBlno") //根据Csr号从LARA拉取到提单号 { var dto = JsonConvert.DeserializeObject(strBody); _publisher.PublishAsync(new ChannelEventSource("BookingAuto:LaraBlno", dto)); } else if (arg.RoutingKey == "LaraBlno") //根据Csr号从LARA拉取到提单号 { var dto = JsonConvert.DeserializeObject(strBody); _publisher.PublishAsync(new ChannelEventSource("BookingAuto:LaraBlno", dto)); } else if (arg.RoutingKey == "InttraBc") //Inttra BC { var dto = JsonConvert.DeserializeObject(strBody); _publisher.PublishAsync(new ChannelEventSource("BookingAuto:InttraBc", dto)); } else if (arg.RoutingKey == "CmaVgmMissing") //CMA VGM Missing { var dto = JsonConvert.DeserializeObject(strBody); _publisher.PublishAsync(new ChannelEventSource("BookingAuto:CmaVgmMissing", dto)); } }; model.BasicConsume(QueueName, true, consumer); } } public override void Dispose() { base.Dispose(); _serviceScope.Dispose(); if (mqConn != null && mqConn.IsOpen) mqConn.Close(); _logger.LogInformation("BookingAutoService Dispose"); } } /// /// BC消息对象 /// public class BookingAutoMessageBC { /// /// 租户ID /// public long TenantId { get; set; } /// /// 大简云公司ID /// public string CompanyId { get; set; } /// /// 船司 /// public string Carrier { get; set; } /// /// 提单号 /// public string MBLNO { get; set; } /// /// 订舱号 /// public string BookingNO { get; set; } /// /// 链接地址 /// public string Url { get; set; } /// /// 附件地址 /// public string AttachUrl { get; set; } } /// /// LARA提单号消息对象 /// public class BookingAutoMessageLaraBlno { /// /// 租户ID /// public long TenantId { get; set; } /// /// 大简云公司ID /// public string CompanyId { get; set; } /// /// 船司 /// public string Carrier { get; set; } /// /// 提单号 /// public string MBLNO { get; set; } /// /// csr号 /// public string CsrCode { get; set; } } /// /// INTTRA BC消息对象 /// public class BookingAutoMessageInttraBC { /// /// 租户ID /// public long TenantId { get; set; } /// /// 大简云公司ID /// public string CompanyId { get; set; } /// /// 船司 /// public string Carrier { get; set; } /// /// 提单号 /// public string MBLNO { get; set; } /// /// 订舱号 /// public string BookingNO { get; set; } } /// /// CMA VGM Missing /// public class BookingAutoMessageCmaVgmMissing { /// /// 租户ID /// public long TenantId { get; set; } /// /// 大简云公司ID /// public string CompanyId { get; set; } /// /// 船司 /// public string Carrier { get; set; } /// /// 详情数据 /// public List DetailData { get; set; } } public class BookingAutoMessageCmaVgmMissingDetail { /// /// 提单号 /// public string MBLNO { get; set; } /// /// 箱号 /// public string CtnNO { get; set; } /// /// 船名 /// public string Vessel { get; set; } /// /// 航次 /// public string Voyno { get; set; } /// /// 起运港 /// public string LoadPort { get; set; } /// /// 截止时间 /// public string SubmitDeadline { get; set; } /// /// 剩余天数 /// public string RemainingDays { get; set; } } }