package com.fdkankan.site.controller; import cn.dev33.satoken.annotation.SaCheckLogin; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.fdkankan.site.common.BaseController; import com.fdkankan.site.common.FilePath; import com.fdkankan.site.common.ResultCode; import com.fdkankan.site.common.ResultData; import com.fdkankan.site.common.util.JwtUtil; import com.fdkankan.site.common.util.UploadToOssUtil; import com.fdkankan.site.common.util.VUtils; import com.fdkankan.site.entity.ProjectBim; import com.fdkankan.site.exception.BusinessException; import com.fdkankan.site.httpClient.bim.BimCallBackDTO; import com.fdkankan.site.httpClient.bim.BimFaceVO; import com.fdkankan.site.httpClient.bim.BimUploadParam; import com.fdkankan.site.httpClient.bim.BusinessStatus; import com.fdkankan.site.httpClient.client.BimClient; import com.fdkankan.site.service.IProjectBimService; import com.fdkankan.site.service.IProjectLogService; import com.fdkankan.site.service.IProjectService; import com.fdkankan.site.service.impl.UploadService; import lombok.extern.slf4j.Slf4j; 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 javax.annotation.Resource; import java.io.File; import java.io.IOException; import java.util.List; import java.util.Objects; @Slf4j @RestController @RequestMapping("/upload") @SaCheckLogin public class UploadController extends BaseController { @Autowired UploadService uploadService; @Resource private UploadToOssUtil uploadToOssUtil; @Value("${upload.query-path}") private String queryPath; @Autowired private IProjectBimService projectBimService; @Autowired private IProjectLogService projectLogService; @Autowired private IProjectService projectService; @Resource BimClient bimClient; @PostMapping("/file") public ResultData file(@RequestParam(required = false) MultipartFile file) { File localFile = uploadService.uploadFile(file); uploadToOssUtil.uploadOss(localFile.getPath(), FilePath.OSS_FILE_PATH + localFile.getName()); localFile.delete(); return ResultData.ok(queryPath + FilePath.OSS_FILE_PATH + localFile.getName() ); } /** * @api.name 通用上传OSS * @undone */ @PostMapping("/{projectId}/{type}/file/") public ResultData uploadFire(@RequestParam("file") MultipartFile file,@PathVariable Integer projectId, @PathVariable String type) throws IOException { if (!file.isEmpty()) { // 上传文件路径 File localFile = uploadService.uploadFile(file); String ossKey = String.format(FilePath.OSS_MEDIA_FILE_PATH,projectId,type,localFile.getName()); uploadToOssUtil.uploadOss(localFile.getPath(), ossKey); return ResultData.ok(queryPath+ossKey ); } return ResultData.error(ResultCode.UPLOAD_FILE_NO_EXIST); } @PostMapping("/bim") public ResultData bim(@RequestParam(required = false) MultipartFile file, @RequestParam(required = false)Integer projectId, @RequestParam(required = false)String projectName) { VUtils.isTure(file == null || file.getSize() <=0 ).throwMessage(ResultCode.PARAM_MISS); VUtils.isTure(projectId == null).throwMessage(ResultCode.PARAM_MISS); VUtils.isTure(StringUtils.isBlank(projectName)).throwMessage(ResultCode.PARAM_MISS); List bims = projectBimService.getByProjectId(projectId); for (ProjectBim bim : bims) { if(bim.getBimStatus().equals(BusinessStatus.DONE.getInfo())){ throw new BusinessException(ResultCode.UPLOAD_BIM_EXIST); } if(!bim.getBimStatus().equals(BusinessStatus.ERROR.getInfo())){ throw new BusinessException(ResultCode.UPLOAD_BIM_ING); } } ProjectBim projectBim = new ProjectBim(); projectBim.setProjectId(projectId); projectBim.setUserName(JwtUtil.getUserName(getToken())); String bimName = StringUtils.isBlank(projectName) ? file.getOriginalFilename(): projectName; if(bimName.contains(".")){ bimName = bimName.split("\\.")[0]; } projectBim.setBimName(bimName); projectBimService.save(projectBim); projectService.updateTime(projectId); projectLogService.addLog(projectBim.getProjectId(),"新增BIM:"+projectBim.getBimName(),getToken(),null); File localFile = null; try { localFile = uploadService.uploadFile(file); String newFileName = localFile.getName().substring(16); String ossKey = String.format(FilePath.OSS_BIM_PATH,projectId,newFileName); uploadToOssUtil.uploadOss(localFile.getPath(), ossKey); if(!uploadToOssUtil.existKey(ossKey)){ throw new BusinessException(ResultCode.UPLOAD_BIM_ERROR); } projectBim.setBimLocalFilePath(queryPath + ossKey); projectBim.setFileName(file.getOriginalFilename()); projectBimService.updateById(projectBim); BimUploadParam param = new BimUploadParam(); String callBackUrl = "http://" + request.getServerName() //服务器地址 + ":" + request.getServerPort() //端口号 + request.getRequestURI().replace("bim","callBack"); param.setTask(projectBim.getBimId().toString()); param.setProjectName(projectName); param.setSource(request.getContextPath()); param.setCallBack(callBackUrl); param.setFileName(projectBim.getFileName()); param.setFileUrl(projectBim.getBimLocalFilePath()); JSONObject jsonObject = bimClient.bimUpload(param); if(jsonObject.getInteger("code") !=200){ throw new BusinessException(ResultCode.UPLOAD_BIM_ERROR); } BimFaceVO faceVO = JSONObject.parseObject(jsonObject.getString("data"),BimFaceVO.class); projectBim.setBimStatus(faceVO.getCallType()); projectBim.setBimServiceId(faceVO.getId()); }catch (Exception e){ log.error("uploadBim-ERROR:",e); projectBim.setBimStatus("ERROR"); throw new BusinessException(ResultCode.UPLOAD_BIM_ERROR); } finally { projectBimService.updateById(projectBim); assert localFile != null; localFile.delete(); } return ResultData.ok(projectBim); } @PostMapping("/callBack") public ResultData callBack(@RequestBody BimCallBackDTO param){ log.info("bim-service-callBack:{}",param); String task = param.getTask(); ProjectBim bim = projectBimService.getById(Integer.valueOf(task)); bim.setBimStatus(param.getCallType()); if(param.getCallType().equals(BusinessStatus.DONE.getInfo())){ //完成 JSONObject jsonObject = bimClient.fileInfoById(bim.getBimServiceId()); if(jsonObject.getInteger("code") !=200){ bim.setBimStatus("ERROR"); }else { BimFaceVO faceVO = JSONObject.parseObject(jsonObject.getString("data"),BimFaceVO.class); bim.setBimOssFilePath(faceVO.getOssUrl()); } bim.setUpdateTime(null); projectBimService.updateById(bim); } return ResultData.ok(); } }