hrms-api-backend/BMA.EHR.Application/Repositories/OrganizationCommonRepository.cs
Suphonchai Phoonsawat c99cb5344e fix bug upload
api ออกคำสั่ง
ส่ง inbox and noti
2023-08-08 15:05:53 +07:00

102 lines
3.1 KiB
C#

using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Responses;
using BMA.EHR.Domain.Models.Organizations;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
namespace BMA.EHR.Application.Repositories
{
public class OrganizationCommonRepository
{
#region " Fields "
private readonly IApplicationDBContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor;
#endregion
#region " Constructor and Destuctor "
public OrganizationCommonRepository(IApplicationDBContext dbContext,
IHttpContextAccessor httpContextAccessor)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
}
#endregion
#region " Methods "
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;
}
if (ret.Length > 2)
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 ocData = _dbContext.Set<OrganizationEntity>()
.Include(x => x.OrganizationOrganization)
.Include(x => x.Parent)
.Select(x => new
{
Id = x.Id,
Name = x.OrganizationOrganization!.Name,
ParentId = x.Parent!.Id
})
.FirstOrDefault(x => x.Id == id);
if (ocData != null)
{
ocList.Add(new OrganizationItem { Id = ocData.Id, Name = ocData.Name });
//if (!showRoot)
//{
// if (!oc.IsRoot)
// ocList.Add(new OrganizationItem { Id = oc.OCId, Name = oc.OrganizationName });
//}
//else
// ocList.Add(new OrganizationItem { Id = oc.OCId, Name = oc.OrganizationName });
if (ocData.ParentId != null)
{
ocList.AddRange(GetOCWithFullPath(ocData.ParentId, showRoot));
}
}
return ocList;
}
catch
{
throw;
}
}
#endregion
}
}