diff --git a/BMA.EHR.Application/Repositories/Reports/InsigniaReportRepository.cs b/BMA.EHR.Application/Repositories/Reports/InsigniaReportRepository.cs index dbb3e3c5..1d783717 100644 --- a/BMA.EHR.Application/Repositories/Reports/InsigniaReportRepository.cs +++ b/BMA.EHR.Application/Repositories/Reports/InsigniaReportRepository.cs @@ -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, diff --git a/BMA.EHR.Infrastructure/InfrastructureServiceRegistration.cs b/BMA.EHR.Infrastructure/InfrastructureServiceRegistration.cs index 85e8a23c..36ba47ef 100644 --- a/BMA.EHR.Infrastructure/InfrastructureServiceRegistration.cs +++ b/BMA.EHR.Infrastructure/InfrastructureServiceRegistration.cs @@ -12,9 +12,7 @@ namespace BMA.EHR.Infrastructure { public static IServiceCollection AddMessageQueue(this IServiceCollection services) { - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); + services.AddHostedService(); return services; } @@ -37,7 +35,7 @@ namespace BMA.EHR.Infrastructure }), ServiceLifetime.Transient); - services.AddScoped(provider => provider.GetService()); + services.AddTransient(provider => provider.GetService()); return services; } @@ -60,7 +58,7 @@ namespace BMA.EHR.Infrastructure }), ServiceLifetime.Transient); - services.AddScoped(provider => provider.GetService()); + services.AddTransient(provider => provider.GetService()); return services; } @@ -83,7 +81,7 @@ namespace BMA.EHR.Infrastructure }), ServiceLifetime.Transient); - services.AddScoped(provider => provider.GetService()); + services.AddTransient(provider => provider.GetService()); var connectionStringExam = configuration.GetConnectionString("ExamConnection"); @@ -97,7 +95,8 @@ namespace BMA.EHR.Infrastructure }), ServiceLifetime.Transient); - services.AddScoped(provider => provider.GetService()); + services.AddTransient(provider => provider.GetService()); + return services; } diff --git a/BMA.EHR.Infrastructure/MessageQueue/InsigniaRequestPeriod.cs b/BMA.EHR.Infrastructure/MessageQueue/InsigniaRequestPeriod.cs deleted file mode 100644 index 6b7d2e5f..00000000 --- a/BMA.EHR.Infrastructure/MessageQueue/InsigniaRequestPeriod.cs +++ /dev/null @@ -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; } - } -} \ No newline at end of file diff --git a/BMA.EHR.Infrastructure/MessageQueue/RabbitMQConnection.cs b/BMA.EHR.Infrastructure/MessageQueue/RabbitMQConnection.cs deleted file mode 100644 index 2c69ece5..00000000 --- a/BMA.EHR.Infrastructure/MessageQueue/RabbitMQConnection.cs +++ /dev/null @@ -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; - - /// - /// - /// - /// - 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(); - } - - /// - /// - /// - /// - public IModel GetChannel() => _channel; - - /// - /// - /// - public void Dispose() - { - _channel?.Close(); - _connection?.Close(); - } - } -} \ No newline at end of file diff --git a/BMA.EHR.Infrastructure/MessageQueue/RabbitMQConsumer.cs b/BMA.EHR.Infrastructure/MessageQueue/RabbitMQConsumer.cs index 81b97c18..dbf1d15f 100644 --- a/BMA.EHR.Infrastructure/MessageQueue/RabbitMQConsumer.cs +++ b/BMA.EHR.Infrastructure/MessageQueue/RabbitMQConsumer.cs @@ -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(); + var insigniaRepo = scope.ServiceProvider.GetRequiredService(); - //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); + } } } - } + } } - /// - /// - /// - 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(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 } } \ No newline at end of file diff --git a/BMA.EHR.Infrastructure/MessageQueue/RabbitMQProducer.cs b/BMA.EHR.Infrastructure/MessageQueue/RabbitMQProducer.cs deleted file mode 100644 index 4c8ff2dc..00000000 --- a/BMA.EHR.Infrastructure/MessageQueue/RabbitMQProducer.cs +++ /dev/null @@ -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"; - - /// - /// - /// - /// - public RabbitMQProducer(RabbitMQConnection connection) - { - _connection = connection; - } - - /// - /// - /// - /// - 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); - } - } -} \ No newline at end of file diff --git a/BMA.EHR.Insignia/BMA.EHR.Insignia.csproj b/BMA.EHR.Insignia/BMA.EHR.Insignia.csproj index 0f7e4232..e4ba6d2b 100644 --- a/BMA.EHR.Insignia/BMA.EHR.Insignia.csproj +++ b/BMA.EHR.Insignia/BMA.EHR.Insignia.csproj @@ -25,6 +25,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs b/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs index 2c843cfc..26726ba9 100644 --- a/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs +++ b/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs @@ -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; /// /// @@ -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 " - /// - /// - /// - /// - [HttpGet("rabbit")] - [AllowAnonymous] - public ActionResult 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 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 " บันทึกหมายเหตุ " diff --git a/BMA.EHR.Insignia/Program.cs b/BMA.EHR.Insignia/Program.cs index ed6a54b9..2fbdb925 100644 --- a/BMA.EHR.Insignia/Program.cs +++ b/BMA.EHR.Insignia/Program.cs @@ -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(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(provider => + //{ + // var serviceScopeFactory = provider.GetRequiredService(); + // var userRepo = provider.GetRequiredService(); + // var insigniaRepo = provider.GetRequiredService(); + // var httpContext = provider.GetRequiredService(); + // var config = provider.GetRequiredService(); + + // 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(); await db.Database.MigrateAsync(); - var rabbitMQConsumer = app.Services.GetRequiredService(); - rabbitMQConsumer.StartReceiving(); + //var rabbitMQConsumer = app.Services.GetRequiredService(); + //rabbitMQConsumer.StartReceiving(); app.Run(); }