package org.jeecg.modules.ctop.job;

import cn.com.ctop.common.module.entity.MaterialInfo;
import cn.com.ctop.common.module.entity.VideoMergeTask;
import cn.com.ctop.common.module.service.IMaterialInfoService;
import cn.com.ctop.common.module.service.IVideoMergeTaskService;
import cn.com.ctop.common.module.utils.LoadFileUtil;
import cn.com.ctop.common.module.utils.MpsUtils;
import cn.com.ctop.common.module.utils.PropertiesUtils;
import com.aliyuncs.mts.model.v20140618.QueryJobListResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.util.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class MergeVideoJob implements Job {
    @Autowired
    private IVideoMergeTaskService videoMergeTaskService;
    @Autowired
    private IMaterialInfoService materialInfoService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        QueryWrapper<VideoMergeTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("status", "Submitted", "Transcoding");
        List<VideoMergeTask> list = videoMergeTaskService.list(queryWrapper);
        if (list != null && list.size() > 0) {
            for (VideoMergeTask videoMergeTask : list) {
                QueryJobListResponse.Job job = MpsUtils.getJobStatus(videoMergeTask.getJobId());
                if ("TranscodeSuccess".equals(job.getState())) {
                    String url = "https://ctop-part.oss-cn-beijing.aliyuncs.com/" + job.getOutput().getOutputFile().getObject();

                    try {
                        String localPath = LoadFileUtil.downLoadFromUrl(url, PropertiesUtils.getValue("kuaishou_config", "video_sava_path"));
//                        String localPath = LoadFileUtil.downLoadFromUrl(url,"D:/video/");
                        String md5 = LoadFileUtil.getMD5(localPath);
                        LoadFileUtil.delFile(localPath);
                        MaterialInfo materialInfo = new MaterialInfo();
                        materialInfo.setCode(md5);
                        materialInfo.setId(md5);
                        materialInfo.setUrl(url);
                        materialInfo.setUserId(videoMergeTask.getUserId());
                        materialInfo.setStatus(0);
                        materialInfo.setType("VIDEO");
                        materialInfo.setMaterialName("合成" + DateUtils.getMillis());
                        materialInfoService.saveOrUpdate(materialInfo);
                        Thread.sleep(3 * 1000);
                  //      materialInfoService.getFile(materialInfo);
                        videoMergeTask.setVideoUrl(url);
                        videoMergeTask.setStatus(job.getState());
                        videoMergeTask.setVideoId(materialInfo.getId());
                        videoMergeTaskService.updateById(videoMergeTask);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
