ย้ายมาออกรายงานผ่านระบบ

This commit is contained in:
Suphonchai Phoonsawat 2023-05-10 20:11:47 +07:00
parent 4987f7c5ad
commit b817b781d2
144 changed files with 5573 additions and 63 deletions

222
Services/MinIOService.cs Normal file
View file

@ -0,0 +1,222 @@
using Amazon.S3;
using Amazon.S3.Model;
using BMA.EHR.Recruit.Service.Core;
using BMA.EHR.Recruit.Service.Models.Documents;
using BMA.EHR.Recruit.Service.Responses.Document;
using BMA.EHR.Report.Service.Data;
using Microsoft.EntityFrameworkCore;
using System.Drawing;
using System.Net.Http.Headers;
namespace BMA.EHR.Recruit.Service.Services
{
public class MinIOService
{
#region " Fields "
private readonly EHRDbContext _context;
private readonly IConfiguration _configuration;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly AmazonS3Client _s3Client;
private string _bucketName = string.Empty;
#endregion
#region " Constructors "
public MinIOService(EHRDbContext context,
IConfiguration configuration,
IWebHostEnvironment webHostEnvironment)
{
_context = context;
_configuration = configuration;
_webHostEnvironment = webHostEnvironment;
var config = new AmazonS3Config
{
ServiceURL = _configuration["MinIO:Endpoint"],
ForcePathStyle = true
};
_s3Client = new AmazonS3Client(_configuration["MinIO:AccessKey"], _configuration["MinIO:SecretKey"], config);
this._bucketName = _configuration["MinIO:BucketName"] ?? "bma-recruit";
}
#endregion
#region " Methods "
public async Task<Document> UploadFileAsync(IFormFile file, string newFileName = "")
{
var fileName = "";
var fileExt = Path.GetExtension(file.FileName);
if (newFileName != "")
fileName = $"{newFileName}";
else
fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
var tmpDir = Path.Combine(_webHostEnvironment.ContentRootPath, "tmp");
if (!Directory.Exists(tmpDir))
Directory.CreateDirectory(tmpDir);
var tmpFile = Path.Combine(tmpDir, $"tmp_{DateTime.Now.ToString("ddMMyyyyHHmmss")}{fileExt}");
try
{
using (var ms = new MemoryStream())
{
var id = Guid.NewGuid();
file.CopyTo(ms);
var fileBytes = ms.ToArray();
System.IO.MemoryStream filestream = new System.IO.MemoryStream(fileBytes);
var request = new PutObjectRequest
{
BucketName = _bucketName,
Key = id.ToString("D"),
InputStream = filestream,
ContentType = file.ContentType,
CannedACL = S3CannedACL.PublicRead
};
await _s3Client.PutObjectAsync(request);
// create document object
var doc = new Document()
{
FileName = fileName,
FileType = file.ContentType,
FileSize = Convert.ToInt32(file.Length),
ObjectRefId = id,
CreatedDate = DateTime.Now
};
await _context.Documents.AddAsync(doc);
await _context.SaveChangesAsync();
return doc;
}
}
catch
{
throw;
}
finally
{
File.Delete(tmpFile);
}
}
public async Task<FileDownloadResponse> DownloadFileAsync(Guid fileId)
{
try
{
var doc = await _context.Documents.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == fileId);
if (doc == null)
throw new Exception(GlobalMessages.FileNotFoundOnServer);
using (var memoryStream = new MemoryStream())
{
GetObjectRequest request = new GetObjectRequest
{
BucketName = _bucketName,
Key = doc.ObjectRefId.ToString("D")
};
using (GetObjectResponse response = await _s3Client.GetObjectAsync(request))
{
using (Stream responseStream = response.ResponseStream)
{
responseStream.CopyTo(memoryStream);
}
}
var fileContent = memoryStream.ToArray();
return new FileDownloadResponse
{
FileName = doc.FileName,
FileType = doc.FileType,
FileContent = fileContent
};
};
}
catch
{
throw;
}
}
public async Task DeleteFileAsync(Guid fileId)
{
try
{
var doc = await _context.Documents.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == fileId);
if (doc == null)
throw new Exception(GlobalMessages.FileNotFoundOnServer);
else
{
DeleteObjectRequest request = new DeleteObjectRequest
{
BucketName = _bucketName,
Key = doc?.ObjectRefId.ToString("D")
};
// delete from minio
await _s3Client.DeleteObjectAsync(request);
_context.Documents.Remove(doc);
await _context.SaveChangesAsync();
}
}
catch
{
throw;
}
}
public async Task<string> GetFilePath(Guid fileId)
{
var doc = await _context.Documents.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == fileId);
if (doc == null)
throw new Exception(GlobalMessages.FileNotFoundOnServer);
//var config = new AmazonS3Config
//{
// ServiceURL = Configuration.GetValue<string>("MinIO:Endpoint"),
// ForcePathStyle = true
//};
DateTime expires = DateTime.UtcNow.AddHours(6);
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
BucketName = _bucketName,
Key = doc?.ObjectRefId.ToString("D"),
Expires = expires,
};
string path = _s3Client.GetPreSignedURL(request);
return path;
}
public Image ByteArrayToImage(byte[] bytesArr)
{
using (MemoryStream memstr = new MemoryStream(bytesArr))
{
Image img = Image.FromStream(memstr);
return img;
}
}
#endregion
}
}

485
Services/ProfileService.cs Normal file
View file

@ -0,0 +1,485 @@
using BMA.EHR.Profile.Service.Models;
using BMA.EHR.Report.Service.Data;
using Microsoft.EntityFrameworkCore;
using System.Text.RegularExpressions;
namespace BMA.EHR.Profile.Service.Services
{
public class OrganizationItem
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public int Order { get; set; } = 0;
}
public class ProfileService
{
private readonly EHRDbContext _context;
public ProfileService(EHRDbContext context)
{
_context = context;
}
public string ConvertRelationshipToEng(string thai_text)
{
switch (thai_text)
{
case "หย่า":
case "หย่าร้าง": return "divorced";
case "โสด": return "single";
case "สมรส": return "married";
case "แยกกันอยู่": return "seperated";
default: return "na";
}
}
public string ConvertGenderToEng(string thai_text)
{
switch (thai_text)
{
case "ชาย": return "male";
case "หญิง": return "female";
default: return "na";
}
}
#region " Organizations "
public string FindOCFullPath(Guid id, bool showRoot = false)
{
try
{
var ocList = GetOcNameFullPath(id, showRoot);
var ret = String.Empty;
foreach (var oc in ocList)
{
ret = oc + " " + ret;
}
ret = ret.Substring(0, ret.Length - 1);
return ret;
}
catch
{
throw;
}
}
public string FindOCFullPathWithNewLine(Guid id, bool showRoot = false, string suppress = "")
{
try
{
var ocList = GetOcNameFullPath(id, showRoot);
var ret = String.Empty;
foreach (var oc in ocList)
{
if ((oc == suppress) && ocList.Count > 1) continue;
ret = $"{oc}\r\n{ret}";
}
ret = ret.Substring(0, ret.Length - 2);
return ret;
}
catch
{
throw;
}
}
public string GetOrganizationNameFullPathNewLine(Guid id, bool showRoot = false, bool descending = false)
{
try
{
var ocList = GetOCWithFullPath(id, showRoot);
if (descending)
ocList = ocList.OrderBy(x => x.Order).ToList();
var ret = String.Empty;
foreach (var oc in ocList)
{
ret = $"{oc.Name}\r\n{ret}";
}
ret = ret.Substring(0, ret.Length - 1);
return ret;
}
catch
{
throw;
}
}
public string GetOrganizationNameFullPath(Guid id, bool showRoot = false, bool descending = false)
{
try
{
var ocList = GetOCWithFullPath(id, showRoot);
if (descending)
ocList = ocList.OrderBy(x => x.Order).ToList();
var ret = String.Empty;
foreach (var oc in ocList)
{
ret = oc.Name + " " + ret;
}
ret = ret.Substring(0, ret.Length - 1);
return ret;
}
catch
{
throw;
}
}
public List<OrganizationItem> GetOCWithFullPath(Guid id, bool showRoot = false)
{
try
{
var ocList = new List<OrganizationItem>();
var organizations = _context.Organizations.ToList();
var organizationOrganizations = _context.OrganizationOrganizations.ToList();
var oc = (from o in organizations
join oc_name in organizationOrganizations on o.OrganizationOrganizationId equals oc_name.Id
select new
{
Id = o.Id,
Name = oc_name.Name,
//o.IsActive,
o.ParentId,
IsRoot = o.ParentId == null
}).FirstOrDefault(x => x.Id == id);
//ocList.Add(new OrganizationItem { Id = oc.Id, Name = oc.Name });
if (!showRoot)
{
if (!oc.IsRoot)
ocList.Add(new OrganizationItem { Id = oc.Id, Name = oc.Name });
}
else
ocList.Add(new OrganizationItem { Id = oc.Id, Name = oc.Name });
if (oc.ParentId != null)
{
ocList.AddRange(GetOCWithFullPath(oc.ParentId.Value, showRoot));
}
return ocList;
}
catch
{
throw;
}
}
public List<string> GetOcNameFullPath(Guid id, bool showRoot = false)
{
try
{
var ocList = new List<string>();
var organizations = _context.Organizations.ToList();
var organizationOrganizations = _context.OrganizationOrganizations.ToList();
var oc = (from o in organizations
join oc_name in organizationOrganizations on o.OrganizationOrganizationId equals oc_name.Id
select new
{
Id = o.Id,
Name = oc_name.Name,
//o.IsActive,
o.ParentId,
IsRoot = o.ParentId == null
}).FirstOrDefault(x => x.Id == id);
if (!showRoot)
{
if (!oc.IsRoot)
ocList.Add(oc.Name);
}
else
ocList.Add(oc.Name);
//ocList.Add(oc.Name);
if (oc.ParentId != null)
{
ocList.AddRange(GetOcNameFullPath(oc.ParentId.Value, showRoot));
}
return ocList;
}
catch
{
throw;
}
}
public List<Guid> GetAllIdByRoot(Guid id)
{
try
{
var ret = new List<Guid>();
var oc = _context.Organizations.FirstOrDefault(x => x.Id == id);
if (oc != null)
ret.Add(oc.Id);
var child = _context.Organizations.AsQueryable().Where(x => x.ParentId == id).ToList();
if (child.Any())
{
foreach (var item in child)
{
ret.AddRange(GetAllIdByRoot(item.Id));
}
}
return ret;
}
catch
{
throw;
}
}
public List<dynamic> GetOCWithChildrenByRoot(Guid id)
{
try
{
var ret = new List<dynamic>();
var organizations = _context.Organizations.ToList();
var organizationOrganizations = _context.OrganizationOrganizations.ToList();
// var oc = _context.Organizations.FirstOrDefault(x => x.Id == id && x.IsActive);
var oc = (from o in organizations
join oc_name in organizationOrganizations on o.OrganizationOrganizationId equals oc_name.Id
select new
{
Id = o.Id,
Name = oc_name.Name,
//o.IsActive,
o.ParentId,
IsRoot = o.ParentId == null
}).FirstOrDefault(x => x.Id == id);
var child = (from o in organizations
join oc_name in organizationOrganizations on o.OrganizationOrganizationId equals oc_name.Id
select new
{
Id = o.Id,
Name = oc_name.Name,
//o.IsActive,
o.ParentId,
IsRoot = o.ParentId == null
})
.Where(x => x.ParentId == id)
.ToList();
// var child = _context.Organizations.AsQueryable().Where(x => x.ParentId == id && x.IsActive).ToList();
if (child.Any())
{
var node = new
{
id = oc.Id,
label = oc.Name,
children = new List<dynamic>()
};
foreach (var item in child)
{
node.children.AddRange(GetOCWithChildrenByRoot(item.Id));
}
ret.Add(node);
}
else
{
var node = new
{
id = oc.Id,
label = oc.Name,
};
ret.Add(node);
}
return ret;
}
catch
{
throw;
}
}
public List<OrganizationEntity> GetOcByRoot(Guid id)
{
try
{
var ret = new List<OrganizationEntity>();
var oc = _context.Organizations.FirstOrDefault(x => x.Id == id);
ret.Add(oc);
var child = _context.Organizations.AsQueryable().Where(x => x.ParentId == id).ToList();
if (child.Any())
{
foreach (var item in child)
{
//ret.Add(item);
var c = _context.Organizations.AsQueryable().Where(x => x.ParentId == item.Id).ToList();
if (c.Any())
ret.AddRange(GetOcByRoot(item.Id));
else
ret.Add(item);
}
}
return ret;
}
catch
{
throw;
}
}
public string GetPositionLevel(Guid id)
{
try
{
var level = (from alv in _context.AvailablePositionLevels
join lv in _context.PositionLevels on alv.PositionLevelId equals lv.Id
where alv.PositionMasterId == id
select lv.Name).ToList();
var count = 0;
var ret = String.Empty;
foreach (var lv in level)
{
if (count != 0)
ret = $"{ret}หรือ\r\n{lv}";
else
ret += lv;
count++;
}
return ret;
}
catch
{
throw;
}
}
#endregion
#region " Profiles "
public async Task<Models.HR.Profile> GetProfileById(Guid profileId)
{
try
{
var profile = await _context.Profiles.AsQueryable()
// .Include(p => p.Organization)
.Include(p => p.Position)
// .ThenInclude(p => p.PositionPath)
// .Include(p => p.PositionNumber)
.Include(p => p.Certificates)
.Include(p => p.Disciplines)
.Include(p => p.Educations)
.Include(p => p.Honors)
.Include(p => p.Insignias)
.Include(p => p.Trainings)
// .Include(p => p.Salaries)
// .ThenInclude(s => s.SalaryPosition)
// .Include(s => s.Salaries)
// .ThenInclude(s => s.SalaryOrganization)
// .Include(p => p.Salaries)
// .ThenInclude(s => s.SalaryPositionNumber)
// .Include(p => p.Salaries)
// .ThenInclude(s => s.SalaryPositionLevel)
// .Include(p => p.Salaries)
// .ThenInclude(s => s.SalaryPositionType)
.Include(x => x.Avatar)
// .Include(x => x.PositionType)
// .Include(x => x.PositionLevel)
.Include(x => x.Childrens)
.FirstOrDefaultAsync(p => p.Id == profileId);
return profile;
}
catch
{
throw;
}
}
public bool ValidateProfileById(string profileId)
{
var idcardno = profileId;
if (string.IsNullOrEmpty(idcardno))
{
return false;
}
if (idcardno.Length != 13)
{
return false;
}
bool isDigit = Regex.IsMatch(idcardno, @"^[0-9]*$");
if (!isDigit)
{
return false;
}
int sum = 0;
for (int i = 0; i < 12; i++)
{
sum += Convert.ToInt32(idcardno.Substring(i, 1)) * (13 - i);
}
int checksum = (11 - (sum % 11)) % 10;
if (checksum != Convert.ToInt32(idcardno.Substring(12)))
{
return false;
}
return true;
}
public async Task<bool> CheckExistCitizenId(string profileId)
{
try
{
var profile = await _context.Profiles.AsQueryable().Where(p => p.CitizenId == profileId).FirstOrDefaultAsync();
return profile != null;
}
catch
{
throw;
}
}
#endregion
}
}