MissiveCcRepository.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.Missive;
  10. using GxPress.Repository.Interface.Missive;
  11. using Microsoft.Extensions.Caching.Distributed;
  12. using Microsoft.Extensions.Options;
  13. using SqlKata;
  14. namespace GxPress.Repository.Implement.Missive
  15. {
  16. public class MissiveCcRepository : IMissiveCcRepository
  17. {
  18. private readonly Repository<MissiveCc> _repository;
  19. private readonly IMapper _mapper;
  20. private readonly IDistributedCache _cache;
  21. private readonly string _connectionString;
  22. private readonly string _databaseTypeStr;
  23. public MissiveCcRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper, IDistributedCache cache)
  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<MissiveCc>(database);
  30. _mapper = mapper;
  31. _cache = cache;
  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(MissiveCc missiveCc)
  37. {
  38. return await _repository.InsertAsync(missiveCc);
  39. }
  40. /// <summary>
  41. /// list 添加站内信抄送人
  42. /// </summary>
  43. /// <param name="missiveCcs"></param>
  44. /// <returns></returns>
  45. public async Task<bool> InsertAsync(List<MissiveCc> missiveCcs)
  46. {
  47. if (missiveCcs.Count == 0)
  48. return false;
  49. var sql = "INSERT INTO `ccpph`.`tede_missive_cc`(`Guid`,`CreatedDate`,`LastModifiedDate`,`MissiveId`,`UserId`,`UserName`)VALUES";
  50. foreach (var item in missiveCcs)
  51. {
  52. var guId = Guid.NewGuid().ToString();
  53. var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  54. sql += $"('{guId}','{createdDate}','{createdDate}',{item.MissiveId},{item.UserId},''),";
  55. }
  56. sql = sql.Remove(sql.Length - 1, 1);
  57. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  58. var database = new Database(databaseType, _connectionString);
  59. var connection = database.GetConnection();
  60. var items = await connection.ExecuteScalarAsync<int>(sql);
  61. return items > 0;
  62. }
  63. public async Task<MissiveCc> GetAsync(int id)
  64. {
  65. return await _repository.GetAsync(id);
  66. }
  67. public async Task<bool> UpdateAsync(MissiveCc missiveCc)
  68. {
  69. return await _repository.UpdateAsync(missiveCc);
  70. }
  71. public async Task<bool> DeleteAsync(Query query)
  72. {
  73. return await _repository.DeleteAsync(query) > 0;
  74. }
  75. public async Task<bool> UpdateAsync(Query query)
  76. {
  77. return await _repository.UpdateAsync(query) > 0;
  78. }
  79. public async Task<IEnumerable<MissiveCc>> GetAllAsync(Query query)
  80. {
  81. return await _repository.GetAllAsync(query);
  82. }
  83. }
  84. }