TopicAddresseeRepository.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.Topic;
  7. using GxPress.Repository.Interface;
  8. using Microsoft.Extensions.Options;
  9. using Datory;
  10. using SqlKata;
  11. using Dapper;
  12. using System;
  13. using System.Linq;
  14. namespace GxPress.Repository.Implement
  15. {
  16. public class TopicAddresseeRepository : ITopicAddresseeRepository
  17. {
  18. private readonly Repository<TopicAddressee> _repository;
  19. private readonly IMapper _mapper;
  20. private readonly string _connectionString;
  21. private readonly string _databaseTypeStr;
  22. public TopicAddresseeRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  23. {
  24. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  25. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  26. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  27. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  28. _repository = new Repository<TopicAddressee>(database);
  29. _mapper = mapper;
  30. }
  31. public IDatabase Database => _repository.Database;
  32. public string TableName => _repository.TableName;
  33. public List<TableColumn> TableColumns => _repository.TableColumns;
  34. public async Task<int> InsertAsync(TopicAddressee request)
  35. {
  36. return await _repository.InsertAsync(request);
  37. }
  38. /// <summary>
  39. /// 添加话题收件人
  40. /// </summary>
  41. /// <param name="request"></param>
  42. /// <returns></returns>
  43. public async Task<bool> InsertAsync(IEnumerable<TopicAddressee> request)
  44. {
  45. var sql = "INSERT INTO `ccpph`.`tede_topic_addressee`(`Guid`,`CreatedDate`,`LastModifiedDate`,`TopicId`,`UserId`,`UserName`,`IsRead`,`IsAdmin`,`IsUpload`)VALUES";
  46. if (request.Count() == 0)
  47. return true;
  48. foreach (var item in request)
  49. {
  50. var isAdmin = item.IsAdmin ? 1 : 0;
  51. sql += $"('{Guid.NewGuid().ToString()}','{DateTime.Now.ToString()}','{DateTime.Now.ToString()}',{item.TopicId},{item.UserId},'',0,{isAdmin},0),";
  52. }
  53. sql = sql.Remove(sql.Length - 1, 1);
  54. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  55. var database = new Database(databaseType, _connectionString);
  56. var connection = database.GetConnection();
  57. return await connection.ExecuteScalarAsync<int>(sql) > 0;
  58. }
  59. public async Task<IEnumerable<TopicAddressee>> GetAllAsync(Query query)
  60. {
  61. return await _repository.GetAllAsync(query);
  62. }
  63. public async Task<bool> DeleteAsync(Query query)
  64. {
  65. return await _repository.DeleteAsync(query) > 0;
  66. }
  67. /// <summary>
  68. /// 根据topicId删除
  69. /// </summary>
  70. /// <param name="topicId"></param>
  71. /// <returns></returns>
  72. public async Task<bool> DeletByTopicIdAsync(int topicId)
  73. {
  74. return await _repository.DeleteAsync(Q.Where(nameof(Entity.Topic.TopicAddressee.TopicId), topicId)) > 0;
  75. }
  76. public async Task<bool> UpdateAsync(Query query)
  77. {
  78. return await _repository.UpdateAsync(query) > 0;
  79. }
  80. }
  81. }