You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
415 lines
14 KiB
415 lines
14 KiB
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
|
|
{
|
|
/// <summary>
|
|
/// 组织架构
|
|
/// </summary>
|
|
|
|
[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<IdentityUserOrganizationUnit> _userOgrepository;
|
|
|
|
|
|
|
|
public OrganizationUnitsAppService(
|
|
IOrganizationUnitRepository organizationUnitRepository,
|
|
PeisOrganizationUnitManager organizationUnitManager,
|
|
IdentityUserManager identityUserManager, IIdentityUserRepository identityUserRepository)
|
|
{
|
|
_organizationUnitRepository = organizationUnitRepository;
|
|
_organizationUnitManager = organizationUnitManager;
|
|
_identityUserManager = identityUserManager;
|
|
_identityUserRepository = identityUserRepository;
|
|
//_userOgrepository = userOgrepository;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
///创建组织
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
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);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除组织
|
|
/// </summary>
|
|
/// <param name="id">组织ID</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task DeleteAsync(Guid id)
|
|
{
|
|
await _organizationUnitManager.CheckAndDeleteAsync(id);
|
|
// await _organizationUnitManager.DeleteAsync(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID获取信息
|
|
/// </summary>
|
|
/// <param name="Id"></param>
|
|
/// <returns></returns>
|
|
public async Task<OrganizationUnitsDto> GetAsync(Guid Id)
|
|
{
|
|
var ent = await _organizationUnitRepository.GetAsync(Id);
|
|
var IsPeis = ent.GetProperty<char>("IsPeis");
|
|
var entdto = ObjectMapper.Map<OrganizationUnit, OrganizationUnitsDto>(ent);
|
|
entdto.IsPeis = IsPeis;
|
|
return entdto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取组织树型结构
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<TreeChildViewDto>> 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, "");
|
|
}
|
|
/// <summary>
|
|
/// 修改组织名称
|
|
/// </summary>
|
|
/// <param name="Id">ID</param>
|
|
/// <param name="DisplayName">名称</param>
|
|
/// <param name="IsPeis">是否为体检中心</param>
|
|
/// <returns></returns>
|
|
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);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 检查用户是否绑定组织
|
|
/// </summary>
|
|
/// <param name="UserId"></param>
|
|
/// <param name="OgId"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> IsInOrganizationUnitAsync(Guid UserId, Guid OgId)
|
|
{
|
|
return await _identityUserManager.IsInOrganizationUnitAsync(UserId, OgId);
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 给用户绑定组织
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
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);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 根据用户ID查询组织
|
|
/// </summary>
|
|
/// <param name="UserId"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<OrganizationUnitsDto>> GetOrganizationUnitByUserIdAsync(Guid UserId)
|
|
{
|
|
var userEnt = (await _identityUserRepository.GetListAsync())
|
|
.Where(x => x.Id == UserId).FirstOrDefault();
|
|
|
|
var ogEnt = await _identityUserManager.GetOrganizationUnitsAsync(userEnt);
|
|
|
|
return ObjectMapper.Map<List<OrganizationUnit>, List<OrganizationUnitsDto>>(ogEnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 获取为体检中心的组织
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<OrganizationUnitsDto>> GetOrganizationUnitByIsPeisAsync()
|
|
{
|
|
var entlist = await _organizationUnitRepository.GetListAsync();
|
|
entlist = entlist.Where(m => m.GetProperty<char>("IsPeis") == 'Y').ToList();
|
|
return ObjectMapper.Map<List<OrganizationUnit>, List<OrganizationUnitsDto>>(entlist);
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取组织树型结构 根据ID 只获取上级
|
|
/// </summary>
|
|
/// <param name="OrganizationUnitId">当前组织ID</param>
|
|
/// <returns></returns>
|
|
[HttpGet("api/app/organizationunit/getbycodeparent")]
|
|
public async Task<List<TreeChildViewDto>> 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 私有方法
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 使用Code进行递归
|
|
/// </summary>
|
|
/// <param name="items"></param>
|
|
/// <param name="deep"></param>
|
|
/// <param name="prefix"></param>
|
|
/// <param name="OrganizationUnitId"></param>
|
|
/// <returns></returns>
|
|
private List<TreeChildViewDto> GetTreeParent(List<TreeChildViewDto> 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();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 使用Code进行递归
|
|
/// </summary>
|
|
/// <param name="items"></param>
|
|
/// <param name="deep"></param>
|
|
/// <param name="prefix"></param>
|
|
/// <returns></returns>
|
|
private List<TreeChildViewDto> GetTree(List<TreeChildViewDto> 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();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 检查是否可以设置为体检中心 创建时用 无下级数据
|
|
/// </summary>
|
|
/// <param name="parentid">上级数据ID</param>
|
|
/// <returns>Ture 可以设置 False 不能设置</returns>
|
|
private async Task<bool> IsParentIsPeis(Guid? parentid)
|
|
{
|
|
var dataList = await _organizationUnitRepository.GetListAsync();
|
|
return !CheckParentIsPeis(dataList, parentid);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 检查上级有没有体检中心
|
|
/// </summary>
|
|
/// <param name="datalist"></param>
|
|
/// <param name="parentid"></param>
|
|
/// <returns>Ture 有体检中心 False 无体检中心</returns>
|
|
private bool CheckParentIsPeis(List<OrganizationUnit> datalist, Guid? parentid)
|
|
{
|
|
if (parentid != null && parentid != Guid.Empty)
|
|
{
|
|
var parentEnt = datalist.FirstOrDefault(m => m.Id == parentid);
|
|
if (parentEnt != null)
|
|
{
|
|
var IsPeis = parentEnt.GetProperty<char>("IsPeis");
|
|
if (IsPeis == 'Y')
|
|
{
|
|
return true; //上级为体检中心,不能设置为体检中心
|
|
}
|
|
else
|
|
{
|
|
return CheckParentIsPeis(datalist, parentEnt.ParentId);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return false; //无上级,可以设置为体检中心
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return false; //无上级,可以设置为体检中心
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查下级是否有体检中心
|
|
/// </summary>
|
|
/// <param name="datalist"></param>
|
|
/// <param name="id"></param>
|
|
/// <returns>Ture 有体检中心 False 无体检中心</returns>
|
|
private bool CheckChildrenIsPeis(List<OrganizationUnit> datalist, Guid id)
|
|
{
|
|
List<OrganizationUnit> datalisttemp = new List<OrganizationUnit>();
|
|
|
|
GetChildren(datalist, id, datalisttemp);
|
|
|
|
datalisttemp = datalisttemp.Where(m => m.Id != id).ToList(); //剔除自身
|
|
|
|
if (datalisttemp.Any())
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 递归查找下级的设置为体检中心的数据
|
|
/// </summary>
|
|
/// <param name="datalist"></param>
|
|
/// <param name="id"></param>
|
|
/// <param name="datalisttemp"></param>
|
|
private void GetChildren(List<OrganizationUnit> datalist, Guid id, List<OrganizationUnit> datalisttemp)
|
|
{
|
|
var ent = datalist.Find(m => m.Id == id);
|
|
if (ent != null)
|
|
{
|
|
var IsPeis = ent.GetProperty<char>("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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|