GroupCategroyRepository.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.Group;
  8. using GxPress.Result.Web;
  9. using Microsoft.Extensions.Caching.Distributed;
  10. using Microsoft.Extensions.Options;
  11. using System.Transactions;
  12. using Dapper;
  13. namespace GxPress.Repository.Implement.Group
  14. {
  15. /// <summary>
  16. /// 小组广场
  17. /// </summary>
  18. public class GroupCategroyRepository : IGroupCategroyRepository
  19. {
  20. private readonly Repository<Entity.tede2.Group.GroupCategroy> _repository;
  21. private readonly Repository<Entity.tede2.Group.UserGroupCategory> userGroupCategoryRepository;
  22. private readonly IMapper _mapper;
  23. private readonly IDistributedCache _cache;
  24. private readonly string _connectionString;
  25. private readonly string _databaseTypestr;
  26. public GroupCategroyRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper, IDistributedCache cache)
  27. {
  28. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  29. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  30. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  31. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  32. _mapper = mapper;
  33. _cache = cache;
  34. _repository = new Repository<Entity.tede2.Group.GroupCategroy>(database);
  35. userGroupCategoryRepository = new Repository<Entity.tede2.Group.UserGroupCategory>(database);
  36. }
  37. public IDatabase Database => _repository.Database;
  38. public string TableName => _repository.TableName;
  39. public List<TableColumn> TableColumns => _repository.TableColumns;
  40. /// <summary>
  41. /// 添加小组广场
  42. /// </summary>
  43. /// <param name="groupCategroy"></param>
  44. /// <returns></returns>
  45. public async Task<int> InsertAsync(Entity.tede2.Group.GroupCategroy groupCategroy)
  46. {
  47. groupCategroy.ImageUrls = StringUtils.RemoveDomain(groupCategroy.ImageUrls);
  48. return await _repository.InsertAsync(groupCategroy);
  49. }
  50. public async Task<bool> UpdateAsync(Entity.tede2.Group.GroupCategroy groupCategroy)
  51. {
  52. if (groupCategroy.Id > 0)
  53. {
  54. var model = await GetAsync(groupCategroy.Id);
  55. if (!string.IsNullOrEmpty(groupCategroy.PiazzaName))
  56. model.PiazzaName = groupCategroy.PiazzaName;
  57. if (groupCategroy.ParentId > 0)
  58. model.ParentId = groupCategroy.ParentId;
  59. if (string.IsNullOrEmpty(groupCategroy.ImageUrls))
  60. model.ImageUrls = StringUtils.RemoveDomain(groupCategroy.ImageUrls);
  61. return await _repository.UpdateAsync(groupCategroy);
  62. }
  63. return false;
  64. }
  65. public async Task<Entity.tede2.Group.GroupCategroy> GetAsync(int id)
  66. {
  67. return await _repository.GetAsync(id);
  68. }
  69. public async Task<bool> DeleteAsync(int id)
  70. {
  71. try
  72. {
  73. using (var transactionScope = new TransactionScope())
  74. {
  75. await _repository.DeleteAsync(id);
  76. transactionScope.Complete();
  77. }
  78. }
  79. catch
  80. {
  81. return false;
  82. }
  83. return true;
  84. }
  85. /// <summary>
  86. /// 根据parendId获取
  87. /// </summary>
  88. /// <param name="parentId"></param>
  89. /// <returns></returns>
  90. public async Task<IEnumerable<GroupCategoryResult>> GetAllAsync(int parentId)
  91. {
  92. var result = await _repository.GetAllAsync<GroupCategoryResult>(Q.Where(nameof(Entity.tede2.Group.GroupCategroy.ParentId), parentId));
  93. foreach (var item in result)
  94. {
  95. item.IsChildren = await _repository.ExistsAsync(Q.Where(nameof(Entity.tede2.Group.GroupCategroy.ParentId), item.Id));
  96. item.ImageUrls = StringUtils.AddDomain(item.ImageUrls);
  97. }
  98. return result;
  99. }
  100. public async Task<IEnumerable<GroupCategoryResult>> GetUserGroupCategroyResult(int userId)
  101. {
  102. string sql = $@"SELECT
  103. a.*
  104. FROM
  105. tede_group_piazza a
  106. INNER JOIN
  107. tede_user_group_category b ON a.id = b.GroupCategroyId
  108. WHERE
  109. b.UserId = {userId}
  110. ORDER BY b.CreatedDate";
  111. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  112. var database = new Database(databaseType, _connectionString);
  113. var connection = database.GetConnection();
  114. var result = await connection.QueryAsync<GroupCategoryResult>(sql);
  115. foreach (var item in result)
  116. {
  117. item.ImageUrls = StringUtils.AddDomainMin(item.ImageUrls);
  118. item.IsChildren = await _repository.ExistsAsync(Q.Where(nameof(Entity.tede2.Group.GroupCategroy.ParentId), item.Id));
  119. }
  120. return result;
  121. }
  122. }
  123. }