hrms-api-backend/BMA.EHR.Application/Repositories/RetirementRepository.cs

152 lines
7.5 KiB
C#
Raw Normal View History

2023-07-22 22:14:50 +07:00
using BMA.EHR.Application.Common.Interfaces;
2023-09-13 13:11:05 +07:00
using BMA.EHR.Application.Repositories.MessageQueue;
2023-07-22 22:14:50 +07:00
using BMA.EHR.Domain.Models.Retirement;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
2025-01-29 12:15:30 +07:00
using Microsoft.Extensions.Configuration;
2025-01-29 13:25:38 +07:00
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text;
2023-07-22 22:14:50 +07:00
namespace BMA.EHR.Application.Repositories
{
public class RetirementRepository : GenericRepository<Guid, RetirementPeriod>
{
private readonly IApplicationDBContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor;
2023-09-13 13:11:05 +07:00
private readonly NotificationRepository _repositoryNoti;
2025-01-29 12:15:30 +07:00
private readonly IConfiguration _configuration;
2023-09-13 13:11:05 +07:00
public RetirementRepository(IApplicationDBContext dbContext,
NotificationRepository repositoryNoti,
2025-01-29 12:15:30 +07:00
IHttpContextAccessor httpContextAccessor,
IConfiguration configuration) : base(dbContext, httpContextAccessor)
2023-07-22 22:14:50 +07:00
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
2023-09-13 13:11:05 +07:00
_repositoryNoti = repositoryNoti;
2025-01-29 12:15:30 +07:00
_configuration = configuration;
2023-07-22 22:14:50 +07:00
}
2025-01-29 13:25:38 +07:00
#region " Properties "
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
private string? token => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"];
#endregion
2023-09-13 13:11:05 +07:00
//ปลดออก
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();
}
2023-07-22 22:14:50 +07:00
2023-09-13 13:11:05 +07:00
//ไล่ออก
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)
2023-09-13 13:11:05 +07:00
.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;
2023-09-13 13:11:05 +07:00
// 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();
}
2025-01-28 16:26:43 +07:00
//เกษียณอายุราชการ
public async Task ExecuteRetirement()
{
2025-01-29 12:15:30 +07:00
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();
//ข้าราชการ
2025-01-29 13:25:38 +07:00
//var apiUrl = $"{_configuration["API"]}/org/unauthorize/retirement";
var apiUrl = $"http://localhost:13001/api/v1/org/unauthorize/retirement";
2025-01-29 12:15:30 +07:00
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)
{
}
}
2025-01-28 16:26:43 +07:00
}
2025-01-29 20:25:19 +07:00
public void TestMethod()
{
return;
}
2023-07-22 22:14:50 +07:00
}
}