GroupCategroyRepository.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. namespace GxPress.Repository.Implement.Group
  13. {
  14. /// <summary>
  15. /// 小组广场
  16. /// </summary>
  17. public class GroupCategroyRepository : IGroupCategroyRepository
  18. {
  19. private readonly Repository<Entity.tede2.Group.GroupCategroy> _repository;
  20. private readonly IMapper _mapper;
  21. private readonly IDistributedCache _cache;
  22. public GroupCategroyRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper, IDistributedCache cache)
  23. {
  24. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  25. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  26. _mapper = mapper;
  27. _cache = cache;
  28. _repository = new Repository<Entity.tede2.Group.GroupCategroy>(database);
  29. }
  30. public IDatabase Database => _repository.Database;
  31. public string TableName => _repository.TableName;
  32. public List<TableColumn> TableColumns => _repository.TableColumns;
  33. /// <summary>
  34. /// 添加小组广场
  35. /// </summary>
  36. /// <param name="groupCategroy"></param>
  37. /// <returns></returns>
  38. public async Task<int> InsertAsync(Entity.tede2.Group.GroupCategroy groupCategroy)
  39. {
  40. return await _repository.InsertAsync(groupCategroy);
  41. }
  42. public async Task<bool> UpdateAsync(Entity.tede2.Group.GroupCategroy groupCategroy)
  43. {
  44. if (groupCategroy.Id > 0)
  45. {
  46. var model = await GetAsync(groupCategroy.Id);
  47. if (!string.IsNullOrEmpty(groupCategroy.PiazzaName))
  48. model.PiazzaName = groupCategroy.PiazzaName;
  49. if (groupCategroy.ParentId > 0)
  50. model.ParentId = groupCategroy.ParentId;
  51. return await _repository.UpdateAsync(groupCategroy);
  52. }
  53. return false;
  54. }
  55. public async Task<Entity.tede2.Group.GroupCategroy> GetAsync(int id)
  56. {
  57. return await _repository.GetAsync(id);
  58. }
  59. public async Task<bool> DeleteAsync(int id)
  60. {
  61. try
  62. {
  63. using (var transactionScope = new TransactionScope())
  64. {
  65. await _repository.DeleteAsync(id);
  66. transactionScope.Complete();
  67. }
  68. }
  69. catch
  70. {
  71. return false;
  72. }
  73. return true;
  74. }
  75. /// <summary>
  76. /// 根据parendId获取
  77. /// </summary>
  78. /// <param name="parentId"></param>
  79. /// <returns></returns>
  80. public async Task<IEnumerable<GroupCategoryResult>> GetAllAsync(int parentId)
  81. {
  82. var result = await _repository.GetAllAsync<GroupCategoryResult>(Q.Where(nameof(Entity.tede2.Group.GroupCategroy.ParentId), parentId));
  83. foreach (var item in result)
  84. item.IsChildren = await _repository.ExistsAsync(Q.Where(nameof(Entity.tede2.Group.GroupCategroy.ParentId), item.Id));
  85. return result;
  86. }
  87. }
  88. }