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

207 lines
7.2 KiB

  1. using BPA.SAAS.Manage.Application.Org.Dtos.Users;
  2. using BPA.SAAS.Manage.Application.Org.Interface;
  3. using BPA.SAAS.Manage.Application.Org.Interface;
  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 Furion.LinqBuilder;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Xml.Linq;
  15. namespace BPA.SAAS.Manage.Application.Org.Services
  16. {
  17. public class UserService : IUserService, ITransient
  18. {
  19. private readonly ISqlSugarClient _db;
  20. public UserService(ISqlSugarClient db)
  21. {
  22. _db=db;
  23. }
  24. /// <summary>
  25. /// 分页
  26. /// </summary>
  27. /// <param name="input"></param>
  28. /// <returns></returns>
  29. public async Task<PageUtil> UserPage(UserDtoPageInput input)
  30. {
  31. RefAsync<int> total = 0;
  32. var data =await _db.Queryable<BPA_Users>().LeftJoin<BPA_Company>((t, x) => t.GroupId == x.Id)
  33. .WhereIF(!input.Name.IsNullOrEmpty(), t => t.Name.Contains(input.Name))
  34. .WhereIF(!input.Account.IsNullOrEmpty(), t => t.Account.Contains(input.Account))
  35. .Where(t => t.IsDeleted != 1)
  36. .OrderBy(t => t.CreateAt, OrderByType.Desc)
  37. .Select((t, x) => new UserDtoOutput
  38. {
  39. Id = t.Id,
  40. Account = t.Account,
  41. AdminType = t.AdminType,
  42. CompanyName = x.Name,
  43. Name = t.Name,
  44. Phone = t.Phone,
  45. SysOrgId = t.SysOrgId,
  46. CreateAt=t.CreateAt
  47. }).ToPageListAsync(input.Current, input.PageSize, total);
  48. PageUtil util = new PageUtil()
  49. {
  50. Total = total,
  51. Data = data
  52. };
  53. return util;
  54. }
  55. /// <summary>
  56. /// 新增
  57. /// </summary>
  58. /// <param name="input"></param>
  59. /// <returns></returns>
  60. public async Task<bool> AddUser(UserDtoInput input)
  61. {
  62. var user = input.Adapt<BPA_Users>();
  63. user.Password = MD5Encryption.Encrypt("123456").ToLower();
  64. var check = _db.Queryable<BPA_Users>().First(t => t.Account == user.Account);
  65. if (check != null)
  66. return false;
  67. return await _db.Insertable(user).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> UpdateUser(UserDtoInput input)
  75. {
  76. var user = _db.Queryable<BPA_Users>().First(t => t.Account == input.Account);
  77. if (user == null)
  78. return false;
  79. user.Name = input.Name;
  80. user.Account = input.Account;
  81. user.Phone = input.Phone;
  82. user.SysOrgId = input.SysOrgId;
  83. var res =await _db.Updateable(user)
  84. .IgnoreColumns(t => t.Password)
  85. .IgnoreColumns(t => t.AdminType)
  86. .ExecuteCommandHasChangeAsync();
  87. return res;
  88. }
  89. /// <summary>
  90. /// 删除
  91. /// </summary>
  92. /// <param name="input"></param>
  93. /// <returns></returns>
  94. public async Task<bool> DeleteUser(List<string> input)
  95. {
  96. var resEntitites = _db.Queryable<BPA_Users>().In(input).ToList();
  97. var res =await _db.Deleteable(resEntitites).ExecuteCommandAsync();
  98. return res > 0;
  99. }
  100. /// <summary>
  101. /// 启用
  102. /// </summary>
  103. /// <param name="Id"></param>
  104. /// <returns></returns>
  105. public async Task<bool> Enable(string Id)
  106. {
  107. var resEntitites = _db.Queryable<BPA_Users>().In(Id).ToList();
  108. resEntitites.ForEach(x =>
  109. {
  110. x.Status = CommonStatus.ENABLE;
  111. });
  112. var res =await _db.Updateable(resEntitites).ExecuteCommandAsync();
  113. return res>0;
  114. }
  115. /// <summary>
  116. /// 禁用
  117. /// </summary>
  118. /// <param name="Id"></param>
  119. /// <returns></returns>
  120. public async Task<bool> Disable(string Id)
  121. {
  122. var resEntitites = _db.Queryable<BPA_Users>().In(Id).ToList();
  123. resEntitites.ForEach(x =>
  124. {
  125. x.Status = CommonStatus.DISABLE;
  126. });
  127. var res = await _db.Updateable(resEntitites).ExecuteCommandAsync();
  128. return res > 0;
  129. }
  130. /// <summary>
  131. /// 重置密码
  132. /// </summary>
  133. /// <param name="Id"></param>
  134. /// <returns></returns>
  135. public async Task<bool> ResetPwd(string Id)
  136. {
  137. var user = _db.Queryable<BPA_Users>().First(t => t.Id == Id);
  138. if (user == null)
  139. return false;
  140. string pwd = MD5Encryption.Encrypt("123456").ToLower();
  141. return await _db.Updateable<BPA_Users>(t => t.Password == pwd).Where(t => t.Id == Id).ExecuteCommandHasChangeAsync();
  142. }
  143. /// <summary>
  144. /// 变更密码
  145. /// </summary>
  146. /// <param name="input"></param>
  147. /// <returns></returns>
  148. public async Task<bool> UpdatePwd(UserPwdDtoInput input)
  149. {
  150. input.NPwd = MD5Encryption.Encrypt(input.NPwd).ToLower();
  151. input.OPwd = MD5Encryption.Encrypt(input.OPwd).ToLower();
  152. return await _db.Updateable<BPA_Users>()
  153. .SetColumns(t => t.Password == input.NPwd)
  154. .Where(t => t.Password == input.OPwd)
  155. .Where(t => t.Account == input.Account)
  156. .ExecuteCommandHasChangeAsync();
  157. }
  158. /// <summary>
  159. /// 获取当前用户的角色
  160. /// </summary>
  161. /// <param name="UserId"></param>
  162. /// <returns></returns>
  163. public async Task<UserRoleDtoInput> GetUserRole(string UserId)
  164. {
  165. string[] sysRoles =await _db.Queryable<BPA_UserRole>().Where(t => t.SysUserId == UserId).Select(t => t.SysRoleId).ToArrayAsync();
  166. UserRoleDtoInput userRoleDtoInput = new UserRoleDtoInput();
  167. userRoleDtoInput.SysUserId = UserId;
  168. userRoleDtoInput.SysRoleId = sysRoles;
  169. return userRoleDtoInput;
  170. }
  171. /// <summary>
  172. /// 新增用户角色
  173. /// </summary>
  174. /// <param name="input"></param>
  175. /// <returns></returns>
  176. public async Task<bool> AddUserRole(UserRoleDtoInput input)
  177. {
  178. List<BPA_UserRole> roles = new List<BPA_UserRole>();
  179. var dara = _db.Queryable<BPA_UserRole>().Where(x => x.SysUserId == input.SysUserId).ToList();
  180. _db.Deleteable<BPA_UserRole>(dara).ExecuteCommand(); //批量删除
  181. foreach (string SysRoleId in input.SysRoleId)
  182. {
  183. BPA_UserRole role = new BPA_UserRole();
  184. role.Id = Guid.NewGuid().ToString();
  185. role.SysUserId = input.SysUserId;
  186. role.SysRoleId = SysRoleId;
  187. roles.Add(role);
  188. }
  189. return await _db.Insertable(roles).ExecuteCommandAsync() > 0;
  190. }
  191. }
  192. }