package org.jeecg.modules.ctop.job;

import cn.com.ctop.common.module.entity.MaterialInfo;
import cn.com.ctop.common.module.entity.VideoWatermarkTask;
import cn.com.ctop.common.module.service.IMaterialInfoService;
import cn.com.ctop.common.module.service.IVideoWatermarkTaskService;
import cn.com.ctop.common.module.utils.Check;
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.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;

public class WatermarkVideoJob implements Job {
    @Autowired
    private IVideoWatermarkTaskService videoWaterMarkService;
    @Autowired
    private IMaterialInfoService materialInfoService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Thread thread = new Thread() {
            @Override
            public void run() {
                QueryWrapper<VideoWatermarkTask> queryWrapper = new QueryWrapper<>();
                queryWrapper.in("status", "Submitted", "Transcoding");
                List<VideoWatermarkTask> list = videoWaterMarkService.list(queryWrapper);
                if (list != null && list.size() > 0) {
                    for (VideoWatermarkTask videoWatermarkTask : list) {
                        MpsUtils mpsUtils = new MpsUtils();
                        QueryJobListResponse.Job job = mpsUtils.getJobStatus(videoWatermarkTask.getJobId());
                        if (job.getState().equals("TranscodeSuccess")) {
                            String url = "https://ctop-media.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);
                                QueryWrapper<MaterialInfo> materialInfoQueryWrapper = new QueryWrapper<>();
                                materialInfoQueryWrapper.eq("watermark_code", job.getJobId());
                                MaterialInfo materialInfo = materialInfoService.getOne(materialInfoQueryWrapper);
                                if (Check.isNull(materialInfo)) {
                                    continue;

                                }
                                materialInfo.setWatermarkUrl(url);
                                materialInfoService.saveOrUpdate(materialInfo);
                                videoWatermarkTask.setVideoUrl(url);
                                videoWatermarkTask.setStatus(job.getState());
                                videoWatermarkTask.setVideoId(materialInfo.getId());
                                videoWaterMarkService.updateById(videoWatermarkTask);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        };
        thread.start();
    }
}
