package com.fdkankan.fusion.service.impl;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.fusion.common.PageInfo;
import com.fdkankan.fusion.common.util.JwtUtil;
import com.fdkankan.fusion.common.FilePath;
import com.fdkankan.fusion.common.ResultCode;
import com.fdkankan.fusion.common.util.OBJToGLBUtil;
import com.fdkankan.fusion.common.util.ShellUtil;
import com.fdkankan.fusion.common.util.UploadToOssUtil;
import com.fdkankan.fusion.entity.CaseNumEntity;
import com.fdkankan.fusion.entity.FusionNum;
import com.fdkankan.fusion.entity.Model;
import com.fdkankan.fusion.exception.BusinessException;
import com.fdkankan.fusion.mapper.IModelMapper;
import com.fdkankan.fusion.request.ModelPram;
import com.fdkankan.fusion.request.ScenePram;
import com.fdkankan.fusion.response.SceneVo;
import com.fdkankan.fusion.service.ICaseNumService;
import com.fdkankan.fusion.service.IFusionNumService;
import com.fdkankan.fusion.service.IModelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jdk.nashorn.tools.Shell;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
/**
*
* 服务实现类
*
*
* @author
* @since 2022-08-03
*/
@Service
public class ModelServiceImpl extends ServiceImpl implements IModelService {
@Autowired
UploadToOssUtil uploadToOssUtil;
@Autowired
UploadService uploadService;
@Autowired
ICaseNumService caseNumService;
@Autowired
IFusionNumService fusionNumService;
@Value("${local.obj_path}")
private String OBJ_PATH;
@Value("${local.glb_path}")
private String GLB_PATH;
@Value("${upload.query-path}")
private String queryPath;
@Override
public void uploadObj(MultipartFile file, String username) throws Exception {
if(file.isEmpty()){
throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
}
if(file.getSize()>10 * 1024 * 1024 * 100){
System.out.println(file.getSize());
throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG);
}
//获取文件名
String fileName = file.getOriginalFilename();
if(StringUtils.isEmpty(fileName)){
throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
}
if(!fileName.toLowerCase().contains("zip") && !fileName.toLowerCase().contains("rar")){
throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
}
//获取文件后缀名
String modelName = fileName.substring(0,fileName.lastIndexOf("."));
Model model = new Model();
model.setModelTitle(modelName);
model.setModelDateType("obj");
model.setModelType("glb");
model.setModelSize(file.getSize());
model.setUserName(username);
this.save(model);
File newObjFile = null;
try {
String objPath = String.format(OBJ_PATH , "modelId_"+model.getModelId());
String glbPath = String.format(GLB_PATH , "modelId_"+model.getModelId()) +"/"+modelName +"/mesh.glb";
String glbOssPath = String.format(FilePath.GLB_OSS_PATH, model.getModelId());
newObjFile = new File(objPath +"/" + fileName);
if(!newObjFile.getParentFile().exists()){
newObjFile.mkdirs();
}
file.transferTo(newObjFile);
if(fileName.toLowerCase().contains("zip")){
ShellUtil.unZip(newObjFile.getPath(),objPath);
}
if(fileName.toLowerCase().contains("rar")){
ShellUtil.unRar(newObjFile.getPath(),objPath);
}
OBJToGLBUtil.objToGlb(objPath+"/"+modelName,glbPath);
uploadToOssUtil.upload(glbPath,glbOssPath);
model.setModelObjUrl(objPath);
model.setModelGlbUrl(queryPath + glbOssPath);
model.setCreateStatus(1); //上传成功
this.updateById(model);
}catch (Exception e){
model.setCreateStatus(-1);
this.updateById(model);
throw e;
}finally {
if(newObjFile!=null){
newObjFile.delete();
}
}
}
@Override
public PageInfo pageList(ModelPram param, String token) {
String username = JwtUtil.getUsername(token);
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Model::getUserName,username);
wrapper.eq(Model::getType,3);
if(param.getStatus()!=null){ //参数2为成功,数据库中成功为1
wrapper.eq(Model::getCreateStatus,param.getStatus() == 2 ? 1 :param.getStatus());
}
if(StringUtils.isNotBlank(param.getModelTitle())){
wrapper.like(Model::getModelTitle,param.getModelTitle());
}
wrapper.orderByDesc(Model::getCreateTime);
Page page = this.page(new Page<>(param.getPageNum(),param.getPageSize()),wrapper);
for (Model model : page.getRecords()) {
if(model.getType() == 3 && StringUtils.isEmpty(model.getNum())) {
model.setNum(model.getModelId().toString());
}
}
return PageInfo.PageInfo(page);
}
@Override
public void delete(Integer modelId) {
List caseNumEntityList = caseNumService.getByNum(modelId.toString());
List fusionNumList = fusionNumService.getByNum(modelId.toString());
if(caseNumEntityList.size() >0 || fusionNumList.size() >0){
throw new BusinessException(ResultCode.CASE_USE);
}
Model model = this.getById(modelId);
if(model == null ){
throw new BusinessException(ResultCode.MODEL_NOT_EXIST);
}
this.removeById(modelId);
if(StringUtils.isNotBlank(model.getModelGlbUrl())){
uploadService.deleteOssUrl(model.getModelGlbUrl());
}
}
@Override
public List getListByNum(List numList) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.in(Model::getNum,numList);
return this.list(wrapper);
}
@Override
public List getListByModelIds(List modelIds) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.in(Model::getModelId,modelIds);
return this.list(wrapper);
}
@Override
public List getListByModelIdStrs(List numList) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.in(Model::getModelId,numList);
return this.list(wrapper);
}
@Override
public void deleteByNum(List numList) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.in(Model::getNum,numList);
this.remove(wrapper);
}
@Override
public List getByUserName(String username) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Model::getUserName,username)
.eq(Model::getType,3);
return this.list(wrapper);
}
@Override
public Model getIsNullNewByNum(String num) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Model::getNum,num);
Model model = this.getOne(wrapper);
if(model == null){
model = new Model();
}
return model;
}
@Override
public Object getInfo(Integer modelId) {
Model model = this.getById(modelId);
SceneVo sceneVo = new SceneVo();
BeanUtils.copyProperties(model,sceneVo);
return sceneVo;
}
}