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.LocalToOssUtil;
import com.fdkankan.fusion.common.util.ShellCmd;
import com.fdkankan.fusion.common.util.ShellUtil;
import com.fdkankan.fusion.config.CacheUtil;
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.Autowired;
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);
}
}
@Autowired
LocalToOssUtil localToOssUtil;
@Override
public CaseImg ffmpegImage(MultipartFile[] files, Integer caseId) {
if(files == null || files.length <=0 || caseId == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
String filePath = FilePath.FFMPEG_IMG_PATH +"/"+ caseId+"/";
String ossPath = FilePath.OSS_FFMPEG_IMG_PATH +"/"+ caseId+"/";
try {
List localList = new ArrayList<>();
List localListOss = new ArrayList<>();
String outFileName = UUID.randomUUID().toString().replace("-","") ;
String outSuffixName = ".jpg";
String outLocalPath = filePath + outFileName +outSuffixName;
String outLocalPathOss = ossPath + 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;
String localFilePathOSS = ossPath + fileName + suffixName;
File file1 = new File(localFilePath);
if(!file1.getParentFile().exists()){
file1.getParentFile().mkdirs();
}
file.transferTo(file1);
localList.add(localFilePath);
localListOss.add(localFilePathOSS);
}
StringBuilder ffmpegCmd = new StringBuilder(ShellCmd.ffmpegCmd);
if(localList.size() >1){
for (String localPath : localList) {
ffmpegCmd.append(" -i ").append(localPath);
}
ffmpegCmd.append(" -filter_complex hstack=inputs="+ localList.size());
ffmpegCmd.append(" " +outLocalPath);
ShellUtil.execCmd(ffmpegCmd.toString());
}else {
outLocalPath = localList.get(0);
outLocalPathOss = localListOss.get(0);
}
if(!FileUtil.exist(outLocalPath)){
throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
}
localToOssUtil.uploadOss(outLocalPath,outLocalPathOss);
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(CacheUtil.mapping +outLocalPathOss);
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){
}
}
return null;
}
}