AdminRepository.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. //修改IP
  103. admin.Ip = request.Ip;
  104. admin.LoginTime = DateTime.Now;
  105. await _repository.UpdateAsync(admin);
  106. //新增获取菜单数据
  107. var result = new AdminSignInResult { AdminId = admin.Id, RoleId = admin.RoleId, Menus = StringUtils.StringCollectionToStringList(admin.Menus).ToList(), Name = admin.Name };
  108. return result;
  109. }
  110. /// <summary>
  111. /// 注册
  112. /// </summary>
  113. /// <param name="request"></param>
  114. /// <returns></returns>
  115. public async Task<int> SignUpAsync(AdminSignUpRequest request)
  116. {
  117. var accountExist = await ExistsAsync(request.Account);
  118. if (accountExist) throw new BusinessException("已存在该账号");
  119. var admin = new Admin
  120. {
  121. Account = request.Account,
  122. Name = request.Name,
  123. Password = EncryptProvider.Md5($"{request.Password}{request.Account}"),
  124. Phone = request.Phone,
  125. Email = request.Email,
  126. Sex = request.Sex,
  127. RoleId = request.RoleId
  128. };
  129. var result = await InsertAsync(admin);
  130. return result;
  131. }
  132. /// <summary>
  133. /// 删除
  134. /// </summary>
  135. /// <param name="id"></param>
  136. /// <returns></returns>
  137. public async Task<bool> DeleteAsync(int id)
  138. {
  139. var admin = await GetAsync(id);
  140. if (admin == null) throw new BusinessException("不存在该账号");
  141. return await _repository.DeleteAsync(id);
  142. }
  143. /// <summary>
  144. /// 分页列表
  145. /// </summary>
  146. /// <param name="request"></param>
  147. /// <returns></returns>
  148. public async Task<PagedList<AdminPageResult>> GetPagedList(AdminPageSearchRequest request)
  149. {
  150. var pagedList = new PagedList<AdminPageResult>();
  151. var query = Q.NewQuery();
  152. if (!string.IsNullOrEmpty(request.Keyword))
  153. {
  154. query.Where(n => n.OrWhereLike(nameof(Admin.Name), $"%{request.Keyword}%")
  155. .OrWhereLike(nameof(Admin.Account), $"%{request.Keyword}%")
  156. .OrWhereLike(nameof(Admin.Phone), $"%{request.Keyword}%"));
  157. }
  158. pagedList.Total = await _repository.CountAsync(query);
  159. var list = await _repository.GetAllAsync(query);
  160. var items = list.Select(user => _mapper.Map<AdminPageResult>(user)).ToList();
  161. pagedList.Items = items;
  162. return pagedList;
  163. }
  164. /// <summary>
  165. /// 获取详情
  166. /// </summary>
  167. /// <param name="id"></param>
  168. /// <returns></returns>
  169. public async Task<AdminDetailResult> GetDetailAsync(int id)
  170. {
  171. var admin = await GetAsync(id);
  172. if (admin == null) throw new BusinessException("该用户不存在");
  173. return new AdminDetailResult
  174. {
  175. Id = admin.Id,
  176. Account = admin.Account,
  177. Name = admin.Name,
  178. Phone = admin.Phone,
  179. Email = admin.Email,
  180. Sex = admin.Sex,
  181. Menus = admin.Menus,
  182. };
  183. }
  184. /// <summary>
  185. /// 修改
  186. /// </summary>
  187. /// <param name="id"></param>
  188. /// <param name="request"></param>
  189. /// <returns></returns>
  190. public async Task<bool> UpdateAsync(int id, Admin request)
  191. {
  192. var admin = await GetAsync(id);
  193. if (admin == null) throw new BusinessException("该用户不存在");
  194. if (!string.IsNullOrEmpty(request.OldPassword) && !string.IsNullOrEmpty(request.NewPassword))
  195. {
  196. var pwd = EncryptProvider.Md5($"{request.OldPassword}{admin.Account}");
  197. if (pwd != admin.Password) throw new BusinessException("旧密码错误");
  198. admin.Password = EncryptProvider.Md5($"{request.NewPassword}{admin.Account}");
  199. request.Password = admin.Password;
  200. }
  201. admin.Name = request.Name;
  202. request.Id = id;
  203. request.CreatedDate = admin.CreatedDate;
  204. return await UpdateAsync(request);
  205. }
  206. /// <summary>
  207. /// 修改根据条件修改
  208. /// </summary>
  209. /// <param name="request"></param>
  210. /// <returns></returns>
  211. public async Task<bool> UpdateQAsync(Admin request)
  212. {
  213. return await _repository.UpdateAsync(Q.Set(nameof(Admin.Menus), request.Menus).Where(nameof(Admin.Id), request.Id)) >
  214. 0;
  215. }
  216. /// <summary>
  217. /// 修改密码
  218. /// </summary>
  219. /// <param name="request"></param>
  220. /// <returns></returns>
  221. public async Task<bool> UpdatePassWord(AdminUpdatePassWordRequest request)
  222. {
  223. //获取用户
  224. var admin = await GetAsync(request.Id);
  225. if (admin == null) throw new BusinessException("该用户不存在");
  226. //验证旧密码
  227. var pwd = EncryptProvider.Md5($"{request.OldPassword}{request.Account}");
  228. if (pwd != admin.Password) throw new BusinessException("旧密码错误");
  229. admin.Password = EncryptProvider.Md5($"{request.NewPassword}{admin.Account}");
  230. //修改
  231. return await _repository.UpdateAsync(Q.Set(nameof(Admin.Password), admin.Password)
  232. .Where(nameof(admin.Id), request.Id)) > 0;
  233. }
  234. public async Task<bool> AdminUpdatePassWord(AdminUpdatePassWordRequest request)
  235. {
  236. //获取用户
  237. var admin = await GetAsync(request.Id);
  238. if (admin == null) throw new BusinessException("该用户不存在");
  239. //生成密码
  240. var pwd = EncryptProvider.Md5($"{request.NewPassword}{admin.Account}");
  241. //修改
  242. return await _repository.UpdateAsync(Q.Set(nameof(Admin.Password), pwd)
  243. .Where(nameof(Admin.Id), request.Id)) > 0;
  244. }
  245. }
  246. }