Merge pull request #4 from Frappet/issue/#1833
raw query to EF query #1833
This commit is contained in:
commit
0c8b88c304
1 changed files with 77 additions and 93 deletions
|
|
@ -1959,103 +1959,87 @@ namespace BMA.EHR.Recruit.Service.Controllers
|
|||
{
|
||||
try
|
||||
{
|
||||
var data = new List<dynamic>();
|
||||
var p_Id = new MySqlParameter("@id", id);
|
||||
int total = 0;
|
||||
var query = _context.Recruits
|
||||
.Include(x => x.RecruitImport)
|
||||
.Include(x => x.Educations)
|
||||
.Include(x => x.Certificates)
|
||||
.OrderBy(x => x.ExamId)
|
||||
.Where(x => x.RecruitImport != null && x.RecruitImport.Id == id);
|
||||
|
||||
// ---------------------------
|
||||
// 1️. ดึงรายละเอียดสอบ (exam_info)
|
||||
// ---------------------------
|
||||
using (var cmd = _context.Database.GetDbConnection().CreateCommand())
|
||||
var keywordParam = req.keyword?.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(keywordParam))
|
||||
{
|
||||
cmd.CommandTimeout = 0;
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.Append(@"
|
||||
SELECT
|
||||
examID, profileID, prefix, fullName, dateofbirth, gender, degree, major, majorgroup,
|
||||
certificateno, certificateIssueDate, score, result, examAttribute, remark, isspecial,
|
||||
applydate, university, position_name, hddPosition, typeTest, position_level, position_type,
|
||||
exam_name, exam_order, score_year,
|
||||
COUNT(*) OVER() AS total_count
|
||||
FROM exam_info
|
||||
WHERE recruit_import_id = @id
|
||||
");
|
||||
|
||||
cmd.Parameters.Clear();
|
||||
cmd.Parameters.Add(p_Id);
|
||||
|
||||
var keywordParam = req.keyword?.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(keywordParam))
|
||||
{
|
||||
sb.Append(@"
|
||||
AND (
|
||||
examID LIKE @kw
|
||||
OR profileID LIKE @kw
|
||||
OR prefix LIKE @kw
|
||||
OR fullName LIKE @kw
|
||||
OR hddPosition LIKE @kw
|
||||
OR position_name LIKE @kw
|
||||
)
|
||||
");
|
||||
cmd.Parameters.Add(new MySqlParameter("@kw", $"%{keywordParam}%"));
|
||||
}
|
||||
|
||||
// ---------------------------
|
||||
// Paging + Sorting
|
||||
// ---------------------------
|
||||
sb.Append(" ORDER BY examID ");
|
||||
sb.Append(" LIMIT @PageSize OFFSET @Offset ");
|
||||
cmd.Parameters.Add(new MySqlParameter("@PageSize", req.PageSize));
|
||||
cmd.Parameters.Add(new MySqlParameter("@Offset", (req.Page - 1) * req.PageSize));
|
||||
|
||||
cmd.CommandText = sb.ToString();
|
||||
|
||||
_context.Database.OpenConnection();
|
||||
|
||||
// ---------------------------
|
||||
// ดึงข้อมูล + total
|
||||
// ---------------------------
|
||||
using (var reader = cmd.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
if (total == 0)
|
||||
total = Convert.ToInt32(reader["total_count"]);
|
||||
|
||||
data.Add(new
|
||||
{
|
||||
examID = reader["examID"].ToString(),
|
||||
profileID = reader["profileID"].ToString(),
|
||||
prefix = reader["prefix"].ToString(),
|
||||
fullName = reader["fullName"].ToString(),
|
||||
dateOfBirth = reader["dateofbirth"] == DBNull.Value ? "" : Convert.ToDateTime(reader["dateofbirth"]).ToThaiShortDate(),
|
||||
gender = reader["gender"].ToString(),
|
||||
degree = reader["degree"].ToString(),
|
||||
major = reader["major"].ToString(),
|
||||
majorgroup = reader["majorgroup"].ToString(),
|
||||
certificateNo = reader["certificateno"].ToString(),
|
||||
certificateIssueDate = reader["certificateIssueDate"] == DBNull.Value ? "" : Convert.ToDateTime(reader["certificateIssueDate"]).ToThaiShortDate(),
|
||||
ExamScore = reader["score"] == DBNull.Value ? 0 : Convert.ToDecimal(reader["score"]),
|
||||
ExamResult = reader["result"].ToString(),
|
||||
ExamAttribute = reader["examAttribute"].ToString(),
|
||||
Remark = reader["remark"].ToString(),
|
||||
IsSpecial = reader["isspecial"].ToString(),
|
||||
applyDate = reader["applydate"] == DBNull.Value ? "" : Convert.ToDateTime(reader["applydate"]).ToThaiShortDate(),
|
||||
university = reader["university"].ToString(),
|
||||
position_name = reader["position_name"].ToString(),
|
||||
hddPosition = reader["hddPosition"].ToString(),
|
||||
typeTest = reader["typeTest"].ToString(),
|
||||
position_level = reader["position_level"].ToString(),
|
||||
position_type = reader["position_type"].ToString(),
|
||||
exam_name = reader["exam_name"].ToString(),
|
||||
exam_order = reader["exam_order"].ToString(),
|
||||
score_year = Convert.ToInt32(reader["score_year"]).ToThaiYear().ToString()
|
||||
});
|
||||
}
|
||||
}
|
||||
query = query.Where(x =>
|
||||
x.ExamId.Contains(keywordParam) ||
|
||||
x.CitizenId.Contains(keywordParam) ||
|
||||
x.Prefix.Contains(keywordParam) ||
|
||||
x.FirstName.Contains(keywordParam) ||
|
||||
x.LastName.Contains(keywordParam) ||
|
||||
x.HddPosition.Contains(keywordParam) ||
|
||||
x.PositionName.Contains(keywordParam)
|
||||
);
|
||||
}
|
||||
|
||||
int total = await query.CountAsync();
|
||||
|
||||
query = query
|
||||
.Skip((req.Page - 1) * req.PageSize)
|
||||
.Take(req.PageSize);
|
||||
|
||||
var data = await query
|
||||
.GroupJoin(
|
||||
_context.RecruitScores.Include(x => x.ScoreImport),
|
||||
rc => new { rc.RecruitImport!.Id, rc.ExamId },
|
||||
sc => new { Id = sc.ScoreImport!.RecruitImportId, sc.ExamId },
|
||||
(recruit, scores) => new { recruit, scores }
|
||||
)
|
||||
.SelectMany(
|
||||
x => x.scores.DefaultIfEmpty(),
|
||||
(x, sr) => new
|
||||
{
|
||||
examID = x.recruit.ExamId,
|
||||
profileID = x.recruit.CitizenId,
|
||||
prefix = x.recruit.Prefix,
|
||||
fullName = $"{x.recruit.FirstName} {x.recruit.LastName}",
|
||||
dateOfBirth = x.recruit.DateOfBirth != null && x.recruit.DateOfBirth != DateTime.MinValue
|
||||
? x.recruit.DateOfBirth.ToThaiShortDate()
|
||||
: "",
|
||||
gender = x.recruit.Gendor,
|
||||
degree = x.recruit.Educations.Any() ? x.recruit.Educations.First().Degree : "",
|
||||
major = x.recruit.Educations.Any() ? x.recruit.Educations.First().Major : "",
|
||||
certificateNo = x.recruit.Certificates.Any()
|
||||
? x.recruit.Certificates.First().CertificateNo ?? ""
|
||||
: "",
|
||||
certificateIssueDate = x.recruit.Certificates.Any() && x.recruit.Certificates.First().IssueDate != null && x.recruit.Certificates.First().IssueDate != DateTime.MinValue
|
||||
? x.recruit.Certificates.First().IssueDate.ToThaiShortDate()
|
||||
: "",
|
||||
examScore = sr == null ? 0.0 : sr.TotalScore,
|
||||
examResult = sr == null ? "" : sr.ExamStatus,
|
||||
examAttribute = x.recruit.Certificates.Any() && x.recruit.Certificates.First().IssueDate != null
|
||||
? _recruitService.CheckValidCertificate(x.recruit.Certificates.First().IssueDate, 5)
|
||||
? "มีคุณสมบัติ" : "ไม่มีคุณสมบัติ"
|
||||
: "ไม่มีคุณสมบัติ",
|
||||
remark = x.recruit.Remark,
|
||||
isSpecial = x.recruit.Isspecial == "Y" ? x.recruit.Isspecial : "",
|
||||
applyDate = x.recruit.ApplyDate != null && x.recruit.ApplyDate != DateTime.MinValue
|
||||
? x.recruit.ApplyDate.ToThaiShortDate()
|
||||
: "",
|
||||
university = x.recruit.Educations.Any() ? x.recruit.Educations.First().University : "",
|
||||
position_name = x.recruit.PositionName,
|
||||
hddPosition = x.recruit.HddPosition,
|
||||
typeTest = x.recruit.typeTest,
|
||||
position_level = x.recruit.PositionLevel,
|
||||
position_type = x.recruit.PositionType,
|
||||
exam_name = x.recruit.RecruitImport!.Name,
|
||||
exam_order = x.recruit.RecruitImport != null && x.recruit.RecruitImport.Order != null
|
||||
? x.recruit.RecruitImport.Order.ToString()
|
||||
: "",
|
||||
score_year = x.recruit.RecruitImport != null && x.recruit.RecruitImport.Year != null
|
||||
? x.recruit.RecruitImport.Year.ToThaiYear().ToString()
|
||||
: "",
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
// ---------------------------
|
||||
// 3️. ดึงสรุปคะแนน
|
||||
// ---------------------------
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue