DocumentService.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.Common.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 IDocumentRepository _documentRepository;
  17. private readonly IDocumentImageRepository _documentImageRepository;
  18. public DocumentService(
  19. IMapper mapper,
  20. IDocumentRepository DocumentRepository,
  21. IDocumentImageRepository documentImageRepository
  22. )
  23. {
  24. _mapper = mapper;
  25. _documentRepository = DocumentRepository;
  26. _documentImageRepository = documentImageRepository;
  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> GetImageListAsync(long id)
  63. {
  64. var result = await _documentImageRepository.Select
  65. .Where(a => a.DocumentId == id)
  66. .OrderByDescending(a=>a.Id)
  67. .ToListAsync(a => a.Url);
  68. return ResponseOutput.Ok(result);
  69. }
  70. public async Task<IResponseOutput> AddGroupAsync(DocumentAddGroupInput input)
  71. {
  72. var entity = _mapper.Map<DocumentEntity>(input);
  73. var id = (await _documentRepository.InsertAsync(entity)).Id;
  74. return ResponseOutput.Result(id > 0);
  75. }
  76. public async Task<IResponseOutput> AddMenuAsync(DocumentAddMenuInput input)
  77. {
  78. var entity = _mapper.Map<DocumentEntity>(input);
  79. var id = (await _documentRepository.InsertAsync(entity)).Id;
  80. return ResponseOutput.Result(id > 0);
  81. }
  82. public async Task<IResponseOutput> AddImageAsync(DocumentAddImageInput input)
  83. {
  84. var entity = _mapper.Map<DocumentImageEntity>(input);
  85. var id = (await _documentImageRepository.InsertAsync(entity)).Id;
  86. return ResponseOutput.Result(id > 0);
  87. }
  88. public async Task<IResponseOutput> UpdateGroupAsync(DocumentUpdateGroupInput input)
  89. {
  90. var result = false;
  91. if (input != null && input.Id > 0)
  92. {
  93. var entity = await _documentRepository.GetAsync(input.Id);
  94. entity = _mapper.Map(input, entity);
  95. result = (await _documentRepository.UpdateAsync(entity)) > 0;
  96. }
  97. return ResponseOutput.Result(result);
  98. }
  99. public async Task<IResponseOutput> UpdateMenuAsync(DocumentUpdateMenuInput input)
  100. {
  101. var result = false;
  102. if (input != null && input.Id > 0)
  103. {
  104. var entity = await _documentRepository.GetAsync(input.Id);
  105. entity = _mapper.Map(input, entity);
  106. result = (await _documentRepository.UpdateAsync(entity)) > 0;
  107. }
  108. return ResponseOutput.Result(result);
  109. }
  110. public async Task<IResponseOutput> UpdateContentAsync(DocumentUpdateContentInput input)
  111. {
  112. var result = false;
  113. if (input != null && input.Id > 0)
  114. {
  115. var entity = await _documentRepository.GetAsync(input.Id);
  116. entity = _mapper.Map(input, entity);
  117. result = (await _documentRepository.UpdateAsync(entity)) > 0;
  118. }
  119. return ResponseOutput.Result(result);
  120. }
  121. public async Task<IResponseOutput> DeleteAsync(long id)
  122. {
  123. var result = false;
  124. if (id > 0)
  125. {
  126. result = (await _documentRepository.DeleteAsync(m => m.Id == id)) > 0;
  127. }
  128. return ResponseOutput.Result(result);
  129. }
  130. public async Task<IResponseOutput> DeleteImageAsync(long documentId, string url)
  131. {
  132. var result = false;
  133. if (documentId > 0 && url.NotNull())
  134. {
  135. result = (await _documentImageRepository.DeleteAsync(m => m.DocumentId == documentId && m.Url == url)) > 0;
  136. }
  137. return ResponseOutput.Result(result);
  138. }
  139. public async Task<IResponseOutput> SoftDeleteAsync(long id)
  140. {
  141. var result = await _documentRepository.SoftDeleteAsync(id);
  142. return ResponseOutput.Result(result);
  143. }
  144. public async Task<IResponseOutput> GetPlainListAsync()
  145. {
  146. var documents = await _documentRepository.Select
  147. .OrderBy(a => a.ParentId)
  148. .OrderBy(a => a.Sort)
  149. .ToListAsync(a => new { a.Id, a.ParentId, a.Label, a.Type, a.Opened });
  150. var menus = documents
  151. .Where(a => (new[] { DocumentType.Group, DocumentType.Markdown }).Contains(a.Type))
  152. .Select(a => new
  153. {
  154. a.Id,
  155. a.ParentId,
  156. a.Label,
  157. a.Type,
  158. a.Opened
  159. });
  160. return ResponseOutput.Ok(menus);
  161. }
  162. }
  163. }