NoticeAddresseeRepository.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 GxPress.Repository.Interface;
  8. using Microsoft.Extensions.Options;
  9. using Datory;
  10. using GxPress.Request.Notice;
  11. using SqlKata;
  12. using GxPress.Result.Notice;
  13. using Dapper;
  14. using System;
  15. namespace GxPress.Repository.Implement
  16. {
  17. public class NoticeAddresseeRepository : INoticeAddresseeRepository
  18. {
  19. private readonly Repository<NoticeAddressee> _repository;
  20. private readonly IMapper _mapper;
  21. private readonly string _connectionString;
  22. private readonly string _databaseTypeStr;
  23. public NoticeAddresseeRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  24. {
  25. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  26. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  27. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  28. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  29. _repository = new Repository<NoticeAddressee>(database);
  30. _mapper = mapper;
  31. }
  32. public IDatabase Database => _repository.Database;
  33. public string TableName => _repository.TableName;
  34. public List<TableColumn> TableColumns => _repository.TableColumns;
  35. /// <summary>
  36. /// 根据通知ID获取收件人
  37. /// </summary>
  38. /// <param name="noticeId"></param>
  39. /// <returns></returns>
  40. public async Task<IEnumerable<NoticeAddressee>> GetNoticeAddresseeByNoticeIdAsync(int noticeId)
  41. {
  42. return await _repository.GetAllAsync(Q.Where(nameof(NoticeAddressee.NoticeId), noticeId));
  43. }
  44. public async Task<bool> DeleteAsync(int noticeId)
  45. {
  46. return await _repository.DeleteAsync(Q.Where(nameof(NoticeAddressee.NoticeId), noticeId)) > 0;
  47. }
  48. public async Task<int> InsertAsync(NoticeAddressee noticeAddressee)
  49. {
  50. return await _repository.InsertAsync(noticeAddressee);
  51. }
  52. /// <summary>
  53. /// 插入收件人
  54. /// </summary>
  55. /// <param name="noticeAddressees"></param>
  56. /// <returns></returns>
  57. public async Task<bool> InsertAsync(List<NoticeAddressee> noticeAddressees)
  58. {
  59. if (noticeAddressees.Count == 0)
  60. return false;
  61. var sql = "INSERT INTO `ccpph`.`tede_notice_addressee`(`Guid`,`CreatedDate`,`LastModifiedDate`,`NoticeId`,`UserId`,`UserName`,`IsRead`,`NoticeAddresseeType`,`NoticeFolderId`,`IsTop`,`IsAdmin`,`IsDelete`)VALUES";
  62. foreach (var item in noticeAddressees)
  63. {
  64. var guId = Guid.NewGuid().ToString();
  65. var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  66. var isRead = item.IsRead == true ? 1 : 0;
  67. var isTop = item.IsTop == true ? 1 : 0;
  68. var isAdmin = item.IsAdmin == true ? 1 : 0;
  69. var isDelete = item.IsDelete == true ? 1 : 0;
  70. sql += $"('{guId}','{createdDate}','{createdDate}',{item.NoticeId},{item.UserId},'',{isRead},{item.NoticeAddresseeType.GetHashCode()},{item.NoticeFolderId},{isTop},{isAdmin},{isDelete}),";
  71. }
  72. sql = sql.Remove(sql.Length - 1, 1);
  73. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  74. var database = new Database(databaseType, _connectionString);
  75. var connection = database.GetConnection();
  76. var items = await connection.ExecuteScalarAsync<int>(sql);
  77. return items > 0;
  78. }
  79. /// <summary>
  80. /// 查询通知的未读已读
  81. /// </summary>
  82. /// <param name="request"></param>
  83. /// <returns></returns>
  84. public async Task<IEnumerable<NoticeReadListResult>> GetNoticeAddresseeByUserIdIdAsync(NoticeReadListRequest request)
  85. {
  86. var sqlValue = "";
  87. if (request.ReadType == 1)
  88. sqlValue += " and a.IsRead=1";
  89. else if (request.ReadType == 2)
  90. sqlValue += " and a.IsRead=0";
  91. if (!string.IsNullOrWhiteSpace(request.Key))
  92. sqlValue += $" and b.Name like '%{request.Key}%'";
  93. var sql = $"SELECT a.*,b.Name,b.AvatarUrl,c.Name FROM ccpph.tede_notice_addressee a inner join ccpph.tede_user b on a.UserId=b.Id inner join ccpph.tede_department c on c.Id=b.DepartmentId where a.NoticeId={request.NoticeId} {sqlValue} order by a.LastModifiedDate desc";
  94. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  95. var database = new Database(databaseType, _connectionString);
  96. var connection = database.GetConnection();
  97. var items = await connection
  98. .QueryAsync<NoticeReadListResult, User, Department, NoticeReadListResult>(sql,
  99. (noticeReadListResult, user, department) =>
  100. {
  101. noticeReadListResult.Name = user != null ? user.Name : "";
  102. noticeReadListResult.AvatarUrl = user != null ? StringUtils.AddDomainMin(user.AvatarUrl) : "";
  103. noticeReadListResult.DepartmentName = department.Name;
  104. noticeReadListResult.LastModifiedDate = Convert.ToDateTime(noticeReadListResult.LastModifiedDate).ToString("yyyy-MM-dd HH:mm:ss");
  105. return noticeReadListResult;
  106. },
  107. splitOn: "Name,Name");
  108. return items;
  109. }
  110. /// <summary>
  111. /// 获取收件人
  112. /// </summary>
  113. /// <param name="noticeId"></param>
  114. /// <returns></returns>
  115. public async Task<IEnumerable<User>> GetUserByNoticeIdAsync(int noticeId)
  116. {
  117. var sql = $"SELECT b.* FROM ccpph.tede_notice_addressee a inner join ccpph.tede_user b on a.UserId=b.Id where a.NoticeId={noticeId} and a.IsAdmin=0;";
  118. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  119. var database = new Database(databaseType, _connectionString);
  120. var connection = database.GetConnection();
  121. var items = await connection
  122. .QueryAsync<User>(sql);
  123. return items;
  124. }
  125. public async Task<int> CountAsync(Query query)
  126. {
  127. return await _repository.CountAsync(query);
  128. }
  129. public async Task<IEnumerable<NoticeAddressee>> GetAllAsync(Query query)
  130. {
  131. return await _repository.GetAllAsync(query);
  132. }
  133. public async Task<bool> UpdateAsync(Query query)
  134. {
  135. return await _repository.UpdateAsync(query) > 0;
  136. }
  137. public async Task<bool> ExistsAsync(Query query)
  138. {
  139. return await _repository.ExistsAsync(query);
  140. }
  141. }
  142. }