AddresseeRepository.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using AutoMapper;
  4. using GxPress.Common.AppOptions;
  5. using GxPress.Common.Tools;
  6. using GxPress.Entity;
  7. using Microsoft.Extensions.Options;
  8. using Datory;
  9. using GxPress.Request.Notice;
  10. using SqlKata;
  11. using GxPress.Result.Notice;
  12. using Dapper;
  13. using System;
  14. using GxPress.Repository.Interface;
  15. using GxPress.Result.App.Missive;
  16. using GxPress.Request.App.Missive;
  17. namespace GxPress.Repository.Implement
  18. {
  19. public class NoticeAddresseeRepository : IAddresseeRepository
  20. {
  21. private readonly Repository<Addressee> _repository;
  22. private readonly IMapper _mapper;
  23. private readonly string _connectionString;
  24. private readonly string _databaseTypeStr;
  25. public NoticeAddresseeRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  26. {
  27. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  28. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  29. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  30. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  31. _repository = new Repository<Addressee>(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. /// 根据通知ID获取收件人
  39. /// </summary>
  40. /// <param name="noticeId"></param>
  41. /// <returns></returns>
  42. public async Task<IEnumerable<Addressee>> GetNoticeAddresseeByNoticeIdAsync(int noticeId)
  43. {
  44. return await _repository.GetAllAsync(Q.Where(nameof(Addressee.SourceId), noticeId));
  45. }
  46. public async Task<bool> DeleteAsync(int noticeId, int sourceType, int isAdmin)
  47. {
  48. var query = new SqlKata.Query();
  49. if (isAdmin == 0)
  50. query.Where(nameof(Addressee.IsAdmin), false);
  51. if (isAdmin == 1)
  52. query.Where(nameof(Addressee.IsAdmin), true);
  53. query.Where(nameof(Addressee.SourceId), noticeId);
  54. query.Where(nameof(Addressee.SourceType), sourceType);
  55. return await _repository.DeleteAsync(query) > 0;
  56. }
  57. public async Task<int> InsertAsync(Addressee noticeAddressee)
  58. {
  59. return await _repository.InsertAsync(noticeAddressee);
  60. }
  61. /// <summary>
  62. /// 插入收件人
  63. /// </summary>
  64. /// <param name="noticeAddressees"></param>
  65. /// <returns></returns>
  66. public async Task<bool> InsertAsync(List<Addressee> noticeAddressees)
  67. {
  68. if (noticeAddressees.Count == 0)
  69. return false;
  70. var sql = "INSERT INTO `tede_addressee`(`Guid`,`CreatedDate`,`LastModifiedDate`,`SourceId`,`SourceType`,`UserId`,`UserName`,`IsRead`,`NoticeAddresseeType`,`IsTop`,`IsAdmin`,`IsDelete`)VALUES";
  71. foreach (var item in noticeAddressees)
  72. {
  73. var guId = Guid.NewGuid().ToString();
  74. var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  75. var isRead = item.IsRead == true ? 1 : 0;
  76. var isTop = item.IsTop == true ? 1 : 0;
  77. var isAdmin = item.IsAdmin == true ? 1 : 0;
  78. var isDelete = item.IsDelete == true ? 1 : 0;
  79. sql += $"('{guId}','{createdDate}','{createdDate}',{item.SourceId},{item.SourceType},{item.UserId},'',{isRead},{item.NoticeAddresseeType.GetHashCode()},{isTop},{isAdmin},{isDelete}),";
  80. }
  81. sql = sql.Remove(sql.Length - 1, 1);
  82. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  83. var database = new Database(databaseType, _connectionString);
  84. var connection = database.GetConnection();
  85. var items = await connection.ExecuteAsync(sql);
  86. return items > 0;
  87. }
  88. /// <summary>
  89. /// 查询通知的未读已读
  90. /// </summary>
  91. /// <param name="request"></param>
  92. /// <returns></returns>
  93. public async Task<IEnumerable<NoticeReadListResult>> GetNoticeAddresseeByUserIdIdAsync(NoticeReadListRequest request)
  94. {
  95. var query = new SqlKata.Query("tede_addressee");
  96. query.Join("tede_user as b", "b.Id", "tede_addressee.UserId");
  97. query.Where("tede_addressee.SourceId", request.NoticeId);
  98. if (!string.IsNullOrEmpty(request.Key))
  99. query.WhereLike("b.Name", $"%{request.Key}%");
  100. if (request.ReadType > 0)
  101. query.Where("tede_addressee.IsRead", request.ReadType == 1);
  102. query.OrderByDesc("tede_addressee.LastModifiedDate");
  103. var departmentNameQuery = new SqlKata.Query("tede_department");
  104. var departmentIdsQuery = new SqlKata.Query("tede_department_user");
  105. departmentIdsQuery.WhereColumns("tede_department_user.userId", "=", "tede_addressee.UserId");
  106. departmentIdsQuery.Select("DepartmentId");
  107. departmentNameQuery.WhereIn(nameof(Entity.Department.Id), departmentIdsQuery);
  108. departmentNameQuery.Limit(1);
  109. departmentNameQuery.Select("Name");
  110. query.Select(departmentNameQuery, "DepartmentName");
  111. query.Select("tede_addressee.{SourceId as NoticeId,LastModifiedDate,UserId}");
  112. query.Select("b.{AvatarUrl,Name}");
  113. var result = await _repository.GetAllAsync<NoticeReadListResult>(query);
  114. foreach (var item in result)
  115. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  116. return result;
  117. }
  118. /// <summary>
  119. /// 获取收件人
  120. /// </summary>
  121. /// <param name="noticeId"></param>
  122. /// <returns></returns>
  123. public async Task<IEnumerable<User>> GetUserByNoticeIdAsync(int noticeId)
  124. {
  125. var sql = $"SELECT b.* FROM tede_addressee a inner join tede_user b on a.UserId=b.Id where a.SourceId={noticeId} and a.IsAdmin=0;";
  126. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  127. var database = new Database(databaseType, _connectionString);
  128. var connection = database.GetConnection();
  129. var items = await connection
  130. .QueryAsync<User>(sql);
  131. return items;
  132. }
  133. public async Task<int> CountAsync(Query query)
  134. {
  135. return await _repository.CountAsync(query);
  136. }
  137. public async Task<IEnumerable<Addressee>> GetAllAsync(Query query)
  138. {
  139. return await _repository.GetAllAsync(query);
  140. }
  141. public async Task<bool> UpdateAsync(Query query)
  142. {
  143. return await _repository.UpdateAsync(query) > 0;
  144. }
  145. public async Task<bool> ExistsAsync(Query query)
  146. {
  147. return await _repository.ExistsAsync(query);
  148. }
  149. /// <summary>
  150. /// 站内信未读
  151. /// </summary>
  152. /// <param name="request"></param>
  153. /// <returns></returns>
  154. public async Task<IEnumerable<MissiveReadListResult>> GetMissiveReadListAsync(MissiveReadListRequest request)
  155. {
  156. var query = new SqlKata.Query("tede_addressee");
  157. query.Join("tede_user as b", "b.Id", "tede_addressee.UserId");
  158. query.Where("tede_addressee.SourceId", request.MissiveId);
  159. if (!string.IsNullOrEmpty(request.Key))
  160. query.WhereLike("b.Name", $"%{request.Key}%");
  161. if (request.ReadType > 0)
  162. query.Where("tede_addressee.IsRead", request.ReadType == 1);
  163. query.OrderByDesc("tede_addressee.LastModifiedDate");
  164. var departmentNameQuery = new SqlKata.Query("tede_department");
  165. var departmentIdsQuery = new SqlKata.Query("tede_department_user");
  166. departmentIdsQuery.WhereColumns("tede_department_user.userId", "=", "tede_addressee.UserId");
  167. departmentIdsQuery.Select("DepartmentId");
  168. departmentNameQuery.WhereIn(nameof(Entity.Department.Id), departmentIdsQuery);
  169. departmentNameQuery.Limit(1);
  170. departmentNameQuery.Select("Name");
  171. query.Select(departmentNameQuery, "DepartmentName");
  172. query.Select("tede_addressee.{SourceId as MissiveId,LastModifiedDate,UserId}");
  173. query.Select("b.{AvatarUrl,Name}");
  174. var result = await _repository.GetAllAsync<MissiveReadListResult>(query);
  175. foreach (var item in result)
  176. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  177. return result;
  178. }
  179. }
  180. }