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

212 lines
7.6 KiB

  1. using BPA.SAAS.Manage.Application.System.Dtos;
  2. using BPA.SAAS.Manage.Application.System.Interface;
  3. using BPA.SAAS.Manage.Comm.Const;
  4. using BPA.SAAS.Manage.Core.Org;
  5. using BPA.SAAS.Manage.Core.System;
  6. using Furion.LinqBuilder;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace BPA.SAAS.Manage.Application.System.Services
  13. {
  14. public class MenuService : IMenuService, ITransient
  15. {
  16. private readonly ISqlSugarClient _db;
  17. public MenuService(ISqlSugarClient db)
  18. {
  19. _db = db;
  20. }
  21. /// <summary>
  22. /// 分页查询
  23. /// </summary>
  24. /// <param name="input"></param>
  25. /// <returns></returns>
  26. public async Task<List<MenuTreeDtoOutput>> GetMenuTreePage(MenuTreeSearchDtoInput input)
  27. {
  28. if (App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value == null)
  29. return null;
  30. var AllMenus =await _db.Queryable<BPA_Menu>().Where(t => t.IsDeleted == 0).OrderBy(t => t.Sort).ToListAsync();
  31. var query = AllMenus.Where(t => t.Pid == "" || t.Pid == null);
  32. if (!input.Name.IsNullOrEmpty())
  33. query = query.Where(t => t.Name.Contains(input.Name));
  34. if (!input.Code.IsNullOrEmpty())
  35. query = query.Where(t => t.Code.Contains(input.Code));
  36. var data = query.Select(t => new MenuTreeDtoOutput
  37. {
  38. Id = t.Id,
  39. Name = t.Name,
  40. CreateAt = t.CreateAt,
  41. Code = t.Code,
  42. Icon = t.Icon,
  43. Router = t.Router,
  44. Pid = t.Pid,
  45. Sort = t.Sort,
  46. Component = t.Component,
  47. Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeDtoOutput
  48. {
  49. Id = x.Id,
  50. Name = x.Name,
  51. CreateAt = x.CreateAt,
  52. Code = x.Code,
  53. Pid = x.Pid,
  54. Icon = x.Icon,
  55. Router = x.Router,
  56. Sort = x.Sort,
  57. Component = x.Component,
  58. Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeDtoOutput
  59. {
  60. Id = y.Id.ToString(),
  61. Name = y.Name,
  62. CreateAt = y.CreateAt,
  63. Code = y.Code,
  64. Icon = y.Icon,
  65. Pid = y.Pid.ToString(),
  66. Router = y.Router,
  67. Sort = y.Sort,
  68. Component = y.Component
  69. }).ToList()
  70. }).ToList()
  71. }).ToList();
  72. return data;
  73. }
  74. /// <summary>
  75. /// 新增菜单
  76. /// </summary>
  77. /// <param name="input"></param>
  78. /// <returns></returns>
  79. public async Task<bool> Add(MenuDtoInput input)
  80. {
  81. var menu = input.Adapt<BPA_Menu>();
  82. menu.Id= Guid.NewGuid().ToString();
  83. menu.CreateAt= DateTime.Now;
  84. menu.CreateBy= App.User?.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
  85. return await _db.Insertable(menu).ExecuteCommandAsync()> 0;
  86. }
  87. /// <summary>
  88. /// 编辑菜单
  89. /// </summary>
  90. /// <param name="input"></param>
  91. /// <returns></returns>
  92. public async Task<bool> Update(MenuDtoInput input)
  93. {
  94. var menu = input.Adapt<BPA_Menu>();
  95. menu.CreateAt = DateTime.Now;
  96. menu.CreateBy = App.User?.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
  97. return await _db.Updateable(menu).ExecuteCommandAsync()>0;
  98. }
  99. /// <summary>
  100. /// 删除菜单
  101. /// </summary>
  102. /// <param name="input"></param>
  103. /// <returns></returns>
  104. public async Task<bool> Delete(List<string> input)
  105. {
  106. try
  107. {
  108. _db.Ado.BeginTran();
  109. // 查询数据库中是否存在未删除的活动信息
  110. var resEntitites = _db.Queryable<BPA_Menu>().In(input).ToList();
  111. resEntitites.ForEach(x =>
  112. {
  113. x.IsDeleted = 1;
  114. });
  115. var res =await _db.Updateable(resEntitites).ExecuteCommandAsync();
  116. _db.Ado.CommitTran();
  117. return res>0;
  118. }
  119. catch (Exception)
  120. {
  121. _db.Ado.RollbackTran();
  122. return false;
  123. }
  124. }
  125. /// <summary>
  126. /// 查询菜单树
  127. /// </summary>
  128. /// <param name="GetType"></param>
  129. /// <returns></returns>
  130. public async Task<List<MenuTreeOutput>> MenuTree(string GetType)
  131. {
  132. if (!string.IsNullOrWhiteSpace(GetType) && GetType.ToLower() == "user")
  133. {
  134. var userId = App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
  135. var GroupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
  136. if (string.IsNullOrWhiteSpace(GroupId) || string.IsNullOrWhiteSpace(userId))
  137. {
  138. return null;
  139. }
  140. List<BPA_Roles> dataRoles =await _db.Queryable<BPA_Roles>().Where(a => a.GroupId == GroupId).ToListAsync();
  141. if (dataRoles.Count() <= 0)
  142. {
  143. return null;
  144. }
  145. var sysRoleId = dataRoles.Select(a => a.Id).ToArray();
  146. var menus = _db.Queryable<BPA_RoleMenu>().Where(t => sysRoleId.Contains(t.SysRoleId))
  147. .Select(x => x.SysMenuId).ToArray();
  148. var AllMenus = _db.Queryable<BPA_Menu>().Where(t => t.IsDeleted == 0 && menus.Contains(t.Id)).OrderBy(t => t.Sort).ToList();
  149. var data = AllMenus.Where(t => t.Pid == null || t.Pid == "").Select(t => new MenuTreeOutput
  150. {
  151. Key = t.Id,
  152. Title = t.Name,
  153. value = t.Name,
  154. Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeOutput
  155. {
  156. Key = x.Id,
  157. Title = x.Name,
  158. value = x.Name,
  159. Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeOutput
  160. {
  161. Key = y.Id,
  162. Title = y.Name,
  163. value = y.Name,
  164. }).ToList()
  165. }).ToList()
  166. }).ToList();
  167. return data;
  168. }
  169. else
  170. {
  171. var AllMenus = _db.Queryable<BPA_Menu>().Where(t => t.IsDeleted == 0).OrderBy(t => t.Sort).ToList();
  172. var data = AllMenus.Where(t => t.Pid == null || t.Pid == "").Select(t => new MenuTreeOutput
  173. {
  174. Key = t.Id,
  175. Title = t.Name,
  176. value = t.Id,
  177. Children = AllMenus.Where(x => x.Pid == t.Id).Select(x => new MenuTreeOutput
  178. {
  179. Key = x.Id,
  180. Title = x.Name,
  181. value = x.Id,
  182. Children = AllMenus.Where(y => y.Pid == x.Id).Select(y => new MenuTreeOutput
  183. {
  184. Key = y.Id,
  185. Title = y.Name,
  186. value = y.Name,
  187. }).ToList()
  188. }).ToList()
  189. }).ToList();
  190. return data;
  191. }
  192. }
  193. }
  194. }