package com.fdkankan.scene.service.impl;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.fyun.face.FYunFileServiceInterface;
import com.fdkankan.model.constants.ConstantFilePath;
import com.fdkankan.model.constants.UploadFilePath;
import com.fdkankan.model.utils.CreateObjUtil;
import com.fdkankan.rabbitmq.util.RabbitMqProducer;
import com.fdkankan.scene.entity.DownloadTourVideo;
import com.fdkankan.scene.mapper.IDownloadTourVideoMapper;
import com.fdkankan.scene.service.IDownloadTourVideoService;
import com.fdkankan.scene.vo.DownloadTourVideoVO;
import com.fdkankan.web.response.ResultData;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import lombok.SneakyThrows;
import org.apache.http.HttpHeaders;
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-10-12
*/
@Service
public class DownloadTourVideoServiceImpl extends ServiceImpl implements IDownloadTourVideoService {
@Value("${queue.scene.transfer-tour-video}")
private String downloadTourVideoQueue;
@Value("${fyun.type}")
private String type;
@Value("${fyun.bucket:4dkankan}")
private String bucket;
@Autowired
private RabbitMqProducer rabbitMqProducer;
@Autowired
private FYunFileServiceInterface fYunFileService;
@Override
public DownloadTourVideo getWaitingByNum(String num) {
return this.getOne(
new LambdaQueryWrapper().eq(DownloadTourVideo::getNum, num)
.eq(DownloadTourVideo::getState, 0));
}
@Override
public void removeByNum(String num) {
this.remove(new LambdaQueryWrapper().eq(DownloadTourVideo::getNum, num));
}
@Override
public ResultData uploadTourVideo(String num, MultipartFile file) throws Exception {
//查询是否有任务正在执行,如果有,直接返回
DownloadTourVideo waiting = this.getWaitingByNum(num);
if(Objects.nonNull(waiting)){
throw new BusinessException(ErrorCode.FAILURE_CODE_5064);
}
String uuid = UUID.randomUUID().toString();
String fileName = file.getOriginalFilename();
String extName = cn.hutool.core.io.FileUtil.extName(fileName);
String tempFileName = uuid + "." + extName;
String srcPath = ConstantFilePath.SCENE_V4_PATH + num + "/tour/" + tempFileName;
File tempFile = new File(srcPath);
if(!tempFile.getParentFile().exists()){
tempFile.getParentFile().mkdirs();
}
file.transferTo(tempFile);
//先将旧纪录置为无效
this.removeByNum(num);
//写入新记录
DownloadTourVideo downloadTourVideo = new DownloadTourVideo();
downloadTourVideo.setNum(num);
downloadTourVideo.setFileName(fileName);
downloadTourVideo.setLocalPath(srcPath);
this.save(downloadTourVideo);
//发送mq
rabbitMqProducer.sendByWorkQueue(downloadTourVideoQueue, downloadTourVideo);
return ResultData.ok();
}
@Override
public void transferTourVideo(DownloadTourVideo downloadTourVideo) {
String destPath = null;
try {
String destFileName = UUID.randomUUID().toString() + ".mp4";
destPath = ConstantFilePath.SCENE_V4_PATH + downloadTourVideo.getNum() + "tour/" + destFileName;
File destFile = new File(destPath);
if(!destFile.getParentFile().exists()){
destFile.getParentFile().mkdir();
}
String srcPath = downloadTourVideo.getLocalPath();
CreateObjUtil.formatMp4(srcPath, destPath);
//上传到oss
String ossPath = String.format(UploadFilePath.DOWNLOADS_TOUR_VIDEO, downloadTourVideo.getNum()) + downloadTourVideo.getFileName();
Map headers = new HashMap<>();
headers.put(HttpHeaders.CONTENT_TYPE, "application/octet-stream");
fYunFileService.uploadFile(bucket, destPath, ossPath, headers);
downloadTourVideo.setDownloadPath(ossPath);
downloadTourVideo.setState(1);
this.updateById(downloadTourVideo);
}catch (Exception e) {
log.error("导览视频转换失败,num:" + downloadTourVideo.getNum() + "源路径:" + downloadTourVideo.getLocalPath(), e);
downloadTourVideo.setReason(ExceptionUtil.stacktraceToString(e, 3000));
downloadTourVideo.setState(2);
this.updateById(downloadTourVideo);
}
}
@Override
public ResultData downloadTourVideo(String num) {
DownloadTourVideoVO result = new DownloadTourVideoVO();
DownloadTourVideo downloadTourVideo = this.getOne(new LambdaQueryWrapper().eq(DownloadTourVideo::getNum, num));
if(Objects.isNull(downloadTourVideo)){
result.setStatus(0);
}else{
result.setStatus(1);
result.setTransferStatus(downloadTourVideo.getState());
result.setPath(downloadTourVideo.getDownloadPath());
}
return ResultData.ok(result);
}
}