HotIconController.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package com.fdkankan.fusion.controller;
  2. import com.fdkankan.fusion.common.FilePath;
  3. import com.fdkankan.fusion.common.ResultCode;
  4. import com.fdkankan.fusion.exception.BusinessException;
  5. import com.fdkankan.fusion.common.ResultData;
  6. import com.fdkankan.fusion.entity.HotIcon;
  7. import com.fdkankan.fusion.service.ICaseTagService;
  8. import com.fdkankan.fusion.service.IHotIconService;
  9. import com.fdkankan.fusion.service.impl.UploadService;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.web.bind.annotation.*;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import java.io.IOException;
  16. import java.util.List;
  17. /**
  18. * <p>
  19. * 前端控制器
  20. * </p>
  21. *
  22. * @author
  23. * @since 2022-08-02
  24. */
  25. @RestController
  26. @RequestMapping("/edit/hotIcon")
  27. public class HotIconController extends BaseController{
  28. @Autowired
  29. IHotIconService hotIconService;
  30. @Autowired
  31. ICaseTagService caseTagService;
  32. @Autowired
  33. UploadService uploadService;
  34. @Value("${spring.profiles.active}")
  35. private String environment;
  36. @GetMapping("/list")
  37. public ResultData list(@RequestParam(required = false) Integer caseId){
  38. if(caseId!=null){
  39. return ResultData.ok(hotIconService.getListByCaseId(caseId));
  40. }
  41. return ResultData.ok(hotIconService.getListByUserName(getUserName()));
  42. }
  43. @PostMapping("/add")
  44. public ResultData add(@RequestParam(required = false) MultipartFile file,
  45. @RequestParam(required = false) String iconTitle) throws IOException {
  46. if(file==null || StringUtils.isEmpty(iconTitle)){
  47. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  48. }
  49. String iconUrl = uploadService.uploadFile(file, true, String.format(FilePath.ICON_OSS_PATH,environment,""));
  50. String username = getUserName();
  51. HotIcon hotIcon = new HotIcon();
  52. hotIcon.setIconTitle(iconTitle);
  53. hotIcon.setIconUrl(iconUrl);
  54. hotIcon.setUserName(username);
  55. if(StringUtils.isEmpty(hotIcon.getIconUrl())){
  56. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  57. }
  58. //取消用户之前新增标记
  59. hotIconService.cancelIsNew(username);
  60. if(StringUtils.isEmpty(hotIcon.getIconTitle())){
  61. String fileName = file.getOriginalFilename();
  62. assert fileName != null;
  63. hotIcon.setIconTitle(fileName.substring(0,fileName.lastIndexOf(".")));
  64. }
  65. hotIconService.save(hotIcon);
  66. return ResultData.ok(hotIcon);
  67. }
  68. @PostMapping("/delete")
  69. public ResultData delete(@RequestBody HotIcon hotIcon){
  70. if(hotIcon.getIconId() == null){
  71. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  72. }
  73. caseTagService.updateDFHotIcon(hotIcon.getIconId());
  74. hotIconService.removeById(hotIcon.getIconId());
  75. return ResultData.ok();
  76. }
  77. }