GoViewProService.cs 8.5 KB

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