MiddleLableNexusRepository.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.Navigation;
  8. using Microsoft.Extensions.Options;
  9. using System.Transactions;
  10. namespace GxPress.Repository.Implement.Navigation
  11. {
  12. public class MiddleLableNexusRepository : IMiddleLableNexusRepository
  13. {
  14. private readonly Repository<Entity.Navigations.MiddleLableNexus> _repository;
  15. private readonly IMapper _mapper;
  16. public MiddleLableNexusRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  17. {
  18. var databaseType =
  19. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  20. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  21. _repository = new Repository<Entity.Navigations.MiddleLableNexus>(database);
  22. _mapper = mapper;
  23. }
  24. public IDatabase Database => _repository.Database;
  25. public string TableName => _repository.TableName;
  26. public List<TableColumn> TableColumns => _repository.TableColumns;
  27. /// <summary>
  28. /// 添加
  29. /// </summary>
  30. /// <param name="middleLableId"></param>
  31. /// <param name="ids"></param>
  32. /// <returns></returns>
  33. public async Task<bool> InsertAsync(int middleLableId, List<int> ids)
  34. {
  35. try
  36. {
  37. using (var transactionScope = new TransactionScope())
  38. {
  39. foreach (var item in ids)
  40. {
  41. var model = new Entity.Navigations.MiddleLableNexus()
  42. {
  43. LableId = item,
  44. MiddleLableId = middleLableId
  45. };
  46. await _repository.InsertAsync(model);
  47. }
  48. transactionScope.Complete();
  49. }
  50. }
  51. catch
  52. {
  53. return false;
  54. }
  55. return true;
  56. }
  57. /// <summary>
  58. /// 查询
  59. /// </summary>
  60. /// <param name="middleLableId"></param>
  61. /// <returns></returns>
  62. public async Task<IEnumerable<int>> GetAllTaskAsync(int middleLableId)
  63. {
  64. var query = Q.NewQuery();
  65. query.Select(nameof(Entity.Navigations.MiddleLableNexus.LableId));
  66. query.Where(nameof(Entity.Navigations.MiddleLableNexus.MiddleLableId), middleLableId);
  67. query.OrderByDesc(nameof(Entity.Navigations.MiddleLableNexus.Sort));
  68. return await _repository.GetAllAsync<int>(query);
  69. }
  70. public async Task<bool> DeleteByMiddleLableIdAsync(int middleLableId)
  71. {
  72. return await _repository.DeleteAsync(Q.Where(nameof(Entity.Navigations.MiddleLableNexus.MiddleLableId), middleLableId)) > 0;
  73. }
  74. public async Task<bool> UpdateAsync(int middleLableId, string lableId)
  75. {
  76. try
  77. {
  78. await DeleteByMiddleLableIdAsync(middleLableId);
  79. var stringId = StringUtils.StringCollectionToStringList(lableId);
  80. var intIds = new List<int>();
  81. foreach (var item in stringId)
  82. intIds.Add(int.Parse(item));
  83. await InsertAsync(middleLableId, intIds);
  84. }
  85. catch
  86. {
  87. return false;
  88. }
  89. return true;
  90. }
  91. }
  92. }