fix: Change Code
This commit is contained in:
parent
8902080336
commit
89ef146c19
4 changed files with 114 additions and 7 deletions
12
BMA.EHR.Infrastructure/MessageQueue/InsigniaRequestPeriod.cs
Normal file
12
BMA.EHR.Infrastructure/MessageQueue/InsigniaRequestPeriod.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BMA.EHR.Infrastructure.MessageQueue
|
||||
{
|
||||
public class InsigniaRequestPeriod
|
||||
{
|
||||
public Guid PeriodId { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,7 @@
|
|||
using System.Text;
|
||||
using BMA.EHR.Application.Repositories;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Newtonsoft.Json;
|
||||
using RabbitMQ.Client.Events;
|
||||
|
||||
namespace BMA.EHR.Infrastructure.MessageQueue
|
||||
|
|
@ -6,15 +9,60 @@ namespace BMA.EHR.Infrastructure.MessageQueue
|
|||
public class RabbitMQConsumer
|
||||
{
|
||||
private readonly RabbitMQConnection _connection;
|
||||
private readonly UserProfileRepository _userProfileRepository;
|
||||
private readonly InsigniaPeriodsRepository _insigniaPeriodsRepository;
|
||||
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="connection"></param>
|
||||
public RabbitMQConsumer(RabbitMQConnection connection)
|
||||
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>
|
||||
|
|
@ -23,7 +71,7 @@ namespace BMA.EHR.Infrastructure.MessageQueue
|
|||
public void StartReceiving()
|
||||
{
|
||||
var channel = _connection.GetChannel();
|
||||
channel.QueueDeclare(queue: "myqueue",
|
||||
channel.QueueDeclare(queue: INSIGNIA_QUEUE,
|
||||
durable: false,
|
||||
exclusive: false,
|
||||
autoDelete: false,
|
||||
|
|
@ -34,10 +82,17 @@ namespace BMA.EHR.Infrastructure.MessageQueue
|
|||
{
|
||||
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: "myqueue",
|
||||
channel.BasicConsume(queue: INSIGNIA_QUEUE,
|
||||
autoAck: true,
|
||||
consumer: consumer,
|
||||
consumerTag: "",
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace BMA.EHR.Infrastructure.MessageQueue
|
||||
{
|
||||
|
|
@ -6,6 +7,8 @@ namespace BMA.EHR.Infrastructure.MessageQueue
|
|||
{
|
||||
private readonly RabbitMQConnection _connection;
|
||||
|
||||
private const string INSIGNIA_QUEUE = "bma_insignia_request";
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
|
@ -36,5 +39,31 @@ namespace BMA.EHR.Infrastructure.MessageQueue
|
|||
basicProperties: null,
|
||||
body: body);
|
||||
}
|
||||
|
||||
public void CalculateInsignia(Guid periodId)
|
||||
{
|
||||
var channel = _connection.GetChannel();
|
||||
|
||||
channel.QueueDeclare(queue: INSIGNIA_QUEUE,
|
||||
durable: false,
|
||||
exclusive: false,
|
||||
autoDelete: false,
|
||||
arguments: null);
|
||||
|
||||
// var req = new InsigniaRequestPeriod
|
||||
// {
|
||||
// PeriodId = periodId
|
||||
// };
|
||||
|
||||
// var serializedObject = JsonConvert.SerializeObject(req);
|
||||
|
||||
var body = Encoding.UTF8.GetBytes(periodId.ToString("D"));
|
||||
|
||||
channel.BasicPublish(exchange: "",
|
||||
routingKey: INSIGNIA_QUEUE,
|
||||
mandatory: false,
|
||||
basicProperties: null,
|
||||
body: body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -595,6 +595,17 @@ namespace BMA.EHR.Insignia.Service.Controllers
|
|||
return Success();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// คำนวณราชชื่อผู้ได้รับเครื่องราช (Rabbit MQ)
|
||||
/// </summary>
|
||||
/// <param name="insigniaPeriodId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("queue/{insigniaPeriodId:length(36)}")]
|
||||
public ActionResult<ResponseObject> InsigniaRequestCalculate(Guid insigniaPeriodId)
|
||||
{
|
||||
_producer.CalculateInsignia(insigniaPeriodId);
|
||||
return Success();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region " บันทึกหมายเหตุ "
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue