513 lines
12 KiB
C#
513 lines
12 KiB
C#
using Amazon.S3.Model.Internal.MarshallTransformations;
|
|
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 GetOrganizationName(Guid id)
|
|
{
|
|
try
|
|
{
|
|
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);
|
|
|
|
return oc == null ? "" : oc.Name;
|
|
}
|
|
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
|
|
}
|
|
}
|