using System.Security.Claims; using System.Text.Json; using BMA.EHR.Extensions; 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 PeriodExamService { #region " Fields " private readonly ApplicationDbContext _context; private readonly MetadataDbContext _contextMetadata; private readonly IHttpContextAccessor _httpContextAccessor; private readonly MinIOService _minioService; private readonly MailService _mailService; #endregion #region " Constructor and Destructor " public PeriodExamService(ApplicationDbContext context, MetadataDbContext contextMetadata, IHttpContextAccessor httpContextAccessor, MinIOService minioService, MailService mailService) { _context = context; _contextMetadata = contextMetadata; _httpContextAccessor = httpContextAccessor; _minioService = minioService; _mailService = mailService; } #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 " private string GenerateStatusCandidate(string status) { switch (status.Trim().ToUpper()) { case "REGISTER": return "รอยืนยันสมัครสอบ"; case "CHECKREGISTER": return "ตรวจสอบข้อมูลสมัครสอบ"; case "PAYMENT": return "รอชำระค่าสมัครสอบ"; case "REJECTREGISTER": return "ปฏิเสธตรวจสอบข้อมูลสมัคร"; case "CHECKPAYMENT": return "ตรวจสอบหลักฐานชำระเงิน"; case "REJECTPAYMENT": return "หลักฐานชำระเงินไม่ถูกต้อง"; case "CHECKSEAT": return "จัดที่นั่งสอบ"; case "CHECKPOINT": return "สรุปคะแนนสอบ"; case "DONE": return "สอบคัดเลือกสำเร็จ"; case "WAIVER": return "สละสิทธิ์สอบ"; default: return status; } } private string GenerateStatusOccupation(string status) { switch (status.Trim().ToUpper()) { case "OFFICIAL": return "ข้าราชการกรุงเทพมหานคร"; case "PERSONNEL": return "บุคลากรกรุงเทพมหานคร"; case "OFFICIALSOTHER": return "ข้าราชการประเภทอื่น"; case "EMPLOYEE": return "ลูกจ้าง/พนักงานราชการของส่วนราชการอื่น"; case "OTHER": return "อื่นๆ"; default: return status; } } public async Task> GetsAsync(string type, bool showAll = true) { return await _context.PeriodExams.AsQueryable() .Where(p => p.IsActive) .Where(p => p.CheckDisability == false) .Where(p => type.ToUpper() == "ALL" ? (p.AnnouncementExam == true || p.AnnouncementExam == false) : (type.ToUpper() == "EXAM" ? p.AnnouncementExam == true : p.AnnouncementExam == false)) .OrderByDescending(d => d.CreatedAt) .Select(x => new PeriodExamCandidateResponseItem { ExamDate = x.ExamDate, AnnouncementEndDate = x.AnnouncementEndDate, AnnouncementStartDate = x.AnnouncementStartDate, AnnouncementDate = x.AnnouncementDate, CheckDisability = x.CheckDisability, CheckDocument = x.CheckDocument, Detail = x.Detail, Fee = x.Fee, Id = x.Id, IsActive = x.IsActive, Name = x.Name, Note = x.Note, OrganizationCodeId = x.OrganizationCodeId, OrganizationCodeName = x.OrganizationCodeName, OrganizationId = x.OrganizationId, OrganizationName = x.OrganizationName, PaymentEndDate = x.PaymentEndDate, PaymentKrungThai = x.PaymentKrungThai, AnnouncementExam = x.AnnouncementExam, Category = x.Category, PaymentStartDate = x.PaymentStartDate, RegisterEndDate = x.RegisterEndDate, RegisterStartDate = x.RegisterStartDate, Round = x.Round, SetSeat = x.SetSeat, Year = x.Year, }) .ToListAsync(); } public async Task GetsExamAndCandidateAsync(string examId, bool showAll = true) { var periodExam = await _context.PeriodExams.AsQueryable() .Where(x => x.CheckDisability == false) .Include(x => x.PositionExam) .Include(x => x.BankExam) .Include(x => x.PeriodExamDocuments) .Select(x => new PeriodExamCandidateResponseItem { ExamDate = x.ExamDate, AnnouncementEndDate = x.AnnouncementEndDate, AnnouncementStartDate = x.AnnouncementStartDate, AnnouncementDate = x.AnnouncementDate, CheckDisability = x.CheckDisability, CheckDocument = x.CheckDocument, Detail = x.Detail, Fee = x.Fee, Id = x.Id, IsActive = x.IsActive, Name = x.Name, Note = x.Note, OrganizationCodeId = x.OrganizationCodeId, OrganizationCodeName = x.OrganizationCodeName, OrganizationId = x.OrganizationId, OrganizationName = x.OrganizationName, PaymentEndDate = x.PaymentEndDate, PaymentKrungThai = x.PaymentKrungThai, AnnouncementExam = x.AnnouncementExam, Category = x.Category, PaymentStartDate = x.PaymentStartDate, RegisterEndDate = x.RegisterEndDate, RegisterStartDate = x.RegisterStartDate, Round = x.Round, SetSeat = x.SetSeat, Year = x.Year, BankExam = x.BankExam.OrderBy(o => o.CreatedAt).Select(b => new BankExam { Id = b.Id, AccountName = b.AccountName, AccountNumber = b.AccountNumber, BankName = b.BankName, }).ToList(), PositionExam = x.PositionExam.OrderBy(o => o.CreatedAt).Select(b => new PositionExam { Id = b.Id, TypeId = b.TypeId, TypeName = b.TypeName, PositionId = b.PositionId, PositionName = b.PositionName, }).ToList(), Documents = x.PeriodExamDocuments.OrderBy(o => o.CreatedAt).Select(b => new FileListResponse { Id = b.Document == null ? "" : b.Document.Id.ToString(), FileName = b.Document == null ? "" : b.Document.FileName, FileSize = b.Document == null ? 0 : b.Document.FileSize, FileType = b.Document == null ? "" : b.Document.FileType, Detail = b.Document == null ? "" : b.Document.Id.ToString(), }).ToList(), Images = x.PeriodExamImages.OrderBy(o => o.CreatedAt).Select(b => new FileListResponse { Id = b.Document == null ? "" : b.Document.Id.ToString(), FileName = b.Document == null ? "" : b.Document.FileName, FileSize = b.Document == null ? 0 : b.Document.FileSize, FileType = b.Document == null ? "" : b.Document.FileType, Detail = b.Document == null ? "" : b.Document.Id.ToString(), }).ToList(), }) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); var i = 0; foreach (var item in periodExam.Images) { if (periodExam.Images[i].Detail != null && periodExam.Images[i].Detail != "") periodExam.Images[i].Detail = _minioService.ImagesPath(Guid.Parse(periodExam.Images[i].Detail)).Result; i++; } i = 0; foreach (var item in periodExam.Documents) { if (periodExam.Documents[i].Detail != null && periodExam.Documents[i].Detail != "") periodExam.Documents[i].Detail = _minioService.ImagesPath(Guid.Parse(periodExam.Documents[i].Detail)).Result; i++; } return periodExam; } public async Task GetsStatusPaymentAsync(string examId, bool showAll = true) { var periodExam = await _context.PeriodExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); return new RequestStatusExam { Name = periodExam.Name, Round = periodExam.Round, Year = periodExam.Year, Status = DateTime.Now > periodExam.PaymentEndDate, SetSeat = periodExam.SetSeat, }; } public async Task GetsNamePositionAsync(string examId, string positionId) { var periodExam = await _context.PeriodExams.AsQueryable() .Where(x => x.CheckDisability == false) .Include(x => x.PositionExam) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); if (positionId != "00000000-0000-0000-0000-000000000000") { return new RequestPositionName { Name = periodExam.Name, Round = periodExam.Round, Year = periodExam.Year, Posiiton = periodExam.PositionExam.FirstOrDefault(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == periodExam), }; } else { return new RequestPositionName { Name = periodExam.Name, Round = periodExam.Round, Year = periodExam.Year, Posiiton = null, }; } } public async Task CreateAsync(RequestPeriodExam inserted) { var periodExam = new PeriodExam { Name = inserted.Name, CheckDocument = inserted.CheckDocument, Round = inserted.Round, Year = inserted.Year, Fee = inserted.Fee, RegisterStartDate = inserted.RegisterStartDate, RegisterEndDate = inserted.RegisterEndDate, PaymentStartDate = inserted.PaymentStartDate, PaymentEndDate = inserted.PaymentEndDate, AnnouncementStartDate = inserted.AnnouncementStartDate, AnnouncementDate = inserted.AnnouncementDate, ExamDate = inserted.ExamDate, AnnouncementEndDate = inserted.AnnouncementEndDate, OrganizationCodeId = inserted.OrganizationCodeId, OrganizationCodeName = inserted.OrganizationCodeName, OrganizationId = inserted.OrganizationId, OrganizationName = inserted.OrganizationName, PaymentKrungThai = inserted.PaymentKrungThai, AnnouncementExam = inserted.AnnouncementExam, Category = inserted.Category, Detail = inserted.Detail, Note = inserted.Note, CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, LastUpdateUserId = UserId ?? "", CreatedFullName = FullName ?? "", LastUpdateFullName = FullName ?? "", }; if (inserted.PaymentKrungThai == "payment2") { foreach (var bank in inserted.BankExam) { var bankExam = new BankExam { PeriodExam = periodExam, AccountName = bank.AccountName, AccountNumber = bank.AccountNumber, BankName = bank.BankName, CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, LastUpdateUserId = UserId ?? "", CreatedFullName = FullName ?? "", LastUpdateFullName = FullName ?? "", }; await _context.BankExams.AddAsync(bankExam); } } foreach (var position in inserted.PositionExam) { var positionExam = new PositionExam { PeriodExam = periodExam, PositionId = position.PositionId, PositionName = position.PositionName, TypeId = position.TypeId, TypeName = position.TypeName, CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, LastUpdateUserId = UserId ?? "", CreatedFullName = FullName ?? "", LastUpdateFullName = FullName ?? "", }; await _context.PositionExams.AddAsync(positionExam); } await _context.PeriodExams.AddAsync(periodExam); await _context.SaveChangesAsync(); return periodExam.Id; } public async Task UpdateAsync(string examId, RequestPeriodExam updated) { var periodExam = await _context.PeriodExams.AsQueryable() .Where(x => x.CheckDisability == false) .Include(x => x.BankExam) .Include(x => x.PositionExam) .Include(x => x.Candidate) .Include(x => x.PeriodExamDocuments) .ThenInclude(x => x.Document) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); periodExam.Name = updated.Name; periodExam.CheckDocument = updated.CheckDocument; periodExam.Round = updated.Round; periodExam.Year = updated.Year; periodExam.Fee = updated.Fee; periodExam.RegisterStartDate = updated.RegisterStartDate; periodExam.RegisterEndDate = updated.RegisterEndDate; periodExam.PaymentStartDate = updated.PaymentStartDate; periodExam.PaymentEndDate = updated.PaymentEndDate; periodExam.AnnouncementStartDate = updated.AnnouncementStartDate; periodExam.AnnouncementDate = updated.AnnouncementDate; periodExam.ExamDate = updated.ExamDate; periodExam.AnnouncementEndDate = updated.AnnouncementEndDate; periodExam.OrganizationCodeId = updated.OrganizationCodeId; periodExam.OrganizationCodeName = updated.OrganizationCodeName; periodExam.OrganizationId = updated.OrganizationId; periodExam.OrganizationName = updated.OrganizationName; periodExam.PaymentKrungThai = updated.PaymentKrungThai; // periodExam.AnnouncementExam = updated.AnnouncementExam; periodExam.Category = updated.Category; periodExam.Detail = updated.Detail; periodExam.Note = updated.Note; periodExam.LastUpdatedAt = DateTime.Now; periodExam.LastUpdateUserId = UserId ?? ""; periodExam.LastUpdateFullName = FullName ?? ""; if (updated.PaymentKrungThai == "payment2") { foreach (var bank in periodExam.BankExam) { var bankData = updated.BankExam .FirstOrDefault(x => x.Id == bank.Id); if (bankData != null) { bank.AccountName = bankData.AccountName; bank.AccountNumber = bankData.AccountNumber; bank.BankName = bankData.BankName; bank.LastUpdatedAt = DateTime.Now; bank.LastUpdateUserId = UserId ?? ""; bank.LastUpdateFullName = FullName ?? ""; } else { var bankDelete = await _context.BankExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == bank.Id); if (bankDelete != null) _context.BankExams.Remove(bankDelete); } } var bankAdd = updated.BankExam .Where(x => x.Id == Guid.Parse("00000000-0000-0000-0000-000000000000")) .ToList(); foreach (var bank in bankAdd) { var bankExam = new BankExam { PeriodExam = periodExam, AccountName = bank.AccountName, AccountNumber = bank.AccountNumber, BankName = bank.BankName, CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, LastUpdateUserId = UserId ?? "", CreatedFullName = FullName ?? "", LastUpdateFullName = FullName ?? "", }; await _context.BankExams.AddAsync(bankExam); } } else { foreach (var bank in periodExam.BankExam) { var bankDelete = await _context.BankExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == bank.Id); if (bankDelete != null) _context.BankExams.Remove(bankDelete); } } foreach (var position in periodExam.PositionExam) { var positionData = updated.PositionExam .FirstOrDefault(x => x.Id == position.Id); if (positionData != null) { position.PositionId = positionData.PositionId; position.PositionName = positionData.PositionName; position.TypeId = positionData.TypeId; position.TypeName = positionData.TypeName; position.LastUpdatedAt = DateTime.Now; position.LastUpdateUserId = UserId ?? ""; position.LastUpdateFullName = FullName ?? ""; } else { var positionDelete = await _context.PositionExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == position.Id); if (positionDelete != null) { if (periodExam.Candidate.Count() > 0) throw new Exception("รอบนี้มีการสมัครแล้วไม่สามารถลบตำแหน่งได้"); _context.PositionExams.Remove(positionDelete); } } } var positionAdd = updated.PositionExam .Where(x => x.Id == Guid.Parse("00000000-0000-0000-0000-000000000000")) .ToList(); foreach (var position in positionAdd) { var positionExam = new PositionExam { PeriodExam = periodExam, PositionId = position.PositionId, PositionName = position.PositionName, TypeId = position.TypeId, TypeName = position.TypeName, CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, LastUpdateUserId = UserId ?? "", CreatedFullName = FullName ?? "", LastUpdateFullName = FullName ?? "", }; await _context.PositionExams.AddAsync(positionExam); } await _context.SaveChangesAsync(); } public async Task UpdateDocAsync(string examId, IFormFileCollection files) { var periodExam = await _context.PeriodExams.AsQueryable() // .Where(x => x.CheckDisability == false) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); foreach (var file in files) { var doc = await _minioService.UploadFileAsync(file); var document = await _context.Documents.AsQueryable() .FirstOrDefaultAsync(x => x.Id == doc.Id); if (document == null) throw new Exception(GlobalMessages.NoFileToUpload); var periodExamDocument = new PeriodExamDocument { PeriodExam = periodExam, Document = document, }; await _context.PeriodExamDocuments.AddAsync(periodExamDocument); } await _context.SaveChangesAsync(); } public async Task UpdateImgAsync(string examId, IFormFileCollection files) { var periodExam = await _context.PeriodExams.AsQueryable() // .Where(x => x.CheckDisability == false) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); foreach (var file in files) { var doc = await _minioService.UploadFileAsync(file); var document = await _context.Documents.AsQueryable() .FirstOrDefaultAsync(x => x.Id == doc.Id); if (document == null) throw new Exception(GlobalMessages.NoFileToUpload); var periodExamImage = new PeriodExamImage { PeriodExam = periodExam, Document = document, }; await _context.PeriodExamImages.AddAsync(periodExamImage); } await _context.SaveChangesAsync(); } public async Task DeleteDocument(string documentId) { await _minioService.DeleteFileAsync(Guid.Parse(documentId)); } public async Task DeleteAsyncDocument(string examId, string documentId) { var exam = await _context.PeriodExams.AsQueryable() // .Where(x => x.CheckDisability == false) .Include(x => x.PositionExam) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (exam == null) throw new Exception(GlobalMessages.ExamNotFound); await _minioService.DeleteFileAsync(Guid.Parse(documentId)); } public async Task DeleteAsync(string examId) { var periodExam = await _context.PeriodExams.AsQueryable() .Where(x => x.CheckDisability == false) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); _context.PeriodExams.Remove(periodExam); await _context.SaveChangesAsync(); } public async Task> GetsCandidateByStatusAsync(string examId, string status) { var periodExam = await _context.PeriodExams.AsQueryable() .Where(x => x.CheckDisability == false) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); if (status == "all") { var candidate = await _context.Candidates.AsQueryable() .Include(x => x.ProfileImg) .OrderByDescending(d => d.CreatedAt) .Where(x => x.PeriodExam == periodExam && x.Status != "register" && x.Status != "rejectRegister") .ToListAsync(); if (candidate.Where(x => x.Status == "done").FirstOrDefault() != null) candidate = candidate.OrderBy(x => x.Number).ToList(); var i = 0; foreach (var item in candidate) { if (candidate[i].ProfileImg != null) candidate[i].ProfileImg.Detail = _minioService.ImagesPath(candidate[i].ProfileImg.Id).Result; i++; } return candidate; } else { var candidate = await _context.Candidates.AsQueryable() .Include(x => x.ProfileImg) .OrderByDescending(d => d.CreatedAt) .Where(x => x.PeriodExam == periodExam && x.Status == status) .ToListAsync(); if (candidate.Where(x => x.Status == "done").FirstOrDefault() != null) candidate = candidate.OrderBy(x => x.Number).ToList(); var i = 0; foreach (var item in candidate) { if (candidate[i].ProfileImg != null) candidate[i].ProfileImg.Detail = _minioService.ImagesPath(candidate[i].ProfileImg.Id).Result; i++; } return candidate; } } public async Task GetsAsyncInformation(string candidateId) { var candidate = await _context.Candidates.AsQueryable() .Where(x => x.Id == Guid.Parse(candidateId)) .Select(x => new CandidateInformationResponseItem { Prefix = x.PrefixName, PrefixId = x.PrefixId != null ? x.PrefixId.ToString() : null, FirstName = x.FirstName, LastName = x.LastName, Nationality = x.Nationality, DateOfBirth = x.DateOfBirth, Relationship = x.RelationshipName, RelationshipId = x.RelationshipId != null ? x.RelationshipId.ToString() : null, CitizenProvince = x.CitizenProvinceName, CitizenProvinceId = x.CitizenProvinceId != null ? x.CitizenProvinceId.ToString() : null, CitizenDistrict = x.CitizenDistrictName, CitizenDistrictId = x.CitizenDistrictId != null ? x.CitizenDistrictId.ToString() : null, CitizenDate = x.CitizenDate, Email = x.Email, CitizenId = x.CitizenId, Telephone = x.Telephone, MobilePhone = x.MobilePhone, Knowledge = x.Knowledge, ProfileImg = x.ProfileImg == null ? "" : x.ProfileImg.Id.ToString(), }) .FirstOrDefaultAsync(); if (candidate == null) return candidate; if (candidate.ProfileImg != null && candidate.ProfileImg != "") candidate.ProfileImg = _minioService.ImagesPath(Guid.Parse(candidate.ProfileImg)).Result; return candidate; } public async Task GetsAsyncProfileImage(string candidateId) { var candidate = await _context.Candidates.AsQueryable() .Include(x => x.ProfileImg) .Where(x => x.Id == Guid.Parse(candidateId)) .FirstOrDefaultAsync(); if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); if (candidate.ProfileImg != null) candidate.ProfileImg.Detail = _minioService.ImagesPath(candidate.ProfileImg.Id).Result; return candidate.ProfileImg == null ? "" : candidate.ProfileImg.Detail; } public async Task GetsAsyncOccupation(string candidateId) { return await _context.Candidates.AsQueryable() .Where(x => x.Id == Guid.Parse(candidateId)) .Select(x => new CandidateOccupationResponseItem { OccupationType = x.OccupationType, OccupationCompany = x.OccupationCompany, OccupationDepartment = x.OccupationDepartment, OccupationEmail = x.OccupationEmail, OccupationTelephone = x.OccupationTelephone, OccupationPosition = x.OccupationPosition, }) .FirstOrDefaultAsync(); } public async Task GetsAsyncAddress(string candidateId) { return await _context.Candidates.AsQueryable() .Where(x => x.Id == Guid.Parse(candidateId)) .Select(x => new CandidateAddressResponseItem { RegistAddress = x.RegistAddress, RegistProvince = x.RegistProvinceName, RegistProvinceId = x.RegistProvinceId != null ? x.RegistProvinceId.ToString() : null, RegistDistrict = x.RegistDistrictName, RegistDistrictId = x.RegistDistrictId != null ? x.RegistDistrictId.ToString() : null, RegistSubDistrict = x.RegistSubDistrictName, RegistSubDistrictId = x.RegistSubDistrictId != null ? x.RegistSubDistrictId.ToString() : null, RegistZipCode = x.RegistZipCode, RegistSame = x.RegistSame, CurrentAddress = x.CurrentAddress, CurrentProvince = x.CurrentProvinceName, CurrentProvinceId = x.CurrentProvinceId != null ? x.CurrentProvinceId.ToString() : null, CurrentDistrict = x.CurrentDistrictName, CurrentDistrictId = x.CurrentDistrictId != null ? x.CurrentDistrictId.ToString() : null, CurrentSubDistrict = x.CurrentSubDistrictName, CurrentSubDistrictId = x.CurrentSubDistrictId != null ? x.CurrentSubDistrictId.ToString() : null, CurrentZipCode = x.CurrentZipCode, }) .FirstOrDefaultAsync(); } public async Task GetsAsyncFamily(string candidateId) { return await _context.Candidates.AsQueryable() .Where(x => x.Id == Guid.Parse(candidateId)) .Select(x => new CandidateFamilyResponseItem { Marry = x.Marry, MarryPrefix = x.MarryPrefixName, MarryPrefixId = x.MarryPrefixId != null ? x.MarryPrefixId.ToString() : null, MarryFirstName = x.MarryFirstName, MarryLastName = x.MarryLastName, MarryOccupation = x.MarryOccupation, MarryNationality = x.MarryNationality, FatherPrefix = x.FatherPrefixName, FatherPrefixId = x.FatherPrefixId != null ? x.FatherPrefixId.ToString() : null, FatherFirstName = x.FatherFirstName, FatherLastName = x.FatherLastName, FatherOccupation = x.FatherOccupation, FatherNationality = x.FatherNationality, MotherPrefix = x.MotherPrefixName, MotherPrefixId = x.MotherPrefixId != null ? x.MotherPrefixId.ToString() : null, MotherFirstName = x.MotherFirstName, MotherLastName = x.MotherLastName, MotherOccupation = x.MotherOccupation, MotherNationality = x.MotherNationality, }) .FirstOrDefaultAsync(); } public async Task> GetsAsyncEducation(string candidateId) { return await _context.Educations.AsQueryable() .Where(x => x.Candidate.Id == Guid.Parse(candidateId)) .OrderBy(d => d.DurationStart) .ToListAsync(); } public async Task> GetsAsyncCareer(string candidateId) { return await _context.Careers.AsQueryable() .Where(x => x.Candidate.Id == Guid.Parse(candidateId)) .OrderBy(d => d.DurationStart) .ToListAsync(); } public async Task> GetsAsyncDocument(string candidateId) { var candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); var document = await _context.CandidateDocuments.AsQueryable() .Where(x => x.Candidate == candidate) .OrderBy(d => d.CreatedAt) .Select(x => new FileListResponse { Id = x.Document == null ? "" : x.Document.Id.ToString(), FileName = x.Document == null ? "" : x.Document.FileName, FileType = x.Document == null ? "" : x.Document.FileType, FileSize = x.Document == null ? 0 : x.Document.FileSize, Detail = x.Document == null ? "" : x.Document.Id.ToString(), }) .ToListAsync(); var i = 0; foreach (var item in document) { if (document[i].Detail != null && document[i].Detail != "") document[i].Detail = _minioService.ImagesPath(Guid.Parse(document[i].Detail)).Result; i++; } return document; } private async Task> ReadExcelCandidate(IFormFile formFile) { var list = new List(); using (var stream = new MemoryStream()) { await formFile.CopyToAsync(stream); using (var package = new ExcelPackage(stream)) { for (int i = 0; i < package.Workbook.Worksheets.Count(); i++) { ExcelWorksheet worksheet = package.Workbook.Worksheets[i]; var rowCount = worksheet.Dimension.Rows; for (int row = 2; row <= rowCount; row++) { System.Diagnostics.Debug.WriteLine(worksheet); if (worksheet.Cells[row, 1].Value != null) { list.Add(new RequestImportSeat { Number = worksheet.Cells[row, 1].Value != null ? worksheet.Cells[row, 1].Value.ToString() : null, CitizenId = worksheet.Cells[row, 2].Value != null ? worksheet.Cells[row, 2].Value.ToString() : null, ExamIdenNumber = worksheet.Cells[row, 3].Value != null ? worksheet.Cells[row, 3].Value.ToString() : null, SeatNumber = worksheet.Cells[row, 4].Value != null ? worksheet.Cells[row, 4].Value.ToString() : null, PointTotalB = worksheet.Cells[row, 5].Value != null ? worksheet.Cells[row, 5].Value.ToString() : null, PointB = worksheet.Cells[row, 6].Value != null ? worksheet.Cells[row, 6].Value.ToString() : null, ResultB = worksheet.Cells[row, 7].Value != null ? worksheet.Cells[row, 7].Value.ToString() : null, PointTotalC = worksheet.Cells[row, 8].Value != null ? worksheet.Cells[row, 8].Value.ToString() : null, PointC = worksheet.Cells[row, 9].Value != null ? worksheet.Cells[row, 9].Value.ToString() : null, ResultC = worksheet.Cells[row, 10].Value != null ? worksheet.Cells[row, 10].Value.ToString() : null, Pass = worksheet.Cells[row, 11].Value != null ? (worksheet.Cells[row, 11].Value.ToString()) : null, }); } } } } } return list; } public async Task UploadSeatCandidateAsync(string examId, IFormFile excels) { var periodExam = await _context.PeriodExams.AsQueryable() .Where(x => x.CheckDisability == false) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); var candidates = await _context.Candidates .AsQueryable() .Where(x => x.PeriodExam == periodExam) .ToListAsync(); var items = await ReadExcelCandidate(excels); foreach (var candidate in candidates) { var item = items.FirstOrDefault(x => x.CitizenId == candidate.CitizenId && x.ExamIdenNumber.Trim().ToUpper() == candidate.ExamIdenNumber.Trim().ToUpper()); if (item != null) { if (candidate.Status == "checkSeat") { candidate.SeatNumber = item.SeatNumber; var subject = "แจ้งผลการสมัครสอบคัดเลือก " + periodExam.Name; var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: รอเจ้าหน้าที่สรุปคะแนนสอบ"; if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); candidate.Status = "checkPoint"; } else { if (candidate.Status != "waiver") { var subject = "แจ้งผลการสมัครสอบคัดเลือก " + periodExam.Name; var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: สละสิทธิ์สอบ"; if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); } candidate.Status = "waiver"; } } else { if (candidate.Status != "waiver") { var subject = "แจ้งผลการสมัครสอบคัดเลือก " + periodExam.Name; var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: สละสิทธิ์สอบ"; if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); } candidate.Status = "waiver"; } periodExam.SetSeat = true; } await _context.SaveChangesAsync(); } public async Task UploadPointCandidateAsync(string examId, IFormFile excels) { var periodExam = await _context.PeriodExams.AsQueryable() .Where(x => x.CheckDisability == false) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); var candidates = await _context.Candidates .AsQueryable() .Where(x => x.PeriodExam == periodExam) .ToListAsync(); var items = await ReadExcelCandidate(excels); foreach (var candidate in candidates) { var item = items.FirstOrDefault(x => x.CitizenId == candidate.CitizenId && x.ExamIdenNumber == candidate.ExamIdenNumber); if (item != null) { if (candidate.Status == "checkPoint" || candidate.Status == "done") { candidate.PointTotalB = item.PointTotalB; candidate.PointB = item.PointB; candidate.ResultB = item.ResultB; candidate.PointTotalC = item.PointTotalC; candidate.PointC = item.PointC; candidate.ResultC = item.ResultC; candidate.Pass = item.Pass; candidate.Number = item.Number; var subject = "แจ้งผลการสมัครสอบคัดเลือก " + periodExam.Name; var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: สอบคัดเลือกสำเร็จ"; if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); candidate.Status = "done"; } else { if (candidate.Status != "waiver") { var subject = "แจ้งผลการสมัครสอบคัดเลือก " + periodExam.Name; var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: สละสิทธิ์สอบ"; if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); } candidate.Status = "waiver"; } } else { if (candidate.Status != "waiver") { var subject = "แจ้งผลการสมัครสอบคัดเลือก " + periodExam.Name; var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: สละสิทธิ์สอบ"; if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); } candidate.Status = "waiver"; } periodExam.SetSeat = true; } await _context.SaveChangesAsync(); } public async Task DownloadCandidateAsync(string examId) { var periodExam = await _context.PeriodExams.AsQueryable() .Where(x => x.CheckDisability == false) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); var candidates = new List(); if (periodExam.SetSeat == true) { candidates = await _context.Candidates .AsQueryable() .OrderBy(x => x.ExamIdenNumber) .Where(x => x.PeriodExam == periodExam) .Where(x => x.Status != "waiver") .ToListAsync(); } else { candidates = await _context.Candidates .AsQueryable() .OrderBy(x => x.ExamIdenNumber) .Where(x => x.PeriodExam == periodExam) .ToListAsync(); } var stream = new MemoryStream(); using (var package = new ExcelPackage(stream)) { var summarySheet = package.Workbook.Worksheets.Add("Candidate"); summarySheet.TabColor = System.Drawing.Color.Black; // summarySheet.DefaultRowHeight = 17; summarySheet.Row(1).Style.Font.Bold = true; summarySheet.Cells[1, 1].Value = "ลำดับที่สอบได้"; summarySheet.Cells[1, 2].Value = "เลขบัตรประชาชน"; summarySheet.Cells[1, 3].Value = "เลขประจำตัวสอบ"; summarySheet.Cells[1, 4].Value = "เลขที่นั่งสอบ"; summarySheet.Cells[1, 5].Value = "คะแนนเต็มภาค ข"; summarySheet.Cells[1, 6].Value = "คะแนนภาค ข"; summarySheet.Cells[1, 7].Value = "ผลสอบภาค ข"; summarySheet.Cells[1, 8].Value = "คะแนนเต็มภาค ค"; summarySheet.Cells[1, 9].Value = "คะแนนภาค ค"; summarySheet.Cells[1, 10].Value = "ผลสอบภาค ค"; summarySheet.Cells[1, 11].Value = "ผลการสอบ"; int row = 2; foreach (var item in candidates) { summarySheet.Cells[row, 1].Value = item.Number; summarySheet.Cells[row, 2].Value = item.CitizenId; summarySheet.Cells[row, 3].Value = item.ExamIdenNumber; summarySheet.Cells[row, 4].Value = item.SeatNumber; summarySheet.Cells[row, 5].Value = item.PointTotalB; summarySheet.Cells[row, 6].Value = item.PointB; summarySheet.Cells[row, 7].Value = item.ResultB; summarySheet.Cells[row, 8].Value = item.PointTotalC; summarySheet.Cells[row, 9].Value = item.PointC; summarySheet.Cells[row, 10].Value = item.ResultC; summarySheet.Cells[row, 11].Value = item.Pass; row++; } summarySheet.Cells[summarySheet.Dimension.Address].AutoFitColumns(); package.Save(); } stream.Position = 0; return stream; } public async Task GetsPaymentExamAsync(string examId) { var periodExam = await _context.PeriodExams.AsQueryable() .Where(x => x.CheckDisability == false) .Include(x => x.BankExam) .Where(x => x.Id == Guid.Parse(examId)) .Select(x => new PeriodExam { BankExam = x.BankExam, Fee = x.Fee, }) .FirstOrDefaultAsync(); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); return periodExam; } public async Task> GetsDashboardPaymentExamAsync(string examId) { var periodExam = await _context.PeriodExams.AsQueryable() .Where(x => x.CheckDisability == false) .Include(x => x.Candidate) .ThenInclude(x => x.PaymentImg) .Where(x => x.Id == Guid.Parse(examId)) .FirstOrDefaultAsync(); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); var dashboard = new List { new DashboardResponseItem { Id = 1, Name = "จำนวนผู้สมัครคัดเลือกทั้งหมด", Count = periodExam.Candidate.Count() }, new DashboardResponseItem { Id = 2, Name = "จำนวนผู้มีสิทธิ์เข้ารับคัดเลือกทั้งหมด", Count = periodExam.Candidate.Where(x=>x.ExamIdenNumber != null).Count() }, new DashboardResponseItem { Id = 3, Name = "จำนวนผู้เข้ารับการคัดเลือกทั้งหมด", Count = periodExam.Candidate.Where(x=>x.PaymentImg!=null).Count() }, new DashboardResponseItem { Id = 4, Name = "ผ่านการสอบ", Count = periodExam.Candidate.Where(x=>x.Pass=="ผ่าน").Count() }, new DashboardResponseItem { Id = 5, Name = "ไม่ผ่านการสอบ", Count = periodExam.Candidate.Where(x=>x.Pass!=null && x.Pass!="ผ่าน").Count() }, new DashboardResponseItem { Id = 6, Name = "เพศชาย", Count = periodExam.Candidate.Where(x=>x.PrefixName != null && x.PrefixName=="นาย").Count() }, new DashboardResponseItem { Id = 7, Name = "เพศหญิง", Count = periodExam.Candidate.Where(x=>x.PrefixName != null && (x.PrefixName=="นาง" || x.PrefixName=="นางสาว")).Count() }, // new DashboardResponseItem // { // Id = 8, // Name = "เพศอื่นๆ", // Count = periodExam.Candidate.Count() -periodExam.Candidate.Where(x=>x.PrefixName != null && x.PrefixName.Contains("ชาย")).Count()-periodExam.Candidate.Where(x=>x.PrefixName != null && x.PrefixName.Contains("หญิง")).Count() // }, }; return dashboard; } public async Task DownloadCandidateAllAsync(string examId) { var periodExam = await _context.PeriodExams.AsQueryable() .Where(x => x.CheckDisability == false) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); var candidates = await _context.Candidates .AsQueryable() .OrderBy(x => x.ExamIdenNumber) .Where(x => x.PeriodExam == periodExam) .Select(c => new { Id = c.Id, Status = c.Status, CitizenId = c.CitizenId, FullName = $"{c.PrefixName}{c.FirstName} {c.LastName}", Nationality = c.Nationality, DateOfBirth = c.DateOfBirth == null ? "" : c.DateOfBirth.Value.Date.ToThaiShortDate(), Age = c.DateOfBirth == null ? "" : c.DateOfBirth.Value.Date.CalculateGovAgeStr(0, 0), RelationshipName = c.RelationshipName, Telephone = c.Telephone, MobilePhone = c.MobilePhone, Email = c.Email, Knowledge = c.Knowledge, RegistAddress = c.RegistAddress, RegistProvinceName = c.RegistProvinceName, RegistDistrictName = c.RegistDistrictName, RegistSubDistrictName = c.RegistSubDistrictName, RegistZipCode = c.RegistZipCode, CurrentAddress = c.CurrentAddress, CurrentProvinceName = c.CurrentProvinceName, CurrentDistrictName = c.CurrentDistrictName, CurrentSubDistrictName = c.CurrentSubDistrictName, CurrentZipCode = c.CurrentZipCode, MarryFullName = $"{c.MarryPrefixName}{c.MarryFirstName} {c.MarryLastName}", FatherFullName = $"{c.FatherPrefixName}{c.FatherFirstName} {c.FatherLastName}", MotherFullName = $"{c.MotherPrefixName}{c.MotherFirstName} {c.MotherLastName}", OccupationType = c.OccupationType, OccupationPosition = c.OccupationPosition, OccupationCompany = c.OccupationCompany, OccupationDepartment = c.OccupationDepartment, OccupationEmail = c.OccupationEmail, OccupationTelephone = c.OccupationTelephone, Number = c.Number, ExamIdenNumber = c.ExamIdenNumber, SeatNumber = c.SeatNumber, PointTotalB = c.PointTotalB, PointB = c.PointB, ResultB = c.ResultB, PointTotalC = c.PointTotalC, PointC = c.PointC, ResultC = c.ResultC, Pass = c.Pass, CreatedAt = c.CreatedAt, }) .ToListAsync(); var stream = new MemoryStream(); using (var package = new ExcelPackage(stream)) { var summarySheet = package.Workbook.Worksheets.Add("Candidate"); summarySheet.TabColor = System.Drawing.Color.Black; // summarySheet.DefaultRowHeight = 17; summarySheet.Row(1).Style.Font.Bold = true; summarySheet.Cells[1, 1].Value = "สถานะสอบคัดเลือก"; summarySheet.Cells[1, 2].Value = "เลขบัตรประชาชน"; summarySheet.Cells[1, 3].Value = "ชื่อ-สกุล"; summarySheet.Cells[1, 4].Value = "สัญชาติ"; summarySheet.Cells[1, 5].Value = "วันเกิด"; summarySheet.Cells[1, 6].Value = "อายุ"; summarySheet.Cells[1, 7].Value = "สถานภาพ"; summarySheet.Cells[1, 8].Value = "โทรศัพท์"; summarySheet.Cells[1, 9].Value = "โทรศัพท์มือถือ"; summarySheet.Cells[1, 10].Value = "อีเมล"; summarySheet.Cells[1, 11].Value = "ความรู้ความสามารถพิเศษ"; summarySheet.Cells[1, 12].Value = "ที่อยู่ตามทะเบียนบ้าน"; summarySheet.Cells[1, 13].Value = "จังหวัดที่อยู่ตามทะเบียนบ้าน"; summarySheet.Cells[1, 14].Value = "เขต/อำเภอที่อยู่ตามทะเบียนบ้าน"; summarySheet.Cells[1, 15].Value = "ตำบล/แขวงที่อยู่ตามทะเบียนบ้าน"; summarySheet.Cells[1, 16].Value = "รหัสไปรษณีย์ที่อยู่ตามทะเบียนบ้าน"; summarySheet.Cells[1, 17].Value = "ที่อยู่ปัจจุบัน"; summarySheet.Cells[1, 18].Value = "จังหวัดที่อยู่ปัจจุบัน"; summarySheet.Cells[1, 19].Value = "เขต/อำเภอที่อยู่ปัจจุบัน"; summarySheet.Cells[1, 20].Value = "ตำบล/แขวงที่อยู่ปัจจุบัน"; summarySheet.Cells[1, 21].Value = "รหัสไปรษณีย์ที่อยู่ปัจจุบัน"; summarySheet.Cells[1, 22].Value = "คู่สมรส"; summarySheet.Cells[1, 23].Value = "บิดา"; summarySheet.Cells[1, 24].Value = "มารดา"; summarySheet.Cells[1, 25].Value = "อาชีพ"; summarySheet.Cells[1, 26].Value = "ตำแหน่ง"; summarySheet.Cells[1, 27].Value = "สำนัก/บริษัท"; summarySheet.Cells[1, 28].Value = "กอง/ฝ่าย"; summarySheet.Cells[1, 29].Value = "อีเมลบริษัท"; summarySheet.Cells[1, 30].Value = "โทรศัพท์บริษัท"; summarySheet.Cells[1, 31].Value = "วุฒิที่ได้รับ"; summarySheet.Cells[1, 32].Value = "สาขา/วิชาเอก"; summarySheet.Cells[1, 33].Value = "คะแนนเฉลี่ยตลอดหลักสูตร"; summarySheet.Cells[1, 34].Value = "ชื่อสถานศีกษา"; summarySheet.Cells[1, 35].Value = "ระยะเวลาเริ่มศึกษา"; summarySheet.Cells[1, 36].Value = "ระยะเวลาสิ้นสุดศึกษา"; summarySheet.Cells[1, 37].Value = "สถานที่ทำงาน/ฝึกงาน"; summarySheet.Cells[1, 38].Value = "ตำแหน่ง/ลักษณะงาน"; summarySheet.Cells[1, 39].Value = "เงินเดือนสุดท้านก่อนออก"; summarySheet.Cells[1, 40].Value = "ระยะเวลาเริ่มทำงาน/ฝึกงาน"; summarySheet.Cells[1, 41].Value = "ระยะเวลาสิ้นสุดทำงาน/ฝึกงาน"; summarySheet.Cells[1, 42].Value = "เหตุผลที่ออก"; summarySheet.Cells[1, 43].Value = "ลำดับที่สอบได้"; summarySheet.Cells[1, 44].Value = "เลขประจำตัวสอบ"; summarySheet.Cells[1, 45].Value = "เลขที่นั่งสอบ"; summarySheet.Cells[1, 46].Value = "คะแนนเต็มภาค ข"; summarySheet.Cells[1, 47].Value = "คะแนนภาค ข"; summarySheet.Cells[1, 48].Value = "ผลสอบภาค ข"; summarySheet.Cells[1, 49].Value = "คะแนนเต็มภาค ค"; summarySheet.Cells[1, 50].Value = "คะแนนภาค ค"; summarySheet.Cells[1, 51].Value = "ผลสอบภาค ค"; summarySheet.Cells[1, 52].Value = "ผลการสอบ"; summarySheet.Cells[1, 53].Value = "วันที่สมัคร"; int row = 2; foreach (var item in candidates) { var educations = await _context.Educations .AsQueryable() .OrderBy(x => x.DurationStart) .Where(x => x.Candidate.Id == item.Id) .ToListAsync(); foreach (var education in educations) { summarySheet.Cells[row, 1].Value = GenerateStatusCandidate(item.Status); summarySheet.Cells[row, 2].Value = item.CitizenId; summarySheet.Cells[row, 3].Value = item.FullName; summarySheet.Cells[row, 4].Value = item.Nationality; summarySheet.Cells[row, 5].Value = item.DateOfBirth; summarySheet.Cells[row, 6].Value = item.Age; summarySheet.Cells[row, 7].Value = item.RelationshipName; summarySheet.Cells[row, 8].Value = item.Telephone; summarySheet.Cells[row, 9].Value = item.MobilePhone; summarySheet.Cells[row, 10].Value = item.Email; summarySheet.Cells[row, 11].Value = item.Knowledge; summarySheet.Cells[row, 12].Value = item.RegistAddress; summarySheet.Cells[row, 13].Value = item.RegistProvinceName; summarySheet.Cells[row, 14].Value = item.RegistDistrictName; summarySheet.Cells[row, 15].Value = item.RegistSubDistrictName; summarySheet.Cells[row, 16].Value = item.RegistZipCode; summarySheet.Cells[row, 17].Value = item.CurrentAddress; summarySheet.Cells[row, 18].Value = item.CurrentProvinceName; summarySheet.Cells[row, 19].Value = item.CurrentDistrictName; summarySheet.Cells[row, 20].Value = item.CurrentSubDistrictName; summarySheet.Cells[row, 21].Value = item.CurrentZipCode; summarySheet.Cells[row, 22].Value = item.MarryFullName; summarySheet.Cells[row, 23].Value = item.FatherFullName; summarySheet.Cells[row, 24].Value = item.MotherFullName; summarySheet.Cells[row, 25].Value = GenerateStatusOccupation(item.OccupationType); summarySheet.Cells[row, 26].Value = item.OccupationPosition; summarySheet.Cells[row, 27].Value = item.OccupationCompany; summarySheet.Cells[row, 28].Value = item.OccupationDepartment; summarySheet.Cells[row, 29].Value = item.OccupationEmail; summarySheet.Cells[row, 30].Value = item.OccupationTelephone; summarySheet.Cells[row, 31].Value = education.EducationLevelName; summarySheet.Cells[row, 32].Value = education.Major; summarySheet.Cells[row, 33].Value = education.Scores; summarySheet.Cells[row, 34].Value = education.Name; summarySheet.Cells[row, 35].Value = education.DurationStart == null ? "" : education.DurationStart.Date.ToThaiShortDate(); summarySheet.Cells[row, 36].Value = education.DurationEnd == null ? "" : education.DurationEnd.Date.ToThaiShortDate(); summarySheet.Cells[row, 43].Value = item.Number; summarySheet.Cells[row, 44].Value = item.ExamIdenNumber; summarySheet.Cells[row, 45].Value = item.SeatNumber; summarySheet.Cells[row, 46].Value = item.PointTotalB; summarySheet.Cells[row, 47].Value = item.PointB; summarySheet.Cells[row, 48].Value = item.ResultB; summarySheet.Cells[row, 49].Value = item.PointTotalC; summarySheet.Cells[row, 50].Value = item.PointC; summarySheet.Cells[row, 51].Value = item.ResultC; summarySheet.Cells[row, 52].Value = item.Pass; summarySheet.Cells[row, 53].Value = item.CreatedAt.Date.ToThaiShortDate(); row++; } var careers = await _context.Careers .AsQueryable() .OrderBy(x => x.DurationStart) .Where(x => x.Candidate.Id == item.Id) .ToListAsync(); foreach (var career in careers) { summarySheet.Cells[row, 1].Value = GenerateStatusCandidate(item.Status); summarySheet.Cells[row, 2].Value = item.CitizenId; summarySheet.Cells[row, 3].Value = item.FullName; summarySheet.Cells[row, 4].Value = item.Nationality; summarySheet.Cells[row, 5].Value = item.DateOfBirth; summarySheet.Cells[row, 6].Value = item.Age; summarySheet.Cells[row, 7].Value = item.RelationshipName; summarySheet.Cells[row, 8].Value = item.Telephone; summarySheet.Cells[row, 9].Value = item.MobilePhone; summarySheet.Cells[row, 10].Value = item.Email; summarySheet.Cells[row, 11].Value = item.Knowledge; summarySheet.Cells[row, 12].Value = item.RegistAddress; summarySheet.Cells[row, 13].Value = item.RegistProvinceName; summarySheet.Cells[row, 14].Value = item.RegistDistrictName; summarySheet.Cells[row, 15].Value = item.RegistSubDistrictName; summarySheet.Cells[row, 16].Value = item.RegistZipCode; summarySheet.Cells[row, 17].Value = item.CurrentAddress; summarySheet.Cells[row, 18].Value = item.CurrentProvinceName; summarySheet.Cells[row, 19].Value = item.CurrentDistrictName; summarySheet.Cells[row, 20].Value = item.CurrentSubDistrictName; summarySheet.Cells[row, 21].Value = item.CurrentZipCode; summarySheet.Cells[row, 22].Value = item.MarryFullName; summarySheet.Cells[row, 23].Value = item.FatherFullName; summarySheet.Cells[row, 24].Value = item.MotherFullName; summarySheet.Cells[row, 25].Value = GenerateStatusOccupation(item.OccupationType); summarySheet.Cells[row, 26].Value = item.OccupationPosition; summarySheet.Cells[row, 27].Value = item.OccupationCompany; summarySheet.Cells[row, 28].Value = item.OccupationDepartment; summarySheet.Cells[row, 29].Value = item.OccupationEmail; summarySheet.Cells[row, 30].Value = item.OccupationTelephone; summarySheet.Cells[row, 37].Value = career.Name; summarySheet.Cells[row, 38].Value = career.Position; summarySheet.Cells[row, 39].Value = career.Salary; summarySheet.Cells[row, 40].Value = career.DurationStart == null ? "" : career.DurationStart.Date.ToThaiShortDate(); summarySheet.Cells[row, 41].Value = career.DurationEnd == null ? "" : career.DurationEnd.Date.ToThaiShortDate(); summarySheet.Cells[row, 42].Value = career.Reason; summarySheet.Cells[row, 43].Value = item.Number; summarySheet.Cells[row, 44].Value = item.ExamIdenNumber; summarySheet.Cells[row, 45].Value = item.SeatNumber; summarySheet.Cells[row, 46].Value = item.PointTotalB; summarySheet.Cells[row, 47].Value = item.PointB; summarySheet.Cells[row, 48].Value = item.ResultB; summarySheet.Cells[row, 49].Value = item.PointTotalC; summarySheet.Cells[row, 50].Value = item.PointC; summarySheet.Cells[row, 51].Value = item.ResultC; summarySheet.Cells[row, 52].Value = item.Pass; summarySheet.Cells[row, 53].Value = item.CreatedAt.Date.ToThaiShortDate(); row++; } if (educations.Count() == 0 && careers.Count() == 0) { summarySheet.Cells[row, 1].Value = GenerateStatusCandidate(item.Status); summarySheet.Cells[row, 2].Value = item.CitizenId; summarySheet.Cells[row, 3].Value = item.FullName; summarySheet.Cells[row, 4].Value = item.Nationality; summarySheet.Cells[row, 5].Value = item.DateOfBirth; summarySheet.Cells[row, 6].Value = item.Age; summarySheet.Cells[row, 7].Value = item.RelationshipName; summarySheet.Cells[row, 8].Value = item.Telephone; summarySheet.Cells[row, 9].Value = item.MobilePhone; summarySheet.Cells[row, 10].Value = item.Email; summarySheet.Cells[row, 11].Value = item.Knowledge; summarySheet.Cells[row, 12].Value = item.RegistAddress; summarySheet.Cells[row, 13].Value = item.RegistProvinceName; summarySheet.Cells[row, 14].Value = item.RegistDistrictName; summarySheet.Cells[row, 15].Value = item.RegistSubDistrictName; summarySheet.Cells[row, 16].Value = item.RegistZipCode; summarySheet.Cells[row, 17].Value = item.CurrentAddress; summarySheet.Cells[row, 18].Value = item.CurrentProvinceName; summarySheet.Cells[row, 19].Value = item.CurrentDistrictName; summarySheet.Cells[row, 20].Value = item.CurrentSubDistrictName; summarySheet.Cells[row, 21].Value = item.CurrentZipCode; summarySheet.Cells[row, 22].Value = item.MarryFullName; summarySheet.Cells[row, 23].Value = item.FatherFullName; summarySheet.Cells[row, 24].Value = item.MotherFullName; summarySheet.Cells[row, 25].Value = GenerateStatusOccupation(item.OccupationType); summarySheet.Cells[row, 26].Value = item.OccupationPosition; summarySheet.Cells[row, 27].Value = item.OccupationCompany; summarySheet.Cells[row, 28].Value = item.OccupationDepartment; summarySheet.Cells[row, 29].Value = item.OccupationEmail; summarySheet.Cells[row, 30].Value = item.OccupationTelephone; summarySheet.Cells[row, 43].Value = item.Number; summarySheet.Cells[row, 44].Value = item.ExamIdenNumber; summarySheet.Cells[row, 45].Value = item.SeatNumber; summarySheet.Cells[row, 46].Value = item.PointTotalB; summarySheet.Cells[row, 47].Value = item.PointB; summarySheet.Cells[row, 48].Value = item.ResultB; summarySheet.Cells[row, 49].Value = item.PointTotalC; summarySheet.Cells[row, 50].Value = item.PointC; summarySheet.Cells[row, 51].Value = item.ResultC; summarySheet.Cells[row, 52].Value = item.Pass; summarySheet.Cells[row, 53].Value = item.CreatedAt.Date.ToThaiShortDate(); row++; } } summarySheet.Cells[summarySheet.Dimension.Address].AutoFitColumns(); package.Save(); } stream.Position = 0; return stream; } public async Task GetsDashboardExamAsync(RequestCandidateDashboard item, string examId) { var periodExam = await _context.PeriodExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); var candidates = await _context.Candidates .AsQueryable() .OrderBy(x => x.ExamIdenNumber) .Where(x => x.CreatedAt.Date <= item.DateEnd.Date) .Where(x => x.CreatedAt.Date >= item.DateStart.Date) .Where(x => x.PeriodExam == periodExam) .Select(c => new { Id = c.Id, Status = c.Status, CitizenId = c.CitizenId, FullName = $"{c.PrefixName}{c.FirstName} {c.LastName}", Nationality = c.Nationality, DateOfBirth = c.DateOfBirth == null ? "" : c.DateOfBirth.Value.Date.ToThaiShortDate(), Age = c.DateOfBirth == null ? 0 : DateTime.Now.Date.Year - c.DateOfBirth.Value.Date.Year, RelationshipName = c.RelationshipName, Telephone = c.Telephone, MobilePhone = c.MobilePhone, Email = c.Email, Knowledge = c.Knowledge, RegistAddress = c.RegistAddress, RegistProvinceName = c.RegistProvinceName, RegistDistrictName = c.RegistDistrictName, RegistSubDistrictName = c.RegistSubDistrictName, RegistZipCode = c.RegistZipCode, CurrentAddress = c.CurrentAddress, CurrentProvinceName = c.CurrentProvinceName, CurrentDistrictName = c.CurrentDistrictName, CurrentSubDistrictName = c.CurrentSubDistrictName, CurrentZipCode = c.CurrentZipCode, MarryFullName = $"{c.MarryPrefixName}{c.MarryFirstName} {c.MarryLastName}", FatherFullName = $"{c.FatherPrefixName}{c.FatherFirstName} {c.FatherLastName}", MotherFullName = $"{c.MotherPrefixName}{c.MotherFirstName} {c.MotherLastName}", OccupationType = c.OccupationType, OccupationPosition = c.OccupationPosition, OccupationCompany = c.OccupationCompany, OccupationDepartment = c.OccupationDepartment, OccupationEmail = c.OccupationEmail, OccupationTelephone = c.OccupationTelephone, Number = c.Number, ExamIdenNumber = c.ExamIdenNumber, SeatNumber = c.SeatNumber, PointTotalB = c.PointTotalB, PointB = c.PointB, ResultB = c.ResultB, PointTotalC = c.PointTotalC, PointC = c.PointC, ResultC = c.ResultC, Pass = c.Pass, CreatedAt = c.CreatedAt, }) .ToListAsync(); var stream = new MemoryStream(); using (var package = new ExcelPackage(stream)) { var summarySheet = package.Workbook.Worksheets.Add("Dashboard"); summarySheet.TabColor = System.Drawing.Color.Black; // summarySheet.DefaultRowHeight = 17; summarySheet.Column(1).Style.Font.Bold = true; summarySheet.Row(4).Style.Font.Bold = true; summarySheet.Cells[1, 3].Style.Font.Bold = true; int rowName = 2; int rowCount = 2; summarySheet.Cells[1, 1].Value = "ตั้งแต่"; summarySheet.Cells[1, 2].Value = item.DateStart.Date.ToThaiShortDate(); summarySheet.Cells[1, 3].Value = "ถึง"; summarySheet.Cells[1, 4].Value = item.DateEnd.Date.ToThaiShortDate(); summarySheet.Cells[2, 1].Value = "ชื่อรอบการสอบ/ชื่อประกาศ"; summarySheet.Cells[2, 2].Value = periodExam.Name; summarySheet.Cells[3, 1].Value = "ครั้งที่"; summarySheet.Cells[3, 2].Value = $"{periodExam.Round}/{periodExam.Year + 543}"; if (candidates.Count() > 0) { summarySheet.Cells[5, 1].Value = "จำนวน"; // summarySheet.Cells[1, 6].Value = "คะแนนภาค ข"; // summarySheet.Cells[1, 7].Value = "ผลสอบภาค ข"; // summarySheet.Cells[1, 8].Value = "คะแนนเต็มภาค ค"; // summarySheet.Cells[1, 9].Value = "คะแนนภาค ค"; // summarySheet.Cells[1, 10].Value = "ผลสอบภาค ค"; // summarySheet.Cells[1, 11].Value = "ผลการสอบ"; var educations = await _context.Educations .AsQueryable() .Where(x => x.Candidate.PeriodExam == periodExam) .Where(x => x.Candidate.CreatedAt.Date <= item.DateEnd.Date) .Where(x => x.Candidate.CreatedAt.Date >= item.DateStart.Date) .GroupBy(x => x.EducationLevelName) .Select(x => new { Name = x.Key, Count = x.Count(), }) .ToListAsync(); foreach (var education in educations) { summarySheet.Cells[4, rowName].Value = education.Name; summarySheet.Cells[5, rowCount].Value = education.Count; rowName++; rowCount++; } var careers = await _context.Careers .AsQueryable() .Where(x => x.Candidate.PeriodExam == periodExam) .Where(x => x.Candidate.CreatedAt.Date <= item.DateEnd.Date) .Where(x => x.Candidate.CreatedAt.Date >= item.DateStart.Date) .GroupBy(x => x.Name) .Select(x => new { Name = x.Key, Count = x.Count(), }) .ToListAsync(); foreach (var career in careers) { summarySheet.Cells[4, rowName].Value = career.Name; summarySheet.Cells[5, rowCount].Value = career.Count; rowName++; rowCount++; } summarySheet.Cells[4, rowName].Value = "อายุ 1-20 ปี"; summarySheet.Cells[5, rowCount].Value = candidates.Where(x => x.Age >= 1 && x.Age <= 20).Count(); rowName++; rowCount++; summarySheet.Cells[4, rowName].Value = "อายุ 21-45 ปี"; summarySheet.Cells[5, rowCount].Value = candidates.Where(x => x.Age >= 21 && x.Age <= 45).Count(); rowName++; rowCount++; summarySheet.Cells[4, rowName].Value = "อายุ 46-60 ปี"; summarySheet.Cells[5, rowCount].Value = candidates.Where(x => x.Age >= 46 && x.Age <= 60).Count(); rowName++; rowCount++; summarySheet.Cells[4, rowName].Value = "อายุ 61-70 ปี"; summarySheet.Cells[5, rowCount].Value = candidates.Where(x => x.Age >= 61 && x.Age <= 70).Count(); rowName++; rowCount++; summarySheet.Cells[4, rowName].Value = "อายุ 71-80 ปี"; summarySheet.Cells[5, rowCount].Value = candidates.Where(x => x.Age >= 71 && x.Age <= 80).Count(); rowName++; rowCount++; summarySheet.Cells[4, rowName].Value = "อายุ 80 ปีขึ้นไป"; summarySheet.Cells[5, rowCount].Value = candidates.Where(x => x.Age >= 81).Count(); rowName++; rowCount++; } else { summarySheet.Cells[5, 1].Value = "ไม่มีผู้สมัครสอบในช่วงเวลานี้"; } summarySheet.Cells[summarySheet.Dimension.Address].AutoFitColumns(); package.Save(); } stream.Position = 0; return stream; } #endregion } }