基础服务api
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.
 
 

193 lines
8.1 KiB

  1. using BPA.SAAS.Manage.Application.Org.Dtos.Organize;
  2. using BPA.SAAS.Manage.Application.Org.Interface;
  3. using BPA.SAAS.Manage.Comm.Const;
  4. using BPA.SAAS.Manage.Comm.Enum;
  5. using BPA.SAAS.Manage.Comm.Util;
  6. using BPA.SAAS.Manage.Core.Base;
  7. using BPA.SAAS.Manage.Core.Org;
  8. using Furion.DatabaseAccessor;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Threading.Tasks;
  15. using System.Xml.Linq;
  16. namespace BPA.SAAS.Manage.Application.Org.Services
  17. {
  18. public class OrganizeService : IOrganizeService, ITransient
  19. {
  20. private readonly ISqlSugarClient _db;
  21. public OrganizeService(ISqlSugarClient db)
  22. {
  23. _db = db;
  24. }
  25. /// <summary>
  26. /// 分页获取组织机构列表
  27. /// </summary>
  28. /// <param name="input"></param>
  29. /// <returns></returns>
  30. public async Task<PageUtil> QueryOrgPageList(OrgPageInput input)
  31. {
  32. RefAsync<int> total = 0;
  33. var name = !string.IsNullOrEmpty(input.Name?.Trim());
  34. var id = !string.IsNullOrEmpty(input.Id?.Trim());
  35. var pId = !string.IsNullOrEmpty(input.Pid?.Trim());
  36. var data = await _db.Queryable<BPA_Organize>()
  37. .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
  38. .WhereIF(!string.IsNullOrWhiteSpace(input.Pid), u => u.Pids.Contains(input.Pid.Trim()) || u.Id == input.Pid.Trim())
  39. .Select<OrgOutput>()
  40. .OrderBy(a => a.CreateAt, OrderByType.Asc)
  41. .ToPageListAsync(input.Current, input.PageSize, total);
  42. PageUtil util = new PageUtil()
  43. {
  44. Total = total,
  45. Data = data
  46. };
  47. return util;
  48. }
  49. /// <summary>
  50. /// 获取组织机构列表
  51. /// </summary>
  52. /// <param name="input"></param>
  53. /// <returns></returns>
  54. public async Task<List<OrgOutput>> GetOrgList(OrgListInput input)
  55. {
  56. var pId = !string.IsNullOrEmpty(input.Pid?.Trim());
  57. var orgs = await _db.Queryable<BPA_Organize>()
  58. .WhereIF(pId, u => u.PID == input.Pid)
  59. .Where(u => u.Status != CommonStatus.DELETED).ToListAsync();
  60. return orgs.Adapt<List<OrgOutput>>();
  61. }
  62. /// <summary>
  63. /// 新增组织
  64. /// </summary>
  65. /// <param name="input"></param>
  66. /// <returns></returns>
  67. public async Task<bool> AddOrg(OrgAddInput input)
  68. {
  69. if (!string.IsNullOrEmpty(input.Tel))
  70. {
  71. var tel = Regex.IsMatch(input.Tel, "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$");
  72. if (tel == false) throw Oops.Oh("手机号错误");
  73. }
  74. var sysOrg = input.Adapt<BPA_Organize>();
  75. await FillPids(sysOrg);
  76. return await _db.Insertable(sysOrg).CallEntityMethod(m => m.Create()).ExecuteCommandAsync()>0;
  77. }
  78. /// <summary>
  79. /// 删除组织机构
  80. /// </summary>
  81. /// <param name="input"></param>
  82. /// <returns></returns>
  83. public async Task<bool> DeleteOrg(string id)
  84. {
  85. var sysOrg = await _db.Queryable<BPA_Organize>().FirstAsync(u => u.Id == id);
  86. // 级联删除子节点
  87. var childIdList = await GetChildIdListWithSelfById(sysOrg.Id);
  88. var orgs = await _db.Queryable<BPA_Organize>().Where(u => childIdList.Contains(u.Id)).ToListAsync();
  89. return await _db.Deleteable(orgs).ExecuteCommandAsync()>0;
  90. }
  91. /// <summary>
  92. /// 更新组织机构
  93. /// </summary>
  94. /// <param name="input"></param>
  95. /// <returns></returns>
  96. public async Task<bool> UpdateOrg(UpdateOrgInput input)
  97. {
  98. if (input.Pid != Guid.Empty.ToString() && !string.IsNullOrEmpty(input.Pid) && input.Pid != "0")
  99. {
  100. var org = await _db.Queryable<BPA_Organize>().FirstAsync(u => u.Id == input.Pid);
  101. _ = org ?? throw Oops.Oh("父机构不存在");
  102. }
  103. if (!string.IsNullOrEmpty(input.Tel))
  104. {
  105. var tel = Regex.IsMatch(input.Tel, "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$");
  106. if (tel == false) throw Oops.Oh("手机号错误");
  107. }
  108. if (input.Id == input.Pid)
  109. throw Oops.Oh("当前机构Id不能与父机构Id相同");
  110. // 如果是编辑,父id不能为自己的子节点
  111. var childIdListById = await GetChildIdListWithSelfById(input.Id);
  112. if (childIdListById.Contains(input.Pid))
  113. throw Oops.Oh("当前机构Id不能与父机构Id相同");
  114. var sysOrg = await _db.Queryable<BPA_Organize>().FirstAsync(u => u.Id == input.Id);
  115. sysOrg.Name=input.Name;
  116. sysOrg.Tel = input.Tel;
  117. sysOrg.Remark = input.Remark;
  118. sysOrg.Sort= input.Sort;
  119. await FillPids(sysOrg);
  120. return await _db.Updateable(sysOrg).IgnoreColumns(ignoreAllNullColumns: true)
  121. .ExecuteCommandAsync()>0;
  122. }
  123. /// <summary>
  124. /// 获取组织机构树
  125. /// </summary>
  126. /// <returns></returns>
  127. public async Task<dynamic> GetOrgTree()
  128. {
  129. var dataScopeList = new List<string>();
  130. var groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value;
  131. var orgs = await _db.Queryable<BPA_Organize>().WhereIF(dataScopeList.Count > 0, u => dataScopeList.Contains(u.Id))
  132. .Where(u => u.Status == CommonStatus.ENABLE && u.GroupId== groupId).OrderBy(u => u.Sort)
  133. .Select(u => new OrgTreeNode
  134. {
  135. key = u.Id,
  136. parentId = u.PID,
  137. title = u.Name,
  138. value = u.Id.ToString(),
  139. weight = u.Sort,
  140. Type = u.Type,
  141. }).ToListAsync();
  142. return new TreeBuildUtil<OrgTreeNode>().Build(orgs);
  143. }
  144. /// <summary>
  145. /// 填充父Ids字段
  146. /// </summary>
  147. /// <param name="sysOrg"></param>
  148. /// <returns></returns>
  149. private async Task FillPids(BPA_Organize sysOrg)
  150. {
  151. if (string.IsNullOrWhiteSpace(sysOrg.PID) || sysOrg.PID == "0")
  152. {
  153. sysOrg.Pids = "[0],";
  154. sysOrg.PID = "0";
  155. }
  156. else
  157. {
  158. var t = await _db.Queryable<BPA_Organize>().FirstAsync(u => u.Id == sysOrg.PID);
  159. sysOrg.Pids = t.Pids + "[" + t.Id + "],";
  160. }
  161. }
  162. /// <summary>
  163. /// 根据节点Id获取所有子节点Id集合,包含自己
  164. /// </summary>
  165. /// <param name="id"></param>
  166. /// <returns></returns>
  167. private async Task<List<string>> GetChildIdListWithSelfById(string id)
  168. {
  169. var childIdList = await _db.Queryable<BPA_Organize>()
  170. .Where(u => u.Pids.Contains(id.ToString()))
  171. .Select(u => u.Id).ToListAsync();
  172. childIdList.Add(id);
  173. return childIdList;
  174. }
  175. public async Task<Dictionary<string, object>> GetOrgList()
  176. {
  177. var data =await _db.Queryable<BPA_Organize>()
  178. .ToDictionaryAsync(x => x.Id, x => x.Name);
  179. return data;
  180. }
  181. }
  182. }