DocumentService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Extensions.Options;
  3. using System;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using ZhonTai.Admin.Core.Configs;
  7. using ZhonTai.Admin.Core.Dto;
  8. using ZhonTai.Admin.Domain.Document;
  9. using ZhonTai.Admin.Domain.DocumentImage;
  10. using ZhonTai.Admin.Services.Document.Dto;
  11. using ZhonTai.DynamicApi;
  12. using ZhonTai.DynamicApi.Attributes;
  13. using ZhonTai.Admin.Core.Helpers;
  14. using ZhonTai.Admin.Core.Consts;
  15. namespace ZhonTai.Admin.Services.Document
  16. {
  17. /// <summary>
  18. /// 文档服务
  19. /// </summary>
  20. [DynamicApi(Area = AdminConsts.AreaName)]
  21. public class DocumentService : BaseService, IDocumentService, IDynamicApi
  22. {
  23. private readonly IDocumentRepository _documentRepository;
  24. private readonly IDocumentImageRepository _documentImageRepository;
  25. public DocumentService(
  26. IDocumentRepository DocumentRepository,
  27. IDocumentImageRepository documentImageRepository
  28. )
  29. {
  30. _documentRepository = DocumentRepository;
  31. _documentImageRepository = documentImageRepository;
  32. }
  33. /// <summary>
  34. /// 查询文档
  35. /// </summary>
  36. /// <param name="id"></param>
  37. /// <returns></returns>
  38. public async Task<IResultOutput> GetAsync(long id)
  39. {
  40. var result = await _documentRepository.GetAsync(id);
  41. return ResultOutput.Ok(result);
  42. }
  43. /// <summary>
  44. /// 查询分组
  45. /// </summary>
  46. /// <param name="id"></param>
  47. /// <returns></returns>
  48. public async Task<IResultOutput> GetGroupAsync(long id)
  49. {
  50. var result = await _documentRepository.GetAsync<DocumentGetGroupOutput>(id);
  51. return ResultOutput.Ok(result);
  52. }
  53. /// <summary>
  54. /// 查询菜单
  55. /// </summary>
  56. /// <param name="id"></param>
  57. /// <returns></returns>
  58. public async Task<IResultOutput> GetMenuAsync(long id)
  59. {
  60. var result = await _documentRepository.GetAsync<DocumentGetMenuOutput>(id);
  61. return ResultOutput.Ok(result);
  62. }
  63. /// <summary>
  64. /// 查询文档内容
  65. /// </summary>
  66. /// <param name="id"></param>
  67. /// <returns></returns>
  68. public async Task<IResultOutput> GetContentAsync(long id)
  69. {
  70. var result = await _documentRepository.GetAsync<DocumentGetContentOutput>(id);
  71. return ResultOutput.Ok(result);
  72. }
  73. /// <summary>
  74. /// 查询文档列表
  75. /// </summary>
  76. /// <param name="key"></param>
  77. /// <param name="start"></param>
  78. /// <param name="end"></param>
  79. /// <returns></returns>
  80. public async Task<IResultOutput> GetListAsync(string key, DateTime? start, DateTime? end)
  81. {
  82. if (end.HasValue)
  83. {
  84. end = end.Value.AddDays(1);
  85. }
  86. var data = await _documentRepository
  87. .WhereIf(key.NotNull(), a => a.Name.Contains(key) || a.Label.Contains(key))
  88. .WhereIf(start.HasValue && end.HasValue, a => a.CreatedTime.Value.BetweenEnd(start.Value, end.Value))
  89. .OrderBy(a => a.ParentId)
  90. .OrderBy(a => a.Sort)
  91. .ToListAsync<DocumentListOutput>();
  92. return ResultOutput.Ok(data);
  93. }
  94. /// <summary>
  95. /// 查询图片列表
  96. /// </summary>
  97. /// <param name="id"></param>
  98. /// <returns></returns>
  99. public async Task<IResultOutput> GetImageListAsync(long id)
  100. {
  101. var result = await _documentImageRepository.Select
  102. .Where(a => a.DocumentId == id)
  103. .OrderByDescending(a => a.Id)
  104. .ToListAsync(a => a.Url);
  105. return ResultOutput.Ok(result);
  106. }
  107. /// <summary>
  108. /// 新增分组
  109. /// </summary>
  110. /// <param name="input"></param>
  111. /// <returns></returns>
  112. public async Task<IResultOutput> AddGroupAsync(DocumentAddGroupInput input)
  113. {
  114. var entity = Mapper.Map<DocumentEntity>(input);
  115. var id = (await _documentRepository.InsertAsync(entity)).Id;
  116. return ResultOutput.Result(id > 0);
  117. }
  118. /// <summary>
  119. /// 新增菜单
  120. /// </summary>
  121. /// <param name="input"></param>
  122. /// <returns></returns>
  123. public async Task<IResultOutput> AddMenuAsync(DocumentAddMenuInput input)
  124. {
  125. var entity = Mapper.Map<DocumentEntity>(input);
  126. var id = (await _documentRepository.InsertAsync(entity)).Id;
  127. return ResultOutput.Result(id > 0);
  128. }
  129. /// <summary>
  130. /// 新增图片
  131. /// </summary>
  132. /// <param name="input"></param>
  133. /// <returns></returns>
  134. public async Task<IResultOutput> AddImageAsync(DocumentAddImageInput input)
  135. {
  136. var entity = Mapper.Map<DocumentImageEntity>(input);
  137. var id = (await _documentImageRepository.InsertAsync(entity)).Id;
  138. return ResultOutput.Result(id > 0);
  139. }
  140. /// <summary>
  141. /// 修改分组
  142. /// </summary>
  143. /// <param name="input"></param>
  144. /// <returns></returns>
  145. public async Task<IResultOutput> UpdateGroupAsync(DocumentUpdateGroupInput input)
  146. {
  147. var result = false;
  148. if (input != null && input.Id > 0)
  149. {
  150. var entity = await _documentRepository.GetAsync(input.Id);
  151. entity = Mapper.Map(input, entity);
  152. result = (await _documentRepository.UpdateAsync(entity)) > 0;
  153. }
  154. return ResultOutput.Result(result);
  155. }
  156. /// <summary>
  157. /// 修改菜单
  158. /// </summary>
  159. /// <param name="input"></param>
  160. /// <returns></returns>
  161. public async Task<IResultOutput> UpdateMenuAsync(DocumentUpdateMenuInput input)
  162. {
  163. var result = false;
  164. if (input != null && input.Id > 0)
  165. {
  166. var entity = await _documentRepository.GetAsync(input.Id);
  167. entity = Mapper.Map(input, entity);
  168. result = (await _documentRepository.UpdateAsync(entity)) > 0;
  169. }
  170. return ResultOutput.Result(result);
  171. }
  172. /// <summary>
  173. /// 修改文档内容
  174. /// </summary>
  175. /// <param name="input"></param>
  176. /// <returns></returns>
  177. public async Task<IResultOutput> UpdateContentAsync(DocumentUpdateContentInput input)
  178. {
  179. var result = false;
  180. if (input != null && input.Id > 0)
  181. {
  182. var entity = await _documentRepository.GetAsync(input.Id);
  183. entity = Mapper.Map(input, entity);
  184. result = (await _documentRepository.UpdateAsync(entity)) > 0;
  185. }
  186. return ResultOutput.Result(result);
  187. }
  188. /// <summary>
  189. /// 彻底删除文档
  190. /// </summary>
  191. /// <param name="id"></param>
  192. /// <returns></returns>
  193. public async Task<IResultOutput> DeleteAsync(long id)
  194. {
  195. var result = false;
  196. if (id > 0)
  197. {
  198. result = (await _documentRepository.DeleteAsync(m => m.Id == id)) > 0;
  199. }
  200. return ResultOutput.Result(result);
  201. }
  202. /// <summary>
  203. /// 彻底删除图片
  204. /// </summary>
  205. /// <param name="documentId"></param>
  206. /// <param name="url"></param>
  207. /// <returns></returns>
  208. public async Task<IResultOutput> DeleteImageAsync(long documentId, string url)
  209. {
  210. var result = false;
  211. if (documentId > 0 && url.NotNull())
  212. {
  213. result = (await _documentImageRepository.DeleteAsync(m => m.DocumentId == documentId && m.Url == url)) > 0;
  214. }
  215. return ResultOutput.Result(result);
  216. }
  217. /// <summary>
  218. /// 删除文档
  219. /// </summary>
  220. /// <param name="id"></param>
  221. /// <returns></returns>
  222. public async Task<IResultOutput> SoftDeleteAsync(long id)
  223. {
  224. var result = await _documentRepository.SoftDeleteAsync(id);
  225. return ResultOutput.Result(result);
  226. }
  227. /// <summary>
  228. /// 查询精简文档列表
  229. /// </summary>
  230. /// <returns></returns>
  231. public async Task<IResultOutput> GetPlainListAsync()
  232. {
  233. var documents = await _documentRepository.Select
  234. .OrderBy(a => a.ParentId)
  235. .OrderBy(a => a.Sort)
  236. .ToListAsync(a => new { a.Id, a.ParentId, a.Label, a.Type, a.Opened });
  237. var menus = documents
  238. .Where(a => (new[] { DocumentTypeEnum.Group, DocumentTypeEnum.Markdown }).Contains(a.Type))
  239. .Select(a => new
  240. {
  241. a.Id,
  242. a.ParentId,
  243. a.Label,
  244. a.Type,
  245. a.Opened
  246. });
  247. return ResultOutput.Ok(menus);
  248. }
  249. /// <summary>
  250. /// 上传文档图片
  251. /// </summary>
  252. /// <param name="input"></param>
  253. /// <returns></returns>
  254. [HttpPost]
  255. public async Task<IResultOutput> UploadImage([FromForm] DocumentUploadImageInput input)
  256. {
  257. var uploadConfig = LazyGetRequiredService<IOptionsMonitor<UploadConfig>>().CurrentValue;
  258. var uploadHelper = LazyGetRequiredService<UploadHelper>();
  259. var config = uploadConfig.Document;
  260. var res = await uploadHelper.UploadAsync(input.File, config, new { input.Id });
  261. if (res.Success)
  262. {
  263. //保存文档图片
  264. var r = await AddImageAsync(
  265. new DocumentAddImageInput
  266. {
  267. DocumentId = input.Id,
  268. Url = res.Data.FileRequestPath
  269. });
  270. if (r.Success)
  271. {
  272. return ResultOutput.Ok(res.Data.FileRequestPath);
  273. }
  274. }
  275. return ResultOutput.NotOk("上传失败!");
  276. }
  277. }
  278. }