SpecialLabelRepository.cs 3.4 KB

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