MissiveAddresseeRepository.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.Entity.Missive;
  11. using GxPress.Repository.Interface.Missive;
  12. using GxPress.Request.App.Missive;
  13. using GxPress.Result.App.Missive;
  14. using Microsoft.Extensions.Options;
  15. using SqlKata;
  16. namespace GxPress.Repository.Implement.Missive
  17. {
  18. public class MissiveAddresseeRepository : IMissiveAddresseeRepository
  19. {
  20. private readonly Repository<MissiveAddressee> _repository;
  21. private readonly IMapper _mapper;
  22. private readonly string _connectionString;
  23. private readonly string _databaseTypeStr;
  24. public MissiveAddresseeRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  25. {
  26. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  27. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  28. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  29. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  30. _repository = new Repository<MissiveAddressee>(database);
  31. _mapper = mapper;
  32. }
  33. public IDatabase Database => _repository.Database;
  34. public string TableName => _repository.TableName;
  35. public List<TableColumn> TableColumns => _repository.TableColumns;
  36. public async Task<int> InsertAsync(MissiveAddressee missiveAddressee)
  37. {
  38. return await _repository.InsertAsync(missiveAddressee);
  39. }
  40. /// <summary>
  41. /// list 添加站内信收件人
  42. /// </summary>
  43. /// <param name="missiveAddressees"></param>
  44. /// <returns></returns>
  45. public async Task<bool> InsertAsync(List<MissiveAddressee> missiveAddressees)
  46. {
  47. if (missiveAddressees.Count == 0)
  48. return false;
  49. var sql = "INSERT INTO `ccpph`.`tede_missive_addressee`(`Guid`,`CreatedDate`,`LastModifiedDate`,`MissiveId`,`UserId`,`UserName`,`IsRead`,`IsAdmin`,`IsDelete`)VALUES";
  50. foreach (var item in missiveAddressees)
  51. {
  52. var guId = Guid.NewGuid().ToString();
  53. var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  54. var isRead = item.IsRead == true ? 1 : 0;
  55. var isAdmin = item.IsAdmin == true ? 1 : 0;
  56. var isDelete = item.IsDelete == true ? 1 : 0;
  57. sql += $"('{guId}','{createdDate}','{createdDate}',{item.MissiveId},{item.UserId},'',{isRead},{isAdmin},{isDelete}),";
  58. }
  59. sql = sql.Remove(sql.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 items = await connection.ExecuteScalarAsync<int>(sql);
  64. return items > 0;
  65. }
  66. public async Task<MissiveAddressee> GetAsync(int id)
  67. {
  68. return await _repository.GetAsync(id);
  69. }
  70. public async Task<bool> UpdateAsync(MissiveAddressee missiveAddressee)
  71. {
  72. return await _repository.UpdateAsync(missiveAddressee);
  73. }
  74. public async Task<bool> DeleteAsync(Query query)
  75. {
  76. return await _repository.DeleteAsync(query) > 0;
  77. }
  78. public async Task<bool> UpdateAsync(Query query)
  79. {
  80. return await _repository.UpdateAsync(query) > 0;
  81. }
  82. public async Task<IEnumerable<MissiveAddressee>> GetAllAsync(Query query)
  83. {
  84. return await _repository.GetAllAsync(query);
  85. }
  86. /// <summary>
  87. /// 获取站内信已读未读数据
  88. /// </summary>
  89. /// <param name="request"></param>
  90. /// <returns></returns>
  91. public async Task<IEnumerable<MissiveReadListResult>> GetMissiveReadListAsync(MissiveReadListRequest request)
  92. {
  93. var databaseType = _databaseTypeStr.ToEnum<DatabaseType>(DatabaseType.MySql);
  94. var database = new Database(databaseType, _connectionString);
  95. var connection = database.GetConnection();
  96. var sqlValue = "";
  97. if (request.ReadType > 0)
  98. {
  99. if (request.ReadType == 2)
  100. request.ReadType = 0;
  101. sqlValue += $" and a.IsRead={request.ReadType} ";
  102. }
  103. if (!string.IsNullOrWhiteSpace(request.Key))
  104. sqlValue += $" and b.Name like '%{request.Key}%'";
  105. var sql = $"SELECT a.*,b.Id,b.Name,b.AvatarUrl,c.Name FROM ccpph.tede_missive_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.MissiveId={request.MissiveId} {sqlValue} order by a.LastModifiedDate desc";
  106. var items = await connection
  107. .QueryAsync<MissiveReadListResult, User, Department, MissiveReadListResult>(sql,
  108. (missiveReadListResult, user, department) =>
  109. {
  110. missiveReadListResult.AvatarUrl = user == null ? "" : StringUtils.AddDomainMin(user.AvatarUrl);
  111. missiveReadListResult.DepartmentName = department == null ? "" : department.Name;
  112. missiveReadListResult.Name = user == null ? "" : user.Name;
  113. //missiveReadListResult.UserId=user == null ? 0 : user.Id;
  114. return missiveReadListResult;
  115. },
  116. splitOn: "Id,Name");
  117. return items;
  118. }
  119. }
  120. }