package com.fdkankan.fusion.controller; import com.fdkankan.fusion.common.FilePath; import com.fdkankan.fusion.common.ResultCode; import com.fdkankan.fusion.exception.BusinessException; import com.fdkankan.fusion.common.ResultData; import com.fdkankan.fusion.entity.HotIcon; import com.fdkankan.fusion.service.ICaseTagService; import com.fdkankan.fusion.service.IHotIconService; import com.fdkankan.fusion.service.impl.UploadService; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.List; /** *

* 前端控制器 *

* * @author * @since 2022-08-02 */ @RestController @RequestMapping("/edit/hotIcon") public class HotIconController extends BaseController{ @Autowired IHotIconService hotIconService; @Autowired ICaseTagService caseTagService; @Autowired UploadService uploadService; @Value("${spring.profiles.active}") private String environment; @GetMapping("/list") public ResultData list(@RequestParam(required = false) Integer caseId){ if(caseId!=null){ return ResultData.ok(hotIconService.getListByCaseId(caseId)); } return ResultData.ok(hotIconService.getListByUserName(getUserName())); } @PostMapping("/add") public ResultData add(@RequestParam(required = false) MultipartFile file, @RequestParam(required = false) String iconTitle) throws IOException { if(file==null || StringUtils.isEmpty(iconTitle)){ throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS); } String iconUrl = uploadService.uploadFile(file, true, String.format(FilePath.ICON_OSS_PATH,environment,"")); String username = getUserName(); HotIcon hotIcon = new HotIcon(); hotIcon.setIconTitle(iconTitle); hotIcon.setIconUrl(iconUrl); hotIcon.setUserName(username); if(StringUtils.isEmpty(hotIcon.getIconUrl())){ throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS); } //取消用户之前新增标记 hotIconService.cancelIsNew(username); if(StringUtils.isEmpty(hotIcon.getIconTitle())){ String fileName = file.getOriginalFilename(); assert fileName != null; hotIcon.setIconTitle(fileName.substring(0,fileName.lastIndexOf("."))); } hotIconService.save(hotIcon); return ResultData.ok(hotIcon); } @PostMapping("/delete") public ResultData delete(@RequestBody HotIcon hotIcon){ if(hotIcon.getIconId() == null){ throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS); } caseTagService.updateDFHotIcon(hotIcon.getIconId()); hotIconService.removeById(hotIcon.getIconId()); return ResultData.ok(); } }