hrms-api-backend/BMA.EHR.Application/Repositories/RetirementRepository.cs
2025-01-29 12:15:30 +07:00

135 lines
6.9 KiB
C#

using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Repositories.MessageQueue;
using BMA.EHR.Domain.Models.Retirement;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace BMA.EHR.Application.Repositories
{
public class RetirementRepository : GenericRepository<Guid, RetirementPeriod>
{
private readonly IApplicationDBContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly NotificationRepository _repositoryNoti;
private readonly IConfiguration _configuration;
public RetirementRepository(IApplicationDBContext dbContext,
NotificationRepository repositoryNoti,
IHttpContextAccessor httpContextAccessor,
IConfiguration configuration) : base(dbContext, httpContextAccessor)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
_repositoryNoti = repositoryNoti;
_configuration = configuration;
}
//ปลดออก
public async Task NotifyDischarge()
{
var cronjobNotis = await _dbContext.Set<RetirementDischarge>()
.Include(x => x.Profile)
.ThenInclude(x => x.Prefix)
.Where(x => x.Date != null && x.Date.Value.Date == DateTime.Now.Date)
.AsQueryable()
.ToListAsync();
foreach (var cronjobNoti in cronjobNotis)
{
cronjobNoti.Profile.IsLeave = true;
cronjobNoti.Profile.LeaveReason = "DISCHARGE";
cronjobNoti.Profile.LeaveDate = DateTime.Now;
// await _repositoryNoti.PushNotificationAsync(
// Guid.Parse("08db721d-ada0-4e64-89d3-7584a893d8b8"),
// $"แจ้งเตือนการปลดออกของ {cronjobNoti.Profile.Prefix?.Name}{cronjobNoti.Profile.FirstName} {cronjobNoti.Profile.LastName}",
// $"แจ้งเตือนการปลดออกของ {cronjobNoti.Profile.Prefix?.Name}{cronjobNoti.Profile.FirstName} {cronjobNoti.Profile.LastName}"
// );
}
await _dbContext.SaveChangesAsync();
}
//ไล่ออก
public async Task NotifyExpulsion()
{
var cronjobNotis = await _dbContext.Set<RetirementExpulsion>()
.Include(x => x.Profile)
.ThenInclude(x => x.Prefix)
.Where(x => x.Date != null && x.Date.Value.Date == DateTime.Now.Date)
.AsQueryable()
.ToListAsync();
foreach (var cronjobNoti in cronjobNotis)
{
cronjobNoti.Profile.IsLeave = true;
cronjobNoti.Profile.LeaveReason = "DISMISS";
cronjobNoti.Profile.LeaveDate = DateTime.Now;
// await _repositoryNoti.PushNotificationAsync(
// Guid.Parse("08db721d-ada0-4e64-89d3-7584a893d8b8"),
// $"แจ้งเตือนการปลดออกของ {cronjobNoti.Profile.Prefix?.Name}{cronjobNoti.Profile.FirstName} {cronjobNoti.Profile.LastName}",
// $"แจ้งเตือนการปลดออกของ {cronjobNoti.Profile.Prefix?.Name}{cronjobNoti.Profile.FirstName} {cronjobNoti.Profile.LastName}"
// );
}
await _dbContext.SaveChangesAsync();
}
//ให้ออก
public async Task NotifyOut()
{
var cronjobNotis = await _dbContext.Set<RetirementOut>()
//.Include(x => x.Profile)
//.ThenInclude(x => x.Prefix)
.Where(x => x.Date != null && x.Date.Value.Date == DateTime.Now.Date)
.AsQueryable()
.ToListAsync();
foreach (var cronjobNoti in cronjobNotis)
{
//cronjobNoti.Profile.IsLeave = true;
//cronjobNoti.Profile.LeaveReason = "LAYOFF";
//cronjobNoti.Profile.LeaveDate = DateTime.Now;
// await _repositoryNoti.PushNotificationAsync(
// Guid.Parse("08db721d-ada0-4e64-89d3-7584a893d8b8"),
// $"แจ้งเตือนการปลดออกของ {cronjobNoti.Profile.Prefix?.Name}{cronjobNoti.Profile.FirstName} {cronjobNoti.Profile.LastName}",
// $"แจ้งเตือนการปลดออกของ {cronjobNoti.Profile.Prefix?.Name}{cronjobNoti.Profile.FirstName} {cronjobNoti.Profile.LastName}"
// );
}
await _dbContext.SaveChangesAsync();
}
//เกษียณอายุราชการ
public async Task ExecuteRetirement()
{
var retirePeriodOfficer = await _dbContext.Set<RetirementPeriod>()
.Include(x => x.RetirementRawProfiles.Where(y => y.Remove != "REMOVE"))
.Where(x => x.Year == /*DateTime.Now.Year*/2026)
.Where(x => x.Type.Trim().ToUpper().Contains("OFFICER"))
.FirstOrDefaultAsync();
var body = retirePeriodOfficer.RetirementProfiles
.Select(x => new {
profileId = x.profileId,
//lastUpdateUserId = UserId,
//lastUpdateFullName = FullName,
})
.ToList();
//ข้าราชการ
var apiUrl = $"{_configuration["API"]}/org/unauthorize/retirement";
//var apiUrl = $"http://localhost:13001/api/v1/org/unauthorize/retirement";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", ""));
client.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]);
var jsonBody = JsonConvert.SerializeObject(body);
var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
var _req = new HttpRequestMessage(HttpMethod.Patch, apiUrl)
{
Content = content
};
var response = await client.SendAsync(_req);
var responseContent = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
}
}
}
}
}