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
|
.Select(x => new
|
||||||
{
|
{
|
||||||
//Gendor = x.Profile.Gender == null ? null : x.Profile.Gender.Name,
|
//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,
|
RequestInsigniaName = x.RequestInsignia.Name,
|
||||||
OcId = x.Request.OrganizationId
|
OcId = x.Request.OrganizationId
|
||||||
})
|
})
|
||||||
|
|
@ -244,7 +244,7 @@ namespace BMA.EHR.Application.Repositories.Reports
|
||||||
.Select(x => new
|
.Select(x => new
|
||||||
{
|
{
|
||||||
//Gendor = x.Profile.Gender == null ? null : x.Profile.Gender.Name,
|
//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,
|
RequestInsigniaName = x.RequestInsignia.Name,
|
||||||
OcId = x.Request.OrganizationId
|
OcId = x.Request.OrganizationId
|
||||||
})
|
})
|
||||||
|
|
@ -303,7 +303,7 @@ namespace BMA.EHR.Application.Repositories.Reports
|
||||||
.Select(x => new
|
.Select(x => new
|
||||||
{
|
{
|
||||||
//Gendor = x.Profile.Gender == null ? null : x.Profile.Gender.Name,
|
//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,
|
RequestInsigniaName = x.RequestInsignia.Name,
|
||||||
OcId = x.Request.OrganizationId
|
OcId = x.Request.OrganizationId
|
||||||
})
|
})
|
||||||
|
|
@ -355,7 +355,7 @@ namespace BMA.EHR.Application.Repositories.Reports
|
||||||
.Select(x => new
|
.Select(x => new
|
||||||
{
|
{
|
||||||
//Gendor = x.Profile.Gender == null ? null : x.Profile.Gender.Name,
|
//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,
|
RequestInsigniaName = x.RequestInsignia.Name,
|
||||||
OcId = x.Request.OrganizationId
|
OcId = x.Request.OrganizationId
|
||||||
})
|
})
|
||||||
|
|
@ -442,10 +442,10 @@ namespace BMA.EHR.Application.Repositories.Reports
|
||||||
InsigniaInitial = r.RequestInsignia.ShortName,
|
InsigniaInitial = r.RequestInsignia.ShortName,
|
||||||
InsigniaName = r.RequestInsignia.Name,
|
InsigniaName = r.RequestInsignia.Name,
|
||||||
ProfileId = r.ProfileId,
|
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}",
|
FullName = $"{r.Prefix}{r.FirstName} {r.LastName}",
|
||||||
Gender = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender == null ? null : _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender,
|
Gender = r.Gender ?? "",
|
||||||
Male = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender == null ? 0 : (_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender == "ชาย" ? 1 : 0),
|
Male = r.Gender == null ? 0 : (r.Gender == "ชาย" ? 1 : 0),
|
||||||
Female = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender == null ? 0 : (_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender == "หญิง" ? 1 : 0),
|
Female = r.Gender == null ? 0 : (r.Gender == "หญิง" ? 1 : 0),
|
||||||
InsigniaId = r.RequestInsignia.Id,
|
InsigniaId = r.RequestInsignia.Id,
|
||||||
OCName = _userProfileRepository.GetOc(r.Request.OrganizationId, 0, AccessToken).Root,
|
OCName = _userProfileRepository.GetOc(r.Request.OrganizationId, 0, AccessToken).Root,
|
||||||
// _organizationCommonRepository.GetOrganizationNameFullPath(r.Request.Organization.Id, false, false)
|
// _organizationCommonRepository.GetOrganizationNameFullPath(r.Request.Organization.Id, false, false)
|
||||||
|
|
@ -668,24 +668,22 @@ namespace BMA.EHR.Application.Repositories.Reports
|
||||||
InsigniaInitial = r.RequestInsignia.ShortName,
|
InsigniaInitial = r.RequestInsignia.ShortName,
|
||||||
InsigniaName = r.RequestInsignia.Name,
|
InsigniaName = r.RequestInsignia.Name,
|
||||||
ProfileId = r.ProfileId,
|
ProfileId = r.ProfileId,
|
||||||
CitizenId = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).CitizenId.ToThaiNumber(), // r.Profile.CitizenId == null ? null : r.Profile.CitizenId.ToThaiNumber(),
|
CitizenId = r.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}",
|
FullName = $"{r.Prefix}{r.FirstName} {r.LastName}",
|
||||||
// $"{r.Profile.Prefix?.Name}{r.Profile.FirstName} {r.Profile.LastName}",
|
// $"{r.Profile.Prefix?.Name}{r.Profile.FirstName} {r.Profile.LastName}",
|
||||||
ShowProfileId = r.ProfileId,
|
ShowProfileId = r.ProfileId,
|
||||||
Type = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).PosType.PosTypeName,
|
Type = r.PosTypeName,
|
||||||
AcademicStanding = "",
|
AcademicStanding = "",
|
||||||
Level = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).PosLevel.PosLevelName,
|
Level = r.PosLevelName,
|
||||||
DateStart = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).DateStart.Value.ToThaiShortDate().ToThaiNumber(),
|
DateStart = r.DateAppoint.Value.ToThaiShortDate().ToThaiNumber(),
|
||||||
SalaryAmount = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).ProfileSalary.Count() == 0 ||
|
SalaryAmount = r.Amount.Value.ToNumericText().ToThaiNumber(),
|
||||||
_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).ProfileSalary == null ? "-" :
|
|
||||||
Convert.ToInt32(_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).ProfileSalary.OrderByDescending(x => x.Order).FirstOrDefault().Amount).ToNumericText().ToThaiNumber(),
|
|
||||||
InsigniaRecv = "",
|
InsigniaRecv = "",
|
||||||
InsigniaRecvDate = "",
|
InsigniaRecvDate = "",
|
||||||
InsigniaRequest = r.RequestInsignia.ShortName,
|
InsigniaRequest = r.RequestInsignia.ShortName,
|
||||||
Remark = "",
|
Remark = "",
|
||||||
Position = (_userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Position ?? "") +
|
Position = (r.Position ?? "") +
|
||||||
(" ประเภท" + _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).PosType?.PosTypeName) +
|
(" ประเภท" + r.PosTypeName) +
|
||||||
(" ระดับ" + _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).PosLevel?.PosLevelName) +
|
(" ระดับ" + r.PosLevelName) +
|
||||||
(" สังกัด" + CRLF + _userProfileRepository.GetOc(r.Request.OrganizationId, 0, AccessToken).Root)
|
(" สังกัด" + CRLF + _userProfileRepository.GetOc(r.Request.OrganizationId, 0, AccessToken).Root)
|
||||||
,
|
,
|
||||||
OCName = _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
|
.Select(x => new
|
||||||
{
|
{
|
||||||
//Gendor = x.Profile.Gender == null ? null : x.Profile.Gender.Name,
|
//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,
|
RequestInsigniaName = x.RequestInsignia.Name,
|
||||||
InsigniaInitial = $"{x.RequestInsignia.Name}({x.RequestInsignia.ShortName})",
|
InsigniaInitial = $"{x.RequestInsignia.Name}({x.RequestInsignia.ShortName})",
|
||||||
OcId = x.Request.OrganizationId
|
OcId = x.Request.OrganizationId
|
||||||
|
|
@ -801,7 +799,7 @@ namespace BMA.EHR.Application.Repositories.Reports
|
||||||
.Select(x => new
|
.Select(x => new
|
||||||
{
|
{
|
||||||
//Gendor = x.Profile.Gender == null ? null : x.Profile.Gender.Name,
|
//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,
|
RequestInsigniaName = x.RequestInsignia.Name,
|
||||||
OcId = x.Request.OrganizationId
|
OcId = x.Request.OrganizationId
|
||||||
})
|
})
|
||||||
|
|
@ -859,8 +857,8 @@ namespace BMA.EHR.Application.Repositories.Reports
|
||||||
&& r.RequestInsignia.InsigniaType.Name != "เหรียญบำเหน็จในราชการ"
|
&& r.RequestInsignia.InsigniaType.Name != "เหรียญบำเหน็จในราชการ"
|
||||||
select new
|
select new
|
||||||
{
|
{
|
||||||
Male = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender == "ชาย" ? 1 : 0,
|
Male = r.Gender == "ชาย" ? 1 : 0,
|
||||||
Female = _userProfileRepository.GetOfficerProfileById(r.ProfileId, AccessToken).Gender == "หญิง" ? 1 : 0,
|
Female = r.Gender == "หญิง" ? 1 : 0,
|
||||||
})
|
})
|
||||||
.Distinct()
|
.Distinct()
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
@ -886,9 +884,9 @@ namespace BMA.EHR.Application.Repositories.Reports
|
||||||
InsigniaInitial = r.RequestInsignia.ShortName,
|
InsigniaInitial = r.RequestInsignia.ShortName,
|
||||||
InsigniaName = r.RequestInsignia.Name,
|
InsigniaName = r.RequestInsignia.Name,
|
||||||
ProfileId = r.ProfileId,
|
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}",
|
// $"{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),
|
Male = gender.Sum(x => x.Male),
|
||||||
Female = gender.Sum(x => x.Female),
|
Female = gender.Sum(x => x.Female),
|
||||||
InsigniaId = r.RequestInsignia.Id,
|
InsigniaId = r.RequestInsignia.Id,
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,7 @@ namespace BMA.EHR.Infrastructure
|
||||||
{
|
{
|
||||||
public static IServiceCollection AddMessageQueue(this IServiceCollection services)
|
public static IServiceCollection AddMessageQueue(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddTransient<RabbitMQConnection>();
|
services.AddHostedService<RabbitMQConsumer>();
|
||||||
services.AddTransient<RabbitMQProducer>();
|
|
||||||
services.AddTransient<RabbitMQConsumer>();
|
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
@ -37,7 +35,7 @@ namespace BMA.EHR.Infrastructure
|
||||||
}),
|
}),
|
||||||
ServiceLifetime.Transient);
|
ServiceLifetime.Transient);
|
||||||
|
|
||||||
services.AddScoped<ILeaveDbContext>(provider => provider.GetService<LeaveDbContext>());
|
services.AddTransient<ILeaveDbContext>(provider => provider.GetService<LeaveDbContext>());
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
@ -60,7 +58,7 @@ namespace BMA.EHR.Infrastructure
|
||||||
}),
|
}),
|
||||||
ServiceLifetime.Transient);
|
ServiceLifetime.Transient);
|
||||||
|
|
||||||
services.AddScoped<IDisciplineDbContext>(provider => provider.GetService<DisciplineDbContext>());
|
services.AddTransient<IDisciplineDbContext>(provider => provider.GetService<DisciplineDbContext>());
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
@ -83,7 +81,7 @@ namespace BMA.EHR.Infrastructure
|
||||||
}),
|
}),
|
||||||
ServiceLifetime.Transient);
|
ServiceLifetime.Transient);
|
||||||
|
|
||||||
services.AddScoped<IApplicationDBContext>(provider => provider.GetService<ApplicationDBContext>());
|
services.AddTransient<IApplicationDBContext>(provider => provider.GetService<ApplicationDBContext>());
|
||||||
|
|
||||||
var connectionStringExam = configuration.GetConnectionString("ExamConnection");
|
var connectionStringExam = configuration.GetConnectionString("ExamConnection");
|
||||||
|
|
||||||
|
|
@ -97,7 +95,8 @@ namespace BMA.EHR.Infrastructure
|
||||||
}),
|
}),
|
||||||
ServiceLifetime.Transient);
|
ServiceLifetime.Transient);
|
||||||
|
|
||||||
services.AddScoped<IApplicationDBExamContext>(provider => provider.GetService<ApplicationDBExamContext>());
|
services.AddTransient<IApplicationDBExamContext>(provider => provider.GetService<ApplicationDBExamContext>());
|
||||||
|
|
||||||
|
|
||||||
return services;
|
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 BMA.EHR.Application.Repositories;
|
||||||
using Microsoft.AspNetCore.Http;
|
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 RabbitMQ.Client.Events;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace BMA.EHR.Infrastructure.MessageQueue
|
namespace BMA.EHR.Infrastructure.MessageQueue
|
||||||
{
|
{
|
||||||
public class RabbitMQConsumer
|
public class RabbitMQConsumer : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly RabbitMQConnection _connection;
|
#region " Fields "
|
||||||
|
|
||||||
private readonly UserProfileRepository _userProfileRepository;
|
private readonly UserProfileRepository _userProfileRepository;
|
||||||
private readonly InsigniaPeriodsRepository _insigniaPeriodsRepository;
|
private readonly InsigniaPeriodsRepository _insigniaPeriodsRepository;
|
||||||
|
|
||||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||||
|
|
||||||
|
|
||||||
private const string INSIGNIA_QUEUE = "bma_insignia_request";
|
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,
|
#endregion
|
||||||
UserProfileRepository userProfileRepository,
|
|
||||||
|
#region " Constructor and Destructor "
|
||||||
|
|
||||||
|
public RabbitMQConsumer(UserProfileRepository userProfileRepository,
|
||||||
InsigniaPeriodsRepository insigniaPeriodsRepository,
|
InsigniaPeriodsRepository insigniaPeriodsRepository,
|
||||||
IHttpContextAccessor httpContextAccessor)
|
IHttpContextAccessor httpContextAccessor,
|
||||||
|
IServiceScopeFactory serviceScopeFactory,
|
||||||
|
IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_connection = connection;
|
_serviceScopeFactory = serviceScopeFactory;
|
||||||
|
_configuration = configuration;
|
||||||
_userProfileRepository = userProfileRepository;
|
_userProfileRepository = userProfileRepository;
|
||||||
_insigniaPeriodsRepository = insigniaPeriodsRepository;
|
_insigniaPeriodsRepository = insigniaPeriodsRepository;
|
||||||
_httpContextAccessor = httpContextAccessor;
|
_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 "
|
#region " Properties "
|
||||||
|
|
||||||
private bool? RoleAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("admin");
|
private bool? RoleAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("admin");
|
||||||
|
|
@ -38,67 +74,71 @@ namespace BMA.EHR.Infrastructure.MessageQueue
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region " Methods "
|
||||||
|
|
||||||
|
#region " Private "
|
||||||
|
|
||||||
private async Task CalculateInsigniaAsync(Guid periodId)
|
private async Task CalculateInsigniaAsync(Guid periodId)
|
||||||
{
|
{
|
||||||
var organizations = await _userProfileRepository.GetActiveRootAsync(AccessToken);
|
using (var scope = _serviceScopeFactory.CreateScope())
|
||||||
|
|
||||||
foreach (var organization in organizations)
|
|
||||||
{
|
{
|
||||||
if (organization == null)
|
var userRepo = scope.ServiceProvider.GetRequiredService<UserProfileRepository>();
|
||||||
continue;
|
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);
|
foreach (var organization in organizations)
|
||||||
if (result != null)
|
|
||||||
{
|
{
|
||||||
Guid period = result.PeriodId;
|
if (organization == null)
|
||||||
string requestStatus = result.RequestStatus;
|
continue;
|
||||||
var candidate = await _insigniaPeriodsRepository.GetInsigniaCandidateBKK(periodId, organization.Id);
|
|
||||||
// ตรวจสอบว่ารายการอยู่ใน table insignia_request_new
|
//if(organization.Id != Guid.Parse("e8493cd1-d371-402e-add6-566e68d5d1b3")) continue;
|
||||||
if (requestStatus == null)
|
|
||||||
|
var result = await insigniaRepo.GetInsigniaRequest(periodId, organization.Id);
|
||||||
|
if (result != null)
|
||||||
{
|
{
|
||||||
// บันทึกรายชื่อ
|
Guid period = result.PeriodId;
|
||||||
await _insigniaPeriodsRepository.InsertCandidate(period, organization.Id, candidate);
|
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>
|
#endregion
|
||||||
///
|
|
||||||
/// </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);
|
#region " Overrides "
|
||||||
|
|
||||||
|
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
|
||||||
|
Console.WriteLine("ExecuteAsync started."); // Log ตรวจสอบ
|
||||||
|
var consumer = new EventingBasicConsumer(_channel);
|
||||||
consumer.Received += (model, ea) =>
|
consumer.Received += (model, ea) =>
|
||||||
{
|
{
|
||||||
var body = ea.Body.ToArray();
|
var body = ea.Body.ToArray();
|
||||||
var message = Encoding.UTF8.GetString(body);
|
var message = Encoding.UTF8.GetString(body);
|
||||||
//var request = JsonConvert.DeserializeObject<InsigniaRequestPeriod>(message);
|
|
||||||
|
|
||||||
Console.WriteLine(" [x] Received {0}", message);
|
|
||||||
var periodId = Guid.Parse(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);
|
var res = CalculateInsigniaAsync(periodId);
|
||||||
Console.WriteLine(" [x] Success Calculate Period {0}", periodId);
|
Console.WriteLine(" [x] Success Calculate Period {0}", periodId);
|
||||||
};
|
};
|
||||||
|
_channel.BasicConsume(queue: INSIGNIA_QUEUE, autoAck: true, consumer: consumer);
|
||||||
|
|
||||||
channel.BasicConsume(queue: INSIGNIA_QUEUE,
|
return Task.CompletedTask;
|
||||||
autoAck: true,
|
|
||||||
consumer: consumer,
|
|
||||||
consumerTag: "",
|
|
||||||
noLocal: false,
|
|
||||||
exclusive: false,
|
|
||||||
arguments: null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#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>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.IdentityModel.Logging" Version="6.32.0" />
|
<PackageReference Include="Microsoft.IdentityModel.Logging" Version="6.32.0" />
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
|
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
|
||||||
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
|
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,10 @@ using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using OfficeOpenXml;
|
using OfficeOpenXml;
|
||||||
|
using RabbitMQ.Client;
|
||||||
using Swashbuckle.AspNetCore.Annotations;
|
using Swashbuckle.AspNetCore.Annotations;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace BMA.EHR.Insignia.Service.Controllers
|
namespace BMA.EHR.Insignia.Service.Controllers
|
||||||
{
|
{
|
||||||
|
|
@ -36,10 +38,10 @@ namespace BMA.EHR.Insignia.Service.Controllers
|
||||||
private readonly IWebHostEnvironment _hostingEnvironment;
|
private readonly IWebHostEnvironment _hostingEnvironment;
|
||||||
private readonly string Royal_Type = "Royal";
|
private readonly string Royal_Type = "Royal";
|
||||||
private readonly UserProfileRepository _userProfileRepository;
|
private readonly UserProfileRepository _userProfileRepository;
|
||||||
|
private const string INSIGNIA_QUEUE = "bma_insignia_request";
|
||||||
private readonly RabbitMQProducer _producer;
|
|
||||||
|
|
||||||
private readonly InsigniaPeriodsRepository _insigniaPeriodRepository;
|
private readonly InsigniaPeriodsRepository _insigniaPeriodRepository;
|
||||||
|
private readonly IConfiguration _configuration;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
|
|
@ -61,7 +63,7 @@ namespace BMA.EHR.Insignia.Service.Controllers
|
||||||
IHttpContextAccessor httpContextAccessor,
|
IHttpContextAccessor httpContextAccessor,
|
||||||
UserProfileRepository userProfileRepository,
|
UserProfileRepository userProfileRepository,
|
||||||
InsigniaPeriodsRepository insigniaPeriodRepository,
|
InsigniaPeriodsRepository insigniaPeriodRepository,
|
||||||
RabbitMQProducer producer)
|
IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
_documentService = documentService;
|
_documentService = documentService;
|
||||||
|
|
@ -71,7 +73,7 @@ namespace BMA.EHR.Insignia.Service.Controllers
|
||||||
_hostingEnvironment = hostingEnvironment;
|
_hostingEnvironment = hostingEnvironment;
|
||||||
_userProfileRepository = userProfileRepository;
|
_userProfileRepository = userProfileRepository;
|
||||||
_insigniaPeriodRepository = insigniaPeriodRepository;
|
_insigniaPeriodRepository = insigniaPeriodRepository;
|
||||||
_producer = producer;
|
_configuration = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
#region " Properties "
|
#region " Properties "
|
||||||
|
|
@ -320,21 +322,7 @@ namespace BMA.EHR.Insignia.Service.Controllers
|
||||||
// }
|
// }
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region " ทดสอบ RabbitMQ "
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
[HttpGet("rabbit")]
|
|
||||||
[AllowAnonymous]
|
|
||||||
public ActionResult<ResponseObject> CallRabbitMQ()
|
|
||||||
{
|
|
||||||
_producer.SendMessage("test send");
|
|
||||||
return Success();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region " จัดทำรายชื่อครูที่มีสิทธิในการยืนขอเครื่องราชฯ "
|
#region " จัดทำรายชื่อครูที่มีสิทธิในการยืนขอเครื่องราชฯ "
|
||||||
|
|
||||||
|
|
@ -603,9 +591,30 @@ namespace BMA.EHR.Insignia.Service.Controllers
|
||||||
[HttpGet("queue/{insigniaPeriodId:length(36)}")]
|
[HttpGet("queue/{insigniaPeriodId:length(36)}")]
|
||||||
public ActionResult<ResponseObject> InsigniaRequestCalculate(Guid insigniaPeriodId)
|
public ActionResult<ResponseObject> InsigniaRequestCalculate(Guid insigniaPeriodId)
|
||||||
{
|
{
|
||||||
_producer.CalculateInsignia(insigniaPeriodId);
|
var host = _configuration["RabbitMQ:URL"];
|
||||||
return Success();
|
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
|
#endregion
|
||||||
|
|
||||||
#region " บันทึกหมายเหตุ "
|
#region " บันทึกหมายเหตุ "
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using BMA.EHR.Application;
|
using BMA.EHR.Application;
|
||||||
|
using BMA.EHR.Application.Repositories;
|
||||||
using BMA.EHR.Application.Repositories.Reports;
|
using BMA.EHR.Application.Repositories.Reports;
|
||||||
using BMA.EHR.Domain.Middlewares;
|
using BMA.EHR.Domain.Middlewares;
|
||||||
using BMA.EHR.Infrastructure;
|
using BMA.EHR.Infrastructure;
|
||||||
|
|
@ -91,8 +92,7 @@ var builder = WebApplication.CreateBuilder(args);
|
||||||
builder.Services.AddLeaveApplication();
|
builder.Services.AddLeaveApplication();
|
||||||
builder.Services.AddLeavePersistence(builder.Configuration);
|
builder.Services.AddLeavePersistence(builder.Configuration);
|
||||||
|
|
||||||
// RabbitMQ
|
|
||||||
builder.Services.AddMessageQueue();
|
|
||||||
|
|
||||||
builder.Services.AddControllers(options =>
|
builder.Services.AddControllers(options =>
|
||||||
{
|
{
|
||||||
|
|
@ -106,7 +106,7 @@ var builder = WebApplication.CreateBuilder(args);
|
||||||
// Register DbContext
|
// Register DbContext
|
||||||
var defaultConnection = builder.Configuration.GetConnectionString("DefaultConnection");
|
var defaultConnection = builder.Configuration.GetConnectionString("DefaultConnection");
|
||||||
builder.Services.AddDbContext<ApplicationDBContext>(options =>
|
builder.Services.AddDbContext<ApplicationDBContext>(options =>
|
||||||
options.UseMySql(defaultConnection, ServerVersion.AutoDetect(defaultConnection)));
|
options.UseMySql(defaultConnection, ServerVersion.AutoDetect(defaultConnection)), ServiceLifetime.Transient);
|
||||||
builder.Services.AddHealthChecks();
|
builder.Services.AddHealthChecks();
|
||||||
// Add Hangfire services.
|
// Add Hangfire services.
|
||||||
builder.Services.AddHangfire(configuration => configuration
|
builder.Services.AddHangfire(configuration => configuration
|
||||||
|
|
@ -129,6 +129,19 @@ var builder = WebApplication.CreateBuilder(args);
|
||||||
TablesPrefix = "Hangfire"
|
TablesPrefix = "Hangfire"
|
||||||
})));
|
})));
|
||||||
builder.Services.AddHangfireServer();
|
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();
|
var app = builder.Build();
|
||||||
|
|
@ -151,6 +164,8 @@ var app = builder.Build();
|
||||||
app.MapHealthChecks("/health");
|
app.MapHealthChecks("/health");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
app.UseCors();
|
app.UseCors();
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
|
|
@ -176,8 +191,8 @@ var app = builder.Build();
|
||||||
await using var db = scope.ServiceProvider.GetRequiredService<ApplicationDBContext>();
|
await using var db = scope.ServiceProvider.GetRequiredService<ApplicationDBContext>();
|
||||||
await db.Database.MigrateAsync();
|
await db.Database.MigrateAsync();
|
||||||
|
|
||||||
var rabbitMQConsumer = app.Services.GetRequiredService<RabbitMQConsumer>();
|
//var rabbitMQConsumer = app.Services.GetRequiredService<RabbitMQConsumer>();
|
||||||
rabbitMQConsumer.StartReceiving();
|
//rabbitMQConsumer.StartReceiving();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue