fix : Optimize Query
This commit is contained in:
parent
89ef146c19
commit
a911648907
9 changed files with 168 additions and 232 deletions
|
|
@ -199,7 +199,7 @@ namespace BMA.EHR.Application.Repositories.Reports
|
|||
.Select(x => new
|
||||
{
|
||||
//Gendor = x.Profile.Gender == null ? null : x.Profile.Gender.Name,
|
||||
Gendor = _userProfileRepository.GetOfficerProfileById(x.ProfileId, AccessToken).Gender ?? "",
|
||||
Gendor = x.Gender ?? "",// _userProfileRepository.GetOfficerProfileById(x.ProfileId, AccessToken).Gender ?? "",
|
||||
RequestInsigniaName = x.RequestInsignia.Name,
|
||||
OcId = x.Request.OrganizationId
|
||||
})
|
||||
|
|
@ -244,7 +244,7 @@ namespace BMA.EHR.Application.Repositories.Reports
|
|||
.Select(x => new
|
||||
{
|
||||
//Gendor = x.Profile.Gender == null ? null : x.Profile.Gender.Name,
|
||||
Gendor = _userProfileRepository.GetOfficerProfileById(x.ProfileId, AccessToken).Gender ?? "",
|
||||
Gendor = x.Gender ?? "", //_userProfileRepository.GetOfficerProfileById(x.ProfileId, AccessToken).Gender ?? "",
|
||||
RequestInsigniaName = x.RequestInsignia.Name,
|
||||
OcId = x.Request.OrganizationId
|
||||
})
|
||||
|
|
@ -303,7 +303,7 @@ namespace BMA.EHR.Application.Repositories.Reports
|
|||
.Select(x => new
|
||||
{
|
||||
//Gendor = x.Profile.Gender == null ? null : x.Profile.Gender.Name,
|
||||
Gendor = _userProfileRepository.GetOfficerProfileById(x.ProfileId, AccessToken).Gender ?? "",
|
||||
Gendor = x.Gender ?? "", //_userProfileRepository.GetOfficerProfileById(x.ProfileId, AccessToken).Gender ?? "",
|
||||
RequestInsigniaName = x.RequestInsignia.Name,
|
||||
OcId = x.Request.OrganizationId
|
||||
})
|
||||
|
|
@ -355,7 +355,7 @@ namespace BMA.EHR.Application.Repositories.Reports
|
|||
.Select(x => new
|
||||
{
|
||||
//Gendor = x.Profile.Gender == null ? null : x.Profile.Gender.Name,
|
||||
Gendor = _userProfileRepository.GetOfficerProfileById(x.ProfileId, AccessToken).Gender ?? "",
|
||||
Gendor = x.Gender ?? "", //_userProfileRepository.GetOfficerProfileById(x.ProfileId, AccessToken).Gender ?? "",
|
||||
RequestInsigniaName = x.RequestInsignia.Name,
|
||||
OcId = x.Request.OrganizationId
|
||||
})
|
||||
|
|
@ -442,10 +442,10 @@ namespace BMA.EHR.Application.Repositories.Reports
|
|||
InsigniaInitial = r.RequestInsignia.ShortName,
|
||||
InsigniaName = r.RequestInsignia.Name,
|
||||
ProfileId = r.ProfileId,
|
||||
FullName = $"{_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).FirstName} {_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).LastName}", // $"{r.Profile.Prefix?.Name}{r.Profile.FirstName} {r.Profile.LastName}",
|
||||
Gender = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender == null ? null : _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender,
|
||||
Male = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender == null ? 0 : (_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender == "ชาย" ? 1 : 0),
|
||||
Female = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender == null ? 0 : (_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender == "หญิง" ? 1 : 0),
|
||||
FullName = $"{r.Prefix}{r.FirstName} {r.LastName}",
|
||||
Gender = r.Gender ?? "",
|
||||
Male = r.Gender == null ? 0 : (r.Gender == "ชาย" ? 1 : 0),
|
||||
Female = r.Gender == null ? 0 : (r.Gender == "หญิง" ? 1 : 0),
|
||||
InsigniaId = r.RequestInsignia.Id,
|
||||
OCName = _userProfileRepository.GetOc(r.Request.OrganizationId, 0, AccessToken).Root,
|
||||
// _organizationCommonRepository.GetOrganizationNameFullPath(r.Request.Organization.Id, false, false)
|
||||
|
|
@ -668,24 +668,22 @@ namespace BMA.EHR.Application.Repositories.Reports
|
|||
InsigniaInitial = r.RequestInsignia.ShortName,
|
||||
InsigniaName = r.RequestInsignia.Name,
|
||||
ProfileId = r.ProfileId,
|
||||
CitizenId = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).CitizenId.ToThaiNumber(), // r.Profile.CitizenId == null ? null : r.Profile.CitizenId.ToThaiNumber(),
|
||||
FullName = $"{_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Prefix}{_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).FirstName} {_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).LastName}",
|
||||
CitizenId = r.CitizenId.ToThaiNumber(), // r.Profile.CitizenId == null ? null : r.Profile.CitizenId.ToThaiNumber(),
|
||||
FullName = $"{r.Prefix}{r.FirstName} {r.LastName}",
|
||||
// $"{r.Profile.Prefix?.Name}{r.Profile.FirstName} {r.Profile.LastName}",
|
||||
ShowProfileId = r.ProfileId,
|
||||
Type = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).PosType.PosTypeName,
|
||||
Type = r.PosTypeName,
|
||||
AcademicStanding = "",
|
||||
Level = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).PosLevel.PosLevelName,
|
||||
DateStart = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).DateStart.Value.ToThaiShortDate().ToThaiNumber(),
|
||||
SalaryAmount = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).ProfileSalary.Count() == 0 ||
|
||||
_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).ProfileSalary == null ? "-" :
|
||||
Convert.ToInt32(_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).ProfileSalary.OrderByDescending(x => x.Order).FirstOrDefault().Amount).ToNumericText().ToThaiNumber(),
|
||||
Level = r.PosLevelName,
|
||||
DateStart = r.DateAppoint.Value.ToThaiShortDate().ToThaiNumber(),
|
||||
SalaryAmount = r.Amount.Value.ToNumericText().ToThaiNumber(),
|
||||
InsigniaRecv = "",
|
||||
InsigniaRecvDate = "",
|
||||
InsigniaRequest = r.RequestInsignia.ShortName,
|
||||
Remark = "",
|
||||
Position = (_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Position ?? "") +
|
||||
(" ประเภท" + _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).PosType?.PosTypeName) +
|
||||
(" ระดับ" + _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).PosLevel?.PosLevelName) +
|
||||
Position = (r.Position ?? "") +
|
||||
(" ประเภท" + r.PosTypeName) +
|
||||
(" ระดับ" + r.PosLevelName) +
|
||||
(" สังกัด" + CRLF + _userProfileRepository.GetOc(r.Request.OrganizationId, 0, AccessToken).Root)
|
||||
,
|
||||
OCName = _userProfileRepository.GetOc(r.Request.OrganizationId, 0, AccessToken).Root,
|
||||
|
|
@ -761,7 +759,7 @@ namespace BMA.EHR.Application.Repositories.Reports
|
|||
.Select(x => new
|
||||
{
|
||||
//Gendor = x.Profile.Gender == null ? null : x.Profile.Gender.Name,
|
||||
Gendor = _userProfileRepository.GetOfficerProfileById(x.ProfileId, AccessToken).Gender ?? "",
|
||||
Gendor = x.Gender, //_userProfileRepository.GetOfficerProfileById(x.ProfileId, AccessToken).Gender ?? "",
|
||||
RequestInsigniaName = x.RequestInsignia.Name,
|
||||
InsigniaInitial = $"{x.RequestInsignia.Name}({x.RequestInsignia.ShortName})",
|
||||
OcId = x.Request.OrganizationId
|
||||
|
|
@ -801,7 +799,7 @@ namespace BMA.EHR.Application.Repositories.Reports
|
|||
.Select(x => new
|
||||
{
|
||||
//Gendor = x.Profile.Gender == null ? null : x.Profile.Gender.Name,
|
||||
Gendor = _userProfileRepository.GetOfficerProfileById(x.ProfileId, AccessToken).Gender ?? "",
|
||||
Gendor = x.Gender, // _userProfileRepository.GetOfficerProfileById(x.ProfileId, AccessToken).Gender ?? "",
|
||||
RequestInsigniaName = x.RequestInsignia.Name,
|
||||
OcId = x.Request.OrganizationId
|
||||
})
|
||||
|
|
@ -859,8 +857,8 @@ namespace BMA.EHR.Application.Repositories.Reports
|
|||
&& r.RequestInsignia.InsigniaType.Name != "เหรียญบำเหน็จในราชการ"
|
||||
select new
|
||||
{
|
||||
Male = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender == "ชาย" ? 1 : 0,
|
||||
Female = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender == "หญิง" ? 1 : 0,
|
||||
Male = r.Gender == "ชาย" ? 1 : 0,
|
||||
Female = r.Gender == "หญิง" ? 1 : 0,
|
||||
})
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
|
@ -886,9 +884,9 @@ namespace BMA.EHR.Application.Repositories.Reports
|
|||
InsigniaInitial = r.RequestInsignia.ShortName,
|
||||
InsigniaName = r.RequestInsignia.Name,
|
||||
ProfileId = r.ProfileId,
|
||||
FullName = $"{_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Prefix}{_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).FirstName} {_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).LastName}",
|
||||
FullName = $"{r.Prefix}{r.FirstName} {r.LastName}",
|
||||
// $"{r.Profile.Prefix?.Name}{r.Profile.FirstName} {r.Profile.LastName}",
|
||||
Gender = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender,
|
||||
Gender = r.Gender,
|
||||
Male = gender.Sum(x => x.Male),
|
||||
Female = gender.Sum(x => x.Female),
|
||||
InsigniaId = r.RequestInsignia.Id,
|
||||
|
|
|
|||
|
|
@ -12,9 +12,7 @@ namespace BMA.EHR.Infrastructure
|
|||
{
|
||||
public static IServiceCollection AddMessageQueue(this IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<RabbitMQConnection>();
|
||||
services.AddTransient<RabbitMQProducer>();
|
||||
services.AddTransient<RabbitMQConsumer>();
|
||||
services.AddHostedService<RabbitMQConsumer>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
|
@ -37,7 +35,7 @@ namespace BMA.EHR.Infrastructure
|
|||
}),
|
||||
ServiceLifetime.Transient);
|
||||
|
||||
services.AddScoped<ILeaveDbContext>(provider => provider.GetService<LeaveDbContext>());
|
||||
services.AddTransient<ILeaveDbContext>(provider => provider.GetService<LeaveDbContext>());
|
||||
|
||||
return services;
|
||||
}
|
||||
|
|
@ -60,7 +58,7 @@ namespace BMA.EHR.Infrastructure
|
|||
}),
|
||||
ServiceLifetime.Transient);
|
||||
|
||||
services.AddScoped<IDisciplineDbContext>(provider => provider.GetService<DisciplineDbContext>());
|
||||
services.AddTransient<IDisciplineDbContext>(provider => provider.GetService<DisciplineDbContext>());
|
||||
|
||||
return services;
|
||||
}
|
||||
|
|
@ -83,7 +81,7 @@ namespace BMA.EHR.Infrastructure
|
|||
}),
|
||||
ServiceLifetime.Transient);
|
||||
|
||||
services.AddScoped<IApplicationDBContext>(provider => provider.GetService<ApplicationDBContext>());
|
||||
services.AddTransient<IApplicationDBContext>(provider => provider.GetService<ApplicationDBContext>());
|
||||
|
||||
var connectionStringExam = configuration.GetConnectionString("ExamConnection");
|
||||
|
||||
|
|
@ -97,7 +95,8 @@ namespace BMA.EHR.Infrastructure
|
|||
}),
|
||||
ServiceLifetime.Transient);
|
||||
|
||||
services.AddScoped<IApplicationDBExamContext>(provider => provider.GetService<ApplicationDBExamContext>());
|
||||
services.AddTransient<IApplicationDBExamContext>(provider => provider.GetService<ApplicationDBExamContext>());
|
||||
|
||||
|
||||
return services;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -25,6 +25,7 @@
|
|||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.Logging" Version="6.32.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
|
||||
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
|
||||
|
|
|
|||
|
|
@ -15,8 +15,10 @@ using Microsoft.EntityFrameworkCore;
|
|||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
using Newtonsoft.Json;
|
||||
using OfficeOpenXml;
|
||||
using RabbitMQ.Client;
|
||||
using Swashbuckle.AspNetCore.Annotations;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
|
||||
namespace BMA.EHR.Insignia.Service.Controllers
|
||||
{
|
||||
|
|
@ -36,10 +38,10 @@ namespace BMA.EHR.Insignia.Service.Controllers
|
|||
private readonly IWebHostEnvironment _hostingEnvironment;
|
||||
private readonly string Royal_Type = "Royal";
|
||||
private readonly UserProfileRepository _userProfileRepository;
|
||||
|
||||
private readonly RabbitMQProducer _producer;
|
||||
private const string INSIGNIA_QUEUE = "bma_insignia_request";
|
||||
|
||||
private readonly InsigniaPeriodsRepository _insigniaPeriodRepository;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
|
|
@ -61,7 +63,7 @@ namespace BMA.EHR.Insignia.Service.Controllers
|
|||
IHttpContextAccessor httpContextAccessor,
|
||||
UserProfileRepository userProfileRepository,
|
||||
InsigniaPeriodsRepository insigniaPeriodRepository,
|
||||
RabbitMQProducer producer)
|
||||
IConfiguration configuration)
|
||||
{
|
||||
_context = context;
|
||||
_documentService = documentService;
|
||||
|
|
@ -71,7 +73,7 @@ namespace BMA.EHR.Insignia.Service.Controllers
|
|||
_hostingEnvironment = hostingEnvironment;
|
||||
_userProfileRepository = userProfileRepository;
|
||||
_insigniaPeriodRepository = insigniaPeriodRepository;
|
||||
_producer = producer;
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
#region " Properties "
|
||||
|
|
@ -320,21 +322,7 @@ namespace BMA.EHR.Insignia.Service.Controllers
|
|||
// }
|
||||
#endregion
|
||||
|
||||
#region " ทดสอบ RabbitMQ "
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("rabbit")]
|
||||
[AllowAnonymous]
|
||||
public ActionResult<ResponseObject> CallRabbitMQ()
|
||||
{
|
||||
_producer.SendMessage("test send");
|
||||
return Success();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " จัดทำรายชื่อครูที่มีสิทธิในการยืนขอเครื่องราชฯ "
|
||||
|
||||
|
|
@ -603,9 +591,30 @@ namespace BMA.EHR.Insignia.Service.Controllers
|
|||
[HttpGet("queue/{insigniaPeriodId:length(36)}")]
|
||||
public ActionResult<ResponseObject> InsigniaRequestCalculate(Guid insigniaPeriodId)
|
||||
{
|
||||
_producer.CalculateInsignia(insigniaPeriodId);
|
||||
return Success();
|
||||
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
|
||||
};
|
||||
|
||||
using (var connection = factory.CreateConnection())
|
||||
using (var channel = connection.CreateModel())
|
||||
{
|
||||
channel.QueueDeclare(queue: INSIGNIA_QUEUE, durable: false, exclusive: false, autoDelete: false, arguments: null);
|
||||
|
||||
var body = Encoding.UTF8.GetBytes(insigniaPeriodId.ToString("D"));
|
||||
|
||||
channel.BasicPublish(exchange: "", routingKey: INSIGNIA_QUEUE, basicProperties: null, body: body);
|
||||
|
||||
return Success();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " บันทึกหมายเหตุ "
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using BMA.EHR.Application;
|
||||
using BMA.EHR.Application.Repositories;
|
||||
using BMA.EHR.Application.Repositories.Reports;
|
||||
using BMA.EHR.Domain.Middlewares;
|
||||
using BMA.EHR.Infrastructure;
|
||||
|
|
@ -91,8 +92,7 @@ var builder = WebApplication.CreateBuilder(args);
|
|||
builder.Services.AddLeaveApplication();
|
||||
builder.Services.AddLeavePersistence(builder.Configuration);
|
||||
|
||||
// RabbitMQ
|
||||
builder.Services.AddMessageQueue();
|
||||
|
||||
|
||||
builder.Services.AddControllers(options =>
|
||||
{
|
||||
|
|
@ -106,7 +106,7 @@ var builder = WebApplication.CreateBuilder(args);
|
|||
// Register DbContext
|
||||
var defaultConnection = builder.Configuration.GetConnectionString("DefaultConnection");
|
||||
builder.Services.AddDbContext<ApplicationDBContext>(options =>
|
||||
options.UseMySql(defaultConnection, ServerVersion.AutoDetect(defaultConnection)));
|
||||
options.UseMySql(defaultConnection, ServerVersion.AutoDetect(defaultConnection)), ServiceLifetime.Transient);
|
||||
builder.Services.AddHealthChecks();
|
||||
// Add Hangfire services.
|
||||
builder.Services.AddHangfire(configuration => configuration
|
||||
|
|
@ -129,6 +129,19 @@ var builder = WebApplication.CreateBuilder(args);
|
|||
TablesPrefix = "Hangfire"
|
||||
})));
|
||||
builder.Services.AddHangfireServer();
|
||||
|
||||
|
||||
// RabbitMQ
|
||||
//builder.Services.AddTransient<RabbitMQConsumer>(provider =>
|
||||
//{
|
||||
// var serviceScopeFactory = provider.GetRequiredService<IServiceScopeFactory>();
|
||||
// var userRepo = provider.GetRequiredService<UserProfileRepository>();
|
||||
// var insigniaRepo = provider.GetRequiredService<InsigniaPeriodsRepository>();
|
||||
// var httpContext = provider.GetRequiredService<IHttpContextAccessor>();
|
||||
// var config = provider.GetRequiredService<IConfiguration>();
|
||||
|
||||
// return new RabbitMQConsumer(userRepo, insigniaRepo, httpContext, serviceScopeFactory, config);
|
||||
//});
|
||||
}
|
||||
|
||||
var app = builder.Build();
|
||||
|
|
@ -151,6 +164,8 @@ var app = builder.Build();
|
|||
app.MapHealthChecks("/health");
|
||||
|
||||
|
||||
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseCors();
|
||||
app.UseAuthentication();
|
||||
|
|
@ -176,8 +191,8 @@ var app = builder.Build();
|
|||
await using var db = scope.ServiceProvider.GetRequiredService<ApplicationDBContext>();
|
||||
await db.Database.MigrateAsync();
|
||||
|
||||
var rabbitMQConsumer = app.Services.GetRequiredService<RabbitMQConsumer>();
|
||||
rabbitMQConsumer.StartReceiving();
|
||||
//var rabbitMQConsumer = app.Services.GetRequiredService<RabbitMQConsumer>();
|
||||
//rabbitMQConsumer.StartReceiving();
|
||||
|
||||
app.Run();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue