hrms-api-backend/BMA.EHR.Infrastructure/MessageQueue/RabbitMQConsumer.cs

142 lines
4.9 KiB
C#
Raw Permalink Normal View History

2024-07-06 12:30:37 +07:00
using BMA.EHR.Application.Repositories;
using BMA.EHR.Domain.Models.Insignias;
using BMA.EHR.Domain.Shared;
2024-07-06 12:30:37 +07:00
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
2024-07-07 09:59:37 +07:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RabbitMQ.Client;
2024-07-06 11:04:12 +07:00
using RabbitMQ.Client.Events;
2024-07-07 09:59:37 +07:00
using System.Text;
2024-07-06 11:04:12 +07:00
namespace BMA.EHR.Infrastructure.MessageQueue
{
2024-07-07 09:59:37 +07:00
public class RabbitMQConsumer : BackgroundService
2024-07-06 11:04:12 +07:00
{
2024-07-07 09:59:37 +07:00
#region " Fields "
2024-07-06 12:30:37 +07:00
private readonly IHttpContextAccessor _httpContextAccessor;
2024-07-07 09:59:37 +07:00
private const string INSIGNIA_QUEUE = "bma_insignia_request";
2024-07-07 09:59:37 +07:00
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly IConfiguration _configuration;
private IConnection _connection;
private IModel _channel;
2024-07-06 12:30:37 +07:00
2024-07-07 09:59:37 +07:00
#endregion
2024-07-06 12:30:37 +07:00
2024-07-07 09:59:37 +07:00
#region " Constructor and Destructor "
2024-07-06 12:30:37 +07:00
public RabbitMQConsumer(IHttpContextAccessor httpContextAccessor,
2024-07-07 09:59:37 +07:00
IServiceScopeFactory serviceScopeFactory,
IConfiguration configuration)
2024-07-06 11:04:12 +07:00
{
2024-07-07 09:59:37 +07:00
_serviceScopeFactory = serviceScopeFactory;
_configuration = configuration;
2024-07-06 12:30:37 +07:00
_httpContextAccessor = httpContextAccessor;
2024-07-07 09:59:37 +07:00
var host = _configuration["RabbitMQ:URL"];
var userName = _configuration["RabbitMQ:UserName"];
var password = _configuration["RabbitMQ:Password"];
2024-07-07 09:59:37 +07:00
var factory = new ConnectionFactory()
{
HostName = host,
UserName = userName,
Password = password
};
2024-07-07 09:59:37 +07:00
_connection = factory.CreateConnection();
_channel = _connection.CreateModel();
_channel.QueueDeclare(queue: INSIGNIA_QUEUE, durable: false, exclusive: false, autoDelete: false, arguments: null);
2024-07-07 09:59:37 +07:00
}
public override void Dispose()
{
_channel.Close();
_connection.Close();
base.Dispose();
2024-07-06 12:30:37 +07:00
}
2024-07-07 09:59:37 +07:00
#endregion
2024-07-06 12:30:37 +07:00
#region " Properties "
private string? AccessToken => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"];
#endregion
2024-07-07 09:59:37 +07:00
#region " Methods "
#region " Private "
2024-07-06 12:30:37 +07:00
private async Task CalculateInsigniaAsync(Guid periodId)
{
2024-07-07 09:59:37 +07:00
using (var scope = _serviceScopeFactory.CreateScope())
2024-07-06 12:30:37 +07:00
{
2024-07-07 09:59:37 +07:00
var userRepo = scope.ServiceProvider.GetRequiredService<UserProfileRepository>();
var insigniaRepo = scope.ServiceProvider.GetRequiredService<InsigniaPeriodsRepository>();
2024-07-06 12:30:37 +07:00
var selectPeriod = await insigniaRepo.GetByIdAsync(periodId);
if (selectPeriod == null)
{
throw new Exception(GlobalMessages.InsigniaPeriodNotFound);
}
var organizations = await userRepo.GetActiveRootAsync(AccessToken, selectPeriod.RevisionId);
2024-07-07 09:59:37 +07:00
foreach (var organization in organizations)
2024-07-06 12:30:37 +07:00
{
2024-07-07 09:59:37 +07:00
if (organization == null)
continue;
2024-07-07 09:59:37 +07:00
var result = await insigniaRepo.GetInsigniaRequest(periodId, organization.Id);
if (result != null)
2024-07-06 12:30:37 +07:00
{
2024-07-07 09:59:37 +07:00
Guid period = result.PeriodId;
string requestStatus = result.RequestStatus;
var candidate = await insigniaRepo.GetInsigniaCandidateBKK(periodId, organization.Id);
// ตรวจสอบว่ารายการอยู่ใน table insignia_request_new
if (requestStatus == null)
{
// บันทึกรายชื่อ
2025-02-19 17:56:02 +07:00
await insigniaRepo.InsertCandidate(period, organization.Id, organization.OrgRootName, candidate);
2024-07-07 09:59:37 +07:00
}
2024-07-06 12:30:37 +07:00
}
}
}
2024-07-06 11:04:12 +07:00
}
2024-07-07 09:59:37 +07:00
#endregion
#region " Overrides "
protected override Task ExecuteAsync(CancellationToken stoppingToken)
2024-07-06 11:04:12 +07:00
{
2024-07-07 09:59:37 +07:00
Console.WriteLine("ExecuteAsync started."); // Log ตรวจสอบ
var consumer = new EventingBasicConsumer(_channel);
2024-07-06 11:04:12 +07:00
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
2024-07-06 12:30:37 +07:00
var periodId = Guid.Parse(message);
2024-07-07 09:59:37 +07:00
Console.WriteLine(" [x] Received {0}", message);
2024-07-06 12:30:37 +07:00
2024-07-07 09:59:37 +07:00
//process insignia request while receive message
2024-07-06 12:30:37 +07:00
var res = CalculateInsigniaAsync(periodId);
Console.WriteLine(" [x] Success Calculate Period {0}", periodId);
2024-07-06 11:04:12 +07:00
};
2024-07-07 09:59:37 +07:00
_channel.BasicConsume(queue: INSIGNIA_QUEUE, autoAck: true, consumer: consumer);
2024-07-06 11:04:12 +07:00
2024-07-07 09:59:37 +07:00
return Task.CompletedTask;
2024-07-06 11:04:12 +07:00
}
2024-07-07 09:59:37 +07:00
#endregion
#endregion
2024-07-06 11:04:12 +07:00
}
}