From 89ef146c19a1120df39f6c2c28f63ab350d85ecd Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Sat, 6 Jul 2024 12:30:37 +0700 Subject: [PATCH] fix: Change Code --- .../MessageQueue/InsigniaRequestPeriod.cs | 12 ++++ .../MessageQueue/RabbitMQConsumer.cs | 69 +++++++++++++++++-- .../MessageQueue/RabbitMQProducer.cs | 29 ++++++++ .../Controllers/InsigniaRequestController.cs | 11 +++ 4 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 BMA.EHR.Infrastructure/MessageQueue/InsigniaRequestPeriod.cs diff --git a/BMA.EHR.Infrastructure/MessageQueue/InsigniaRequestPeriod.cs b/BMA.EHR.Infrastructure/MessageQueue/InsigniaRequestPeriod.cs new file mode 100644 index 00000000..6b7d2e5f --- /dev/null +++ b/BMA.EHR.Infrastructure/MessageQueue/InsigniaRequestPeriod.cs @@ -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; } + } +} \ No newline at end of file diff --git a/BMA.EHR.Infrastructure/MessageQueue/RabbitMQConsumer.cs b/BMA.EHR.Infrastructure/MessageQueue/RabbitMQConsumer.cs index faa33258..81b97c18 100644 --- a/BMA.EHR.Infrastructure/MessageQueue/RabbitMQConsumer.cs +++ b/BMA.EHR.Infrastructure/MessageQueue/RabbitMQConsumer.cs @@ -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; - /// - /// - /// - /// - 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); + } + } + } } /// @@ -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(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: "", diff --git a/BMA.EHR.Infrastructure/MessageQueue/RabbitMQProducer.cs b/BMA.EHR.Infrastructure/MessageQueue/RabbitMQProducer.cs index e7dde3d7..4c8ff2dc 100644 --- a/BMA.EHR.Infrastructure/MessageQueue/RabbitMQProducer.cs +++ b/BMA.EHR.Infrastructure/MessageQueue/RabbitMQProducer.cs @@ -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"; + /// /// /// @@ -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); + } } } \ No newline at end of file diff --git a/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs b/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs index a2022981..2c843cfc 100644 --- a/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs +++ b/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs @@ -595,6 +595,17 @@ namespace BMA.EHR.Insignia.Service.Controllers return Success(); } + /// + /// คำนวณราชชื่อผู้ได้รับเครื่องราช (Rabbit MQ) + /// + /// + /// + [HttpGet("queue/{insigniaPeriodId:length(36)}")] + public ActionResult InsigniaRequestCalculate(Guid insigniaPeriodId) + { + _producer.CalculateInsignia(insigniaPeriodId); + return Success(); + } #endregion #region " บันทึกหมายเหตุ "