package com.fdkankan.fusion.service.impl;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fdkankan.fusion.common.FilePath;
import com.fdkankan.fusion.common.ResultCode;
import com.fdkankan.fusion.common.util.ShellUtil;
import com.fdkankan.fusion.entity.CaseImg;
import com.fdkankan.fusion.exception.BusinessException;
import com.fdkankan.fusion.mapper.ICaseImgMapper;
import com.fdkankan.fusion.request.CaseImgParam;
import com.fdkankan.fusion.service.ICaseImgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
*
* 服务实现类
*
*
* @author
* @since 2024-07-04
*/
@Service
@Slf4j
public class CaseImgServiceImpl extends ServiceImpl implements ICaseImgService {
@Override
public List getByCaseId(Integer caseId,Integer type) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CaseImg::getCaseId,caseId);
if(type != null){
wrapper.eq(CaseImg::getType,type);
}
wrapper.orderByAsc(CaseImg::getSort);
wrapper.orderByAsc(CaseImg::getId);
return this.list(wrapper);
}
@Override
public void updateSort(CaseImgParam param) {
if(param.getParamList() == null || param.getParamList().isEmpty()){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
for (CaseImgParam caseImgParam : param.getParamList()) {
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(CaseImg::getId,caseImgParam.getId());
wrapper.set(CaseImg::getSort,caseImgParam.getSort());
this.update(wrapper);
}
}
@Value("${upload.query-path}")
private String queryPath;
@Override
public CaseImg ffmpegImage(MultipartFile[] files, Integer caseId) {
if(files.length <=0 || caseId == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
String filePath = FilePath.FFMPEG_IMG_PATH +"/"+ caseId+"/";
try {
List localList = new ArrayList<>();
String outFileName = UUID.randomUUID().toString().replace("-","") ;
String outSuffixName = ".jpg";
String outLocalPath = filePath + outFileName +outSuffixName;
for (MultipartFile file : files) {
String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
//重新生成文件名
String fileName = UUID.randomUUID().toString().replace("-","") ;
String localFilePath = filePath + fileName + suffixName;
File file1 = new File(localFilePath);
if(!file1.getParentFile().exists()){
file1.getParentFile().mkdirs();
}
file.transferTo(file1);
localList.add(localFilePath);
}
StringBuilder ffmpegCmd = new StringBuilder("ffmpeg ");
if(files.length >1){
for (String localPath : localList) {
ffmpegCmd.append(" -i ").append(localPath);
}
ffmpegCmd.append(" -filter_complex hstack=inputs="+ files.length);
ffmpegCmd.append(" " +outLocalPath);
ShellUtil.execCmd(ffmpegCmd.toString());
}else {
outLocalPath = localList.get(0);
}
if(!FileUtil.exist(outLocalPath)){
throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
}
ShellUtil.yunUpload(outLocalPath,outLocalPath.replace("/mnt/",""));
List caseImgList = this.getByCaseId(caseId, 1);
CaseImg caseImg = null;
if(caseImgList ==null || caseImgList.isEmpty()){
caseImg = new CaseImg();
}else {
caseImg = caseImgList.get(0);
}
caseImg.setCaseId(caseId);
caseImg.setImgUrl(queryPath + outLocalPath.replace("/mnt/",""));
caseImg.setImgInfo("照片卷");
caseImg.setType(1);
this.saveOrUpdate(caseImg);
return caseImg;
}catch (Exception e){
log.info("ffmpeg错误:{}",caseId,e);
}finally {
try {
Thread.sleep(2000L);
FileUtil.del(filePath);
}catch (Exception e){
log.info("删除失败:{}",e);
}
}
return null;
}
}