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

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using NPOI.OpenXmlFormats.Dml.Diagram;
  4. using Shentun.Peis.Models;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using Volo.Abp;
  10. using Volo.Abp.Application.Services;
  11. using Volo.Abp.Data;
  12. using Volo.Abp.DependencyInjection;
  13. using Volo.Abp.Domain.Repositories;
  14. using Volo.Abp.Identity;
  15. using Volo.Abp.ObjectMapping;
  16. namespace Shentun.Peis.OrganizationUnits
  17. {
  18. /// <summary>
  19. /// 组织架构
  20. /// </summary>
  21. [ApiExplorerSettings(GroupName = "Work")]
  22. [Authorize]
  23. [Dependency(ReplaceServices = true)]
  24. [ExposeServices(typeof(IOrganizationUnitsAppService))]
  25. public class OrganizationUnitsAppService : ApplicationService, IOrganizationUnitsAppService
  26. {
  27. private readonly IOrganizationUnitRepository _organizationUnitRepository;
  28. private readonly PeisOrganizationUnitManager _organizationUnitManager;
  29. private readonly IdentityUserManager _identityUserManager;
  30. private readonly IIdentityUserRepository _identityUserRepository;
  31. //private readonly IRepository<IdentityUserOrganizationUnit> _userOgrepository;
  32. public OrganizationUnitsAppService(
  33. IOrganizationUnitRepository organizationUnitRepository,
  34. PeisOrganizationUnitManager organizationUnitManager,
  35. IdentityUserManager identityUserManager, IIdentityUserRepository identityUserRepository)
  36. {
  37. _organizationUnitRepository = organizationUnitRepository;
  38. _organizationUnitManager = organizationUnitManager;
  39. _identityUserManager = identityUserManager;
  40. _identityUserRepository = identityUserRepository;
  41. //_userOgrepository = userOgrepository;
  42. }
  43. /// <summary>
  44. ///创建组织
  45. /// </summary>
  46. /// <param name="input"></param>
  47. /// <returns></returns>
  48. public async Task CreatreAsync(OrganizationUnitsCreateDto input)
  49. {
  50. OrganizationUnit ent = new OrganizationUnit(Guid.NewGuid(), input.DisplayName, input.ParentId);
  51. ent.SetProperty("IsPeis", input.IsPeis);
  52. if (input.ParentId != null && input.ParentId != Guid.Empty && input.IsPeis == 'Y')
  53. {
  54. //创建子级
  55. if (!await IsParentIsPeis(input.ParentId))
  56. {
  57. throw new UserFriendlyException($"上级组织已设置为体检中心,无法设置");
  58. }
  59. }
  60. await _organizationUnitManager.CreateAsync(ent);
  61. }
  62. /// <summary>
  63. /// 删除组织
  64. /// </summary>
  65. /// <param name="id">组织ID</param>
  66. /// <returns></returns>
  67. /// <exception cref="NotImplementedException"></exception>
  68. public async Task DeleteAsync(Guid id)
  69. {
  70. await _organizationUnitManager.CheckAndDeleteAsync(id);
  71. // await _organizationUnitManager.DeleteAsync(id);
  72. }
  73. /// <summary>
  74. /// 根据ID获取信息
  75. /// </summary>
  76. /// <param name="Id"></param>
  77. /// <returns></returns>
  78. public async Task<OrganizationUnitsDto> GetAsync(Guid Id)
  79. {
  80. var ent = await _organizationUnitRepository.GetAsync(Id);
  81. var IsPeis = ent.GetProperty<char>("IsPeis");
  82. var entdto = ObjectMapper.Map<OrganizationUnit, OrganizationUnitsDto>(ent);
  83. entdto.IsPeis = IsPeis;
  84. return entdto;
  85. }
  86. /// <summary>
  87. /// 获取组织树型结构
  88. /// </summary>
  89. /// <returns></returns>
  90. public async Task<List<TreeChildViewDto>> GetByCodeAllAsync()
  91. {
  92. var dataList = await _organizationUnitRepository.GetListAsync();
  93. var items = from p in dataList
  94. select new TreeChildViewDto()
  95. {
  96. Id = p.Id,
  97. ParentId = p.ParentId,
  98. Code = p.Code,
  99. DisplayName = p.DisplayName
  100. };
  101. return GetTree(items.ToList(), 0, "");
  102. }
  103. /// <summary>
  104. /// 修改组织名称
  105. /// </summary>
  106. /// <param name="Id">ID</param>
  107. /// <param name="DisplayName">名称</param>
  108. /// <param name="IsPeis">是否为体检中心</param>
  109. /// <returns></returns>
  110. public async Task UpdateAsync(Guid Id, string DisplayName, char IsPeis)
  111. {
  112. var ent = await _organizationUnitRepository.GetAsync(Id);
  113. ent.DisplayName = DisplayName;
  114. if (IsPeis == 'Y')
  115. {
  116. var datalist = await _organizationUnitRepository.GetListAsync();
  117. var isParent = CheckParentIsPeis(datalist, ent.ParentId);
  118. if (isParent)
  119. {
  120. throw new UserFriendlyException("该部门下级已经有体检中心");
  121. }
  122. var isChildren = CheckChildrenIsPeis(datalist, ent.Id);
  123. if (isChildren)
  124. {
  125. throw new UserFriendlyException("该部门上级已经有体检中心");
  126. }
  127. }
  128. ent.SetProperty("IsPeis", IsPeis);
  129. await _organizationUnitManager.UpdateAsync(ent);
  130. }
  131. /// <summary>
  132. /// 检查用户是否绑定组织
  133. /// </summary>
  134. /// <param name="UserId"></param>
  135. /// <param name="OgId"></param>
  136. /// <returns></returns>
  137. public async Task<bool> IsInOrganizationUnitAsync(Guid UserId, Guid OgId)
  138. {
  139. return await _identityUserManager.IsInOrganizationUnitAsync(UserId, OgId);
  140. }
  141. /// <summary>
  142. /// 给用户绑定组织
  143. /// </summary>
  144. /// <param name="input"></param>
  145. /// <returns></returns>
  146. public async Task AddUserOrganizationUnitAsync(SetOrganizationUnitsDto input)
  147. {
  148. await _identityUserManager.SetOrganizationUnitsAsync(input.UserId, input.OrgId);
  149. // var userEnt = (await _identityUserRepository.GetListAsync())
  150. //.Where(x => x.Id == UserId).FirstOrDefault();
  151. // var userOgList = await _identityUserManager.GetOrganizationUnitsAsync(userEnt);
  152. // if (userOgList.Count > 0)
  153. // {
  154. // for (var i = 0; i < userOgList.Count; i++)
  155. // {
  156. // await _identityUserManager.RemoveFromOrganizationUnitAsync(UserId, userOgList[i].Id);
  157. // }
  158. // }
  159. // //SetOrganizationUnitsAsync
  160. // await _identityUserManager.AddToOrganizationUnitAsync(UserId, OgId);
  161. }
  162. /// <summary>
  163. /// 根据用户ID查询组织
  164. /// </summary>
  165. /// <param name="UserId"></param>
  166. /// <returns></returns>
  167. public async Task<List<OrganizationUnitsDto>> GetOrganizationUnitByUserIdAsync(Guid UserId)
  168. {
  169. var userEnt = (await _identityUserRepository.GetListAsync())
  170. .Where(x => x.Id == UserId).FirstOrDefault();
  171. var ogEnt = await _identityUserManager.GetOrganizationUnitsAsync(userEnt);
  172. return ObjectMapper.Map<List<OrganizationUnit>, List<OrganizationUnitsDto>>(ogEnt);
  173. }
  174. /// <summary>
  175. /// 获取为体检中心的组织
  176. /// </summary>
  177. /// <returns></returns>
  178. public async Task<List<OrganizationUnitsDto>> GetOrganizationUnitByIsPeisAsync()
  179. {
  180. var entlist = await _organizationUnitRepository.GetListAsync();
  181. entlist = entlist.Where(m => m.GetProperty<char>("IsPeis") == 'Y').ToList();
  182. return ObjectMapper.Map<List<OrganizationUnit>, List<OrganizationUnitsDto>>(entlist);
  183. }
  184. /// <summary>
  185. /// 获取组织树型结构 根据ID 只获取上级
  186. /// </summary>
  187. /// <param name="OrganizationUnitId">当前组织ID</param>
  188. /// <returns></returns>
  189. [HttpGet("api/app/organizationunit/getbycodeparent")]
  190. public async Task<List<TreeChildViewDto>> GetByCodeParentAsync(Guid OrganizationUnitId)
  191. {
  192. var dataList = await _organizationUnitRepository.GetListAsync();
  193. var items = from p in dataList
  194. select new TreeChildViewDto()
  195. {
  196. Id = p.Id,
  197. ParentId = p.ParentId,
  198. Code = p.Code,
  199. DisplayName = p.DisplayName
  200. };
  201. return GetTreeParent(items.ToList(), 0, "", OrganizationUnitId);
  202. }
  203. #region 私有方法
  204. /// <summary>
  205. /// 使用Code进行递归
  206. /// </summary>
  207. /// <param name="items"></param>
  208. /// <param name="deep"></param>
  209. /// <param name="prefix"></param>
  210. /// <param name="OrganizationUnitId"></param>
  211. /// <returns></returns>
  212. private List<TreeChildViewDto> GetTreeParent(List<TreeChildViewDto> items, int deep, string prefix, Guid OrganizationUnitId)
  213. {
  214. return (from p in items
  215. where p.Code.StartsWith(prefix) && p.Code.Count(a => a == '.') == deep && p.Id != OrganizationUnitId
  216. let subs = GetTreeParent(items, deep + 1, p.Code, OrganizationUnitId)
  217. select new TreeChildViewDto()
  218. {
  219. Id = p.Id,
  220. ParentId = p.ParentId,
  221. Code = p.Code,
  222. DisplayName = p.DisplayName,
  223. TreeChildren = subs.ToList()
  224. }
  225. ).ToList();
  226. }
  227. /// <summary>
  228. /// 使用Code进行递归
  229. /// </summary>
  230. /// <param name="items"></param>
  231. /// <param name="deep"></param>
  232. /// <param name="prefix"></param>
  233. /// <returns></returns>
  234. private List<TreeChildViewDto> GetTree(List<TreeChildViewDto> items, int deep, string prefix)
  235. {
  236. return (from p in items
  237. where p.Code.StartsWith(prefix) && p.Code.Count(a => a == '.') == deep
  238. let subs = GetTree(items, deep + 1, p.Code)
  239. select new TreeChildViewDto()
  240. {
  241. Id = p.Id,
  242. ParentId = p.ParentId,
  243. Code = p.Code,
  244. DisplayName = p.DisplayName,
  245. TreeChildren = subs.ToList()
  246. }
  247. ).ToList();
  248. }
  249. /// <summary>
  250. /// 检查是否可以设置为体检中心 创建时用 无下级数据
  251. /// </summary>
  252. /// <param name="parentid">上级数据ID</param>
  253. /// <returns>Ture 可以设置 False 不能设置</returns>
  254. private async Task<bool> IsParentIsPeis(Guid? parentid)
  255. {
  256. var dataList = await _organizationUnitRepository.GetListAsync();
  257. return !CheckParentIsPeis(dataList, parentid);
  258. }
  259. /// <summary>
  260. /// 检查上级有没有体检中心
  261. /// </summary>
  262. /// <param name="datalist"></param>
  263. /// <param name="parentid"></param>
  264. /// <returns>Ture 有体检中心 False 无体检中心</returns>
  265. private bool CheckParentIsPeis(List<OrganizationUnit> datalist, Guid? parentid)
  266. {
  267. if (parentid != null && parentid != Guid.Empty)
  268. {
  269. var parentEnt = datalist.FirstOrDefault(m => m.Id == parentid);
  270. if (parentEnt != null)
  271. {
  272. var IsPeis = parentEnt.GetProperty<char>("IsPeis");
  273. if (IsPeis == 'Y')
  274. {
  275. return true; //上级为体检中心,不能设置为体检中心
  276. }
  277. else
  278. {
  279. return CheckParentIsPeis(datalist, parentEnt.ParentId);
  280. }
  281. }
  282. else
  283. {
  284. return false; //无上级,可以设置为体检中心
  285. }
  286. }
  287. else
  288. {
  289. return false; //无上级,可以设置为体检中心
  290. }
  291. }
  292. /// <summary>
  293. /// 检查下级是否有体检中心
  294. /// </summary>
  295. /// <param name="datalist"></param>
  296. /// <param name="id"></param>
  297. /// <returns>Ture 有体检中心 False 无体检中心</returns>
  298. private bool CheckChildrenIsPeis(List<OrganizationUnit> datalist, Guid id)
  299. {
  300. List<OrganizationUnit> datalisttemp = new List<OrganizationUnit>();
  301. GetChildren(datalist, id, datalisttemp);
  302. datalisttemp = datalisttemp.Where(m => m.Id != id).ToList(); //剔除自身
  303. if (datalisttemp.Any())
  304. {
  305. return true;
  306. }
  307. else
  308. {
  309. return false;
  310. }
  311. }
  312. /// <summary>
  313. /// 递归查找下级的设置为体检中心的数据
  314. /// </summary>
  315. /// <param name="datalist"></param>
  316. /// <param name="id"></param>
  317. /// <param name="datalisttemp"></param>
  318. private void GetChildren(List<OrganizationUnit> datalist, Guid id, List<OrganizationUnit> datalisttemp)
  319. {
  320. var ent = datalist.Find(m => m.Id == id);
  321. if (ent != null)
  322. {
  323. var IsPeis = ent.GetProperty<char>("IsPeis");
  324. if (IsPeis == 'Y')
  325. {
  326. datalisttemp.Add(ent);
  327. }
  328. }
  329. var entlist = datalist.Where(m => m.ParentId == id).ToList();
  330. if (entlist.Count > 0)
  331. {
  332. foreach (var ents in entlist)
  333. {
  334. GetChildren(entlist, ents.Id, datalisttemp);
  335. }
  336. }
  337. }
  338. #endregion
  339. }
  340. }