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