CcRepository.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 System;
  11. using Dapper;
  12. namespace GxPress.Repository.Implement
  13. {
  14. public class CcRepository : ICcRepository
  15. {
  16. private readonly Repository<Cc> _repository;
  17. private readonly IMapper _mapper;
  18. private readonly string _connectionString;
  19. private readonly string _databaseTypeStr;
  20. public CcRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  21. {
  22. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  23. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  24. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  25. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  26. _repository = new Repository<Cc>(database);
  27. _mapper = mapper;
  28. }
  29. public IDatabase Database => _repository.Database;
  30. public string TableName => _repository.TableName;
  31. public List<TableColumn> TableColumns => _repository.TableColumns;
  32. /// <summary>
  33. /// 根据通知ID获取抄送人
  34. /// </summary>
  35. /// <param name="noticeId"></param>
  36. /// <returns></returns>
  37. public async Task<IEnumerable<Cc>> GetNoticeCcByNoticeIdAsync(int noticeId, int sourceType)
  38. {
  39. return await _repository.GetAllAsync(Q.Where(nameof(Cc.SourceId), noticeId).Where(nameof(Cc.SourceType), sourceType));
  40. }
  41. /// <summary>
  42. /// 删除
  43. /// </summary>
  44. /// <param name="sourceId"></param>
  45. /// <param name="sourceType"></param>
  46. /// <returns></returns>
  47. public async Task<bool> DeleteAsync(int sourceId, int sourceType)
  48. {
  49. return await _repository.DeleteAsync(Q.Where(nameof(Cc.SourceId), sourceId).Where(nameof(Cc.SourceType), sourceType)) > 0;
  50. }
  51. public async Task<int> InsertAsync(Cc cc)
  52. {
  53. return await _repository.InsertAsync(cc);
  54. }
  55. /// <summary>
  56. /// 添加通知抄送人
  57. /// </summary>
  58. /// <param name="noticeCcs"></param>
  59. /// <returns></returns>
  60. public async Task<bool> InsertAsync(List<Cc> noticeCcs)
  61. {
  62. if (noticeCcs.Count == 0)
  63. return false;
  64. var sql = "INSERT INTO `tede_cc`(`Guid`,`CreatedDate`,`LastModifiedDate`,`SourceId`,`SourceType`,`UserId`,`UserName`,`IsRead`)VALUES";
  65. foreach (var item in noticeCcs)
  66. {
  67. var guId = Guid.NewGuid().ToString();
  68. var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  69. var isRead = item.IsRead == true ? 1 : 0;
  70. sql += $"('{guId}','{createdDate}','{createdDate}',{item.SourceId},{item.SourceType},{item.UserId},'',{isRead}),";
  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.ExecuteAsync(sql);
  77. return items > 0;
  78. }
  79. }
  80. }