AdminVerifyRepository.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using Dapper;
  6. using Datory;
  7. using GxPress.Common.AppOptions;
  8. using GxPress.Common.Tools;
  9. using GxPress.Entity;
  10. using GxPress.EnumConst;
  11. using GxPress.Repository.Interface.AdminVerify;
  12. using Microsoft.Extensions.Options;
  13. using SqlKata;
  14. namespace GxPress.Repository.Implement.AdminVerify
  15. {
  16. public class AdminVerifyRepository : IAdminVerifyRepository
  17. {
  18. private readonly Repository<Entity.AdminVerify> _repository;
  19. private readonly IMapper _mapper;
  20. private readonly string _connectionString;
  21. private readonly string _databaseTypeStr;
  22. private readonly Repository<GroupUser> _groupUserRepository;
  23. public AdminVerifyRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  24. {
  25. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  26. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  27. var databaseType =
  28. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  29. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  30. _repository = new Repository<Entity.AdminVerify>(database);
  31. _groupUserRepository = new Repository<GroupUser>(database);
  32. _mapper = mapper;
  33. }
  34. public IDatabase Database => _repository.Database;
  35. public string TableName => _repository.TableName;
  36. public List<TableColumn> TableColumns => _repository.TableColumns;
  37. /// <summary>
  38. /// 添加
  39. /// </summary>
  40. /// <param name="adminVerify"></param>
  41. /// <returns></returns>
  42. public async Task<int> InsertAsync(Entity.AdminVerify adminVerify)
  43. {
  44. return await _repository.InsertAsync(adminVerify);
  45. }
  46. public async Task<bool> InsertAsync(List<Entity.AdminVerify> adminVerifys)
  47. {
  48. string sql = @"INSERT INTO `tede_admin_verify`(`Guid`,`CreatedDate`,`LastModifiedDate`,`VerifyType`,`DisposeType`,
  49. `AdminId`,`UserId`,`SourceId`,`Name`,`AvatarUrl`,`Remark`,`SourceType`,`SourceName`,`IsDelete`)VALUES";
  50. if (adminVerifys.Count == 0)
  51. return false;
  52. var sqlValue = "";
  53. foreach (var item in adminVerifys)
  54. {
  55. var guId = Guid.NewGuid().ToString();
  56. var createdDate = DateTime.Now.ToString();
  57. var isDelete = item.IsDelete ? 1 : 0;
  58. sqlValue += $@"('{guId}','{createdDate}','{createdDate}',{item.VerifyType.GetHashCode()},{item.DisposeType},{item.AdminId},{item.UserId},{item.SourceId},'{item.Name}','{item.AvatarUrl}','{item.Remark}',{item.SourceType},'{item.SourceName}',{isDelete}),";
  59. }
  60. sql = sql + sqlValue.Remove(sqlValue.Length - 1, 1);
  61. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  62. var database = new Database(databaseType, _connectionString);
  63. var connection = database.GetConnection();
  64. var result = await connection.ExecuteAsync(sql);
  65. return result > 0;
  66. }
  67. /// <summary>
  68. /// 清空
  69. /// </summary>
  70. /// <param name="userId"></param>
  71. /// <param name="phone"></param>
  72. /// <returns></returns>
  73. public async Task<bool> DeleteAllAsync(int userId)
  74. {
  75. var query = Q.NewQuery();
  76. query.Where(nameof(Entity.AdminVerify.AdminId), userId);
  77. query.Set(nameof(Entity.AdminVerify.IsDelete), true);
  78. query.Set(nameof(Entity.AdminVerify.DisposeType), 2);
  79. return await _repository.UpdateAsync(query) > 0;
  80. }
  81. /// <summary>
  82. /// 修改
  83. /// </summary>
  84. /// <param name="id"></param>
  85. /// <param name="disposeTyeConst"></param>
  86. /// <returns></returns>
  87. public async Task<bool> UpdateAsync(int id, AdminVerifyTypeConst disposeTyeConst)
  88. {
  89. return await _repository.UpdateAsync(Q.Set(nameof(Entity.AdminVerify.DisposeType), disposeTyeConst).Where(nameof(Entity.AdminVerify.Id), id)) > 0;
  90. }
  91. /// <summary>
  92. /// 根据ID查询
  93. /// </summary>
  94. /// <param name="id"></param>
  95. /// <returns></returns>
  96. public async Task<Entity.AdminVerify> GetAsync(int id)
  97. {
  98. return await _repository.GetAsync(id);
  99. }
  100. /// <summary>
  101. /// 根据ID查询
  102. /// </summary>
  103. /// <param name="id"></param>
  104. /// <returns></returns>
  105. public async Task<Entity.AdminVerify> GetAsync(SqlKata.Query query)
  106. {
  107. return await _repository.GetAsync(query);
  108. }
  109. /// <summary>
  110. /// 根据用户查询
  111. /// </summary>
  112. /// <param name="userId"></param>
  113. /// <param name="phone"></param>
  114. /// <returns></returns>
  115. public async Task<IEnumerable<Entity.AdminVerify>> GetAllAsync(SqlKata.Query query)
  116. {
  117. return await _repository.GetAllAsync(query.Where(nameof(Entity.AdminVerify.IsDelete),false));
  118. }
  119. /// <summary>
  120. /// 查询未处理
  121. /// </summary>
  122. /// <param name="userId"></param>
  123. /// <returns></returns>
  124. public async Task<int> GetUDisposeCount(int userId)
  125. {
  126. var query = Q.NewQuery();
  127. query.Where(nameof(Entity.AdminVerify.AdminId), userId);
  128. query.Where(nameof(Entity.AdminVerify.DisposeType), AdminVerifyTypeConst.UDispose);
  129. query.Where(nameof(Entity.AdminVerify.IsDelete), false);
  130. return await _repository.CountAsync();
  131. }
  132. public async Task<bool> UpdateAsync(Query query)
  133. {
  134. return await _repository.UpdateAsync(query) > 0;
  135. }
  136. /// <summary>
  137. /// 获取用户的验证时间信息
  138. /// </summary>
  139. /// <param name="userId"></param>
  140. /// <returns></returns>
  141. public async Task<DateTime> GetConfirmationDate(int userId)
  142. {
  143. var query = Q.NewQuery();
  144. query.Select(nameof(Entity.AdminVerify.CreatedDate));
  145. query.Where(nameof(Entity.AdminVerify.AdminId), userId);
  146. query.OrderByDesc(nameof(Entity.AdminVerify.CreatedDate));
  147. return await _repository.GetAsync<DateTime>(query);
  148. }
  149. }
  150. }