using System.Security.Claims; using System.Text.Json; using BMA.EHR.Recurit.Exam.Service.Core; using BMA.EHR.Recurit.Exam.Service.Data; using BMA.EHR.Recurit.Exam.Service.Models; using BMA.EHR.Recurit.Exam.Service.Models.Documents; using BMA.EHR.Recurit.Exam.Service.Request; using BMA.EHR.Recurit.Exam.Service.Response; using BMA.EHR.Recurit.Exam.Service.Responses.Document; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json.Linq; using OfficeOpenXml; namespace BMA.EHR.Recurit.Exam.Service.Services { public class CMSCandidateService { #region " Fields " private readonly ApplicationDbContext _context; private readonly IHttpContextAccessor _httpContextAccessor; private readonly MinIOService _minioService; #endregion #region " Constructor and Destructor " public CMSCandidateService(ApplicationDbContext context, IHttpContextAccessor httpContextAccessor, MinIOService minioService) { _context = context; _httpContextAccessor = httpContextAccessor; _minioService = minioService; } #endregion #region " Properties " private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value; private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value; #endregion #region " Methods " public async Task GetsAsync() { var cms = await createCMS(); return cms; } public async Task createCMS() { var cmsCandidates = await _context.CMSCandidates.AsQueryable() .Include(x => x.BannerImg) .Include(x => x.LogoImg) .Include(x => x.CMSAgencys) .Include(x => x.CMSGovernments) .FirstOrDefaultAsync(); if (cmsCandidates == null) { cmsCandidates = new CMSCandidate { CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, LastUpdateUserId = UserId ?? "", CreatedFullName = FullName ?? "", LastUpdateFullName = FullName ?? "", }; await _context.CMSCandidates.AddAsync(cmsCandidates); } return cmsCandidates; } public async Task UpdateDetailAsync(RequestCMSAbout updated) { var cms = await createCMS(); cms.NameTh = updated.NameTh; cms.NameEn = updated.NameEn; cms.Description = updated.Description; cms.LastUpdatedAt = DateTime.Now; cms.LastUpdateUserId = UserId ?? ""; cms.LastUpdateFullName = FullName ?? ""; await _context.SaveChangesAsync(); } public async Task UpdateAboutAsync(RequestCMSAbout updated) { var cms = await createCMS(); if (updated.ProvinceId != null) { var province = await _context.Provinces.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.ProvinceId)); if (province == null) throw new Exception(GlobalMessages.ProvinceNotFound); cms.Province = province; } if (updated.DistrictId != null) { var pistrict = await _context.Districts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.DistrictId)); if (pistrict == null) throw new Exception(GlobalMessages.DistrictNotFound); cms.District = pistrict; } if (updated.SubDistrictId != null) { var subDistrict = await _context.SubDistricts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.SubDistrictId)); if (subDistrict == null) throw new Exception(GlobalMessages.SubDistrictNotFound); cms.SubDistrict = subDistrict; cms.ZipCode = subDistrict.ZipCode; } cms.NameTh = updated.About; cms.NameEn = updated.Address; cms.Description = updated.Telephone; cms.LastUpdatedAt = DateTime.Now; cms.LastUpdateUserId = UserId ?? ""; cms.LastUpdateFullName = FullName ?? ""; await _context.SaveChangesAsync(); } public async Task UpdateLogoAsync(IFormFile file) { var cms = await createCMS(); if (cms.LogoImg != null) { await DeleteAsyncDocument(cms.LogoImg.Id.ToString()); } var doc = await _minioService.UploadFileAsync(file); var document = await _context.Documents.AsQueryable() .FirstOrDefaultAsync(x => x.Id == doc.Id); cms.LogoImg = document; await _context.SaveChangesAsync(); } public async Task UpdateBannerAsync(IFormFile file) { var cms = await createCMS(); if (cms.BannerImg != null) { await DeleteAsyncDocument(cms.BannerImg.Id.ToString()); } var doc = await _minioService.UploadFileAsync(file); var document = await _context.Documents.AsQueryable() .FirstOrDefaultAsync(x => x.Id == doc.Id); cms.BannerImg = document; await _context.SaveChangesAsync(); } public async Task DeleteAsyncDocument(string documentId) { await _minioService.DeleteFileAsync(Guid.Parse(documentId)); } public async Task UpdateAgencyAsync(List updated) { var cms = await createCMS(); _context.CMSAgencys.RemoveRange(cms.CMSAgencys); foreach (var agencyData in updated) { var agency = new CMSAgency { CMSCandidate = cms, Name = agencyData.Name, Link = agencyData.Link, CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, LastUpdateUserId = UserId ?? "", CreatedFullName = FullName ?? "", LastUpdateFullName = FullName ?? "", }; await _context.AddAsync(agency); } await _context.SaveChangesAsync(); } public async Task UpdateGovernmentAsync(List updated) { var cms = await createCMS(); _context.CMSGovernments.RemoveRange(cms.CMSGovernments); foreach (var governmentData in updated) { var government = new CMSGovernment { CMSCandidate = cms, Name = governmentData.Name, Link = governmentData.Link, CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, LastUpdateUserId = UserId ?? "", CreatedFullName = FullName ?? "", LastUpdateFullName = FullName ?? "", }; await _context.AddAsync(government); } await _context.SaveChangesAsync(); } #endregion } }