0
0

DocumentService.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using Admin.Core.Repository.Admin;
  6. using Admin.Core.Model.Admin;
  7. using Admin.Core.Model.Output;
  8. using Admin.Core.Service.Admin.Document.Input;
  9. using Admin.Core.Service.Admin.Document.Output;
  10. using Admin.Core.Common.Cache;
  11. namespace Admin.Core.Service.Admin.Document
  12. {
  13. public class DocumentService : IDocumentService
  14. {
  15. private readonly IMapper _mapper;
  16. private readonly ICache _cache;
  17. private readonly IDocumentRepository _DocumentRepository;
  18. public DocumentService(
  19. IMapper mapper,
  20. ICache cache,
  21. IDocumentRepository DocumentRepository
  22. )
  23. {
  24. _mapper = mapper;
  25. _cache = cache;
  26. _DocumentRepository = DocumentRepository;
  27. }
  28. public async Task<IResponseOutput> GetAsync(long id)
  29. {
  30. var result = await _DocumentRepository.GetAsync(id);
  31. return ResponseOutput.Ok(result);
  32. }
  33. public async Task<IResponseOutput> GetGroupAsync(long id)
  34. {
  35. var result = await _DocumentRepository.GetAsync<DocumentGetGroupOutput>(id);
  36. return ResponseOutput.Ok(result);
  37. }
  38. public async Task<IResponseOutput> GetMenuAsync(long id)
  39. {
  40. var result = await _DocumentRepository.GetAsync<DocumentGetMenuOutput>(id);
  41. return ResponseOutput.Ok(result);
  42. }
  43. public async Task<IResponseOutput> GetContentAsync(long id)
  44. {
  45. var result = await _DocumentRepository.GetAsync<DocumentGetContentOutput>(id);
  46. return ResponseOutput.Ok(result);
  47. }
  48. public async Task<IResponseOutput> GetListAsync(string key, DateTime? start, DateTime? end)
  49. {
  50. if (end.HasValue)
  51. {
  52. end = end.Value.AddDays(1);
  53. }
  54. var data = await _DocumentRepository
  55. .WhereIf(key.NotNull(), a => a.Name.Contains(key) || a.Label.Contains(key))
  56. .WhereIf(start.HasValue && end.HasValue, a => a.CreatedTime.Value.BetweenEnd(start.Value, end.Value))
  57. .OrderBy(a => a.ParentId)
  58. .OrderBy(a => a.Sort)
  59. .ToListAsync<DocumentListOutput>();
  60. return ResponseOutput.Ok(data);
  61. }
  62. public async Task<IResponseOutput> AddGroupAsync(DocumentAddGroupInput input)
  63. {
  64. var entity = _mapper.Map<DocumentEntity>(input);
  65. var id = (await _DocumentRepository.InsertAsync(entity)).Id;
  66. return ResponseOutput.Ok(id > 0);
  67. }
  68. public async Task<IResponseOutput> AddMenuAsync(DocumentAddMenuInput input)
  69. {
  70. var entity = _mapper.Map<DocumentEntity>(input);
  71. var id = (await _DocumentRepository.InsertAsync(entity)).Id;
  72. return ResponseOutput.Ok(id > 0);
  73. }
  74. public async Task<IResponseOutput> UpdateGroupAsync(DocumentUpdateGroupInput input)
  75. {
  76. var result = false;
  77. if (input != null && input.Id > 0)
  78. {
  79. var entity = await _DocumentRepository.GetAsync(input.Id);
  80. entity = _mapper.Map(input, entity);
  81. result = (await _DocumentRepository.UpdateAsync(entity)) > 0;
  82. }
  83. return ResponseOutput.Result(result);
  84. }
  85. public async Task<IResponseOutput> UpdateMenuAsync(DocumentUpdateMenuInput input)
  86. {
  87. var result = false;
  88. if (input != null && input.Id > 0)
  89. {
  90. var entity = await _DocumentRepository.GetAsync(input.Id);
  91. entity = _mapper.Map(input, entity);
  92. result = (await _DocumentRepository.UpdateAsync(entity)) > 0;
  93. }
  94. return ResponseOutput.Result(result);
  95. }
  96. public async Task<IResponseOutput> UpdateContentAsync(DocumentUpdateContentInput input)
  97. {
  98. var result = false;
  99. if (input != null && input.Id > 0)
  100. {
  101. var entity = await _DocumentRepository.GetAsync(input.Id);
  102. entity = _mapper.Map(input, entity);
  103. result = (await _DocumentRepository.UpdateAsync(entity)) > 0;
  104. }
  105. return ResponseOutput.Result(result);
  106. }
  107. public async Task<IResponseOutput> DeleteAsync(long id)
  108. {
  109. var result = false;
  110. if (id > 0)
  111. {
  112. result = (await _DocumentRepository.DeleteAsync(m => m.Id == id)) > 0;
  113. }
  114. return ResponseOutput.Result(result);
  115. }
  116. public async Task<IResponseOutput> SoftDeleteAsync(long id)
  117. {
  118. var result = await _DocumentRepository.SoftDeleteAsync(id);
  119. return ResponseOutput.Result(result);
  120. }
  121. public async Task<IResponseOutput> GetPlainListAsync()
  122. {
  123. var documents = await _DocumentRepository.Select
  124. .OrderBy(a => a.ParentId)
  125. .OrderBy(a => a.Sort)
  126. .ToListAsync(a => new { a.Id, a.ParentId, a.Label, a.Type, a.Opened });
  127. var menus = documents
  128. .Where(a => (new[] { DocumentType.Group, DocumentType.Markdown }).Contains(a.Type))
  129. .Select(a => new
  130. {
  131. a.Id,
  132. a.ParentId,
  133. a.Label,
  134. a.Type,
  135. a.Opened
  136. });
  137. return ResponseOutput.Ok(menus);
  138. }
  139. }
  140. }