平安校园
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.

SysOrgService.cs 25 KiB

4 月之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. 
  2. //
  3. using System.Diagnostics;
  4. namespace SafeCampus.System;
  5. /// <inheritdoc cref="ISysOrgService"/>
  6. public class SysOrgService : DbRepository<SysOrg>, ISysOrgService
  7. {
  8. private readonly ILogger<SysOrgService> _logger;
  9. private readonly ISimpleCacheService _simpleCacheService;
  10. private readonly IEventPublisher _eventPublisher;
  11. public SysOrgService(ILogger<SysOrgService> logger, ISimpleCacheService simpleCacheService, IEventPublisher eventPublisher)
  12. {
  13. _logger = logger;
  14. _simpleCacheService = simpleCacheService;
  15. _eventPublisher = eventPublisher;
  16. }
  17. #region 查询
  18. /// <inheritdoc />
  19. public async Task<List<SysOrg>> GetListAsync(bool showDisabled = true)
  20. {
  21. var sysOrgList = _simpleCacheService.Get<List<SysOrg>>(SystemConst.CACHE_SYS_ORG);
  22. if (sysOrgList == null)
  23. {
  24. //redis没有就去数据库拿
  25. sysOrgList = await base.GetListAsync();
  26. if (sysOrgList.Count > 0)
  27. {
  28. //插入Redis
  29. _simpleCacheService.Set(SystemConst.CACHE_SYS_ORG, sysOrgList);
  30. }
  31. }
  32. if (!showDisabled)
  33. {
  34. sysOrgList = sysOrgList.Where(it => it.Status == CommonStatusConst.ENABLE).ToList();
  35. }
  36. return sysOrgList;
  37. }
  38. /// <inheritdoc />
  39. public async Task<SysOrg> GetSysOrgById(long id)
  40. {
  41. var sysOrg = await GetListAsync();
  42. var result = sysOrg.Where(it => it.Id == id).FirstOrDefault();
  43. return result;
  44. }
  45. /// <inheritdoc />
  46. public async Task<List<SysOrg>> GetChildListById(long orgId, bool isContainOneself = true, List<SysOrg> sysOrgList = null)
  47. {
  48. //获取所有组织
  49. sysOrgList ??= await GetListAsync();
  50. //查找下级
  51. var childList = GetSysOrgChildren(sysOrgList, orgId);
  52. if (isContainOneself)//如果包含自己
  53. {
  54. //获取自己的组织信息
  55. var self = sysOrgList.Where(it => it.Id == orgId).FirstOrDefault();
  56. if (self != null) childList.Insert(0, self);//如果组织不为空就插到第一个
  57. }
  58. return childList;
  59. }
  60. /// <inheritdoc />
  61. public async Task<List<long>> GetOrgChildIds(long orgId, bool isContainOneself = true, List<SysOrg> sysOrgList = null)
  62. {
  63. var orgIds = new List<long>();//组织列表
  64. if (orgId > 0)//如果orgId有值
  65. {
  66. //获取所有子集
  67. var childList = await GetChildListById(orgId, isContainOneself, sysOrgList);
  68. orgIds = childList.Select(x => x.Id).ToList();//提取ID列表
  69. }
  70. return orgIds;
  71. }
  72. /// <inheritdoc/>
  73. public async Task<SqlSugarPagedList<SysOrg>> Page(SysOrgPageInput input)
  74. {
  75. var query = Context.Queryable<SysOrg>()
  76. .WhereIF(input.ParentId > 0,
  77. it => it.ParentId == input.ParentId || it.Id == input.ParentId || SqlFunc.JsonLike(it.ParentIdList, input.ParentId.ToString()))//父级
  78. .WhereIF(!string.IsNullOrEmpty(input.Name), it => it.Name.Contains(input.Name))//根据名称查询
  79. .WhereIF(!string.IsNullOrEmpty(input.Category), it => it.Category == input.Category)//根据分类查询
  80. .WhereIF(!string.IsNullOrEmpty(input.Code), it => it.Code.Contains(input.Code))//根据编码查询
  81. .WhereIF(!string.IsNullOrEmpty(input.Status), it => it.Status == input.Status)//根据状态查询
  82. .OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}").OrderBy(it => it.SortCode)
  83. .OrderBy(it => it.CreateTime);//排序
  84. var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
  85. return pageInfo;
  86. }
  87. /// <inheritdoc />
  88. public async Task<List<SysOrg>> Tree(List<long> orgIds = null, SysOrgTreeInput treeInput = null)
  89. {
  90. long parentId = SafeCampusConst.ZERO;//父级ID
  91. //获取所有组织
  92. var sysOrgList = await GetListAsync(false);
  93. if (orgIds != null)
  94. sysOrgList = GetParentListByIds(sysOrgList, orgIds);//如果组织ID不为空则获取组织ID列表的所有父节点
  95. //如果选择器ID不为空则表示是懒加载,只加载子节点
  96. if (treeInput != null && treeInput.ParentId != null)
  97. {
  98. parentId = treeInput.ParentId.Value;
  99. sysOrgList = GetSysOrgChildrenLazy(sysOrgList, treeInput.ParentId.Value);//获取懒加载下级
  100. }
  101. sysOrgList = sysOrgList.OrderBy(it => it.SortCode).ToList();//排序
  102. //构建组织树
  103. var result = ConstructOrgTrees(sysOrgList, parentId);
  104. return result;
  105. }
  106. /// <inheritdoc />
  107. public async Task<SysOrg> Detail(BaseIdInput input)
  108. {
  109. var sysOrgList = await GetListAsync();
  110. var orgDetail = sysOrgList.Where(it => it.Id == input.Id).FirstOrDefault();
  111. if (orgDetail.DirectorId != null)
  112. {
  113. //获取主管信息
  114. orgDetail.DirectorInfo = await Tenant.QueryableWithAttr<SysUser>().Where(it => it.Id == orgDetail.DirectorId).Select(it =>
  115. new UserSelectorOutPut
  116. {
  117. Id = it.Id,
  118. Name = it.Name,
  119. Account = it.Account
  120. }).FirstAsync();
  121. }
  122. return orgDetail;
  123. }
  124. /// <inheritdoc />
  125. public List<SysOrg> GetOrgParents(List<SysOrg> allOrgList, long orgId, bool includeSelf = true)
  126. {
  127. //找到组织
  128. var sysOrgList = allOrgList.Where(it => it.Id == orgId).FirstOrDefault();
  129. if (sysOrgList != null)//如果组织不为空
  130. {
  131. var data = new List<SysOrg>();
  132. var parents = GetOrgParents(allOrgList, sysOrgList.ParentId, includeSelf);//递归获取父节点
  133. data.AddRange(parents);//添加父节点;
  134. if (includeSelf)
  135. data.Add(sysOrgList);//添加到列表
  136. return data;//返回结果
  137. }
  138. return new List<SysOrg>();
  139. }
  140. /// <inheritdoc />
  141. public bool IsExistOrgByName(List<SysOrg> sysOrgList, string orgName, long parentId,
  142. out long orgId)
  143. {
  144. orgId = 0;
  145. var sysOrg = sysOrgList.Where(it => it.ParentId == parentId && it.Name == orgName).FirstOrDefault();
  146. if (sysOrg != null)
  147. {
  148. orgId = sysOrg.Id;
  149. return true;
  150. }
  151. return false;
  152. }
  153. /// <inheritdoc />
  154. public async Task<List<SysOrg>> GetOrgListByIdList(IdListInput input)
  155. {
  156. var sysOrgList = await GetListAsync();
  157. var orgList = sysOrgList.Where(it => input.IdList.Contains(it.Id)).ToList();// 获取指定ID的组织列表
  158. return orgList;
  159. }
  160. /// <inheritdoc />
  161. public async Task<List<SysOrg>> GetTenantList()
  162. {
  163. var tenantList = _simpleCacheService.Get<List<SysOrg>>(SystemConst.CACHE_SYS_TENANT);
  164. if (tenantList == null)
  165. {
  166. var orgList = await GetListAsync(false);
  167. tenantList = orgList.Where(it => it.Category == CateGoryConst.ORG_COMPANY).ToList();
  168. if (tenantList.Count > 0)
  169. {
  170. //插入Redis
  171. _simpleCacheService.Set(SystemConst.CACHE_SYS_TENANT, tenantList);
  172. }
  173. }
  174. return tenantList;
  175. }
  176. /// <inheritdoc />
  177. public async Task<long?> GetTenantIdByOrgId(long orgId, List<SysOrg> sysOrgList = null)
  178. {
  179. //先从缓存拿租户Id
  180. var tenantId = _simpleCacheService.HashGetOne<long?>(SystemConst.CACHE_SYS_ORG_TENANT, orgId.ToString());
  181. if (tenantId == null)
  182. {
  183. //获取所有组织
  184. sysOrgList ??= await GetListAsync();
  185. var userOrg = sysOrgList.FirstOrDefault(it => it.Id == orgId);
  186. if (userOrg != null)
  187. {
  188. //如果是公司直接返回
  189. if (userOrg.Category == CateGoryConst.ORG_COMPANY)
  190. {
  191. tenantId = userOrg.Id;
  192. }
  193. else
  194. {
  195. var parentIds = userOrg.ParentIdList;//获取父级ID列表
  196. //从最后一个往前遍历,取第一个公司ID为租户ID
  197. for (var i = parentIds.Count - 1; i >= 0; i--)
  198. {
  199. var parentId = parentIds[i];
  200. var org = sysOrgList.FirstOrDefault(it => it.Id == parentId);
  201. if (org.Category == CateGoryConst.ORG_COMPANY)
  202. {
  203. tenantId = org.Id;//租户ID
  204. break;
  205. }
  206. }
  207. }
  208. if (tenantId != null)
  209. _simpleCacheService.HashAdd(SystemConst.CACHE_SYS_ORG_TENANT, orgId.ToString(), tenantId);//插入缓存
  210. }
  211. }
  212. return tenantId;
  213. }
  214. #endregion 查询
  215. #region 新增
  216. /// <inheritdoc />
  217. public async Task Add(SysOrgAddInput input, string name = SystemConst.SYS_ORG)
  218. {
  219. await CheckInput(input, name);//检查参数
  220. var sysOrg = input.Adapt<SysOrg>();//实体转换
  221. if (await InsertAsync(sysOrg))//插入数据
  222. await RefreshCache();//刷新缓存
  223. }
  224. /// <inheritdoc />
  225. public async Task Copy(SysOrgCopyInput input)
  226. {
  227. var orgList = await GetListAsync();//获取所有
  228. var positionList = await Context.Queryable<SysPosition>().ToListAsync();//获取所有职位
  229. var ids = new HashSet<long>();//定义不重复Id集合
  230. var addOrgList = new List<SysOrg>();//添加机构列表
  231. var addPositionList = new List<SysPosition>();//添加职位列表
  232. var alreadyIds = new HashSet<long>();//定义已经复制过得组织Id
  233. ids.AddRange(input.Ids);//加到集合
  234. if (ids.Contains(input.TargetId))
  235. throw Oops.Bah("不能包含自己");
  236. //获取目标组织
  237. var target = orgList.Where(it => it.Id == input.TargetId).FirstOrDefault();
  238. if (target != null)
  239. {
  240. //需要复制的组织名称列表
  241. var orgNames = orgList.Where(it => ids.Contains(it.Id)).Select(it => it.Name).ToList();
  242. //目标组织的一级子组织名称列表
  243. var targetChildNames = orgList.Where(it => it.ParentId == input.TargetId).Select(it => it.Name).ToList();
  244. orgNames.ForEach(it =>
  245. {
  246. if (targetChildNames.Contains(it)) throw Oops.Bah($"已存在{it}");
  247. });
  248. foreach (var id in input.Ids)
  249. {
  250. var org = orgList.Where(o => o.Id == id).FirstOrDefault();//获取组织
  251. if (org != null && !alreadyIds.Contains(id))
  252. {
  253. alreadyIds.Add(id);//添加到已复制列表
  254. RedirectOrg(org);//生成新的实体
  255. org.ParentId = input.TargetId;//父id为目标Id
  256. addOrgList.Add(org);
  257. //是否包含职位
  258. if (input.ContainsPosition)
  259. {
  260. var positions = positionList.Where(p => p.OrgId == id).ToList();//获取机构下的职位
  261. positions.ForEach(p =>
  262. {
  263. p.OrgId = org.Id;//赋值新的机构ID
  264. p.Id = CommonUtils.GetSingleId();//生成新的ID
  265. p.Code = RandomHelper.CreateRandomString(10);//生成新的Code
  266. addPositionList.Add(p);//添加到职位列表
  267. });
  268. }
  269. //是否包含下级
  270. if (input.ContainsChild)
  271. {
  272. var childIds = await GetOrgChildIds(id, false);//获取下级id列表
  273. alreadyIds.AddRange(childIds);//添加到已复制id
  274. var childList = orgList.Where(c => childIds.Contains(c.Id)).ToList();//获取下级
  275. var sysOrgChildren = CopySysOrgChildren(childList, id, org.Id, input.ContainsPosition,
  276. positionList);//赋值下级组织
  277. addOrgList.AddRange(sysOrgChildren.Item1);//添加到组织列表
  278. addPositionList.AddRange(sysOrgChildren.Item2);//添加到职位列表
  279. }
  280. }
  281. }
  282. orgList.AddRange(addOrgList);//要添加的组织添加到组织列表
  283. //遍历机构重新赋值全称和父Id列表
  284. addOrgList.ForEach(it =>
  285. {
  286. it.Names = it.Name;
  287. if (it.ParentId != SafeCampusConst.ZERO)
  288. {
  289. GetNames(orgList, it.ParentId, it.Name, out var names,
  290. out var parentIdList);
  291. it.Names = names;
  292. it.ParentIdList = parentIdList;
  293. }
  294. });
  295. //事务
  296. var result = await Tenant.UseTranAsync(async () =>
  297. {
  298. await InsertRangeAsync(addOrgList);//插入组织
  299. if (addPositionList.Count > 0)
  300. {
  301. await Context.Insertable(addPositionList).ExecuteCommandAsync();
  302. }
  303. });
  304. if (result.IsSuccess)//如果成功了
  305. {
  306. await RefreshCache();//刷新缓存
  307. if (addPositionList.Count > 0) _simpleCacheService.Remove(SystemConst.CACHE_SYS_POSITION);//删除机构缓存
  308. }
  309. else
  310. {
  311. _logger.LogError(result.ErrorException, result.ErrorMessage);
  312. throw Oops.Oh($"复制失败");
  313. }
  314. }
  315. }
  316. #endregion 新增
  317. #region 编辑
  318. /// <inheritdoc />
  319. public async Task Edit(SysOrgEditInput input, string name = SystemConst.SYS_ORG)
  320. {
  321. await CheckInput(input, name);//检查参数
  322. var sysOrg = input.Adapt<SysOrg>();//实体转换
  323. if (await UpdateAsync(sysOrg))//更新数据
  324. {
  325. //如果状态为禁用
  326. if (sysOrg.Status == CommonStatusConst.DISABLED)
  327. {
  328. var orgIds = await GetOrgChildIds(sysOrg.Id, true);//获取所有下级
  329. await _eventPublisher.PublishAsync(EventSubscriberConst.CLEAR_ORG_TOKEN, orgIds);//清除角色下用户缓存
  330. }
  331. await RefreshCache();//刷新缓存
  332. }
  333. }
  334. #endregion 编辑
  335. #region 删除
  336. /// <inheritdoc />
  337. public async Task Delete(BaseIdListInput input, string name = SystemConst.SYS_ORG)
  338. {
  339. //获取所有ID
  340. var ids = input.Ids;
  341. if (ids.Count > 0)
  342. {
  343. var sysOrgList = await GetListAsync();//获取所有组织
  344. var sysDeleteOrgList = new List<long>();//需要删除的组织ID集合
  345. ids.ForEach(it =>
  346. {
  347. var children = GetSysOrgChildren(sysOrgList, it);//查找下级组织
  348. sysDeleteOrgList.AddRange(children.Select(it => it.Id).ToList());
  349. sysDeleteOrgList.Add(it);
  350. });
  351. //如果组织下有用户则不能删除
  352. if (await Context.Queryable<SysUser>().AnyAsync(it => sysDeleteOrgList.Contains(it.OrgId)))
  353. {
  354. throw Oops.Bah($"请先删除{name}下的用户");
  355. }
  356. //判断组织下是否有角色
  357. var hasRole = await Context.Queryable<SysRole>().Where(it => sysDeleteOrgList.Contains(it.OrgId.Value)).CountAsync() > 0;
  358. if (hasRole) throw Oops.Bah($"请先删除{name}下的角色");
  359. // 判断组织下是否有职位
  360. var hasPosition = await Context.Queryable<SysPosition>().Where(it => sysDeleteOrgList.Contains(it.OrgId)).CountAsync() > 0;
  361. if (hasPosition) throw Oops.Bah($"请先删除{name}下的职位");
  362. //删除组织
  363. if (await DeleteByIdsAsync(sysDeleteOrgList.Cast<object>().ToArray()))
  364. await RefreshCache();//刷新缓存
  365. }
  366. }
  367. #endregion 删除
  368. #region 其他
  369. /// <inheritdoc />
  370. public async Task RefreshCache()
  371. {
  372. _simpleCacheService.Remove(SystemConst.CACHE_SYS_ORG);//从redis删除
  373. _simpleCacheService.Remove(SystemConst.CACHE_SYS_TENANT);//从redis删除
  374. _simpleCacheService.Remove(SystemConst.CACHE_SYS_USER);//清空redis所有的用户信息
  375. await GetListAsync();//刷新缓存
  376. }
  377. /// <inheritdoc />
  378. public List<SysOrg> ConstructOrgTrees(List<SysOrg> orgList, long parentId = SafeCampusConst.ZERO)
  379. {
  380. //找下级ID列表
  381. var orgInfos = orgList.Where(it => it.ParentId == parentId).OrderBy(it => it.SortCode).ToList();
  382. if (orgInfos.Count > 0)//如果数量大于0
  383. {
  384. var data = new List<SysOrg>();
  385. foreach (var item in orgInfos)//遍历组织
  386. {
  387. item.Children = ConstructOrgTrees(orgList, item.Id);//添加子节点
  388. data.Add(item);//添加到列表
  389. }
  390. return data;//返回结果
  391. }
  392. return new List<SysOrg>();
  393. }
  394. #endregion 其他
  395. #region 方法
  396. /// <summary>
  397. /// 检查输入参数
  398. /// </summary>
  399. /// <param name="sysOrg"></param>
  400. /// <param name="name"></param>
  401. private async Task CheckInput(SysOrg sysOrg, string name)
  402. {
  403. //判断分类是否正确
  404. if (sysOrg.Category != CateGoryConst.ORG_COMPANY && sysOrg.Category != CateGoryConst.ORG_DEPT)
  405. throw Oops.Bah($"{name}所属分类错误:{sysOrg.Category}");
  406. var sysOrgList = await GetListAsync();//获取全部
  407. if (sysOrgList.Any(it => it.ParentId == sysOrg.ParentId && it.Name == sysOrg.Name && it.Id != sysOrg.Id))//判断同级是否有名称重复的
  408. throw Oops.Bah($"存在重复的同级{name}:{sysOrg.Name}");
  409. sysOrg.Names = sysOrg.Name;//全称默认自己
  410. if (sysOrg.ParentId != 0)
  411. {
  412. //获取父级,判断父级ID正不正确
  413. var parent = sysOrgList.Where(it => it.Id == sysOrg.ParentId).FirstOrDefault();
  414. if (parent != null)
  415. {
  416. if (parent.Id == sysOrg.Id)
  417. throw Oops.Bah($"上级{name}不能选择自己");
  418. }
  419. else
  420. {
  421. throw Oops.Bah($"上级{name}不存在:{sysOrg.ParentId}");
  422. }
  423. GetNames(sysOrgList, sysOrg.ParentId, sysOrg.Name, out var names,
  424. out var parentIdList);
  425. sysOrg.Names = names;
  426. sysOrg.ParentIdList = parentIdList;
  427. }
  428. //如果code没填
  429. if (string.IsNullOrEmpty(sysOrg.Code))
  430. {
  431. sysOrg.Code = RandomHelper.CreateRandomString(10);//赋值Code
  432. }
  433. else
  434. {
  435. //判断是否有相同的Code
  436. if (sysOrgList.Any(it => it.Code == sysOrg.Code && it.Id != sysOrg.Id))
  437. throw Oops.Bah($"存在重复的编码:{sysOrg.Code}");
  438. }
  439. }
  440. /// <summary>
  441. /// 根据组织Id列表获取所有父级组织
  442. /// </summary>
  443. /// <param name="allOrgList"></param>
  444. /// <param name="orgIds"></param>
  445. /// <returns></returns>
  446. public List<SysOrg> GetParentListByIds(List<SysOrg> allOrgList, List<long> orgIds)
  447. {
  448. var sysOrgList = new HashSet<SysOrg>();//结果列表
  449. //遍历组织ID
  450. orgIds.ForEach(it =>
  451. {
  452. //获取该组织ID的所有父级
  453. var parents = GetOrgParents(allOrgList, it);
  454. sysOrgList.AddRange(parents);//添加到结果
  455. });
  456. return sysOrgList.ToList();
  457. }
  458. /// <summary>
  459. /// 获取组织所有下级
  460. /// </summary>
  461. /// <param name="orgList"></param>
  462. /// <param name="parentId"></param>
  463. /// <returns></returns>
  464. public List<SysOrg> GetSysOrgChildren(List<SysOrg> orgList, long parentId)
  465. {
  466. //找下级组织ID列表
  467. var orgInfos = orgList.Where(it => it.ParentId == parentId).ToList();
  468. if (orgInfos.Count > 0)//如果数量大于0
  469. {
  470. var data = new List<SysOrg>();
  471. foreach (var item in orgInfos)//遍历组织
  472. {
  473. var children = GetSysOrgChildren(orgList, item.Id);//获取子节点
  474. data.AddRange(children);//添加子节点);
  475. data.Add(item);//添加到列表
  476. }
  477. return data;//返回结果
  478. }
  479. return new List<SysOrg>();
  480. }
  481. /// <summary>
  482. /// 获取组织下级(懒加载)
  483. /// </summary>
  484. /// <param name="orgList"></param>
  485. /// <param name="parentId"></param>
  486. /// <returns></returns>
  487. public List<SysOrg> GetSysOrgChildrenLazy(List<SysOrg> orgList, long parentId)
  488. {
  489. //找下级组织ID列表
  490. var orgInfos = orgList.Where(it => it.ParentId == parentId).ToList();
  491. if (orgInfos.Count > 0)//如果数量大于0
  492. {
  493. var data = new List<SysOrg>();
  494. foreach (var item in orgInfos)//遍历组织
  495. {
  496. var children = orgList.Where(it => it.ParentId == item.Id).ToList();//获取子节点
  497. //遍历子节点
  498. children.ForEach(it =>
  499. {
  500. if (!orgList.Any(org => org.ParentId == it.Id))
  501. it.Leaf = true;//如果没有下级,则设置为叶子节点
  502. });
  503. data.AddRange(children);//添加子节点);
  504. data.Add(item);//添加到列表
  505. }
  506. return data;//返回结果
  507. }
  508. return new List<SysOrg>();
  509. }
  510. /// <summary>
  511. /// 赋值组织的所有下级
  512. /// </summary>
  513. /// <param name="orgList">组织列表</param>
  514. /// <param name="parentId">父Id</param>
  515. /// <param name="newParentId">新父Id</param>
  516. /// <param name="isCopyPosition"></param>
  517. /// <param name="positions"></param>
  518. /// <returns></returns>
  519. public Tuple<List<SysOrg>, List<SysPosition>> CopySysOrgChildren(List<SysOrg> orgList, long parentId, long newParentId,
  520. bool isCopyPosition,
  521. List<SysPosition> positions)
  522. {
  523. //找下级组织列表
  524. var orgInfos = orgList.Where(it => it.ParentId == parentId).ToList();
  525. if (orgInfos.Count > 0)//如果数量大于0
  526. {
  527. var result = new Tuple<List<SysOrg>, List<SysPosition>>(
  528. new List<SysOrg>(), new List<SysPosition>()
  529. );
  530. foreach (var item in orgInfos)//遍历组织
  531. {
  532. var oldId = item.Id;//获取旧Id
  533. RedirectOrg(item);//实体重新赋值
  534. var children = CopySysOrgChildren(orgList, oldId, item.Id, isCopyPosition,
  535. positions);//获取子节点
  536. item.ParentId = newParentId;//赋值新的父Id
  537. result.Item1.AddRange(children.Item1);//添加下级组织;
  538. if (isCopyPosition)//如果包含职位
  539. {
  540. var positionList = positions.Where(it => it.OrgId == oldId).ToList();//获取职位列表
  541. positionList.ForEach(it =>
  542. {
  543. it.OrgId = item.Id;//赋值新的机构ID
  544. it.Id = CommonUtils.GetSingleId();//生成新的ID
  545. it.Code = RandomHelper.CreateRandomString(10);//生成新的Code
  546. });
  547. result.Item2.AddRange(positionList);//添加职位列表
  548. }
  549. }
  550. return result;//返回结果
  551. }
  552. return new Tuple<List<SysOrg>, List<SysPosition>>(
  553. new List<SysOrg>(), new List<SysPosition>()
  554. );
  555. }
  556. /// <summary>
  557. /// 重新生成组织实体
  558. /// </summary>
  559. /// <param name="org"></param>
  560. private void RedirectOrg(SysOrg org)
  561. {
  562. //重新生成ID并赋值
  563. var newId = CommonUtils.GetSingleId();
  564. org.Id = newId;
  565. org.Code = RandomHelper.CreateRandomString(10);
  566. org.CreateTime = DateTime.Now;
  567. org.CreateUser = UserManager.UserAccount;
  568. org.CreateUserId = UserManager.UserId;
  569. }
  570. /// <summary>
  571. /// 获取全称
  572. /// </summary>
  573. /// <param name="sysOrgList">组织列表</param>
  574. /// <param name="parentId">父Id</param>
  575. /// <param name="orgName">组织名称</param>
  576. /// <param name="names">组织全称</param>
  577. /// <param name="parentIdList">组织父Id列表</param>
  578. public void GetNames(List<SysOrg> sysOrgList, long parentId, string orgName,
  579. out string names, out List<long> parentIdList)
  580. {
  581. names = "";
  582. //获取父级菜单
  583. var parents = GetOrgParents(sysOrgList, parentId);
  584. foreach (var item in parents)
  585. {
  586. names += $"{item.Name}/";
  587. }
  588. names += orgName;//赋值全称
  589. parentIdList = parents.Select(it => it.Id).ToList();//赋值父Id列表
  590. }
  591. #endregion 方法
  592. }