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下载完成 { } }; 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; } } }