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; using Newtonsoft.Json; using System.Net.Http.Headers; using System.Security.Claims; using System.Text; namespace BMA.EHR.Application.Repositories { public class RetirementRepository : GenericRepository { 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; } #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 //ปลดออก public async Task NotifyDischarge() { var cronjobNotis = await _dbContext.Set() .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() .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() //.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() // .Include(x => x.RetirementRawProfiles.Where(y => y.Remove != "REMOVE")) // .Where(x => x.Year == DateTime.Now.Year) // .Where(x => x.Type.Trim().ToUpper().Contains("OFFICER")) // .FirstOrDefaultAsync(); // if (retirePeriodOfficer == null) // return; // var body = new // { // data = retirePeriodOfficer.RetirementRawProfiles // .Select(x => new // { // profileId = x.profileId // }) // .ToList() // }; // //ข้าราชการ // //var apiUrl = $"{_configuration["API"]}/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) // { // } // } //} public void TestMethod() { return; } } }