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

226 lines
8.0 KiB

  1. using BPA.SAAS.Manage.Application.Org.Dtos.Role;
  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.Core.Base;
  6. using BPA.SAAS.Manage.Core.Org;
  7. using BPA.SAAS.Manage.Core.System;
  8. using Furion.LinqBuilder;
  9. using SqlSugar;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace BPA.SAAS.Manage.Application.Org.Services
  16. {
  17. public class RoleService: IRoleService, ITransient
  18. {
  19. private readonly ISqlSugarClient _db;
  20. public RoleService(ISqlSugarClient db)
  21. {
  22. _db=db;
  23. }
  24. public async Task<PageUtil> Page(RoleDtoPageInput input)
  25. {
  26. RefAsync<int> total = 0;
  27. var res =await _db.Queryable<BPA_Roles>()
  28. .WhereIF(!input.Code.IsNullOrEmpty(), t => t.Code.Contains(input.Code))
  29. .WhereIF(!input.Name.IsNullOrEmpty(), t => t.Name.Contains(input.Name))
  30. .Select(t => new RoleDtoOutput
  31. {
  32. CreateAt = t.CreateAt,
  33. Code = t.Code,
  34. Id = t.Id,
  35. Name = t.Name,
  36. Remark = t.Remark,
  37. RoleType = t.RoleType,
  38. Sort = t.Sort,
  39. Status= t.Status,
  40. }).OrderBy(t => t.Sort).OrderBy(x => x.CreateAt, OrderByType.Desc).ToPageListAsync(input.Current, input.PageSize, total);
  41. PageUtil util = new PageUtil()
  42. {
  43. Total = total,
  44. Data = res
  45. };
  46. return util;
  47. }
  48. /// <summary>
  49. /// 添加
  50. /// </summary>
  51. /// <param name="input"></param>
  52. /// <returns></returns>
  53. public async Task<bool> Add(RoleDtoInput input)
  54. {
  55. if (input.Code != "sys_manager_role")
  56. {
  57. var resEntitites = _db.Queryable<BPA_Roles>().Where(a => a.Code == input.Code).ToList();
  58. if (resEntitites.Count() > 0)
  59. {
  60. throw Oops.Oh("已存在相同编码的角色");
  61. }
  62. }
  63. var role = input.Adapt<BPA_Roles>();
  64. // role.DataScopeType = DataScopeType.ALL;
  65. return await _db.Insertable(role).CallEntityMethod(t => t.Create()).ExecuteCommandAsync() > 0;
  66. }
  67. /// <summary>
  68. /// 修改
  69. /// </summary>
  70. /// <param name="input"></param>
  71. /// <returns></returns>
  72. public async Task<bool> Update(RoleDtoInput input)
  73. {
  74. var BPA_Roles = await _db.Queryable<BPA_Roles>().Where(a => a.Code == input.Code).FirstAsync();
  75. var code = BPA_Roles.Code;
  76. if (code != input.Code)
  77. {
  78. throw Oops.Oh("角色编码不能进行修改");
  79. }
  80. var Roles = await _db.Queryable<BPA_Roles>().Where(a => a.Id == input.Id).FirstAsync();
  81. Roles.Name= input.Name;
  82. Roles.Code= input.Code;
  83. Roles.Remark= input.Remark;
  84. Roles.Sort = input.Sort;
  85. return _db.Updateable(Roles).ExecuteCommandHasChange();
  86. }
  87. /// <summary>
  88. /// 删除
  89. /// </summary>
  90. /// <param name="input"></param>
  91. /// <returns></returns>
  92. public async Task<bool> Delete(List<string> input)
  93. {
  94. try
  95. {
  96. _db.Ado.BeginTran();
  97. // 查询数据库中是否存在未删除的活动信息
  98. var resEntitites = _db.Queryable<BPA_Roles>().In(input).ToList();
  99. var ids = resEntitites.Select(a => a.Id).ToArray();
  100. //校验用户
  101. var dara = _db.Queryable<BPA_UserRole>().Where(x => ids.Contains(x.SysRoleId)).ToList();
  102. if (dara.Count() <= 0)
  103. {
  104. var res =await _db.Deleteable(resEntitites).ExecuteCommandAsync();
  105. _db.Ado.CommitTran();
  106. return res > 0;
  107. }
  108. else
  109. {
  110. throw Oops.Oh("删除失败, 请检查用户是否关联该角色");
  111. }
  112. }
  113. catch (Exception)
  114. {
  115. _db.Ado.RollbackTran();
  116. throw Oops.Oh("删除失败");
  117. }
  118. }
  119. /// <summary>
  120. /// 根据角色查询角色菜单
  121. /// </summary>
  122. /// <param name="RoleId"></param>
  123. /// <returns></returns>
  124. public async Task<List<QueryRoleMenuListDto>> GetMenuRole(string RoleId)
  125. {
  126. var MenuName =await _db.Queryable<BPA_RoleMenu>().LeftJoin<BPA_Menu>((t, x) => t.SysMenuId == x.Id)
  127. .Where(t => t.SysRoleId == RoleId).OrderBy((t, x) => x.CreateAt).Select((t, x) => new QueryRoleMenuListDto()
  128. {
  129. Name = x.Name,
  130. Id = x.Id,
  131. IsParentMenu = string.IsNullOrWhiteSpace(x.Pid)
  132. }).ToListAsync();
  133. return MenuName;
  134. }
  135. /// <summary>
  136. /// 编辑角色菜单
  137. /// </summary>
  138. /// <param name="input"></param>
  139. /// <returns></returns>
  140. public async Task<bool> AddOrUpdateMenuRole(RoleMenuDtoInput input)
  141. {
  142. List<BPA_RoleMenu> models = new List<BPA_RoleMenu>();
  143. List<string> NewMenuId = new List<string>();
  144. if (input.SysMenuId != null)
  145. {
  146. var Menus = _db.Queryable<BPA_Menu>().ToList();
  147. foreach (var item in input.SysMenuId)
  148. {
  149. var Pid = Menus.FirstOrDefault(t => t.Id == item).Pid;
  150. if (Pid != null && Pid != "")
  151. {
  152. var Pid2 = Menus.FirstOrDefault(t => t.Id == Pid).Pid;
  153. if (Pid2 != null)
  154. {
  155. NewMenuId.Add(Pid2);
  156. }
  157. NewMenuId.Add(Pid);
  158. }
  159. NewMenuId.Add(item);
  160. }
  161. NewMenuId.Distinct().ToList().ForEach(item =>
  162. {
  163. models.Add(new BPA_RoleMenu
  164. {
  165. Id = Guid.NewGuid().ToString(),
  166. SysMenuId = item,
  167. SysRoleId = input.SysRoleId
  168. });
  169. });
  170. }
  171. if (models.Count <= 0)
  172. return true;
  173. _db.Deleteable<BPA_RoleMenu>(t => t.SysRoleId == input.SysRoleId).ExecuteCommandHasChange();
  174. return await _db.Insertable(models).ExecuteCommandAsync() > 0;
  175. }
  176. /// <summary>
  177. /// 启用
  178. /// </summary>
  179. /// <param name="Id"></param>
  180. /// <returns></returns>
  181. public async Task<bool> Enable(string Id)
  182. {
  183. var resEntitites = _db.Queryable<BPA_Roles>().In(Id).ToList();
  184. resEntitites.ForEach(x =>
  185. {
  186. x.Status = CommonStatus.ENABLE;
  187. });
  188. var res =await _db.Updateable(resEntitites).ExecuteCommandAsync();
  189. return res>0;
  190. }
  191. /// <summary>
  192. /// 禁用
  193. /// </summary>
  194. /// <param name="Id"></param>
  195. /// <returns></returns>
  196. public async Task<bool> Disable(string Id)
  197. {
  198. var resEntitites = _db.Queryable<BPA_Roles>().In(Id).ToList();
  199. resEntitites.ForEach(x =>
  200. {
  201. x.Status = CommonStatus.DISABLE;
  202. });
  203. var res =await _db.Updateable(resEntitites).ExecuteCommandAsync();
  204. return res>0;
  205. }
  206. public async Task<Dictionary<string, object>> GetRole()
  207. {
  208. var res =await _db.Queryable<BPA_Roles>().Where(t => t.IsDeleted == 0 && t.Status == CommonStatus.ENABLE).ToDictionaryAsync(t => t.Id, t => t.Name);
  209. return res;
  210. }
  211. }
  212. }