package cn.reghao.tnb.file.app.zjob.task;

import cn.reghao.jutil.jdk.media.FFmpegWrapper;
import cn.reghao.tnb.content.api.iface.AdminVideoService;
import cn.reghao.tnb.file.app.zjob.db.JobDetailMapper;
import cn.reghao.tnb.file.app.zjob.model.constant.JobStatus;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.time.LocalDateTime;

/**
 * @author reghao
 * @date 2024-11-25 09:01:04
 */
public class ConvertTask implements Runnable {
    private final long jobId;
    private final JobDetailMapper jobDetailMapper;
    private final ConvertTaskInfo convertTaskInfo;
    private final AdminVideoService adminVideoService;

    public ConvertTask(long jobId, JobDetailMapper jobDetailMapper, ConvertTaskInfo convertTaskInfo) {
        this.jobId = jobId;
        this.jobDetailMapper = jobDetailMapper;
        this.convertTaskInfo = convertTaskInfo;
        this.adminVideoService = convertTaskInfo.getAdminVideoService();
    }

    @Override
    public void run() {
        String videoFileId = convertTaskInfo.getVideoFileId();
        String srcPath = convertTaskInfo.getSrcPath();
        String destPath = convertTaskInfo.getDestPath();
        String format = "mp4";
        try {
            String status = JobStatus.Success.getDesc();
            int ret = FFmpegWrapper.formatCovert(srcPath, destPath, format);
            if (ret == 0) {
                int channelCode = convertTaskInfo.getChannelCode();
                File destFile = new File(destPath);
                /*UploadFileRet uploadFileRet = ossConsoleClient.postObject(destFile, channelCode);
                if (uploadFileRet != null) {
                    String uploadId = uploadFileRet.getUploadId();
                    VideoConvertedDto videoConvertedDto = new VideoConvertedDto(videoFileId, channelCode, uploadId, format);
                    adminVideoService.addConvertedVideo(videoConvertedDto);
                }*/
            } else {
                status = JobStatus.Fail.getDesc();
            }

            FileUtils.deleteQuietly(new File(srcPath));
            FileUtils.deleteQuietly(new File(destPath));

            LocalDateTime current = LocalDateTime.now();
            jobDetailMapper.updateSetEnd(jobId, status, current);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
