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

644 lines
25 KiB

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