SystemLableMediaRepository.cs 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using AutoMapper;
  4. using Datory;
  5. using GxPress.Common.AppOptions;
  6. using GxPress.Common.Tools;
  7. using GxPress.Repository.Interface.SystemLabel;
  8. using GxPress.Request.SystemLabel;
  9. using Microsoft.Extensions.Options;
  10. using System.Transactions;
  11. using Dapper;
  12. namespace GxPress.Repository.Implement.SystemLabel
  13. {
  14. public class SystemLableMediaRepository : ISystemLableMediaRepository
  15. {
  16. private readonly Repository<Entity.SystemLabel.SystemLableMedia> _repository;
  17. private readonly IMapper _mapper;
  18. private readonly string _connectionString;
  19. private readonly string _databaseTypestr;
  20. private readonly Repository<Entity.SystemLabel.SystemLableMedia> mediaLableRepository;
  21. public SystemLableMediaRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  22. {
  23. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  24. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  25. var databaseType =
  26. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  27. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  28. _repository = new Repository<Entity.SystemLabel.SystemLableMedia>(database);
  29. _mapper = mapper;
  30. mediaLableRepository = new Repository<Entity.SystemLabel.SystemLableMedia>(database);
  31. }
  32. public IDatabase Database => _repository.Database;
  33. public string TableName => _repository.TableName;
  34. public List<TableColumn> TableColumns => _repository.TableColumns;
  35. public async Task<Entity.SystemLabel.SystemLableMedia> GetAsync(int id)
  36. {
  37. return await _repository.GetAsync(id);
  38. }
  39. /// <summary>
  40. /// 根据标签ID获取媒体ID
  41. /// </summary>
  42. /// <param name="lableId"></param>
  43. /// <returns></returns>
  44. public async Task<IEnumerable<int>> GetMediaIdsAsync(int lableId, int typeValue)
  45. {
  46. return await _repository.GetAllAsync<int>(Q.Where(nameof(Entity.SystemLabel.SystemLableMedia.LableId), lableId).Where(nameof(Entity.SystemLabel.SystemLableMedia.TypeValue), typeValue).Select(nameof(Entity.SystemLabel.SystemLableMedia.MediaId)));
  47. }
  48. /// <summary>
  49. /// 根据分类获取媒体ID
  50. /// </summary>
  51. /// <param name="lableId"></param>
  52. /// <returns></returns>
  53. public async Task<IEnumerable<int>> GetMediaIdsByTypeValueAsync(int typeValue)
  54. {
  55. return await _repository.GetAllAsync<int>(Q.Where(nameof(Entity.SystemLabel.SystemLableMedia.TypeValue), typeValue).Select(nameof(Entity.SystemLabel.SystemLableMedia.MediaId)));
  56. }
  57. public async Task<bool> DeleteAsync(List<int> ids)
  58. {
  59. return await _repository.DeleteAsync(Q.WhereIn(nameof(Entity.SystemLabel.SystemLableMedia.Id), ids)) > 0;
  60. }
  61. /// <summary>
  62. /// 添加
  63. /// </summary>
  64. /// <param name="request"></param>
  65. /// <returns></returns>
  66. public async Task<bool> InsertAsync(SystemLableMediaRequest request)
  67. {
  68. try
  69. {
  70. using (var transactions = new TransactionScope())
  71. {
  72. await _repository.DeleteAsync(Q.Where(nameof(Entity.SystemLabel.SystemLableMedia.LableId), request.LableId));
  73. var sql = $@"INSERT INTO `tede_system_lable_media`(`Guid`,`CreatedDate`,`LastModifiedDate`,`LableId`,`MediaId`,`TypeValue`) VALUES";
  74. var createdDate = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
  75. foreach (var item in request.MediaIds)
  76. {
  77. var guid = System.Guid.NewGuid().ToString();
  78. sql += $@"('{guid}','{createdDate}','{createdDate}',{request.LableId},{item},{request.TypeValue}),";
  79. }
  80. sql = sql.Remove(sql.Length - 1, 1);
  81. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  82. var database = new Database(databaseType, _connectionString);
  83. var connection = database.GetConnection();
  84. await connection.ExecuteAsync(sql);
  85. transactions.Complete();
  86. }
  87. }
  88. catch
  89. {
  90. return false;
  91. }
  92. return true;
  93. }
  94. }
  95. }