using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Responses; using BMA.EHR.Domain.Models.MetaData; 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, string seperator = " ") { 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 + seperator + ret; } if (ret.Length > 2) ret = ret.Substring(0, ret.Length - 1); return ret; } catch { throw; } } public List GetOCWithFullPath(Guid id, bool showRoot = false) { try { var ocList = new List(); var ocData = _dbContext.Set() .Include(x => x.OrganizationOrganization) .Include(x => x.Parent) .Where(x => x.Parent != null) .Select(x => new { Id = x.Id, Name = x.OrganizationOrganization!.Name, ParentId = x.Parent == null ? Guid.Empty : 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 != Guid.Empty) { ocList.AddRange(GetOCWithFullPath(ocData.ParentId, showRoot)); } } return ocList; } catch { throw; } } private List GetOcNameFullPath(Guid id, bool showRoot = false) { try { var ocList = new List(); var oc = (from o in _dbContext.Set().Include(x => x.Parent).Include(x => x.OrganizationOrganization).Where(x => x.OrganizationOrganization != null).AsQueryable() join oc_name in _dbContext.Set().AsQueryable() on o.OrganizationOrganization.Id equals oc_name.Id where o.Parent != null select new { Id = o.Id, Name = oc_name.Name, o.IsActive, o.Parent }).FirstOrDefault(x => x.Id == id && x.IsActive); if (oc == null) return ocList; ocList.Add(oc.Name); if (oc.Parent?.Id != null) { ocList.AddRange(GetOcNameFullPath(oc.Parent.Id, showRoot)); } return ocList; } catch { throw; } } private 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; } if (ret.Length > 2) ret = ret.Substring(0, ret.Length - 1); return ret; } catch { throw; } } #endregion } }