AdminVerifyRepository.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. 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}','{item.IsDelete}'),";
  58. }
  59. sql = sql + sqlValue.Remove(sqlValue.Length - 1, 1);
  60. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  61. var database = new Database(databaseType, _connectionString);
  62. var connection = database.GetConnection();
  63. var result = await connection.ExecuteAsync(sql);
  64. return result > 0;
  65. }
  66. /// <summary>
  67. /// 清空
  68. /// </summary>
  69. /// <param name="userId"></param>
  70. /// <param name="phone"></param>
  71. /// <returns></returns>
  72. public async Task<bool> DeleteAllAsync(int userId)
  73. {
  74. return await _repository.DeleteAsync(Q.Where(nameof(Entity.AdminVerify.AdminId), userId)) > 0;
  75. }
  76. /// <summary>
  77. /// 修改
  78. /// </summary>
  79. /// <param name="id"></param>
  80. /// <param name="disposeTyeConst"></param>
  81. /// <returns></returns>
  82. public async Task<bool> UpdateAsync(int id, AdminVerifyTypeConst disposeTyeConst)
  83. {
  84. return await _repository.UpdateAsync(Q.Set(nameof(Entity.AdminVerify.DisposeType), disposeTyeConst).Where(nameof(Entity.AdminVerify.Id), id)) > 0;
  85. }
  86. /// <summary>
  87. /// 根据ID查询
  88. /// </summary>
  89. /// <param name="id"></param>
  90. /// <returns></returns>
  91. public async Task<Entity.AdminVerify> GetAsync(int id)
  92. {
  93. return await _repository.GetAsync(id);
  94. }
  95. /// <summary>
  96. /// 根据ID查询
  97. /// </summary>
  98. /// <param name="id"></param>
  99. /// <returns></returns>
  100. public async Task<Entity.AdminVerify> GetAsync(SqlKata.Query query)
  101. {
  102. return await _repository.GetAsync(query);
  103. }
  104. /// <summary>
  105. /// 根据用户查询
  106. /// </summary>
  107. /// <param name="userId"></param>
  108. /// <param name="phone"></param>
  109. /// <returns></returns>
  110. public async Task<IEnumerable<Entity.AdminVerify>> GetAllAsync(SqlKata.Query query)
  111. {
  112. return await _repository.GetAllAsync(query);
  113. }
  114. /// <summary>
  115. /// 查询未处理
  116. /// </summary>
  117. /// <param name="userId"></param>
  118. /// <returns></returns>
  119. public async Task<int> GetUDisposeCount(int userId)
  120. {
  121. return await _repository.CountAsync(Q.Where(nameof(Entity.AdminVerify.AdminId), userId).Where(nameof(Entity.AdminVerify.DisposeType), AdminVerifyTypeConst.UDispose));
  122. }
  123. public async Task<bool> UpdateAsync(Query query)
  124. {
  125. return await _repository.UpdateAsync(query) > 0;
  126. }
  127. /// <summary>
  128. /// 获取用户的验证时间信息
  129. /// </summary>
  130. /// <param name="userId"></param>
  131. /// <returns></returns>
  132. public async Task<string> GetConfirmationDate(int userId)
  133. {
  134. var query = Q.NewQuery();
  135. query.Select(nameof(Entity.AdminVerify.CreatedDate));
  136. query.Where(nameof(Entity.AdminVerify.AdminId), userId);
  137. query.OrderByDesc(nameof(Entity.AdminVerify.CreatedDate));
  138. return await _repository.GetAsync<string>(query);
  139. }
  140. }
  141. }