GoViewProService.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. namespace Admin.NET.Plugin.GoView.Service;
  2. /// <summary>
  3. /// 项目管理服务
  4. /// </summary>
  5. [UnifyProvider("GoView")]
  6. [ApiDescriptionSettings(GoViewConst.GroupName, Module = "goview", Name = "project", Order = 100)]
  7. public class GoViewProService : IDynamicApiController
  8. {
  9. private readonly SqlSugarRepository<GoViewPro> _goViewProRep;
  10. private readonly SqlSugarRepository<GoViewProData> _goViewProDataRep;
  11. private readonly SqlSugarRepository<SysFile> _sysFileRep;
  12. private readonly SysFileService _fileService;
  13. public GoViewProService(SqlSugarRepository<GoViewPro> goViewProjectRep,
  14. SqlSugarRepository<GoViewProData> goViewProjectDataRep,
  15. SqlSugarRepository<SysFile> fileRep,
  16. SysFileService fileService)
  17. {
  18. _goViewProRep = goViewProjectRep;
  19. _goViewProDataRep = goViewProjectDataRep;
  20. _sysFileRep = fileRep;
  21. _fileService = fileService;
  22. }
  23. /// <summary>
  24. /// 获取项目列表
  25. /// </summary>
  26. /// <param name="page"></param>
  27. /// <param name="limit"></param>
  28. /// <returns></returns>
  29. [DisplayName("获取项目列表")]
  30. public async Task<List<GoViewProItemOutput>> GetList([FromQuery] int page = 1, [FromQuery] int limit = 12)
  31. {
  32. var res = await _goViewProRep.AsQueryable()
  33. .Select(u => new GoViewProItemOutput(), true)
  34. .ToPagedListAsync(page, limit);
  35. return res.Items.ToList();
  36. }
  37. /// <summary>
  38. /// 新增项目
  39. /// </summary>
  40. /// <param name="input"></param>
  41. /// <returns></returns>
  42. [ApiDescriptionSettings(Name = "Create")]
  43. [DisplayName("新增项目")]
  44. public async Task<GoViewProCreateOutput> Create(GoViewProCreateInput input)
  45. {
  46. var project = await _goViewProRep.AsInsertable(input.Adapt<GoViewPro>()).ExecuteReturnEntityAsync();
  47. return new GoViewProCreateOutput
  48. {
  49. Id = project.Id
  50. };
  51. }
  52. /// <summary>
  53. /// 修改项目
  54. /// </summary>
  55. /// <param name="input"></param>
  56. /// <returns></returns>
  57. [DisplayName("修改项目")]
  58. public async Task Edit(GoViewProEditInput input)
  59. {
  60. await _goViewProRep.AsUpdateable(input.Adapt<GoViewPro>()).IgnoreColumns(true).ExecuteCommandAsync();
  61. }
  62. /// <summary>
  63. /// 删除项目
  64. /// </summary>
  65. [ApiDescriptionSettings(Name = "Delete")]
  66. [DisplayName("删除项目")]
  67. [UnitOfWork]
  68. public async Task Delete([FromQuery] string ids)
  69. {
  70. var idList = ids.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(u => Convert.ToInt64(u)).ToList();
  71. await _goViewProRep.AsDeleteable().Where(u => idList.Contains(u.Id)).ExecuteCommandAsync();
  72. await _goViewProDataRep.AsDeleteable().Where(u => idList.Contains(u.Id)).ExecuteCommandAsync();
  73. }
  74. /// <summary>
  75. /// 修改发布状态
  76. /// </summary>
  77. [HttpPut]
  78. [DisplayName("修改发布状态")]
  79. public async Task Publish(GoViewProPublishInput input)
  80. {
  81. await _goViewProRep.AsUpdateable()
  82. .SetColumns(u => new GoViewPro
  83. {
  84. State = input.State
  85. })
  86. .Where(u => u.Id == input.Id)
  87. .ExecuteCommandAsync();
  88. }
  89. /// <summary>
  90. /// 获取项目数据
  91. /// </summary>
  92. /// <param name="projectId"></param>
  93. /// <returns></returns>
  94. [AllowAnonymous]
  95. [ApiDescriptionSettings(Name = "GetData")]
  96. [DisplayName("获取项目数据")]
  97. public async Task<GoViewProDetailOutput> GetData([FromQuery] long projectId)
  98. {
  99. var projectData = await _goViewProDataRep.GetFirstAsync(u => u.Id == projectId);
  100. if (projectData == null) return null;
  101. var project = await _goViewProRep.GetFirstAsync(u => u.Id == projectId);
  102. var projectDetail = project.Adapt<GoViewProDetailOutput>();
  103. projectDetail.Content = projectData.Content;
  104. return projectDetail;
  105. }
  106. /// <summary>
  107. /// 保存项目数据
  108. /// </summary>
  109. [ApiDescriptionSettings(Name = "save/data")]
  110. [DisplayName("保存项目数据")]
  111. public async Task SaveData([FromForm] GoViewProSaveDataInput input)
  112. {
  113. if (await _goViewProDataRep.IsAnyAsync(u => u.Id == input.ProjectId))
  114. {
  115. await _goViewProDataRep.AsUpdateable()
  116. .SetColumns(u => new GoViewProData
  117. {
  118. Content = input.Content
  119. })
  120. .Where(u => u.Id == input.ProjectId)
  121. .ExecuteCommandAsync();
  122. }
  123. else
  124. {
  125. await _goViewProDataRep.InsertAsync(new GoViewProData
  126. {
  127. Id = input.ProjectId,
  128. Content = input.Content,
  129. });
  130. }
  131. }
  132. /// <summary>
  133. /// 上传预览图
  134. /// </summary>
  135. [DisplayName("上传预览图")]
  136. public async Task<GoViewProUploadOutput> Upload(IFormFile @object)
  137. {
  138. /*
  139. * 前端逻辑(useSync.hook.ts 的 dataSyncUpdate 方法):
  140. * 如果 FileUrl 不为空,使用 FileUrl
  141. * 否则使用 GetOssInfo 接口获取到的 BucketUrl 和 FileName 进行拼接
  142. */
  143. //文件名格式示例 13414795568325_index_preview.png
  144. var fileNameSplit = @object.FileName.Split('_');
  145. var idStr = fileNameSplit[0];
  146. if (!long.TryParse(idStr, out var id)) return new GoViewProUploadOutput();
  147. //将预览图转换成 Base64
  148. var ms = new MemoryStream();
  149. await @object.CopyToAsync(ms);
  150. var base64Image = Convert.ToBase64String(ms.ToArray());
  151. //保存
  152. if (await _goViewProDataRep.IsAnyAsync(u => u.Id == id))
  153. {
  154. await _goViewProDataRep.AsUpdateable()
  155. .SetColumns(u => new GoViewProData
  156. {
  157. IndexImageData = base64Image
  158. })
  159. .Where(u => u.Id == id)
  160. .ExecuteCommandAsync();
  161. }
  162. else
  163. {
  164. await _goViewProDataRep.InsertAsync(new GoViewProData
  165. {
  166. Id = id,
  167. IndexImageData = base64Image,
  168. });
  169. }
  170. var output = new GoViewProUploadOutput
  171. {
  172. Id = id,
  173. BucketName = null,
  174. CreateTime = null,
  175. CreateUserId = null,
  176. FileName = null,
  177. FileSize = 0,
  178. FileSuffix = "png",
  179. FileUrl = $"api/goview/project/getIndexImage/{id}",
  180. UpdateTime = null,
  181. UpdateUserId = null
  182. };
  183. #region 使用 SysFileService 方式(已注释)
  184. ////删除已存在的预览图
  185. //var uploadFileName = Path.GetFileNameWithoutExtension(@object.FileName);
  186. //var existFiles = await _fileRep.GetListAsync(u => u.FileName == uploadFileName);
  187. //foreach (var f in existFiles)
  188. // await _fileService.DeleteFile(new DeleteFileInput { Id = f.Id });
  189. ////保存预览图
  190. //var result = await _fileService.UploadFile(@object, "");
  191. //var file = await _fileRep.GetFirstAsync(u => u.Id == result.Id);
  192. //int.TryParse(file.SizeKb, out var size);
  193. ////本地存储,使用拼接的地址
  194. //var fileUrl = file.BucketName == "Local" ? $"{file.FilePath}/{file.Id}{file.Suffix}" : file.Url;
  195. //var output = new ProjectUploadOutput
  196. //{
  197. // Id = file.Id,
  198. // BucketName = file.BucketName,
  199. // CreateTime = file.CreateTime,
  200. // CreateUserId = file.CreateUserId,
  201. // FileName = $"{file.FileName}{file.Suffix}",
  202. // FileSize = size,
  203. // FileSuffix = file.Suffix?[1..],
  204. // FileUrl = fileUrl,
  205. // UpdateTime = null,
  206. // UpdateUserId = null
  207. //};
  208. #endregion 使用 SysFileService 方式(已注释)
  209. return output;
  210. }
  211. /// <summary>
  212. /// 获取预览图
  213. /// </summary>
  214. /// <returns></returns>
  215. [AllowAnonymous]
  216. [NonUnify]
  217. [ApiDescriptionSettings(Name = "GetIndexImage")]
  218. [DisplayName("获取预览图")]
  219. public async Task<IActionResult> GetIndexImage(long id)
  220. {
  221. var projectData = await _goViewProDataRep.AsQueryable().IgnoreColumns(u => u.Content).FirstAsync(u => u.Id == id);
  222. if (projectData?.IndexImageData == null)
  223. return new NoContentResult();
  224. var bytes = Convert.FromBase64String(projectData.IndexImageData);
  225. return new FileStreamResult(new MemoryStream(bytes), "image/png");
  226. }
  227. }