104 lines
No EOL
4.2 KiB
C#
104 lines
No EOL
4.2 KiB
C#
using System.Text;
|
|
using BMA.EHR.Application.Repositories;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Newtonsoft.Json;
|
|
using RabbitMQ.Client.Events;
|
|
|
|
namespace BMA.EHR.Infrastructure.MessageQueue
|
|
{
|
|
public class RabbitMQConsumer
|
|
{
|
|
private readonly RabbitMQConnection _connection;
|
|
private readonly UserProfileRepository _userProfileRepository;
|
|
private readonly InsigniaPeriodsRepository _insigniaPeriodsRepository;
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
|
|
private const string INSIGNIA_QUEUE = "bma_insignia_request";
|
|
|
|
|
|
public RabbitMQConsumer(RabbitMQConnection connection,
|
|
UserProfileRepository userProfileRepository,
|
|
InsigniaPeriodsRepository insigniaPeriodsRepository,
|
|
IHttpContextAccessor httpContextAccessor)
|
|
{
|
|
_connection = connection;
|
|
_userProfileRepository = userProfileRepository;
|
|
_insigniaPeriodsRepository = insigniaPeriodsRepository;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
}
|
|
|
|
#region " Properties "
|
|
|
|
private bool? RoleAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("admin");
|
|
private bool? RoleInsignia1 => _httpContextAccessor?.HttpContext?.User?.IsInRole("insignia1");
|
|
private bool? RoleInsignia2 => _httpContextAccessor?.HttpContext?.User?.IsInRole("insignia2");
|
|
private string? AccessToken => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"];
|
|
|
|
#endregion
|
|
|
|
private async Task CalculateInsigniaAsync(Guid periodId)
|
|
{
|
|
var organizations = await _userProfileRepository.GetActiveRootAsync(AccessToken);
|
|
|
|
foreach (var organization in organizations)
|
|
{
|
|
if (organization == null)
|
|
continue;
|
|
|
|
//if(organization.Id != Guid.Parse("e8493cd1-d371-402e-add6-566e68d5d1b3")) continue;
|
|
|
|
var result = await _insigniaPeriodsRepository.GetInsigniaRequest(periodId, organization.Id);
|
|
if (result != null)
|
|
{
|
|
Guid period = result.PeriodId;
|
|
string requestStatus = result.RequestStatus;
|
|
var candidate = await _insigniaPeriodsRepository.GetInsigniaCandidateBKK(periodId, organization.Id);
|
|
// ตรวจสอบว่ารายการอยู่ใน table insignia_request_new
|
|
if (requestStatus == null)
|
|
{
|
|
// บันทึกรายชื่อ
|
|
await _insigniaPeriodsRepository.InsertCandidate(period, organization.Id, candidate);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public void StartReceiving()
|
|
{
|
|
var channel = _connection.GetChannel();
|
|
channel.QueueDeclare(queue: INSIGNIA_QUEUE,
|
|
durable: false,
|
|
exclusive: false,
|
|
autoDelete: false,
|
|
arguments: null);
|
|
|
|
var consumer = new EventingBasicConsumer(channel);
|
|
consumer.Received += (model, ea) =>
|
|
{
|
|
var body = ea.Body.ToArray();
|
|
var message = Encoding.UTF8.GetString(body);
|
|
//var request = JsonConvert.DeserializeObject<InsigniaRequestPeriod>(message);
|
|
|
|
Console.WriteLine(" [x] Received {0}", message);
|
|
var periodId = Guid.Parse(message);
|
|
|
|
// process insignia request while receive message
|
|
var res = CalculateInsigniaAsync(periodId);
|
|
Console.WriteLine(" [x] Success Calculate Period {0}", periodId);
|
|
};
|
|
|
|
channel.BasicConsume(queue: INSIGNIA_QUEUE,
|
|
autoAck: true,
|
|
consumer: consumer,
|
|
consumerTag: "",
|
|
noLocal: false,
|
|
exclusive: false,
|
|
arguments: null);
|
|
}
|
|
}
|
|
} |