AttachRepository.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.Attach;
  9. using GxPress.Request.Attach;
  10. using Microsoft.Extensions.Options;
  11. namespace GxPress.Repository.Implement.Attach
  12. {
  13. public class AttachRepository : IAttachRepository
  14. {
  15. private readonly Repository<Entity.tede2.Attach.Attach> _repository;
  16. private readonly IMapper _mapper;
  17. private readonly string _connectionString;
  18. private readonly string _databaseTypestr;
  19. public AttachRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  20. {
  21. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  22. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  23. var databaseType =
  24. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  25. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  26. _repository = new Repository<Entity.tede2.Attach.Attach>(database);
  27. _mapper = mapper;
  28. }
  29. public IDatabase Database => _repository.Database;
  30. public string TableName => _repository.TableName;
  31. public List<TableColumn> TableColumns => _repository.TableColumns;
  32. public async Task<bool> DeleteAsync(int id)
  33. {
  34. return await _repository.DeleteAsync(id);
  35. }
  36. public async Task<PagedList<Entity.tede2.Attach.Attach>> GetAllAsync(AttachRequest request)
  37. {
  38. var result = new PagedList<Entity.tede2.Attach.Attach>();
  39. var query = Q.NewQuery();
  40. query.Where(nameof(Entity.tede2.Attach.Attach.AttachType), request.TypeId);
  41. if (!string.IsNullOrEmpty(request.KeyWord))
  42. query.WhereLike(nameof(Entity.tede2.Attach.Attach.Name), $"%{request.KeyWord}%");
  43. if (request.CategoryId > 0)
  44. query.Where(nameof(Entity.tede2.Attach.Attach.CategoryId), request.CategoryId);
  45. if (request.AdminId > 0)
  46. query.Where(nameof(Entity.tede2.Attach.Attach.CategoryId), request.AdminId);
  47. if (request.WithdrawType > 0)
  48. query.Where(nameof(Entity.tede2.Attach.Attach.IsWithdraw), request.WithdrawType == 2);
  49. result.Items = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  50. result.Total = await _repository.CountAsync(query);
  51. return result;
  52. }
  53. public async Task<Entity.tede2.Attach.Attach> GetAsync(int id)
  54. {
  55. return await _repository.GetAsync(id);
  56. }
  57. public async Task<int> InsertAsync(Entity.tede2.Attach.Attach note)
  58. {
  59. var categoryId = "";
  60. var categoryIds = StringUtils.StringCollectionToStringList(note.CategoryId);
  61. foreach (var item in categoryIds)
  62. {
  63. categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
  64. }
  65. categoryId = categoryId.Remove(categoryId.Length - 1, 1);
  66. note.FullCategoryId = note.CategoryId;
  67. note.CategoryId = categoryId;
  68. return await _repository.InsertAsync(note);
  69. }
  70. public async Task<bool> UpdateAsync(Entity.tede2.Attach.Attach note)
  71. {
  72. var categoryId = "";
  73. var categoryIds = StringUtils.StringCollectionToStringList(note.CategoryId);
  74. foreach (var item in categoryIds)
  75. {
  76. categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
  77. }
  78. categoryId = categoryId.Remove(categoryId.Length - 1, 1);
  79. note.FullCategoryId = note.CategoryId;
  80. note.CategoryId = categoryId;
  81. return await _repository.UpdateAsync(note);
  82. }
  83. }
  84. }