using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using BMA.EHR.Recruit.Service.Data; using BMA.EHR.Recruit.Service.Models.Recruits; using BMA.EHR.Recruit.Service.Core; namespace BMA.EHR.Recruit.Service.Services { public class RecruitService { private readonly ApplicationDbContext _context; private readonly MinIOService _minIOService; public RecruitService(ApplicationDbContext context, MinIOService minIOService) { _context = context; _minIOService = minIOService; } public int GetExamCount(string citizenId) { try { var count = _context.Recruits.AsQueryable() .Where(x => x.CitizenId == citizenId) .Count(); return count; } catch { throw; } } public async Task GetExamAttributeAsync(Guid period, Guid exam) { try { var payment = await _context.RecruitPayments.AsQueryable() .Include(x => x.Recruit) .ThenInclude(x => x.RecruitImport) .Where(x => x.Recruit.Id == exam) .Where(x => x.Recruit.RecruitImport.Id == period) .FirstOrDefaultAsync(); return payment != null ? "มีคุณสมบัติ" : "ไม่มีคุณสมบัติ"; } catch { throw; } } public bool CheckValidCertificate(DateTime certDate, int nextYear = 5) { var valid = true; if (DateTime.Now.Date > certDate.Date.AddYears(nextYear)) valid = false; return valid; } public async Task UpdateDocAsync(Guid ImportId, IFormFileCollection files) { var periodExam = await _context.RecruitImports.AsQueryable() .FirstOrDefaultAsync(x => x.Id == ImportId); if (periodExam == null) throw new Exception(GlobalMessages.DataNotFound); foreach (var file in files) { var doc = await _minIOService.UploadFileAsync(file); var periodExamDocument = new RecruitImportDocument { RecruitImportId = ImportId, DocumentId = doc.Id, }; await _context.RecruitImportDocuments.AddAsync(periodExamDocument); } await _context.SaveChangesAsync(); } public async Task UpdateImageAsync(Guid ImportId, IFormFileCollection files) { var periodExam = await _context.RecruitImports.AsQueryable() .FirstOrDefaultAsync(x => x.Id == ImportId); if (periodExam == null) throw new Exception(GlobalMessages.DataNotFound); foreach (var file in files) { var doc = await _minIOService.UploadFileAsync(file); var periodExamImage = new RecruitImportImage { RecruitImportId = ImportId, DocumentId = doc.Id, }; await _context.RecruitImportImages.AddAsync(periodExamImage); } await _context.SaveChangesAsync(); } public async Task DeleteImageAsync(Guid id) { var image = await _context.RecruitImportImages.AsQueryable() .Include(x => x.Document) .FirstOrDefaultAsync(x => x.Id == id); if (image == null) throw new Exception(GlobalMessages.DataNotFound); var doc_id = image.Document.Id; _context.RecruitImportImages.Remove(image); await _context.SaveChangesAsync(); await _minIOService.DeleteFileAsync(doc_id); } public async Task DeleteDocAsync(Guid id) { var doc = await _context.RecruitImportDocuments.AsQueryable() .Include(x => x.Document) .FirstOrDefaultAsync(x => x.Id == id); if (doc == null) throw new Exception(GlobalMessages.DataNotFound); var doc_id = doc.Document.Id; _context.RecruitImportDocuments.Remove(doc); await _context.SaveChangesAsync(); await _minIOService.DeleteFileAsync(doc_id); } } }