fix : Optimize Query
This commit is contained in:
parent
89ef146c19
commit
a911648907
9 changed files with 168 additions and 232 deletions
|
|
@ -1,12 +0,0 @@
|
|||
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,45 +0,0 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using RabbitMQ.Client;
|
||||
|
||||
namespace BMA.EHR.Infrastructure.MessageQueue
|
||||
{
|
||||
public class RabbitMQConnection
|
||||
{
|
||||
private readonly IConnection _connection;
|
||||
private readonly IModel _channel;
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="configuration"></param>
|
||||
public RabbitMQConnection(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
var hostName = _configuration["RabbitMQ:URL"];
|
||||
var userName = _configuration["RabbitMQ:UserName"];
|
||||
var password = _configuration["RabbitMQ:Password"];
|
||||
|
||||
|
||||
var factory = new ConnectionFactory() { HostName = hostName, UserName = userName, Password = password };
|
||||
_connection = factory.CreateConnection();
|
||||
_channel = _connection.CreateModel();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IModel GetChannel() => _channel;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_channel?.Close();
|
||||
_connection?.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,34 +1,70 @@
|
|||
using System.Text;
|
||||
using BMA.EHR.Application.Repositories;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Newtonsoft.Json;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using RabbitMQ.Client;
|
||||
using RabbitMQ.Client.Events;
|
||||
using System.Text;
|
||||
|
||||
namespace BMA.EHR.Infrastructure.MessageQueue
|
||||
{
|
||||
public class RabbitMQConsumer
|
||||
public class RabbitMQConsumer : BackgroundService
|
||||
{
|
||||
private readonly RabbitMQConnection _connection;
|
||||
#region " Fields "
|
||||
|
||||
private readonly UserProfileRepository _userProfileRepository;
|
||||
private readonly InsigniaPeriodsRepository _insigniaPeriodsRepository;
|
||||
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
|
||||
private const string INSIGNIA_QUEUE = "bma_insignia_request";
|
||||
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||
private readonly IConfiguration _configuration;
|
||||
private IConnection _connection;
|
||||
private IModel _channel;
|
||||
|
||||
public RabbitMQConsumer(RabbitMQConnection connection,
|
||||
UserProfileRepository userProfileRepository,
|
||||
#endregion
|
||||
|
||||
#region " Constructor and Destructor "
|
||||
|
||||
public RabbitMQConsumer(UserProfileRepository userProfileRepository,
|
||||
InsigniaPeriodsRepository insigniaPeriodsRepository,
|
||||
IHttpContextAccessor httpContextAccessor)
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IServiceScopeFactory serviceScopeFactory,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
_connection = connection;
|
||||
_serviceScopeFactory = serviceScopeFactory;
|
||||
_configuration = configuration;
|
||||
_userProfileRepository = userProfileRepository;
|
||||
_insigniaPeriodsRepository = insigniaPeriodsRepository;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
|
||||
var host = _configuration["RabbitMQ:URL"];
|
||||
var userName = _configuration["RabbitMQ:UserName"];
|
||||
var password = _configuration["RabbitMQ:Password"];
|
||||
|
||||
var factory = new ConnectionFactory()
|
||||
{
|
||||
HostName = host,
|
||||
UserName = userName,
|
||||
Password = password
|
||||
};
|
||||
|
||||
_connection = factory.CreateConnection();
|
||||
_channel = _connection.CreateModel();
|
||||
_channel.QueueDeclare(queue: INSIGNIA_QUEUE, durable: false, exclusive: false, autoDelete: false, arguments: null);
|
||||
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
_channel.Close();
|
||||
_connection.Close();
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Properties "
|
||||
|
||||
private bool? RoleAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("admin");
|
||||
|
|
@ -38,67 +74,71 @@ namespace BMA.EHR.Infrastructure.MessageQueue
|
|||
|
||||
#endregion
|
||||
|
||||
#region " Methods "
|
||||
|
||||
#region " Private "
|
||||
|
||||
private async Task CalculateInsigniaAsync(Guid periodId)
|
||||
{
|
||||
var organizations = await _userProfileRepository.GetActiveRootAsync(AccessToken);
|
||||
|
||||
foreach (var organization in organizations)
|
||||
using (var scope = _serviceScopeFactory.CreateScope())
|
||||
{
|
||||
if (organization == null)
|
||||
continue;
|
||||
var userRepo = scope.ServiceProvider.GetRequiredService<UserProfileRepository>();
|
||||
var insigniaRepo = scope.ServiceProvider.GetRequiredService<InsigniaPeriodsRepository>();
|
||||
|
||||
//if(organization.Id != Guid.Parse("e8493cd1-d371-402e-add6-566e68d5d1b3")) continue;
|
||||
var organizations = await userRepo.GetActiveRootAsync(AccessToken);
|
||||
|
||||
var result = await _insigniaPeriodsRepository.GetInsigniaRequest(periodId, organization.Id);
|
||||
if (result != null)
|
||||
foreach (var organization in organizations)
|
||||
{
|
||||
Guid period = result.PeriodId;
|
||||
string requestStatus = result.RequestStatus;
|
||||
var candidate = await _insigniaPeriodsRepository.GetInsigniaCandidateBKK(periodId, organization.Id);
|
||||
// ตรวจสอบว่ารายการอยู่ใน table insignia_request_new
|
||||
if (requestStatus == null)
|
||||
if (organization == null)
|
||||
continue;
|
||||
|
||||
//if(organization.Id != Guid.Parse("e8493cd1-d371-402e-add6-566e68d5d1b3")) continue;
|
||||
|
||||
var result = await insigniaRepo.GetInsigniaRequest(periodId, organization.Id);
|
||||
if (result != null)
|
||||
{
|
||||
// บันทึกรายชื่อ
|
||||
await _insigniaPeriodsRepository.InsertCandidate(period, organization.Id, candidate);
|
||||
Guid period = result.PeriodId;
|
||||
string requestStatus = result.RequestStatus;
|
||||
var candidate = await insigniaRepo.GetInsigniaCandidateBKK(periodId, organization.Id);
|
||||
// ตรวจสอบว่ารายการอยู่ใน table insignia_request_new
|
||||
if (requestStatus == null)
|
||||
{
|
||||
// บันทึกรายชื่อ
|
||||
await insigniaRepo.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);
|
||||
#endregion
|
||||
|
||||
var consumer = new EventingBasicConsumer(channel);
|
||||
#region " Overrides "
|
||||
|
||||
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
|
||||
Console.WriteLine("ExecuteAsync started."); // Log ตรวจสอบ
|
||||
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);
|
||||
Console.WriteLine(" [x] Received {0}", message);
|
||||
|
||||
// process insignia request while receive 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);
|
||||
|
||||
channel.BasicConsume(queue: INSIGNIA_QUEUE,
|
||||
autoAck: true,
|
||||
consumer: consumer,
|
||||
consumerTag: "",
|
||||
noLocal: false,
|
||||
exclusive: false,
|
||||
arguments: null);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
@ -1,69 +0,0 @@
|
|||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace BMA.EHR.Infrastructure.MessageQueue
|
||||
{
|
||||
public class RabbitMQProducer
|
||||
{
|
||||
private readonly RabbitMQConnection _connection;
|
||||
|
||||
private const string INSIGNIA_QUEUE = "bma_insignia_request";
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="connection"></param>
|
||||
public RabbitMQProducer(RabbitMQConnection connection)
|
||||
{
|
||||
_connection = connection;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public void SendMessage(string message)
|
||||
{
|
||||
var channel = _connection.GetChannel();
|
||||
channel.QueueDeclare(queue: "myqueue",
|
||||
durable: false,
|
||||
exclusive: false,
|
||||
autoDelete: false,
|
||||
arguments: null);
|
||||
|
||||
var body = Encoding.UTF8.GetBytes(message);
|
||||
|
||||
channel.BasicPublish(exchange: "",
|
||||
routingKey: "myqueue",
|
||||
mandatory: false,
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue