160 lines
8.4 KiB
C#
160 lines
8.4 KiB
C#
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using BMA.EHR.Application.Common.Interfaces;
|
|
using BMA.EHR.Application.Repositories.MessageQueue;
|
|
using BMA.EHR.Domain.Models.Discipline;
|
|
using BMA.EHR.Domain.Models.Retirement;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json;
|
|
using Microsoft.Extensions.Configuration;
|
|
using BMA.EHR.Application.Requests;
|
|
|
|
namespace BMA.EHR.Application.Repositories
|
|
{
|
|
public class DisciplineRepository : GenericRepository<Guid, RetirementPeriod>
|
|
{
|
|
private readonly IApplicationDBContext _dbContext;
|
|
private readonly IDisciplineDbContext _dbDisContext;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
private readonly NotificationRepository _repositoryNoti;
|
|
private readonly IConfiguration _configuration;
|
|
public DisciplineRepository(IApplicationDBContext dbContext,
|
|
IDisciplineDbContext dbDisContext,
|
|
NotificationRepository repositoryNoti,
|
|
IConfiguration configuration,
|
|
IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
|
|
{
|
|
_dbContext = dbContext;
|
|
_dbDisContext = dbDisContext;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
_repositoryNoti = repositoryNoti;
|
|
_configuration = configuration;
|
|
}
|
|
|
|
//เรื่องร้องเรียน
|
|
public async Task NotifyDisciplineComplaint()
|
|
{
|
|
var cronjobNotis = await _dbDisContext.Set<DisciplineComplaint>()
|
|
.Include(x => x.DisciplineComplaint_Profiles)
|
|
.Where(x => x.DateNotification != null)
|
|
.Where(x => x.DateNotification.Value.Date == DateTime.Now.Date)
|
|
.AsQueryable()
|
|
.ToListAsync();
|
|
foreach (var cronjobNoti in cronjobNotis)
|
|
{
|
|
var baseAPIOrg = _configuration["API"];
|
|
var apiUrlOrg = $"{baseAPIOrg}/org/workflow/find/director";
|
|
var refId = new List<Guid>();
|
|
using (var client = new HttpClient())
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken.Replace("Bearer ", ""));
|
|
client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]);
|
|
var _res = await client.PostAsJsonAsync(apiUrlOrg, new
|
|
{
|
|
refId = cronjobNoti.DisciplineComplaint_Profiles.Select(x => x.PersonId),
|
|
});
|
|
var _result = await _res.Content.ReadAsStringAsync();
|
|
var org = JsonConvert.DeserializeObject<DirectorRequest>(_result);
|
|
if (_res.IsSuccessStatusCode)
|
|
{
|
|
refId = org.result.Select(x => Guid.Parse(x.id)).ToList();
|
|
}
|
|
}
|
|
await _repositoryNoti.PushNotificationsAsync(
|
|
refId.ToArray(),
|
|
$"แจ้งเตือนบันทึกข้อมูลร้องเรียนก่อนวันสิ้นสุดเรื่อง {cronjobNoti.Title}",
|
|
$"แจ้งเตือนบันทึกข้อมูลร้องเรียนก่อนวันสิ้นสุดเรื่อง {cronjobNoti.Title}",
|
|
"",
|
|
"",
|
|
true,
|
|
true
|
|
);
|
|
}
|
|
|
|
await _dbContext.SaveChangesAsync();
|
|
}
|
|
|
|
//เรื่องสืบสวน
|
|
public async Task NotifyDisciplineInvestigate()
|
|
{
|
|
var cronjobNotis = await _dbDisContext.Set<DisciplineInvestigate>()
|
|
.Include(x => x.DisciplineInvestigate_ProfileComplaints)
|
|
.Where(x => x.InvestigationDateEnd != null && x.InvestigationDateEnd.Value.Date.AddDays(-7) == DateTime.Now.Date)
|
|
.AsQueryable()
|
|
.ToListAsync();
|
|
foreach (var cronjobNoti in cronjobNotis)
|
|
{
|
|
var baseAPIOrg = _configuration["API"];
|
|
var apiUrlOrg = $"{baseAPIOrg}/org/workflow/find/director";
|
|
var refId = new List<Guid>();
|
|
using (var client = new HttpClient())
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken.Replace("Bearer ", ""));
|
|
client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]);
|
|
var _res = await client.PostAsJsonAsync(apiUrlOrg, new
|
|
{
|
|
refId = cronjobNoti.DisciplineInvestigate_ProfileComplaints.Select(x => x.PersonId),
|
|
});
|
|
var _result = await _res.Content.ReadAsStringAsync();
|
|
var org = JsonConvert.DeserializeObject<DirectorRequest>(_result);
|
|
if (_res.IsSuccessStatusCode)
|
|
{
|
|
refId = org.result.Select(x => Guid.Parse(x.id)).ToList();
|
|
}
|
|
}
|
|
await _repositoryNoti.PushNotificationsAsync(
|
|
refId.ToArray(),
|
|
$"แจ้งเตือนบันทึกข้อมูลสืบสวนก่อนวันสิ้นสุดเรื่อง {cronjobNoti.Title}",
|
|
$"แจ้งเตือนบันทึกข้อมูลสืบสวนก่อนวันสิ้นสุดเรื่อง {cronjobNoti.Title}",
|
|
"",
|
|
"",
|
|
true,
|
|
true
|
|
);
|
|
}
|
|
await _dbContext.SaveChangesAsync();
|
|
}
|
|
|
|
//เรื่องสอบสวน
|
|
public async Task NotifyDisciplineDisciplinary()
|
|
{
|
|
var cronjobNotis = await _dbDisContext.Set<DisciplineDisciplinary>()
|
|
.Include(x => x.DisciplineDisciplinary_ProfileComplaintInvestigates)
|
|
.Where(x => x.DisciplinaryDateEnd != null && x.DisciplinaryDateEnd.Value.Date.AddDays(-7) == DateTime.Now.Date)
|
|
.AsQueryable()
|
|
.ToListAsync();
|
|
foreach (var cronjobNoti in cronjobNotis)
|
|
{
|
|
var baseAPIOrg = _configuration["API"];
|
|
var apiUrlOrg = $"{baseAPIOrg}/org/workflow/find/director";
|
|
var refId = new List<Guid>();
|
|
using (var client = new HttpClient())
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken.Replace("Bearer ", ""));
|
|
client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]);
|
|
var _res = await client.PostAsJsonAsync(apiUrlOrg, new
|
|
{
|
|
refId = cronjobNoti.DisciplineDisciplinary_ProfileComplaintInvestigates.Select(x => x.PersonId),
|
|
});
|
|
var _result = await _res.Content.ReadAsStringAsync();
|
|
var org = JsonConvert.DeserializeObject<DirectorRequest>(_result);
|
|
if (_res.IsSuccessStatusCode)
|
|
{
|
|
refId = org.result.Select(x => Guid.Parse(x.id)).ToList();
|
|
}
|
|
}
|
|
await _repositoryNoti.PushNotificationsAsync(
|
|
refId.ToArray(),
|
|
$"แจ้งเตือนบันทึกข้อมูลสอบสวนก่อนวันสิ้นสุดเรื่อง {cronjobNoti.Title}",
|
|
$"แจ้งเตือนบันทึกข้อมูลสอบสวนก่อนวันสิ้นสุดเรื่อง {cronjobNoti.Title}",
|
|
"",
|
|
"",
|
|
true,
|
|
true
|
|
);
|
|
}
|
|
await _dbContext.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|