AdminRepository.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using AutoMapper;
  7. using GxPress.Common.AppOptions;
  8. using GxPress.Common.Encrypt;
  9. using GxPress.Common.Exceptions;
  10. using GxPress.Common.Page;
  11. using GxPress.Common.Tools;
  12. using GxPress.Entity;
  13. using GxPress.Repository.Interface;
  14. using GxPress.Request.Admin;
  15. using GxPress.Result.Admin;
  16. using Microsoft.Extensions.Options;
  17. using SqlKata;
  18. using Datory;
  19. using Microsoft.Extensions.Caching.Distributed;
  20. using GxPress.Common.Extensions;
  21. namespace GxPress.Repository.Implement
  22. {
  23. public class AdminRepository : IAdminRepository
  24. {
  25. private readonly Repository<Admin> _repository;
  26. private readonly IMapper _mapper;
  27. private readonly IDistributedCache _cache;
  28. public AdminRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper, IDistributedCache cache)
  29. {
  30. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  31. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  32. _repository = new Repository<Admin>(database);
  33. _mapper = mapper;
  34. _cache = cache;
  35. }
  36. public IDatabase Database => _repository.Database;
  37. public string TableName => _repository.TableName;
  38. public List<TableColumn> TableColumns => _repository.TableColumns;
  39. //public async Task<IEnumerable<AdminPageResult>> GetPagedList(AdminPageSearchRequest request)
  40. //{
  41. // var fieldSql = @"Id,Account,Name,Cdt";
  42. // var fromSql = @"Admin";
  43. // var whereSql = new StringBuilder("where 1=1");
  44. // var queryParam = new DynamicParameters();
  45. // if (!string.IsNullOrEmpty(request.Keyword))
  46. // {
  47. // whereSql.Append("and (Account like @keyword or Name like @keyword)");
  48. // queryParam.Add("keyword", "%" + request.Keyword + "%");
  49. // }
  50. // var result = await _repository.GetAllAsync<AdminPageResult>();
  51. // return result;
  52. //}
  53. public async Task<Admin> GetByAccountAsync(string account)
  54. {
  55. return await _repository.GetAsync(Q.Where(nameof(Admin.Account), account));
  56. }
  57. public async Task<bool> ExistsAsync(string account)
  58. {
  59. return await _repository.ExistsAsync(Q.Where(nameof(Admin.Account), account));
  60. }
  61. public async Task<Admin> GetAsync(int id)
  62. {
  63. return await _repository.GetAsync(id);
  64. }
  65. public async Task<int> InsertAsync(Admin admin)
  66. {
  67. return await _repository.InsertAsync(admin);
  68. }
  69. public async Task<bool> UpdateAsync(Admin admin)
  70. {
  71. return await _repository.UpdateAsync(admin);
  72. }
  73. public async Task<int> GetCountAsync()
  74. {
  75. return await _repository.CountAsync();
  76. }
  77. /// <summary>
  78. /// Set the limit and offset for a given page.
  79. /// </summary>
  80. /// <param name="page"></param>
  81. /// <param name="perPage"></param>
  82. /// <returns></returns>
  83. public async Task<IEnumerable<Admin>> GetPageListAsync(int page, int perPage)
  84. {
  85. return await _repository.GetAllAsync(Q.ForPage(page, perPage));
  86. }
  87. /// <summary>
  88. /// 登录
  89. /// </summary>
  90. /// <param name="request"></param>
  91. /// <returns></returns>
  92. public async Task<AdminSignInResult> SignInAsync(AdminSignInRequest request)
  93. {
  94. var key = $"captcha:{request.Guid.ToString()}";
  95. var captcha = await _cache.GetStringAsync(key);
  96. if (!captcha.Equals(request.Captcha, StringComparison.OrdinalIgnoreCase))
  97. throw new BusinessException("验证码错误");
  98. var admin = await GetByAccountAsync(request.Account);
  99. if (admin == null) throw new BusinessException("不存在该账号");
  100. var pwd = EncryptProvider.Md5($"{request.Password}{request.Account}");
  101. if (admin.Password != pwd) throw new BusinessException("密码错误");
  102. //新增获取菜单数据
  103. var result = new AdminSignInResult { AdminId = admin.Id, RoleId = admin.RoleId, Menus = StringUtils.StringCollectionToStringList(admin.Menus).ToList(), Name = admin.Name };
  104. return result;
  105. }
  106. /// <summary>
  107. /// 注册
  108. /// </summary>
  109. /// <param name="request"></param>
  110. /// <returns></returns>
  111. public async Task<int> SignUpAsync(AdminSignUpRequest request)
  112. {
  113. var accountExist = await ExistsAsync(request.Account);
  114. if (accountExist) throw new BusinessException("已存在该账号");
  115. var admin = new Admin
  116. {
  117. Account = request.Account,
  118. Name = request.Name,
  119. Password = EncryptProvider.Md5($"{request.Password}{request.Account}"),
  120. Phone = request.Phone,
  121. Email = request.Email,
  122. Sex = request.Sex,
  123. RoleId = request.RoleId
  124. };
  125. var result = await InsertAsync(admin);
  126. return result;
  127. }
  128. /// <summary>
  129. /// 删除
  130. /// </summary>
  131. /// <param name="id"></param>
  132. /// <returns></returns>
  133. public async Task<bool> DeleteAsync(int id)
  134. {
  135. var admin = await GetAsync(id);
  136. if (admin == null) throw new BusinessException("不存在该账号");
  137. return await _repository.DeleteAsync(id);
  138. }
  139. /// <summary>
  140. /// 分页列表
  141. /// </summary>
  142. /// <param name="request"></param>
  143. /// <returns></returns>
  144. public async Task<PagedList<AdminPageResult>> GetPagedList(AdminPageSearchRequest request)
  145. {
  146. var pagedList = new PagedList<AdminPageResult>
  147. {
  148. Total = await GetCountAsync()
  149. };
  150. var list = await GetPageListAsync(request.Page, request.PerPage);
  151. var items = list.Select(user => _mapper.Map<AdminPageResult>(user)).ToList();
  152. pagedList.Items = items;
  153. return pagedList;
  154. }
  155. /// <summary>
  156. /// 获取详情
  157. /// </summary>
  158. /// <param name="id"></param>
  159. /// <returns></returns>
  160. public async Task<AdminDetailResult> GetDetailAsync(int id)
  161. {
  162. var admin = await GetAsync(id);
  163. if (admin == null) throw new BusinessException("该用户不存在");
  164. return new AdminDetailResult
  165. {
  166. Id = admin.Id,
  167. Account = admin.Account,
  168. Name = admin.Name,
  169. Phone = admin.Phone,
  170. Email = admin.Email,
  171. Sex = admin.Sex,
  172. Menus = admin.Menus,
  173. };
  174. }
  175. /// <summary>
  176. /// 修改
  177. /// </summary>
  178. /// <param name="id"></param>
  179. /// <param name="request"></param>
  180. /// <returns></returns>
  181. public async Task<bool> UpdateAsync(int id, Admin request)
  182. {
  183. var admin = await GetAsync(id);
  184. if (admin == null) throw new BusinessException("该用户不存在");
  185. if (!string.IsNullOrEmpty(request.OldPassword) && !string.IsNullOrEmpty(request.NewPassword))
  186. {
  187. var pwd = EncryptProvider.Md5($"{request.OldPassword}{admin.Account}");
  188. if (pwd != admin.Password) throw new BusinessException("旧密码错误");
  189. admin.Password = EncryptProvider.Md5($"{request.NewPassword}{admin.Account}");
  190. request.Password = admin.Password;
  191. }
  192. admin.Name = request.Name;
  193. request.Id = id;
  194. request.CreatedDate = admin.CreatedDate;
  195. return await UpdateAsync(request);
  196. }
  197. /// <summary>
  198. /// 修改根据条件修改
  199. /// </summary>
  200. /// <param name="request"></param>
  201. /// <returns></returns>
  202. public async Task<bool> UpdateQAsync(Admin request)
  203. {
  204. return await _repository.UpdateAsync(Q.Set(nameof(Admin.Menus), request.Menus).Where(nameof(Admin.Id), request.Id)) >
  205. 0;
  206. }
  207. /// <summary>
  208. /// 修改密码
  209. /// </summary>
  210. /// <param name="request"></param>
  211. /// <returns></returns>
  212. public async Task<bool> UpdatePassWord(AdminUpdatePassWordRequest request)
  213. {
  214. //获取用户
  215. var admin = await GetAsync(request.Id);
  216. if (admin == null) throw new BusinessException("该用户不存在");
  217. //验证旧密码
  218. var pwd = EncryptProvider.Md5($"{request.OldPassword}{request.Account}");
  219. if (pwd != admin.Password) throw new BusinessException("旧密码错误");
  220. admin.Password = EncryptProvider.Md5($"{request.NewPassword}{admin.Account}");
  221. //修改
  222. return await _repository.UpdateAsync(Q.Set(nameof(Admin.Password), admin.Password)
  223. .Where(nameof(admin.Id), request.Id)) > 0;
  224. }
  225. public async Task<bool> AdminUpdatePassWord(AdminUpdatePassWordRequest request)
  226. {
  227. //获取用户
  228. var admin = await GetAsync(request.Id);
  229. if (admin == null) throw new BusinessException("该用户不存在");
  230. //生成密码
  231. var pwd = EncryptProvider.Md5($"{request.NewPassword}{admin.Account}");
  232. //修改
  233. return await _repository.UpdateAsync(Q.Set(nameof(Admin.Password), pwd)
  234. .Where(nameof(Admin.Id), request.Id)) > 0;
  235. }
  236. }
  237. }