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

237 lines
8.4 KiB

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