SpecialLabelRepository.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.Page;
  7. using GxPress.Common.Tools;
  8. using GxPress.Repository.Interface.SpecialLabel;
  9. using GxPress.Request.SpecialLabel;
  10. using Microsoft.Extensions.Options;
  11. namespace GxPress.Repository.Implement.SpecialLabel
  12. {
  13. /// <summary>
  14. /// 特殊标签管理
  15. /// </summary>
  16. public class SpecialLabelRepository : ISpecialLabelRepository
  17. {
  18. private readonly Repository<Entity.SystemLabel.SpecialLabel> _repository;
  19. private readonly IMapper _mapper;
  20. public SpecialLabelRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  21. {
  22. var databaseType =
  23. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  24. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  25. _repository = new Repository<Entity.SystemLabel.SpecialLabel>(database);
  26. _mapper = mapper;
  27. }
  28. public IDatabase Database => _repository.Database;
  29. public string TableName => _repository.TableName;
  30. public List<TableColumn> TableColumns => _repository.TableColumns;
  31. /// <summary>
  32. /// 添加
  33. /// </summary>
  34. /// <param name="model"></param>
  35. /// <returns></returns>
  36. public async Task<int> InsertAsync(Entity.SystemLabel.SpecialLabel model)
  37. {
  38. return await _repository.InsertAsync(model);
  39. }
  40. /// <summary>
  41. /// 修改
  42. /// </summary>
  43. /// <param name="model"></param>
  44. /// <returns></returns>
  45. public async Task<bool> UpdateAsync(Entity.SystemLabel.SpecialLabel model)
  46. {
  47. var specialLabel = await GetAsync(model.Id);
  48. if (!string.IsNullOrEmpty(model.ImageUrls))
  49. specialLabel.ImageUrls = model.ImageUrls;
  50. if (!string.IsNullOrEmpty(model.Remark))
  51. specialLabel.Remark = model.Remark;
  52. if (model.Sort > 0)
  53. specialLabel.Sort = model.Sort;
  54. if (!string.IsNullOrEmpty(model.Title))
  55. specialLabel.Title = model.Title;
  56. if (model.TypeValue > 0)
  57. specialLabel.TypeValue = model.TypeValue;
  58. return await _repository.UpdateAsync(specialLabel);
  59. }
  60. /// <summary>
  61. /// 获取
  62. /// </summary>
  63. /// <param name="id"></param>
  64. /// <returns></returns>
  65. public async Task<Entity.SystemLabel.SpecialLabel> GetAsync(int id)
  66. {
  67. return await _repository.GetAsync(id);
  68. }
  69. /// <summary>
  70. /// 删除
  71. /// </summary>
  72. /// <param name="id"></param>
  73. /// <returns></returns>
  74. public async Task<bool> DeleteAsync(int id)
  75. {
  76. return await _repository.DeleteAsync(id);
  77. }
  78. /// <summary>
  79. /// 获取类型下的数据
  80. /// </summary>
  81. /// <param name="typeValue"></param>
  82. /// <returns></returns>
  83. public async Task<IEnumerable<Entity.SystemLabel.SpecialLabel>> GetAllAsync(int typeValue)
  84. {
  85. var query = Q.NewQuery();
  86. query.Where(nameof(Entity.SystemLabel.SpecialLabel.TypeValue), typeValue);
  87. query.OrderByDesc(nameof(Entity.SystemLabel.SpecialLabel.Sort)).Take(8);
  88. return await _repository.GetAllAsync(query);
  89. }
  90. /// <summary>
  91. ///特使标签数据
  92. /// </summary>
  93. /// <param name="typeValue"></param>
  94. /// <returns></returns>
  95. public async Task<PagedList<Entity.SystemLabel.SpecialLabel>> GetAllAsync(SpecialLabelSearchRequest request)
  96. {
  97. var result = new PagedList<Entity.SystemLabel.SpecialLabel>();
  98. var query = Q.NewQuery();
  99. if (request.TypeValue > 0)
  100. query.Where(nameof(Entity.SystemLabel.SpecialLabel.TypeValue), request.TypeValue);
  101. if (!string.IsNullOrEmpty(request.KeyWord))
  102. query.WhereLike(nameof(Entity.SystemLabel.SpecialLabel.Title), $"%{request.KeyWord}%");
  103. result.Total = await _repository.CountAsync(query);
  104. query.OrderByDesc(nameof(Entity.SystemLabel.SpecialLabel.Sort));
  105. result.Items = await _repository.GetAllAsync(query);
  106. return result;
  107. }
  108. }
  109. }