แก้ไฟล์ build
This commit is contained in:
parent
9fb82fb45a
commit
d6573a6d0c
15 changed files with 1676 additions and 374 deletions
|
|
@ -27,8 +27,8 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AWSSDK.S3" Version="3.7.103.35" />
|
||||
<PackageReference Include="BMA.EHR.Core" Version="1.0.0" />
|
||||
<PackageReference Include="BMA.EHR.Extensions" Version="1.0.1" />
|
||||
<!-- <PackageReference Include="BMA.EHR.Core" Version="1.0.0" />
|
||||
<PackageReference Include="BMA.EHR.Extensions" Version="1.0.1" /> -->
|
||||
<PackageReference Include="itext7" Version="7.2.5" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.5" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.5" />
|
||||
|
|
|
|||
|
|
@ -1,90 +1,90 @@
|
|||
using BMA.EHR.Core;
|
||||
// using BMA.EHR.Core;
|
||||
using BMA.EHR.Report.Service.Responses;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace BMA.EHR.Profile.Service.Controllers
|
||||
{
|
||||
public class BaseController : ControllerBase
|
||||
{
|
||||
#region " Methods "
|
||||
public class BaseController : ControllerBase
|
||||
{
|
||||
#region " Methods "
|
||||
|
||||
#region " Protected "
|
||||
#region " Protected "
|
||||
|
||||
#region " IActionResult "
|
||||
#region " IActionResult "
|
||||
|
||||
protected virtual ActionResult<ResponseObject> Success(string message, object? result = null)
|
||||
{
|
||||
if (result != null)
|
||||
{
|
||||
return Ok(new ResponseObject
|
||||
{
|
||||
Status = StatusCodes.Status200OK,
|
||||
Message = message,
|
||||
Result = result
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
return Ok(new ResponseObject
|
||||
{
|
||||
Status = StatusCodes.Status200OK,
|
||||
Message = message
|
||||
});
|
||||
}
|
||||
protected virtual ActionResult<ResponseObject> Success(string message, object? result = null)
|
||||
{
|
||||
if (result != null)
|
||||
{
|
||||
return Ok(new ResponseObject
|
||||
{
|
||||
Status = StatusCodes.Status200OK,
|
||||
Message = message,
|
||||
Result = result
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
return Ok(new ResponseObject
|
||||
{
|
||||
Status = StatusCodes.Status200OK,
|
||||
Message = message
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual ActionResult<ResponseObject> Success(object? result = null)
|
||||
{
|
||||
return Success(GlobalMessages.Success, result);
|
||||
}
|
||||
protected virtual ActionResult<ResponseObject> Success(object? result = null)
|
||||
{
|
||||
return Success("สำเร็จ", result);
|
||||
}
|
||||
|
||||
protected virtual ActionResult<ResponseObject> Error(string message, string result, int statusCode = StatusCodes.Status500InternalServerError)
|
||||
{
|
||||
return StatusCode((int)statusCode, new ResponseObject
|
||||
{
|
||||
Status = statusCode,
|
||||
Message = message,
|
||||
Result = result
|
||||
});
|
||||
}
|
||||
protected virtual ActionResult<ResponseObject> Error(string message, string result, int statusCode = StatusCodes.Status500InternalServerError)
|
||||
{
|
||||
return StatusCode((int)statusCode, new ResponseObject
|
||||
{
|
||||
Status = statusCode,
|
||||
Message = message,
|
||||
Result = result
|
||||
});
|
||||
}
|
||||
|
||||
protected virtual ActionResult<ResponseObject> Error(string message, int statusCode = StatusCodes.Status500InternalServerError)
|
||||
{
|
||||
return Error(message, message, statusCode);
|
||||
}
|
||||
protected virtual ActionResult<ResponseObject> Error(string message, int statusCode = StatusCodes.Status500InternalServerError)
|
||||
{
|
||||
return Error(message, message, statusCode);
|
||||
}
|
||||
|
||||
protected virtual ActionResult<ResponseObject> Error(Exception exception, string message, int statusCode = StatusCodes.Status500InternalServerError)
|
||||
{
|
||||
var msg = exception.Message;
|
||||
var inner = exception.InnerException;
|
||||
while (inner != null)
|
||||
{
|
||||
msg += $" {inner.Message}\r\n";
|
||||
inner = inner.InnerException;
|
||||
}
|
||||
protected virtual ActionResult<ResponseObject> Error(Exception exception, string message, int statusCode = StatusCodes.Status500InternalServerError)
|
||||
{
|
||||
var msg = exception.Message;
|
||||
var inner = exception.InnerException;
|
||||
while (inner != null)
|
||||
{
|
||||
msg += $" {inner.Message}\r\n";
|
||||
inner = inner.InnerException;
|
||||
}
|
||||
|
||||
return Error(message, msg, statusCode);
|
||||
}
|
||||
return Error(message, msg, statusCode);
|
||||
}
|
||||
|
||||
protected virtual ActionResult<ResponseObject> Error(Exception exception, int statusCode = StatusCodes.Status500InternalServerError)
|
||||
{
|
||||
var msg = exception.Message;
|
||||
var inner = exception.InnerException;
|
||||
while (inner != null)
|
||||
{
|
||||
msg += $" {inner.Message}\r\n";
|
||||
inner = inner.InnerException;
|
||||
}
|
||||
protected virtual ActionResult<ResponseObject> Error(Exception exception, int statusCode = StatusCodes.Status500InternalServerError)
|
||||
{
|
||||
var msg = exception.Message;
|
||||
var inner = exception.InnerException;
|
||||
while (inner != null)
|
||||
{
|
||||
msg += $" {inner.Message}\r\n";
|
||||
inner = inner.InnerException;
|
||||
}
|
||||
|
||||
return Error(msg, msg, statusCode);
|
||||
}
|
||||
return Error(msg, msg, statusCode);
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ using Swashbuckle.AspNetCore.Annotations;
|
|||
using Telerik.Reporting.Processing;
|
||||
using Telerik.Reporting;
|
||||
using BMA.EHR.Report.Service.Responses;
|
||||
using BMA.EHR.Extensions;
|
||||
using BMA.EHR.Core;
|
||||
using BMA.EHR.Organization.Service.Extensions;
|
||||
// using BMA.EHR.Core;
|
||||
using System.Text;
|
||||
|
||||
namespace BMA.EHR.Report.Service.Controllers
|
||||
|
|
@ -344,7 +344,7 @@ namespace BMA.EHR.Report.Service.Controllers
|
|||
}).ToListAsync();
|
||||
|
||||
if (data.Count == 0)
|
||||
return Error(GlobalMessages.DataNotFound);
|
||||
return Error("ไม่พบข้อมูลในระบบ");
|
||||
|
||||
var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptCandidateList.trdp");
|
||||
ReportPackager reportPackager = new ReportPackager();
|
||||
|
|
@ -427,7 +427,7 @@ namespace BMA.EHR.Report.Service.Controllers
|
|||
.ToListAsync();
|
||||
|
||||
if (data.Count == 0)
|
||||
return Error(GlobalMessages.DataNotFound);
|
||||
return Error("ไม่พบข้อมูลในระบบ");
|
||||
|
||||
var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptPassExamList.trdp");
|
||||
ReportPackager reportPackager = new ReportPackager();
|
||||
|
|
@ -496,7 +496,7 @@ namespace BMA.EHR.Report.Service.Controllers
|
|||
.ToListAsync();
|
||||
|
||||
if (data.Count == 0)
|
||||
return Error(GlobalMessages.DataNotFound);
|
||||
return Error("ไม่พบข้อมูลในระบบ");
|
||||
|
||||
var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptPassExamList.trdp");
|
||||
ReportPackager reportPackager = new ReportPackager();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
using BMA.EHR.Core;
|
||||
using BMA.EHR.Extensions;
|
||||
// using BMA.EHR.Core;
|
||||
using BMA.EHR.Organization.Service.Extensions;
|
||||
using BMA.EHR.Profile.Service.Controllers;
|
||||
using BMA.EHR.Profile.Service.Services;
|
||||
using BMA.EHR.Recruit.Service.Services;
|
||||
|
|
@ -746,7 +746,7 @@ namespace BMA.EHR.Report.Service.Controllers
|
|||
}).ToList();
|
||||
|
||||
if (!profile.Any())
|
||||
return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound);
|
||||
return Error("ไม่พบข้อมูลในระบบ", StatusCodes.Status404NotFound);
|
||||
|
||||
// certificate
|
||||
var cert = (from c in _context.ProfileCertificates.AsQueryable()
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
using BMA.EHR.Extensions;
|
||||
using BMA.EHR.Organization.Service.Extensions;
|
||||
using BMA.EHR.Profile.Service.Controllers;
|
||||
using BMA.EHR.Recruit.Service.Services;
|
||||
using BMA.EHR.Report.Service.Data;
|
||||
|
|
@ -13,355 +13,355 @@ using Telerik.Reporting.Processing;
|
|||
|
||||
namespace BMA.EHR.Report.Service.Controllers
|
||||
{
|
||||
[Route("api/v{version:apiVersion}/report/recruit")]
|
||||
[ApiVersion("1.0")]
|
||||
[ApiController]
|
||||
[Produces("application/json")]
|
||||
[Authorize]
|
||||
[SwaggerTag("รายงานข้อมูลการสอบแข่งขัน")]
|
||||
public class RecruitReportController : BaseController
|
||||
{
|
||||
#region " Fields "
|
||||
[Route("api/v{version:apiVersion}/report/recruit")]
|
||||
[ApiVersion("1.0")]
|
||||
[ApiController]
|
||||
[Produces("application/json")]
|
||||
[Authorize]
|
||||
[SwaggerTag("รายงานข้อมูลการสอบแข่งขัน")]
|
||||
public class RecruitReportController : BaseController
|
||||
{
|
||||
#region " Fields "
|
||||
|
||||
private readonly ApplicationDbContext _context;
|
||||
private readonly IWebHostEnvironment _hostingEnvironment;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly string space = "ㅤ";
|
||||
private readonly RecruitService _recruitService;
|
||||
private readonly ApplicationDbContext _context;
|
||||
private readonly IWebHostEnvironment _hostingEnvironment;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly string space = "ㅤ";
|
||||
private readonly RecruitService _recruitService;
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region " Constructor and Destructor "
|
||||
#region " Constructor and Destructor "
|
||||
|
||||
public RecruitReportController(ApplicationDbContext context,
|
||||
IWebHostEnvironment hostingEnvironment,
|
||||
IConfiguration configuration,
|
||||
RecruitService recruitService)
|
||||
{
|
||||
this._context = context;
|
||||
this._hostingEnvironment = hostingEnvironment;
|
||||
this._configuration = configuration;
|
||||
this._recruitService = recruitService;
|
||||
}
|
||||
public RecruitReportController(ApplicationDbContext context,
|
||||
IWebHostEnvironment hostingEnvironment,
|
||||
IConfiguration configuration,
|
||||
RecruitService recruitService)
|
||||
{
|
||||
this._context = context;
|
||||
this._hostingEnvironment = hostingEnvironment;
|
||||
this._configuration = configuration;
|
||||
this._recruitService = recruitService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region " Methods "
|
||||
#region " Methods "
|
||||
|
||||
/// <summary>
|
||||
/// แสดงหนังสือรับรอง
|
||||
/// </summary>
|
||||
/// <param name="id">รหัสรอบการสอบ</param>
|
||||
/// <param name="examId">เลขประจำตัวผู้สมัครสอบ</param>
|
||||
/// <param name="type">ชนิดของรายงาน</param>
|
||||
/// <returns></returns>
|
||||
/// <response code="200">เมื่อแสดงรายงานสำเร็จ</response>
|
||||
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
/// <summary>
|
||||
/// แสดงหนังสือรับรอง
|
||||
/// </summary>
|
||||
/// <param name="id">รหัสรอบการสอบ</param>
|
||||
/// <param name="examId">เลขประจำตัวผู้สมัครสอบ</param>
|
||||
/// <param name="type">ชนิดของรายงาน</param>
|
||||
/// <returns></returns>
|
||||
/// <response code="200">เมื่อแสดงรายงานสำเร็จ</response>
|
||||
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
|
||||
[HttpGet("certificate/{type:int}/{id:length(36)}/{examId}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> GetCertificateReportAsync(Guid id, string examId, int type)
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await _context.Recruits.AsQueryable()
|
||||
.Include(x => x.RecruitImport)
|
||||
.Where(x => x.RecruitImport.Id == id)
|
||||
.Where(x => x.ExamId == examId)
|
||||
.Join(_context.RecruitScores.AsQueryable()
|
||||
.Include(x => x.ScoreImport),
|
||||
rc => new { rc.RecruitImport.Year, rc.ExamId },
|
||||
sc => new { sc.ScoreImport.Year, sc.ExamId },
|
||||
(p, sr) => new
|
||||
{
|
||||
ExamID = p.ExamId,
|
||||
p.CitizenId,
|
||||
Order = p.RecruitImport.Order,
|
||||
Year = p.RecruitImport.Year.ToThaiYear(),
|
||||
FullName = $"{p.Prefix}{p.FirstName} {p.LastName}",
|
||||
ExamResult = sr == null ? "" : sr.ExamStatus,
|
||||
EndDate = p.RecruitImport.RegisterEndDate == null ? "-" : p.RecruitImport.RegisterEndDate.Value.ToThaiFullDate3(),
|
||||
AuthName = "นายณัฐพงศ์ ดิษยบุตร",
|
||||
AuthPosition = "หัวหน้าสำนักงาน ก.ก."
|
||||
})
|
||||
.FirstOrDefaultAsync();
|
||||
[HttpGet("certificate/{type:int}/{id:length(36)}/{examId}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> GetCertificateReportAsync(Guid id, string examId, int type)
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await _context.Recruits.AsQueryable()
|
||||
.Include(x => x.RecruitImport)
|
||||
.Where(x => x.RecruitImport.Id == id)
|
||||
.Where(x => x.ExamId == examId)
|
||||
.Join(_context.RecruitScores.AsQueryable()
|
||||
.Include(x => x.ScoreImport),
|
||||
rc => new { rc.RecruitImport.Year, rc.ExamId },
|
||||
sc => new { sc.ScoreImport.Year, sc.ExamId },
|
||||
(p, sr) => new
|
||||
{
|
||||
ExamID = p.ExamId,
|
||||
p.CitizenId,
|
||||
Order = p.RecruitImport.Order,
|
||||
Year = p.RecruitImport.Year.ToThaiYear(),
|
||||
FullName = $"{p.Prefix}{p.FirstName} {p.LastName}",
|
||||
ExamResult = sr == null ? "" : sr.ExamStatus,
|
||||
EndDate = p.RecruitImport.RegisterEndDate == null ? "-" : p.RecruitImport.RegisterEndDate.Value.ToThaiFullDate3(),
|
||||
AuthName = "นายณัฐพงศ์ ดิษยบุตร",
|
||||
AuthPosition = "หัวหน้าสำนักงาน ก.ก."
|
||||
})
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptCertificate{type}.trdp");
|
||||
ReportPackager reportPackager = new ReportPackager();
|
||||
Telerik.Reporting.Report report = null;
|
||||
using (var sourceStream = System.IO.File.OpenRead(rptFile))
|
||||
{
|
||||
report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
|
||||
}
|
||||
var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptCertificate{type}.trdp");
|
||||
ReportPackager reportPackager = new ReportPackager();
|
||||
Telerik.Reporting.Report report = null;
|
||||
using (var sourceStream = System.IO.File.OpenRead(rptFile))
|
||||
{
|
||||
report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
|
||||
}
|
||||
|
||||
report.DataSource = data;
|
||||
System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();
|
||||
report.DataSource = data;
|
||||
System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();
|
||||
|
||||
InstanceReportSource instanceReportSource = new InstanceReportSource()
|
||||
{
|
||||
ReportDocument = report
|
||||
};
|
||||
InstanceReportSource instanceReportSource = new InstanceReportSource()
|
||||
{
|
||||
ReportDocument = report
|
||||
};
|
||||
|
||||
|
||||
ReportProcessor reportProcessor = new ReportProcessor(_configuration);
|
||||
RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo);
|
||||
ReportProcessor reportProcessor = new ReportProcessor(_configuration);
|
||||
RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo);
|
||||
|
||||
var content = result.DocumentBytes;
|
||||
return File(content, "application/pdf", $"หนังสือรับรอง_{data.CitizenId}_{data.FullName}.pdf");
|
||||
var content = result.DocumentBytes;
|
||||
return File(content, "application/pdf", $"หนังสือรับรอง_{data.CitizenId}_{data.FullName}.pdf");
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex, "เกิดข้อผิดพลาดในการแสดงรายงาน");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex, "เกิดข้อผิดพลาดในการแสดงรายงาน");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// แสดงเอกสารผลสอบ
|
||||
/// </summary>
|
||||
/// <param name="id">รหัสรอบการสอบ</param>
|
||||
/// <param name="examId">เลขประจำตัวผู้สมัครสอบ</param>
|
||||
/// <returns></returns>
|
||||
/// <response code="200">เมื่อแสดงรายงานสำเร็จ</response>
|
||||
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
[HttpGet("score/{id:length(36)}/{examId}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> GetScoreReportAsync(Guid id, string examId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await _context.Recruits.AsQueryable()
|
||||
.Include(x => x.RecruitImport)
|
||||
.Include(x => x.Documents)
|
||||
.ThenInclude(x => x.DocumentFile)
|
||||
.Where(x => x.RecruitImport.Id == id)
|
||||
.Where(x => x.ExamId == examId)
|
||||
.Join(_context.RecruitScores.AsQueryable()
|
||||
.Include(x => x.ScoreImport),
|
||||
rc => new { rc.RecruitImport.Year, rc.ExamId },
|
||||
sc => new { sc.ScoreImport.Year, sc.ExamId },
|
||||
(p, sr) => new
|
||||
{
|
||||
ExamId = p.ExamId,
|
||||
CitizenId = p.CitizenId,
|
||||
p.Prefix,
|
||||
FullName = $"{p.Prefix}{p.FirstName} {p.LastName}",
|
||||
DateOfBirth = p.DateOfBirth.ToThaiShortDate(),
|
||||
Gender = p.Gendor,
|
||||
Degree = p.Educations.First().Degree,
|
||||
Major = p.Educations.First().Major,
|
||||
/// <summary>
|
||||
/// แสดงเอกสารผลสอบ
|
||||
/// </summary>
|
||||
/// <param name="id">รหัสรอบการสอบ</param>
|
||||
/// <param name="examId">เลขประจำตัวผู้สมัครสอบ</param>
|
||||
/// <returns></returns>
|
||||
/// <response code="200">เมื่อแสดงรายงานสำเร็จ</response>
|
||||
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
[HttpGet("score/{id:length(36)}/{examId}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> GetScoreReportAsync(Guid id, string examId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await _context.Recruits.AsQueryable()
|
||||
.Include(x => x.RecruitImport)
|
||||
.Include(x => x.Documents)
|
||||
.ThenInclude(x => x.DocumentFile)
|
||||
.Where(x => x.RecruitImport.Id == id)
|
||||
.Where(x => x.ExamId == examId)
|
||||
.Join(_context.RecruitScores.AsQueryable()
|
||||
.Include(x => x.ScoreImport),
|
||||
rc => new { rc.RecruitImport.Year, rc.ExamId },
|
||||
sc => new { sc.ScoreImport.Year, sc.ExamId },
|
||||
(p, sr) => new
|
||||
{
|
||||
ExamId = p.ExamId,
|
||||
CitizenId = p.CitizenId,
|
||||
p.Prefix,
|
||||
FullName = $"{p.Prefix}{p.FirstName} {p.LastName}",
|
||||
DateOfBirth = p.DateOfBirth.ToThaiShortDate(),
|
||||
Gender = p.Gendor,
|
||||
Degree = p.Educations.First().Degree,
|
||||
Major = p.Educations.First().Major,
|
||||
|
||||
ExamResult = sr == null ? "" : sr.ExamStatus,
|
||||
ExamResult = sr == null ? "" : sr.ExamStatus,
|
||||
|
||||
University = p.Educations.First().University,
|
||||
PositionName = p.PositionName,
|
||||
ExamName = $"{p.RecruitImport.Name} ครั้งที่ {p.RecruitImport.Order}/{p.RecruitImport.Year.ToThaiYear()}",
|
||||
University = p.Educations.First().University,
|
||||
PositionName = p.PositionName,
|
||||
ExamName = $"{p.RecruitImport.Name} ครั้งที่ {p.RecruitImport.Order}/{p.RecruitImport.Year.ToThaiYear()}",
|
||||
|
||||
|
||||
Number = sr == null ? "" : sr.Number,
|
||||
ExamCount = _recruitService.GetExamCount(p.CitizenId),
|
||||
ScoreExpire = p.RecruitImport.AnnouncementDate == null ? "" : p.RecruitImport.AnnouncementDate.Value.AddYears(2).ToThaiShortDate(),
|
||||
Number = sr == null ? "" : sr.Number,
|
||||
ExamCount = _recruitService.GetExamCount(p.CitizenId),
|
||||
ScoreExpire = p.RecruitImport.AnnouncementDate == null ? "" : p.RecruitImport.AnnouncementDate.Value.AddYears(2).ToThaiShortDate(),
|
||||
|
||||
|
||||
FullA = sr == null ? 0 : sr.FullA,
|
||||
SumA = sr == null ? 0 : sr.SumA,
|
||||
FullB = sr == null ? 0 : sr.FullB,
|
||||
SumB = sr == null ? 0 : sr.SumB,
|
||||
FullC = sr == null ? 0 : sr.FullC,
|
||||
SumC = sr == null ? 0 : sr.SumC,
|
||||
FullA = sr == null ? 0 : sr.FullA,
|
||||
SumA = sr == null ? 0 : sr.SumA,
|
||||
FullB = sr == null ? 0 : sr.FullB,
|
||||
SumB = sr == null ? 0 : sr.SumB,
|
||||
FullC = sr == null ? 0 : sr.FullC,
|
||||
SumC = sr == null ? 0 : sr.SumC,
|
||||
|
||||
|
||||
|
||||
})
|
||||
.FirstOrDefaultAsync();
|
||||
})
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptExamResult.trdp");
|
||||
ReportPackager reportPackager = new ReportPackager();
|
||||
Telerik.Reporting.Report report = null;
|
||||
using (var sourceStream = System.IO.File.OpenRead(rptFile))
|
||||
{
|
||||
report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
|
||||
}
|
||||
var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptExamResult.trdp");
|
||||
ReportPackager reportPackager = new ReportPackager();
|
||||
Telerik.Reporting.Report report = null;
|
||||
using (var sourceStream = System.IO.File.OpenRead(rptFile))
|
||||
{
|
||||
report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
|
||||
}
|
||||
|
||||
report.DataSource = data;
|
||||
System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();
|
||||
report.DataSource = data;
|
||||
System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();
|
||||
|
||||
InstanceReportSource instanceReportSource = new InstanceReportSource()
|
||||
{
|
||||
ReportDocument = report
|
||||
};
|
||||
InstanceReportSource instanceReportSource = new InstanceReportSource()
|
||||
{
|
||||
ReportDocument = report
|
||||
};
|
||||
|
||||
|
||||
ReportProcessor reportProcessor = new ReportProcessor(_configuration);
|
||||
RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo);
|
||||
ReportProcessor reportProcessor = new ReportProcessor(_configuration);
|
||||
RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo);
|
||||
|
||||
var content = result.DocumentBytes;
|
||||
return File(content, "application/pdf", $"ผลคะแนนสอบ_{data.CitizenId}_{data.FullName}.pdf");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex, "เกิดข้อผิดพลาดในการแสดงรายงาน");
|
||||
}
|
||||
}
|
||||
var content = result.DocumentBytes;
|
||||
return File(content, "application/pdf", $"ผลคะแนนสอบ_{data.CitizenId}_{data.FullName}.pdf");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex, "เกิดข้อผิดพลาดในการแสดงรายงาน");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("candidate/{id:length(36)}")]
|
||||
[AllowAnonymous]
|
||||
public async Task<ActionResult<ResponseObject>> GetCandidateListReportAsync(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await _context.Recruits.AsQueryable()
|
||||
.Include(x => x.RecruitImport)
|
||||
.Include(x => x.Documents)
|
||||
.ThenInclude(x => x.DocumentFile)
|
||||
.Where(x => x.RecruitImport.Id == id)
|
||||
.OrderBy(x => x.ExamId)
|
||||
.Select(p => new
|
||||
{
|
||||
ExamId = p.ExamId,
|
||||
FullName = $"{p.Prefix}{p.FirstName} {p.LastName}",
|
||||
PositionName = p.PositionName,
|
||||
ExamName =
|
||||
$"{p.RecruitImport.Name} ครั้งที่ {p.RecruitImport.Order}/{p.RecruitImport.Year.ToThaiYear()}",
|
||||
}).ToListAsync();
|
||||
[HttpGet("candidate/{id:length(36)}")]
|
||||
[AllowAnonymous]
|
||||
public async Task<ActionResult<ResponseObject>> GetCandidateListReportAsync(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await _context.Recruits.AsQueryable()
|
||||
.Include(x => x.RecruitImport)
|
||||
.Include(x => x.Documents)
|
||||
.ThenInclude(x => x.DocumentFile)
|
||||
.Where(x => x.RecruitImport.Id == id)
|
||||
.OrderBy(x => x.ExamId)
|
||||
.Select(p => new
|
||||
{
|
||||
ExamId = p.ExamId,
|
||||
FullName = $"{p.Prefix}{p.FirstName} {p.LastName}",
|
||||
PositionName = p.PositionName,
|
||||
ExamName =
|
||||
$"{p.RecruitImport.Name} ครั้งที่ {p.RecruitImport.Order}/{p.RecruitImport.Year.ToThaiYear()}",
|
||||
}).ToListAsync();
|
||||
|
||||
if (data.Count == 0) return Success();
|
||||
if (data.Count == 0) return Success();
|
||||
|
||||
var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptCandidateList.trdp");
|
||||
ReportPackager reportPackager = new ReportPackager();
|
||||
Telerik.Reporting.Report report = null;
|
||||
using (var sourceStream = System.IO.File.OpenRead(rptFile))
|
||||
{
|
||||
report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
|
||||
}
|
||||
var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptCandidateList.trdp");
|
||||
ReportPackager reportPackager = new ReportPackager();
|
||||
Telerik.Reporting.Report report = null;
|
||||
using (var sourceStream = System.IO.File.OpenRead(rptFile))
|
||||
{
|
||||
report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
|
||||
}
|
||||
|
||||
report.DataSource = data;
|
||||
System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();
|
||||
report.DataSource = data;
|
||||
System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();
|
||||
|
||||
InstanceReportSource instanceReportSource = new InstanceReportSource()
|
||||
{
|
||||
ReportDocument = report
|
||||
};
|
||||
InstanceReportSource instanceReportSource = new InstanceReportSource()
|
||||
{
|
||||
ReportDocument = report
|
||||
};
|
||||
|
||||
|
||||
ReportProcessor reportProcessor = new ReportProcessor(_configuration);
|
||||
RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo);
|
||||
ReportProcessor reportProcessor = new ReportProcessor(_configuration);
|
||||
RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo);
|
||||
|
||||
var content = result.DocumentBytes;
|
||||
return File(content, "application/pdf", $"รายชื่อผู้มีสิทธิ์สอบ_{data.First().ExamName}.pdf");
|
||||
var content = result.DocumentBytes;
|
||||
return File(content, "application/pdf", $"รายชื่อผู้มีสิทธิ์สอบ_{data.First().ExamName}.pdf");
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("pass/{id:length(36)}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> GetPassExamListReportAsync(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await _context.Recruits.AsQueryable()
|
||||
.Include(x => x.RecruitImport)
|
||||
.ThenInclude(x => x.ScoreImport)
|
||||
.Include(x => x.Documents)
|
||||
.ThenInclude(x => x.DocumentFile)
|
||||
[HttpGet("pass/{id:length(36)}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> GetPassExamListReportAsync(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await _context.Recruits.AsQueryable()
|
||||
.Include(x => x.RecruitImport)
|
||||
.ThenInclude(x => x.ScoreImport)
|
||||
.Include(x => x.Documents)
|
||||
.ThenInclude(x => x.DocumentFile)
|
||||
|
||||
|
||||
.Join(_context.RecruitScores.AsQueryable()
|
||||
.Include(x => x.ScoreImport)
|
||||
.Where(x => x.ScoreImport.RecruitImportId == id)
|
||||
.Where(x => x.ExamStatus == "ผ่าน"),
|
||||
rc => new { id = rc.RecruitImport.Id, examId = rc.ExamId },
|
||||
sc => new { id = sc.ScoreImport.RecruitImportId, examId = sc.ExamId },
|
||||
(p, sr) => new
|
||||
{
|
||||
Id = p.RecruitImport.Id,
|
||||
ExamId = p.ExamId,
|
||||
CitizenId = p.CitizenId,
|
||||
p.Prefix,
|
||||
FullName = $"{p.Prefix}{p.FirstName} {p.LastName}",
|
||||
DateOfBirth = p.DateOfBirth.ToThaiShortDate(),
|
||||
Gender = p.Gendor,
|
||||
Degree = p.Educations.First().Degree,
|
||||
Major = p.Educations.First().Major,
|
||||
.Join(_context.RecruitScores.AsQueryable()
|
||||
.Include(x => x.ScoreImport)
|
||||
.Where(x => x.ScoreImport.RecruitImportId == id)
|
||||
.Where(x => x.ExamStatus == "ผ่าน"),
|
||||
rc => new { id = rc.RecruitImport.Id, examId = rc.ExamId },
|
||||
sc => new { id = sc.ScoreImport.RecruitImportId, examId = sc.ExamId },
|
||||
(p, sr) => new
|
||||
{
|
||||
Id = p.RecruitImport.Id,
|
||||
ExamId = p.ExamId,
|
||||
CitizenId = p.CitizenId,
|
||||
p.Prefix,
|
||||
FullName = $"{p.Prefix}{p.FirstName} {p.LastName}",
|
||||
DateOfBirth = p.DateOfBirth.ToThaiShortDate(),
|
||||
Gender = p.Gendor,
|
||||
Degree = p.Educations.First().Degree,
|
||||
Major = p.Educations.First().Major,
|
||||
|
||||
ExamResult = sr == null ? "" : sr.ExamStatus,
|
||||
ExamResult = sr == null ? "" : sr.ExamStatus,
|
||||
|
||||
University = p.Educations.First().University,
|
||||
PositionName = p.PositionName,
|
||||
ExamName = $"{p.RecruitImport.Name} ครั้งที่ {p.RecruitImport.Order}/{p.RecruitImport.Year.ToThaiYear()}",
|
||||
University = p.Educations.First().University,
|
||||
PositionName = p.PositionName,
|
||||
ExamName = $"{p.RecruitImport.Name} ครั้งที่ {p.RecruitImport.Order}/{p.RecruitImport.Year.ToThaiYear()}",
|
||||
|
||||
|
||||
Number = sr == null ? 99999 : Convert.ToInt32(sr.Number),
|
||||
ExamCount = _recruitService.GetExamCount(p.CitizenId),
|
||||
ScoreExpire = p.RecruitImport.AnnouncementDate == null ? "" : p.RecruitImport.AnnouncementDate.Value.AddYears(2).ToThaiShortDate(),
|
||||
Number = sr == null ? 99999 : Convert.ToInt32(sr.Number),
|
||||
ExamCount = _recruitService.GetExamCount(p.CitizenId),
|
||||
ScoreExpire = p.RecruitImport.AnnouncementDate == null ? "" : p.RecruitImport.AnnouncementDate.Value.AddYears(2).ToThaiShortDate(),
|
||||
|
||||
|
||||
FullA = sr == null ? 0 : sr.FullA,
|
||||
SumA = sr == null ? 0 : sr.SumA,
|
||||
FullB = sr == null ? 0 : sr.FullB,
|
||||
SumB = sr == null ? 0 : sr.SumB,
|
||||
FullC = sr == null ? 0 : sr.FullC,
|
||||
SumC = sr == null ? 0 : sr.SumC,
|
||||
SumScore = sr == null ? 0 : sr.SumA + sr.SumB + sr.SumC,
|
||||
FullA = sr == null ? 0 : sr.FullA,
|
||||
SumA = sr == null ? 0 : sr.SumA,
|
||||
FullB = sr == null ? 0 : sr.FullB,
|
||||
SumB = sr == null ? 0 : sr.SumB,
|
||||
FullC = sr == null ? 0 : sr.FullC,
|
||||
SumC = sr == null ? 0 : sr.SumC,
|
||||
SumScore = sr == null ? 0 : sr.SumA + sr.SumB + sr.SumC,
|
||||
|
||||
|
||||
|
||||
})
|
||||
.OrderBy(x => x.Number)
|
||||
.Where(x => x.Id == id)
|
||||
.Where(x => x.ExamResult == "ผ่าน")
|
||||
.ToListAsync();
|
||||
})
|
||||
.OrderBy(x => x.Number)
|
||||
.Where(x => x.Id == id)
|
||||
.Where(x => x.ExamResult == "ผ่าน")
|
||||
.ToListAsync();
|
||||
|
||||
if (data.Count == 0) return Success();
|
||||
if (data.Count == 0) return Success();
|
||||
|
||||
var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptPassExamList.trdp");
|
||||
ReportPackager reportPackager = new ReportPackager();
|
||||
Telerik.Reporting.Report report = null;
|
||||
using (var sourceStream = System.IO.File.OpenRead(rptFile))
|
||||
{
|
||||
report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
|
||||
}
|
||||
var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptPassExamList.trdp");
|
||||
ReportPackager reportPackager = new ReportPackager();
|
||||
Telerik.Reporting.Report report = null;
|
||||
using (var sourceStream = System.IO.File.OpenRead(rptFile))
|
||||
{
|
||||
report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
|
||||
}
|
||||
|
||||
report.DataSource = data;
|
||||
System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();
|
||||
report.DataSource = data;
|
||||
System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();
|
||||
|
||||
InstanceReportSource instanceReportSource = new InstanceReportSource()
|
||||
{
|
||||
ReportDocument = report
|
||||
};
|
||||
InstanceReportSource instanceReportSource = new InstanceReportSource()
|
||||
{
|
||||
ReportDocument = report
|
||||
};
|
||||
|
||||
|
||||
ReportProcessor reportProcessor = new ReportProcessor(_configuration);
|
||||
RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo);
|
||||
ReportProcessor reportProcessor = new ReportProcessor(_configuration);
|
||||
RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo);
|
||||
|
||||
var content = result.DocumentBytes;
|
||||
return File(content, "application/pdf", $"รายชื่อผู้สอบแข่งขันได้_{data.First().ExamName}.pdf");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex, "เกิดข้อผิดพลาดในการแสดงรายงาน");
|
||||
}
|
||||
}
|
||||
var content = result.DocumentBytes;
|
||||
return File(content, "application/pdf", $"รายชื่อผู้สอบแข่งขันได้_{data.First().ExamName}.pdf");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex, "เกิดข้อผิดพลาดในการแสดงรายงาน");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
|||
13
Extensions/DataReaderExtension.cs
Normal file
13
Extensions/DataReaderExtension.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
using System.Data.Common;
|
||||
|
||||
namespace BMA.EHR.Organization.Service.Extensions
|
||||
{
|
||||
public static class DataReaderExtension
|
||||
{
|
||||
public static bool IsDbNull(this DbDataReader dataReader, string columnName)
|
||||
{
|
||||
return dataReader[columnName] == DBNull.Value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
415
Extensions/DateTimeExtension.cs
Normal file
415
Extensions/DateTimeExtension.cs
Normal file
|
|
@ -0,0 +1,415 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
namespace BMA.EHR.Organization.Service.Extensions
|
||||
{
|
||||
public static class DateTimeExtension
|
||||
{
|
||||
private static CultureInfo _culture = new CultureInfo("th-TH");
|
||||
|
||||
#region " Methods "
|
||||
|
||||
#region " Public "
|
||||
|
||||
public static int DiffDay(this DateTime startDate, DateTime endDate)
|
||||
{
|
||||
if (endDate.Date < startDate.Date)
|
||||
{
|
||||
throw new ArgumentException("End Date must greater than start date.");
|
||||
}
|
||||
return (int)(endDate.Date - startDate.Date).TotalDays + 1;
|
||||
}
|
||||
|
||||
public static double DiffYear(this DateTime currentDate)
|
||||
{
|
||||
return (DateTime.Today - currentDate).TotalDays / 365.2425;
|
||||
}
|
||||
|
||||
public static int ToUnixTimeStamp(this DateTime value)
|
||||
{
|
||||
return (int)Math.Truncate((value.ToUniversalTime().Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
|
||||
}
|
||||
|
||||
public static string ToThaiFullDate(this DateTime value)
|
||||
{
|
||||
var yy = value.Year < 2400 ? value.Year + 543 : value.Year;
|
||||
return $"วันที่ {value.Day} เดือน {value.ToString("MMMM", _culture.DateTimeFormat)} พ.ศ. {yy}";
|
||||
}
|
||||
|
||||
public static string ToThaiFullDate2(this DateTime value)
|
||||
{
|
||||
var yy = value.Year < 2400 ? value.Year + 543 : value.Year;
|
||||
return $"{value.Day} {value.ToString("MMMM", _culture.DateTimeFormat)} พ.ศ. {yy}";
|
||||
}
|
||||
|
||||
public static string ToThaiFullDate3(this DateTime value)
|
||||
{
|
||||
var yy = value.Year < 2400 ? value.Year + 543 : value.Year;
|
||||
return $"{value.Day} {value.ToString("MMMM", _culture.DateTimeFormat)} {yy}";
|
||||
}
|
||||
|
||||
public static string ToThaiShortDateTime(this DateTime value)
|
||||
{
|
||||
var yy = value.Year < 2400 ? value.Year + 543 : value.Year;
|
||||
return $"{value.Day} {value.ToString("MMM", _culture.DateTimeFormat)} {yy} {value.ToString("HH:mm:ss")}";
|
||||
}
|
||||
|
||||
public static string ToThaiShortDate(this DateTime value)
|
||||
{
|
||||
var yy = value.Year < 2400 ? value.Year + 543 : value.Year;
|
||||
return $"{value.Day} {value.ToString("MMM", _culture.DateTimeFormat)} {yy}";
|
||||
}
|
||||
|
||||
public static string ToThaiShortDate2(this DateTime value)
|
||||
{
|
||||
var yy = value.Year < 2400 ? value.Year + 543 : value.Year;
|
||||
return $"{value.Day} {value.ToString("MMM", _culture.DateTimeFormat)} {yy.ToString().Right(2)}";
|
||||
}
|
||||
|
||||
public static string ToThaiDate(this DateTime value)
|
||||
{
|
||||
var yy = value.Year < 2400 ? value.Year + 543 : value.Year;
|
||||
return $"{value.Day}/{value.Month}/{yy}";
|
||||
}
|
||||
|
||||
public static string ToCeDate(this DateTime value)
|
||||
{
|
||||
var yy = value.Year >= 2400 ? value.Year - 543 : value.Year;
|
||||
return $"{value.Day}/{value.Month}/{yy}";
|
||||
}
|
||||
|
||||
public static string ToThaiDateFullDigit(this DateTime value)
|
||||
{
|
||||
var yy = value.Year < 2400 ? value.Year + 543 : value.Year;
|
||||
return $"{value.Day.ToString("00")}/{value.Month.ToString("00")}/{yy.ToString("00")}";
|
||||
}
|
||||
|
||||
public static string GetDay(this DateTime value)
|
||||
{
|
||||
return value.Day.ToString();
|
||||
}
|
||||
|
||||
public static string GetMonth(this DateTime value)
|
||||
{
|
||||
return value.ToString("MMMM", _culture.DateTimeFormat);
|
||||
}
|
||||
|
||||
public static string GetYear(this DateTime value)
|
||||
{
|
||||
return value.Year.ToThaiYear().ToString();
|
||||
}
|
||||
|
||||
public static string CalculateAgeStrV2(this DateTime date, int plusYear = 0, int subtractYear = 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
var currentDate = DateTime.Now;
|
||||
var age = currentDate - date;
|
||||
|
||||
|
||||
if (date > currentDate)
|
||||
{
|
||||
throw new ArgumentException("วันเกิดต้องไม่มากกว่าวันที่ปัจจุบัน");
|
||||
}
|
||||
|
||||
int years = currentDate.Year - date.Year;
|
||||
int months = 0;
|
||||
int days = 0;
|
||||
|
||||
if (currentDate.Month < date.Month ||
|
||||
(currentDate.Month == date.Month && currentDate.Day < date.Day))
|
||||
{
|
||||
years--;
|
||||
months = 12 - date.Month + currentDate.Month;
|
||||
|
||||
if (currentDate.Day < date.Day)
|
||||
{
|
||||
months--;
|
||||
int lastMonthDays = 0;
|
||||
if (currentDate.Month == 1)
|
||||
lastMonthDays = DateTime.DaysInMonth(currentDate.Year - 1, 12);
|
||||
else
|
||||
lastMonthDays = DateTime.DaysInMonth(currentDate.Year, currentDate.Month - 1);
|
||||
days = lastMonthDays - date.Day + currentDate.Day;
|
||||
}
|
||||
else
|
||||
{
|
||||
days = currentDate.Day - date.Day;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
months = currentDate.Month - date.Month;
|
||||
|
||||
if (currentDate.Day < date.Day)
|
||||
{
|
||||
months--;
|
||||
int lastMonthDays = 0;
|
||||
if (currentDate.Month == 1)
|
||||
lastMonthDays = DateTime.DaysInMonth(currentDate.Year - 1, 12);
|
||||
else
|
||||
lastMonthDays = DateTime.DaysInMonth(currentDate.Year, currentDate.Month - 1);
|
||||
days = lastMonthDays - date.Day + currentDate.Day;
|
||||
}
|
||||
else
|
||||
{
|
||||
days = currentDate.Day - date.Day;
|
||||
}
|
||||
}
|
||||
|
||||
//return new Age(years, months, days);
|
||||
|
||||
|
||||
//// แปลงเป็นอายุในรูปแบบวันที่
|
||||
//int years = (int)(age.Days / 365.25) + plusYear - subtractYear;
|
||||
//int months = (int)((age.Days % 365.25) / 30.436875);
|
||||
//int days = (int)((age.Days % 365.25) % 30.436875);
|
||||
|
||||
return $"{years} ปี {months} เดือน {days} วัน";
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public static int CalculateAge(this DateTime date, int plusYear = 0, int subtractYear = 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
var currentDate = DateTime.Now;
|
||||
var age = currentDate - date;
|
||||
|
||||
// แปลงเป็นอายุในรูปแบบวันที่
|
||||
int years = (int)(age.Days / 365.25) + plusYear - subtractYear;
|
||||
|
||||
return years;
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public static int CalculateGovAge(this DateTime appointDate, int plusYear = 0, int subtractYear = 0)
|
||||
{
|
||||
if (DateTime.Now.Month - appointDate.Month >= 6)
|
||||
return (DateTime.Now.Year - appointDate.Year) + 1 + plusYear - subtractYear;
|
||||
else
|
||||
return (DateTime.Now.Year - appointDate.Year) + plusYear - subtractYear;
|
||||
}
|
||||
|
||||
|
||||
public static string CalculateGovAgeStr(this DateTime appointDate, int plusYear = 0, int subtractYear = 0)
|
||||
{
|
||||
var d2 = DateTime.Now;
|
||||
TimeSpan sp = d2.Subtract(appointDate);
|
||||
var alldays = sp.Days + ((plusYear - subtractYear) * 365);
|
||||
|
||||
int yy = alldays / 365;
|
||||
int mm = (alldays - (yy * 365)) / 30;
|
||||
int dd = (alldays - (yy * 365) - (mm * 30));
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.Clear();
|
||||
sb.Append(yy == 0 ? "" : $"{yy} ปี ");
|
||||
sb.Append(mm == 0 ? "" : $"{mm} เดือน ");
|
||||
sb.Append(dd == 0 ? "" : $"{dd} วัน ");
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static string CalculateGovAgeInYear(this DateTime appointDate, int plusYear = 0, int subtractYear = 0)
|
||||
{
|
||||
var d2 = DateTime.Now;
|
||||
TimeSpan sp = d2.Subtract(appointDate);
|
||||
var alldays = sp.Days + ((plusYear - subtractYear) * 365);
|
||||
|
||||
int yy = alldays / 365;
|
||||
|
||||
return yy.ToString();
|
||||
}
|
||||
|
||||
public static int GetDifferenceInYears(this DateTime startDate, DateTime endDate)
|
||||
{
|
||||
int finalResult = 0;
|
||||
|
||||
const int daysInYear = 365;
|
||||
|
||||
//DateTime endDate = DateTime.Now;
|
||||
|
||||
TimeSpan timeSpan = endDate - startDate;
|
||||
|
||||
if (timeSpan.TotalDays > 365)
|
||||
{
|
||||
finalResult = (int)Math.Round((timeSpan.TotalDays / daysInYear), MidpointRounding.ToEven);
|
||||
}
|
||||
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
public static DateTime CalculateRetireDate(this DateTime birthDate)
|
||||
{
|
||||
var dd = birthDate.Day;
|
||||
var mm = birthDate.Month;
|
||||
var yy = birthDate.Year;
|
||||
|
||||
var g1 = true;
|
||||
switch (mm)
|
||||
{
|
||||
case 10: if (dd >= 2) g1 = false; break;
|
||||
case 11:
|
||||
case 12: g1 = false; break;
|
||||
}
|
||||
|
||||
if (g1)
|
||||
return new DateTime(yy + 60, 9, 30);
|
||||
else
|
||||
return new DateTime(yy + 61, 9, 30);
|
||||
}
|
||||
|
||||
public static bool IsBetween(this DateTime now, TimeSpan start, TimeSpan end)
|
||||
{
|
||||
var time = now.TimeOfDay;
|
||||
// Scenario 1: If the start time and the end time are in the same day.
|
||||
if (start <= end)
|
||||
return time >= start && time <= end;
|
||||
// Scenario 2: The start time and end time is on different days.
|
||||
return time >= start || time <= end;
|
||||
}
|
||||
|
||||
public static string CalculateBetweenDate(this DateTime startDate, DateTime endDate)
|
||||
{
|
||||
// var d2 = DateTime.Now;
|
||||
TimeSpan sp = endDate.Subtract(startDate);
|
||||
var alldays = sp.Days;
|
||||
|
||||
int yy = alldays / 365;
|
||||
int mm = (alldays - (yy * 365)) / 30;
|
||||
int dd = (alldays - (yy * 365) - (mm * 30));
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.Clear();
|
||||
sb.Append(yy == 0 ? "" : $"{yy} ปี ");
|
||||
sb.Append(mm == 0 ? "" : $"{mm} เดือน ");
|
||||
sb.Append(dd == 0 ? "" : $"{dd} วัน ");
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static string CalculateBetweenDateV2(this DateTime startDate, DateTime endDate)
|
||||
{
|
||||
if (startDate == null || endDate == null)
|
||||
return "-";
|
||||
|
||||
DateTime today = endDate;
|
||||
DateTime birthDate = Convert.ToDateTime(startDate).AddDays(-1);
|
||||
int years = new DateTime(endDate.Subtract(birthDate).Ticks).Year - 1;
|
||||
DateTime pastYearDate = birthDate.AddYears(years);
|
||||
int months = 0;
|
||||
for (int i = 1; i <= 12; i++)
|
||||
{
|
||||
if (pastYearDate.AddMonths(i) == today)
|
||||
{
|
||||
months = i;
|
||||
break;
|
||||
}
|
||||
else if (pastYearDate.AddMonths(i) >= today)
|
||||
{
|
||||
months = i - 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
int days = today.Subtract(pastYearDate.AddMonths(months)).Days;
|
||||
if (today.Day < pastYearDate.Day)
|
||||
{
|
||||
if (System.DateTime.DaysInMonth(pastYearDate.Year, pastYearDate.Month) > System.DateTime.DaysInMonth(pastYearDate.AddMonths(months).Year, pastYearDate.AddMonths(months).Month))
|
||||
{
|
||||
days += 1;
|
||||
}
|
||||
if (System.DateTime.DaysInMonth(pastYearDate.Year, pastYearDate.Month) < System.DateTime.DaysInMonth(pastYearDate.AddMonths(months).Year, pastYearDate.AddMonths(months).Month))
|
||||
{
|
||||
days -= 1;
|
||||
}
|
||||
}
|
||||
// int Hours = Today.Subtract(PastYearDate).Hours;
|
||||
// int Minutes = Today.Subtract(PastYearDate).Minutes;
|
||||
// int Seconds = Today.Subtract(PastYearDate).Seconds;
|
||||
Console.WriteLine(today);
|
||||
Console.WriteLine(pastYearDate);
|
||||
Console.WriteLine(months);
|
||||
Console.WriteLine(pastYearDate.AddMonths(months));
|
||||
var sb = new StringBuilder();
|
||||
sb.Clear();
|
||||
sb.Append(years == 0 ? "" : $"{years} ปี ");
|
||||
sb.Append(months == 0 ? "" : $"{months} เดือน ");
|
||||
sb.Append(days == 0 ? "" : $"{days} วัน ");
|
||||
return sb.ToString();
|
||||
}
|
||||
public static CalculateBetweenDateV2ValueObj CalculateBetweenDateV2Value(this DateTime startDate, DateTime endDate)
|
||||
{
|
||||
// if (startDate == null || endDate == null)
|
||||
// return null;
|
||||
DateTime today = endDate.AddDays(1);
|
||||
DateTime birthDate = startDate;
|
||||
int years = new DateTime(endDate.Subtract(birthDate).Ticks).Year - 1;
|
||||
DateTime pastYearDate = birthDate.AddYears(years);
|
||||
int months = 0;
|
||||
for (int i = 1; i <= 12; i++)
|
||||
{
|
||||
if (pastYearDate.AddMonths(i) == today)
|
||||
{
|
||||
months = i;
|
||||
break;
|
||||
}
|
||||
else if (pastYearDate.AddMonths(i) >= today)
|
||||
{
|
||||
months = i - 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
int days = today.Subtract(pastYearDate.AddMonths(months)).Days;
|
||||
if (today.Day < pastYearDate.Day)
|
||||
{
|
||||
if (System.DateTime.DaysInMonth(pastYearDate.Year, pastYearDate.Month) > System.DateTime.DaysInMonth(pastYearDate.AddMonths(months).Year, pastYearDate.AddMonths(months).Month))
|
||||
{
|
||||
days += 1;
|
||||
}
|
||||
if (System.DateTime.DaysInMonth(pastYearDate.Year, pastYearDate.Month) < System.DateTime.DaysInMonth(pastYearDate.AddMonths(months).Year, pastYearDate.AddMonths(months).Month))
|
||||
{
|
||||
days -= 1;
|
||||
}
|
||||
}
|
||||
if (days >= 30)
|
||||
{
|
||||
months = months + 1;
|
||||
days = 0;
|
||||
if (months >= 12)
|
||||
{
|
||||
years = years + 1;
|
||||
months = 0;
|
||||
}
|
||||
}
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.Clear();
|
||||
sb.Append(years == 0 ? "" : $"{years} ปี ");
|
||||
sb.Append(months == 0 ? "" : $"{months} เดือน ");
|
||||
sb.Append(days == 0 ? "" : $"{days} วัน ");
|
||||
return new CalculateBetweenDateV2ValueObj { years = years, months = months, days = days };
|
||||
}
|
||||
public class CalculateBetweenDateV2ValueObj
|
||||
{
|
||||
public int years { get; set; }
|
||||
|
||||
public int months { get; set; }
|
||||
|
||||
public int days { get; set; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
256
Extensions/DoubleExtension.cs
Normal file
256
Extensions/DoubleExtension.cs
Normal file
|
|
@ -0,0 +1,256 @@
|
|||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace BMA.EHR.Organization.Service.Extensions
|
||||
{
|
||||
public static class DoubleExtension
|
||||
{
|
||||
#region " Fields "
|
||||
private static string[] _ones =
|
||||
{
|
||||
"zero",
|
||||
"one",
|
||||
"two",
|
||||
"three",
|
||||
"four",
|
||||
"five",
|
||||
"six",
|
||||
"seven",
|
||||
"eight",
|
||||
"nine"
|
||||
};
|
||||
|
||||
private static string[] _teens =
|
||||
{
|
||||
"ten",
|
||||
"eleven",
|
||||
"twelve",
|
||||
"thirteen",
|
||||
"fourteen",
|
||||
"fifteen",
|
||||
"sixteen",
|
||||
"seventeen",
|
||||
"eighteen",
|
||||
"nineteen"
|
||||
};
|
||||
|
||||
private static string[] _tens =
|
||||
{
|
||||
"",
|
||||
"ten",
|
||||
"twenty",
|
||||
"thirty",
|
||||
"forty",
|
||||
"fifty",
|
||||
"sixty",
|
||||
"seventy",
|
||||
"eighty",
|
||||
"ninety"
|
||||
};
|
||||
|
||||
// US Nnumbering:
|
||||
private static string[] _thousands =
|
||||
{
|
||||
"",
|
||||
"thousand",
|
||||
"million",
|
||||
"billion",
|
||||
"trillion",
|
||||
"quadrillion"
|
||||
};
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Methods "
|
||||
|
||||
#region " Conversion "
|
||||
|
||||
public static string ToNumericText(this double number)
|
||||
{
|
||||
return number.ToString("#,##0.00");
|
||||
}
|
||||
public static string ToNumericNoDecimalText(this double number)
|
||||
{
|
||||
return number.ToString("#,##");
|
||||
}
|
||||
|
||||
public static string ToReadText(this double value)
|
||||
{
|
||||
string digits, temp;
|
||||
bool showThousands = false;
|
||||
bool allZeros = true;
|
||||
|
||||
// Use StringBuilder to build result
|
||||
StringBuilder builder = new StringBuilder();
|
||||
// Convert integer portion of value to string
|
||||
digits = ((long)value).ToString();
|
||||
// Traverse characters in reverse order
|
||||
for (int i = digits.Length - 1; i >= 0; i--)
|
||||
{
|
||||
int ndigit = (int)(digits[i] - '0');
|
||||
int column = (digits.Length - (i + 1));
|
||||
|
||||
// Determine if ones, tens, or hundreds column
|
||||
switch (column % 3)
|
||||
{
|
||||
case 0: // Ones position
|
||||
showThousands = true;
|
||||
if (i == 0)
|
||||
{
|
||||
// First digit in number (last in loop)
|
||||
temp = String.Format("{0} ", _ones[ndigit]);
|
||||
}
|
||||
else if (digits[i - 1] == '1')
|
||||
{
|
||||
// This digit is part of "teen" value
|
||||
temp = String.Format("{0} ", _teens[ndigit]);
|
||||
// Skip tens position
|
||||
i--;
|
||||
}
|
||||
else if (ndigit != 0)
|
||||
{
|
||||
// Any non-zero digit
|
||||
temp = String.Format("{0} ", _ones[ndigit]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// This digit is zero. If digit in tens and hundreds
|
||||
// column are also zero, don't show "thousands"
|
||||
temp = String.Empty;
|
||||
// Test for non-zero digit in this grouping
|
||||
if (digits[i - 1] != '0' || (i > 1 && digits[i - 2] != '0'))
|
||||
showThousands = true;
|
||||
else
|
||||
showThousands = false;
|
||||
}
|
||||
|
||||
// Show "thousands" if non-zero in grouping
|
||||
if (showThousands)
|
||||
{
|
||||
if (column > 0)
|
||||
{
|
||||
temp = String.Format("{0}{1}{2}",
|
||||
temp,
|
||||
_thousands[column / 3],
|
||||
allZeros ? " " : ", ");
|
||||
}
|
||||
// Indicate non-zero digit encountered
|
||||
allZeros = false;
|
||||
}
|
||||
builder.Insert(0, temp);
|
||||
break;
|
||||
|
||||
case 1: // Tens column
|
||||
if (ndigit > 0)
|
||||
{
|
||||
temp = String.Format("{0}{1}",
|
||||
_tens[ndigit],
|
||||
(digits[i + 1] != '0') ? "-" : " ");
|
||||
builder.Insert(0, temp);
|
||||
}
|
||||
break;
|
||||
|
||||
case 2: // Hundreds column
|
||||
if (ndigit > 0)
|
||||
{
|
||||
temp = String.Format("{0} hundred ", _ones[ndigit]);
|
||||
builder.Insert(0, temp);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Append fractional portion/cents
|
||||
double val = (value - (long)value) * 100;
|
||||
if (val > 0)
|
||||
builder.AppendFormat("and {0:00}/100", (value - (long)value) * 100);
|
||||
|
||||
// Capitalize first letter
|
||||
return String.Format("{0}{1}",
|
||||
Char.ToUpper(builder[0]),
|
||||
builder.ToString(1, builder.Length - 1));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Round "
|
||||
|
||||
public static double RoundUp(this double input)
|
||||
{
|
||||
return Math.Ceiling(input);
|
||||
}
|
||||
|
||||
public static double RoundDown(this double input)
|
||||
{
|
||||
return Math.Floor(input);
|
||||
}
|
||||
|
||||
|
||||
public static double RoundUpStang(this double input)
|
||||
{
|
||||
string temp = "";
|
||||
temp = input.ToNumericText();
|
||||
string[] floating = temp.Split('.');
|
||||
if (double.Parse(floating[1]) == 0)
|
||||
{
|
||||
return input;
|
||||
}
|
||||
if (double.Parse(floating[1]) >= 1 && double.Parse(floating[1]) <= 24)
|
||||
{
|
||||
return (double.Parse(floating[0])) + 0.25;
|
||||
}
|
||||
else if (double.Parse(floating[1]) >= 26 && double.Parse(floating[1]) <= 49)
|
||||
{
|
||||
return (double.Parse(floating[0])) + 0.5;
|
||||
}
|
||||
else if (double.Parse(floating[1]) >= 51 && double.Parse(floating[1]) <= 74)
|
||||
{
|
||||
return (double.Parse(floating[0])) + 0.75;
|
||||
}
|
||||
else if (double.Parse(floating[1]) >= 76 && double.Parse(floating[1]) <= 99)
|
||||
{
|
||||
return (double.Parse(floating[0])) + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
||||
public static double RoundDownStang(this double input)
|
||||
{
|
||||
string temp = "";
|
||||
temp = input.ToNumericText();
|
||||
string[] floating = temp.Split('.');
|
||||
if (double.Parse(floating[1]) == 0)
|
||||
{
|
||||
return input;
|
||||
}
|
||||
if (double.Parse(floating[1]) >= 1 && double.Parse(floating[1]) <= 24)
|
||||
{
|
||||
return Math.Floor(input);
|
||||
}
|
||||
else if (double.Parse(floating[1]) >= 26 && double.Parse(floating[1]) <= 49)
|
||||
{
|
||||
return (double.Parse(floating[0])) + 0.25;
|
||||
}
|
||||
else if (double.Parse(floating[1]) >= 51 && double.Parse(floating[1]) <= 74)
|
||||
{
|
||||
return (double.Parse(floating[0])) + 0.5;
|
||||
}
|
||||
else if (double.Parse(floating[1]) >= 76 && double.Parse(floating[1]) <= 99)
|
||||
{
|
||||
return (double.Parse(floating[0])) + 0.75;
|
||||
}
|
||||
else
|
||||
{
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
74
Extensions/IntegerExtension.cs
Normal file
74
Extensions/IntegerExtension.cs
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
using System.Text;
|
||||
using GreatFriends.ThaiBahtText;
|
||||
|
||||
namespace BMA.EHR.Organization.Service.Extensions
|
||||
{
|
||||
public static class IntegerExtension
|
||||
{
|
||||
public static string ToThaiBahtText(this int value, bool appendBahtOnly)
|
||||
{
|
||||
var decValue = (decimal)value;
|
||||
|
||||
return decValue.ThaiBahtText(appendBahtOnly: appendBahtOnly);
|
||||
}
|
||||
|
||||
public static string ToThaiMonth(this int value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case 1: return "มกราคม";
|
||||
case 2: return "กุมภาพันธ์";
|
||||
case 3: return "มีนาคม";
|
||||
case 4: return "เมษายน";
|
||||
case 5: return "พฤษภาคม";
|
||||
case 6: return "มิถุนายน";
|
||||
case 7: return "กรกฎาคม";
|
||||
case 8: return "สิงหาคม";
|
||||
case 9: return "กันยายน";
|
||||
case 10: return "ตุลาคม";
|
||||
case 11: return "พฤศจิกายน";
|
||||
case 12: return "ธันวาคม";
|
||||
default: return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static int ToThaiYear(this int value)
|
||||
{
|
||||
if (value < 2400)
|
||||
return value + 543;
|
||||
else return value;
|
||||
}
|
||||
|
||||
public static int ToCeYear(this int value)
|
||||
{
|
||||
if (value >= 2400)
|
||||
return value - 543;
|
||||
else return value;
|
||||
}
|
||||
|
||||
public static string ToNumericText(this int number)
|
||||
{
|
||||
return number.ToString("#,##0");
|
||||
}
|
||||
|
||||
public static string ToThaiNumber(this string value)
|
||||
{
|
||||
string arabicNumbers = "0123456789";
|
||||
string thaiNumbers = "๐๑๒๓๔๕๖๗๘๙";
|
||||
StringBuilder result = new StringBuilder();
|
||||
foreach (char digit in value)
|
||||
{
|
||||
int index = arabicNumbers.IndexOf(digit);
|
||||
if (index >= 0)
|
||||
{
|
||||
result.Append(thaiNumbers[index]);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Append(digit);
|
||||
}
|
||||
}
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
37
Extensions/ListExtension.cs
Normal file
37
Extensions/ListExtension.cs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
|
||||
namespace BMA.EHR.Organization.Service.Extensions
|
||||
{
|
||||
public static class ListExtension
|
||||
{
|
||||
#region " Methods "
|
||||
|
||||
#region " Convert "
|
||||
|
||||
public static DataTable ToDataTable<T>(this List<T> list)
|
||||
{
|
||||
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
|
||||
DataTable table = new DataTable();
|
||||
for (int i = 0; i < props.Count; i++)
|
||||
{
|
||||
PropertyDescriptor prop = props[i];
|
||||
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
|
||||
}
|
||||
object[] values = new object[props.Count];
|
||||
foreach (T item in list)
|
||||
{
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
values[i] = props[i].GetValue(item) ?? DBNull.Value; table.Rows.Add(values);
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
51
Extensions/ObjectExtension.cs
Normal file
51
Extensions/ObjectExtension.cs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BMA.EHR.Organization.Service.Extensions
|
||||
{
|
||||
public static class ObjectExtension
|
||||
{
|
||||
public static bool DeepCompare(this object obj, object another)
|
||||
{
|
||||
if (ReferenceEquals(obj, another)) return true;
|
||||
if ((obj == null) || (another == null)) return false;
|
||||
//Compare two object's class, return false if they are difference
|
||||
if (obj.GetType() != another.GetType()) return false;
|
||||
|
||||
var result = true;
|
||||
//Get all properties of obj
|
||||
//And compare each other
|
||||
foreach (var property in obj.GetType().GetProperties())
|
||||
{
|
||||
var objValue = property.GetValue(obj);
|
||||
var anotherValue = property.GetValue(another);
|
||||
if (!objValue.Equals(anotherValue)) result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool Compare(this object obj, object another)
|
||||
{
|
||||
if (ReferenceEquals(obj, another)) return true;
|
||||
if ((obj == null) || (another == null)) return false;
|
||||
if (obj.GetType() != another.GetType()) return false;
|
||||
|
||||
//properties: int, double, DateTime, etc, not class
|
||||
if (!obj.GetType().IsClass) return obj.Equals(another);
|
||||
|
||||
var result = true;
|
||||
foreach (var property in obj.GetType().GetProperties())
|
||||
{
|
||||
var objValue = property.GetValue(obj);
|
||||
var anotherValue = property.GetValue(another);
|
||||
//Recursion
|
||||
if (!objValue.DeepCompare(anotherValue)) result = false;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
29
Extensions/StreamExtension.cs
Normal file
29
Extensions/StreamExtension.cs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace BMA.EHR.Organization.Service.Extensions
|
||||
{
|
||||
public static class StreamExtension
|
||||
{
|
||||
#region " Methods "
|
||||
|
||||
#region " Public "
|
||||
|
||||
public static string ToBase64(this MemoryStream stream)
|
||||
{
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
byte[] bytes;
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
stream.CopyTo(memoryStream);
|
||||
bytes = memoryStream.ToArray();
|
||||
}
|
||||
|
||||
return Convert.ToBase64String(bytes);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
301
Extensions/StringExtension.cs
Normal file
301
Extensions/StringExtension.cs
Normal file
|
|
@ -0,0 +1,301 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace BMA.EHR.Organization.Service.Extensions
|
||||
{
|
||||
public enum DateTimeFormat
|
||||
{
|
||||
Dmy,
|
||||
Mdy,
|
||||
Ymd
|
||||
}
|
||||
|
||||
public static partial class StringExtension
|
||||
{
|
||||
#region " Methods "
|
||||
|
||||
#region " Utilities "
|
||||
|
||||
public static string Format(this string input, int digit)
|
||||
{
|
||||
var fmt = $"D{digit}";
|
||||
return input.ToInteger().ToString(fmt);
|
||||
|
||||
}
|
||||
|
||||
public static string RemoveComma(this string value)
|
||||
{
|
||||
string ret;
|
||||
if (value.Contains(","))
|
||||
{
|
||||
ret = value.Replace(",", "");
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (value != "")
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else
|
||||
return "0.00";
|
||||
}
|
||||
}
|
||||
|
||||
public static string RemoveSpace(this string value)
|
||||
{
|
||||
string ret;
|
||||
if (value.Contains(","))
|
||||
{
|
||||
ret = value.Replace(" ", "");
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (value != "")
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static string Left(this string input, int length)
|
||||
{
|
||||
if (length > 0 && input.Length >= length)
|
||||
{
|
||||
return input.Substring(0, length);
|
||||
}
|
||||
else
|
||||
{
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
||||
public static string Right(this string input, int length)
|
||||
{
|
||||
if (length > 0 && input.Length >= length)
|
||||
{
|
||||
return input.Substring(input.Length - length, length);
|
||||
}
|
||||
else
|
||||
{
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Validation "
|
||||
|
||||
public static bool IsDateTime(this string input)
|
||||
{
|
||||
try
|
||||
{
|
||||
var d = Convert.ToDateTime(input);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsUrl(this string input)
|
||||
{
|
||||
if (input.Trim() != "")
|
||||
{
|
||||
if ((input.Left(7).ToLower() != "http://") || (input.Left(8).ToLower() != "https://"))
|
||||
{
|
||||
input = "http://" + input;
|
||||
}
|
||||
Regex reg = new Regex("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?");
|
||||
return reg.Match(input).Success;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether this instance is email.
|
||||
/// </summary>
|
||||
/// <param name="input">The input.</param>
|
||||
/// <returns></returns>
|
||||
public static bool IsEmail(this string input)
|
||||
{
|
||||
if (input.Trim() != "")
|
||||
{
|
||||
Regex reg = new Regex("\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
|
||||
return reg.Match(input).Success;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether this instance is numeric.
|
||||
/// </summary>
|
||||
/// <param name="input">The input.</param>
|
||||
/// <returns></returns>
|
||||
public static bool IsNumeric(this string input)
|
||||
{
|
||||
if (input.Length > 0)
|
||||
{
|
||||
double tempDouble = (double)(NumberStyles.Number);
|
||||
return double.TryParse(input, out tempDouble);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static string IsNull(this string input, string value)
|
||||
{
|
||||
if (input == null)
|
||||
return value;
|
||||
else
|
||||
return input;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Conversion "
|
||||
|
||||
public static DateTime ToDateTime(this string input, DateTimeFormat format, string seperator = "/")
|
||||
{
|
||||
try
|
||||
{
|
||||
var dd = input.Split(seperator);
|
||||
switch (format)
|
||||
{
|
||||
case DateTimeFormat.Dmy:
|
||||
return new DateTime(dd[2].ToInteger().ToCeYear(), dd[1].ToInteger(), dd[0].ToInteger());
|
||||
case DateTimeFormat.Mdy:
|
||||
return new DateTime(dd[2].ToInteger().ToCeYear(), dd[0].ToInteger(), dd[1].ToInteger());
|
||||
case DateTimeFormat.Ymd:
|
||||
return new DateTime(dd[0].ToInteger().ToCeYear(), dd[1].ToInteger(), dd[2].ToInteger());
|
||||
default:
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static string ValueOrBlank(this string input)
|
||||
{
|
||||
if (input == null) return "";
|
||||
|
||||
if (input == "0")
|
||||
return "";
|
||||
else
|
||||
return input;
|
||||
}
|
||||
|
||||
public static MemoryStream ToMemoryStream(this string base64String)
|
||||
{
|
||||
if (base64String == null || base64String == String.Empty) return null;
|
||||
|
||||
var bytes = Convert.FromBase64String(base64String);
|
||||
var stream = new MemoryStream(bytes);
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
public static double ToDouble(this string input)
|
||||
{
|
||||
if (input == null) return 0.0;
|
||||
|
||||
if (input == "") return 0.0;
|
||||
|
||||
double ret = 0.0;
|
||||
input = input.RemoveComma();
|
||||
if (input.IsNumeric())
|
||||
{
|
||||
ret = double.Parse(input);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static float ToFloat(this string input)
|
||||
{
|
||||
if (input == null) return 0F;
|
||||
|
||||
if (input == "") return 0F;
|
||||
|
||||
float ret = 0F;
|
||||
input = input.RemoveComma();
|
||||
if (input.IsNumeric())
|
||||
{
|
||||
ret = float.Parse(input);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static int ToInteger(this string input)
|
||||
{
|
||||
if (input == null) return 0;
|
||||
|
||||
if (input == "") return 0;
|
||||
|
||||
int ret = 0;
|
||||
input = input.RemoveComma();
|
||||
int.TryParse(input, out ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static string ToBlank(this string value)
|
||||
{
|
||||
if (value == "0")
|
||||
return "";
|
||||
else
|
||||
return value;
|
||||
}
|
||||
|
||||
public static DateTime ToCeDateTime(this string date)
|
||||
{
|
||||
try
|
||||
{
|
||||
DateTime dddd;
|
||||
if (!DateTime.TryParse(date, out dddd))
|
||||
return DateTime.MinValue;
|
||||
|
||||
|
||||
if (date == "01/01/0001")
|
||||
return Convert.ToDateTime(date);
|
||||
|
||||
string[] dd = date.Split('/');
|
||||
|
||||
int yy = 0;
|
||||
if (dd[2].Length == 2)
|
||||
yy = dd[2].Left(2).ToInteger() + 2500;
|
||||
else
|
||||
yy = dd[2].Left(4).ToInteger();
|
||||
if (yy >= 2400) yy -= 543;
|
||||
DateTime birthdate = new DateTime(yy, dd[1].ToInteger(), dd[0].ToInteger());
|
||||
return birthdate;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
}
|
||||
127
Extensions/TimeSpanExtension.cs
Normal file
127
Extensions/TimeSpanExtension.cs
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
using System;
|
||||
|
||||
namespace BMA.EHR.Organization.Service.Extensions
|
||||
{
|
||||
public static class TimeSpanExtension
|
||||
{
|
||||
#region " To days "
|
||||
|
||||
public static double ConvertMillisecondsToDays(this double milliseconds)
|
||||
{
|
||||
return TimeSpan.FromMilliseconds(milliseconds).TotalDays;
|
||||
}
|
||||
|
||||
public static double ConvertSecondsToDays(this double seconds)
|
||||
{
|
||||
return TimeSpan.FromSeconds(seconds).TotalDays;
|
||||
}
|
||||
|
||||
public static double ConvertMinutesToDays(this double minutes)
|
||||
{
|
||||
return TimeSpan.FromMinutes(minutes).TotalDays;
|
||||
}
|
||||
|
||||
public static double ConvertHoursToDays(this double hours)
|
||||
{
|
||||
return TimeSpan.FromHours(hours).TotalDays;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " To hours "
|
||||
|
||||
public static double ConvertMillisecondsToHours(this double milliseconds)
|
||||
{
|
||||
return TimeSpan.FromMilliseconds(milliseconds).TotalHours;
|
||||
}
|
||||
|
||||
public static double ConvertSecondsToHours(this double seconds)
|
||||
{
|
||||
return TimeSpan.FromSeconds(seconds).TotalHours;
|
||||
}
|
||||
|
||||
public static double ConvertMinutesToHours(this double minutes)
|
||||
{
|
||||
return TimeSpan.FromMinutes(minutes).TotalHours;
|
||||
}
|
||||
|
||||
public static double ConvertDaysToHours(this double days)
|
||||
{
|
||||
return TimeSpan.FromHours(days).TotalHours;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " To minutes "
|
||||
|
||||
public static double ConvertMillisecondsToMinutes(this double milliseconds)
|
||||
{
|
||||
return TimeSpan.FromMilliseconds(milliseconds).TotalMinutes;
|
||||
}
|
||||
|
||||
public static double ConvertSecondsToMinutes(this double seconds)
|
||||
{
|
||||
return TimeSpan.FromSeconds(seconds).TotalMinutes;
|
||||
}
|
||||
|
||||
public static double ConvertHoursToMinutes(this double hours)
|
||||
{
|
||||
return TimeSpan.FromHours(hours).TotalMinutes;
|
||||
}
|
||||
|
||||
public static double ConvertDaysToMinutes(this double days)
|
||||
{
|
||||
return TimeSpan.FromDays(days).TotalMinutes;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " To seconds "
|
||||
|
||||
public static double ConvertMillisecondsToSeconds(this double milliseconds)
|
||||
{
|
||||
return TimeSpan.FromMilliseconds(milliseconds).TotalSeconds;
|
||||
}
|
||||
|
||||
public static double ConvertMinutesToSeconds(this double minutes)
|
||||
{
|
||||
return TimeSpan.FromMinutes(minutes).TotalSeconds;
|
||||
}
|
||||
|
||||
public static double ConvertHoursToSeconds(this double hours)
|
||||
{
|
||||
return TimeSpan.FromHours(hours).TotalSeconds;
|
||||
}
|
||||
|
||||
public static double ConvertDaysToSeconds(this double days)
|
||||
{
|
||||
return TimeSpan.FromDays(days).TotalSeconds;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " To milliseconds "
|
||||
|
||||
public static double ConvertSecondsToMilliseconds(this double seconds)
|
||||
{
|
||||
return TimeSpan.FromSeconds(seconds).TotalMilliseconds;
|
||||
}
|
||||
|
||||
public static double ConvertMinutesToMilliseconds(this double minutes)
|
||||
{
|
||||
return TimeSpan.FromMinutes(minutes).TotalMilliseconds;
|
||||
}
|
||||
|
||||
public static double ConvertHoursToMilliseconds(this double hours)
|
||||
{
|
||||
return TimeSpan.FromHours(hours).TotalMilliseconds;
|
||||
}
|
||||
|
||||
public static double ConvertDaysToMilliseconds(this double days)
|
||||
{
|
||||
return TimeSpan.FromDays(days).TotalMilliseconds;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
using Amazon.Internal;
|
||||
using BMA.EHR.Core;
|
||||
using BMA.EHR.Extensions;
|
||||
// using BMA.EHR.Core;
|
||||
using BMA.EHR.Organization.Service.Extensions;
|
||||
using BMA.EHR.Profile.Service.Services;
|
||||
using BMA.EHR.Report.Service.Data;
|
||||
|
|
@ -111,7 +110,7 @@ namespace BMA.EHR.Report.Service.Services
|
|||
.Select(x => new { x.Id, x.ParentId })
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (oc == null)
|
||||
throw new Exception(GlobalMessages.DataNotFound);
|
||||
throw new Exception("ไม่พบข้อมูลในระบบ");
|
||||
ret.Add(oc.Id);
|
||||
|
||||
var child = await _context.Organizations.AsQueryable()
|
||||
|
|
@ -143,7 +142,7 @@ namespace BMA.EHR.Report.Service.Services
|
|||
.Select(x => new { x.Id, x.ParentId, x.OrganizationOrder })
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (oc == null)
|
||||
throw new Exception(GlobalMessages.DataNotFound);
|
||||
throw new Exception("ไม่พบข้อมูลในระบบ");
|
||||
var thisLevel = level;
|
||||
//var thisLevel = oc.OrganizationOrder.Value;
|
||||
ret.Add(new OrganizationItem { Id = oc.Id, Order = thisLevel.ToString() });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue