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

300 lines
8.8 KiB
C#

6 months ago
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下载完成
{
}
5 months ago
else if (arg.RoutingKey == "LaraBlno") //根据Csr号从LARA拉取到提单号
{
var dto = JsonConvert.DeserializeObject<BookingAutoMessageLaraBlno>(strBody);
_publisher.PublishAsync(new ChannelEventSource("BookingAuto:LaraBlno", dto));
}
else if (arg.RoutingKey == "LaraBlno") //根据Csr号从LARA拉取到提单号
{
var dto = JsonConvert.DeserializeObject<BookingAutoMessageLaraBlno>(strBody);
_publisher.PublishAsync(new ChannelEventSource("BookingAuto:LaraBlno", dto));
}
else if (arg.RoutingKey == "InttraBc") //Inttra BC
{
var dto = JsonConvert.DeserializeObject<BookingAutoMessageInttraBC>(strBody);
_publisher.PublishAsync(new ChannelEventSource("BookingAuto:InttraBc", dto));
}
else if (arg.RoutingKey == "CmaVgmMissing") //CMA VGM Missing
{
var dto = JsonConvert.DeserializeObject<BookingAutoMessageCmaVgmMissing>(strBody);
_publisher.PublishAsync(new ChannelEventSource("BookingAuto:CmaVgmMissing", dto));
}
6 months ago
};
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; }
}
5 months ago
/// <summary>
/// LARA提单号消息对象
/// </summary>
public class BookingAutoMessageLaraBlno
{
/// <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>
/// csr号
/// </summary>
public string CsrCode { get; set; }
}
/// <summary>
/// INTTRA BC消息对象
/// </summary>
public class BookingAutoMessageInttraBC
{
/// <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>
/// CMA VGM Missing
/// </summary>
public class BookingAutoMessageCmaVgmMissing
{
/// <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 List<BookingAutoMessageCmaVgmMissingDetail> DetailData { get; set; }
}
public class BookingAutoMessageCmaVgmMissingDetail
{
/// <summary>
/// 提单号
/// </summary>
public string MBLNO { get; set; }
/// <summary>
/// 箱号
/// </summary>
public string CtnNO { get; set; }
/// <summary>
/// 船名
/// </summary>
public string Vessel { get; set; }
/// <summary>
/// 航次
/// </summary>
public string Voyno { get; set; }
/// <summary>
/// 起运港
/// </summary>
public string LoadPort { get; set; }
/// <summary>
/// 截止时间
/// </summary>
public string SubmitDeadline { get; set; }
/// <summary>
/// 剩余天数
/// </summary>
public string RemainingDays { get; set; }
}
6 months ago
}