using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NPOI.OpenXmlFormats.Dml.Diagram;
using Shentun.Peis.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Volo.Abp.ObjectMapping;
namespace Shentun.Peis.OrganizationUnits
{
///
/// 组织架构
///
[ApiExplorerSettings(GroupName = "Work")]
[Authorize]
[Dependency(ReplaceServices = true)]
[ExposeServices(typeof(IOrganizationUnitsAppService))]
public class OrganizationUnitsAppService : ApplicationService, IOrganizationUnitsAppService
{
private readonly IOrganizationUnitRepository _organizationUnitRepository;
private readonly PeisOrganizationUnitManager _organizationUnitManager;
private readonly IdentityUserManager _identityUserManager;
private readonly IIdentityUserRepository _identityUserRepository;
//private readonly IRepository _userOgrepository;
public OrganizationUnitsAppService(
IOrganizationUnitRepository organizationUnitRepository,
PeisOrganizationUnitManager organizationUnitManager,
IdentityUserManager identityUserManager, IIdentityUserRepository identityUserRepository)
{
_organizationUnitRepository = organizationUnitRepository;
_organizationUnitManager = organizationUnitManager;
_identityUserManager = identityUserManager;
_identityUserRepository = identityUserRepository;
//_userOgrepository = userOgrepository;
}
///
///创建组织
///
///
///
public async Task CreatreAsync(OrganizationUnitsCreateDto input)
{
OrganizationUnit ent = new OrganizationUnit(Guid.NewGuid(), input.DisplayName, input.ParentId);
ent.SetProperty("IsPeis", input.IsPeis);
if (input.ParentId != null && input.ParentId != Guid.Empty && input.IsPeis == 'Y')
{
//创建子级
if (!await IsParentIsPeis(input.ParentId))
{
throw new UserFriendlyException($"上级组织已设置为体检中心,无法设置");
}
}
await _organizationUnitManager.CreateAsync(ent);
}
///
/// 删除组织
///
/// 组织ID
///
///
public async Task DeleteAsync(Guid id)
{
await _organizationUnitManager.CheckAndDeleteAsync(id);
// await _organizationUnitManager.DeleteAsync(id);
}
///
/// 根据ID获取信息
///
///
///
public async Task GetAsync(Guid Id)
{
var ent = await _organizationUnitRepository.GetAsync(Id);
var IsPeis = ent.GetProperty("IsPeis");
var entdto = ObjectMapper.Map(ent);
entdto.IsPeis = IsPeis;
return entdto;
}
///
/// 获取组织树型结构
///
///
public async Task> GetByCodeAllAsync()
{
var dataList = await _organizationUnitRepository.GetListAsync();
var items = from p in dataList
select new TreeChildViewDto()
{
Id = p.Id,
ParentId = p.ParentId,
Code = p.Code,
DisplayName = p.DisplayName
};
return GetTree(items.ToList(), 0, "");
}
///
/// 修改组织名称
///
/// ID
/// 名称
/// 是否为体检中心
///
public async Task UpdateAsync(Guid Id, string DisplayName, char IsPeis)
{
var ent = await _organizationUnitRepository.GetAsync(Id);
ent.DisplayName = DisplayName;
if (IsPeis == 'Y')
{
var datalist = await _organizationUnitRepository.GetListAsync();
var isParent = CheckParentIsPeis(datalist, ent.ParentId);
if (isParent)
{
throw new UserFriendlyException("该部门下级已经有体检中心");
}
var isChildren = CheckChildrenIsPeis(datalist, ent.Id);
if (isChildren)
{
throw new UserFriendlyException("该部门上级已经有体检中心");
}
}
ent.SetProperty("IsPeis", IsPeis);
await _organizationUnitManager.UpdateAsync(ent);
}
///
/// 检查用户是否绑定组织
///
///
///
///
public async Task IsInOrganizationUnitAsync(Guid UserId, Guid OgId)
{
return await _identityUserManager.IsInOrganizationUnitAsync(UserId, OgId);
}
///
/// 给用户绑定组织
///
///
///
public async Task AddUserOrganizationUnitAsync(SetOrganizationUnitsDto input)
{
await _identityUserManager.SetOrganizationUnitsAsync(input.UserId, input.OrgId);
// var userEnt = (await _identityUserRepository.GetListAsync())
//.Where(x => x.Id == UserId).FirstOrDefault();
// var userOgList = await _identityUserManager.GetOrganizationUnitsAsync(userEnt);
// if (userOgList.Count > 0)
// {
// for (var i = 0; i < userOgList.Count; i++)
// {
// await _identityUserManager.RemoveFromOrganizationUnitAsync(UserId, userOgList[i].Id);
// }
// }
// //SetOrganizationUnitsAsync
// await _identityUserManager.AddToOrganizationUnitAsync(UserId, OgId);
}
///
/// 根据用户ID查询组织
///
///
///
public async Task> GetOrganizationUnitByUserIdAsync(Guid UserId)
{
var userEnt = (await _identityUserRepository.GetListAsync())
.Where(x => x.Id == UserId).FirstOrDefault();
var ogEnt = await _identityUserManager.GetOrganizationUnitsAsync(userEnt);
return ObjectMapper.Map, List>(ogEnt);
}
///
/// 获取为体检中心的组织
///
///
public async Task> GetOrganizationUnitByIsPeisAsync()
{
var entlist = await _organizationUnitRepository.GetListAsync();
entlist = entlist.Where(m => m.GetProperty("IsPeis") == 'Y').ToList();
return ObjectMapper.Map, List>(entlist);
}
///
/// 获取组织树型结构 根据ID 只获取上级
///
/// 当前组织ID
///
[HttpGet("api/app/organizationunit/getbycodeparent")]
public async Task> GetByCodeParentAsync(Guid OrganizationUnitId)
{
var dataList = await _organizationUnitRepository.GetListAsync();
var items = from p in dataList
select new TreeChildViewDto()
{
Id = p.Id,
ParentId = p.ParentId,
Code = p.Code,
DisplayName = p.DisplayName
};
return GetTreeParent(items.ToList(), 0, "", OrganizationUnitId);
}
#region 私有方法
///
/// 使用Code进行递归
///
///
///
///
///
///
private List GetTreeParent(List items, int deep, string prefix, Guid OrganizationUnitId)
{
return (from p in items
where p.Code.StartsWith(prefix) && p.Code.Count(a => a == '.') == deep && p.Id != OrganizationUnitId
let subs = GetTreeParent(items, deep + 1, p.Code, OrganizationUnitId)
select new TreeChildViewDto()
{
Id = p.Id,
ParentId = p.ParentId,
Code = p.Code,
DisplayName = p.DisplayName,
TreeChildren = subs.ToList()
}
).ToList();
}
///
/// 使用Code进行递归
///
///
///
///
///
private List GetTree(List items, int deep, string prefix)
{
return (from p in items
where p.Code.StartsWith(prefix) && p.Code.Count(a => a == '.') == deep
let subs = GetTree(items, deep + 1, p.Code)
select new TreeChildViewDto()
{
Id = p.Id,
ParentId = p.ParentId,
Code = p.Code,
DisplayName = p.DisplayName,
TreeChildren = subs.ToList()
}
).ToList();
}
///
/// 检查是否可以设置为体检中心 创建时用 无下级数据
///
/// 上级数据ID
/// Ture 可以设置 False 不能设置
private async Task IsParentIsPeis(Guid? parentid)
{
var dataList = await _organizationUnitRepository.GetListAsync();
return !CheckParentIsPeis(dataList, parentid);
}
///
/// 检查上级有没有体检中心
///
///
///
/// Ture 有体检中心 False 无体检中心
private bool CheckParentIsPeis(List datalist, Guid? parentid)
{
if (parentid != null && parentid != Guid.Empty)
{
var parentEnt = datalist.FirstOrDefault(m => m.Id == parentid);
if (parentEnt != null)
{
var IsPeis = parentEnt.GetProperty("IsPeis");
if (IsPeis == 'Y')
{
return true; //上级为体检中心,不能设置为体检中心
}
else
{
return CheckParentIsPeis(datalist, parentEnt.ParentId);
}
}
else
{
return false; //无上级,可以设置为体检中心
}
}
else
{
return false; //无上级,可以设置为体检中心
}
}
///
/// 检查下级是否有体检中心
///
///
///
/// Ture 有体检中心 False 无体检中心
private bool CheckChildrenIsPeis(List datalist, Guid id)
{
List datalisttemp = new List();
GetChildren(datalist, id, datalisttemp);
datalisttemp = datalisttemp.Where(m => m.Id != id).ToList(); //剔除自身
if (datalisttemp.Any())
{
return true;
}
else
{
return false;
}
}
///
/// 递归查找下级的设置为体检中心的数据
///
///
///
///
private void GetChildren(List datalist, Guid id, List datalisttemp)
{
var ent = datalist.Find(m => m.Id == id);
if (ent != null)
{
var IsPeis = ent.GetProperty("IsPeis");
if (IsPeis == 'Y')
{
datalisttemp.Add(ent);
}
}
var entlist = datalist.Where(m => m.ParentId == id).ToList();
if (entlist.Count > 0)
{
foreach (var ents in entlist)
{
GetChildren(entlist, ents.Id, datalisttemp);
}
}
}
#endregion
}
}