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/MQ/BookingAutoService.cs

149 lines
4.7 KiB
C#

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<BookingAutoService> _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<ILogger<BookingAutoService>>();
_publisher = _serviceScope.ServiceProvider.GetService<IEventPublisher>();
}
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<SqlSugarRepository<SysConfig>>();
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<BookingAutoMessageBC>(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");
}
}
/// <summary>
/// BC消息对象
/// </summary>
public class BookingAutoMessageBC
{
/// <summary>
/// 租户ID
/// </summary>
public long TenantId { get; set; }
/// <summary>
/// 大简云公司ID
/// </summary>
public string CompanyId { get; set; }
/// <summary>
/// 船司
/// </summary>
public string Carrier { get; set; }
/// <summary>
/// 提单号
/// </summary>
public string MBLNO { get; set; }
/// <summary>
/// 订舱号
/// </summary>
public string BookingNO { get; set; }
/// <summary>
/// 链接地址
/// </summary>
public string Url { get; set; }
/// <summary>
/// 附件地址
/// </summary>
public string AttachUrl { get; set; }
}
}