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 fusionId){
if(fusionId!=null){
return ResultData.ok(hotIconService.getListByFusionId(fusionId));
}
return ResultData.ok(hotIconService.getListByUserName(getUserName()));
}
@GetMapping("/treeList")
public ResultData listTree(@RequestParam(required = false) Integer fusionId){
List iconList ;
if(fusionId!=null){
iconList = hotIconService.getListByFusionId(fusionId);
}else {
iconList = hotIconService.getListByUserName(getUserName());
}
List treeList = hotIconService.treeList(iconList);
return ResultData.ok(treeList);
}
@PostMapping("/add")
public ResultData add(@RequestParam(required = false) MultipartFile file,
@RequestParam(required = false) String iconTitle,
@RequestParam(required = false) Integer fusionId,
@RequestParam(required = false,defaultValue = "279") Integer parentId) throws IOException {
if(file==null || StringUtils.isEmpty(iconTitle) || fusionId == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
String iconUrl = uploadService.uploadFile(file, true, String.format(FilePath.ICON_OSS_PATH,environment,""));
HotIcon hotIcon = new HotIcon();
hotIcon.setIconTitle(iconTitle);
hotIcon.setIconUrl(iconUrl);
hotIcon.setFusionId(fusionId);
hotIcon.setParentId(parentId);
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);
}
if(hotIcon.getIconId() == 1){
return ResultData.ok();
}
HotIcon byId = hotIconService.getById(hotIcon.getIconId());
if(byId == null){
return ResultData.ok();
}
if(byId .getIsSystem() == 1){
throw new BusinessException(ResultCode.SYSTEM_HOT_ICON_NOT_DELETE);
}
caseTagService.updateDFHotIcon(hotIcon.getIconId());
hotIconService.removeById(hotIcon.getIconId());
return ResultData.ok();
}
}