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

import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.jutil.jdk.result.ResultStatus;
import cn.reghao.tnb.content.api.iface.UserContentService;
import cn.reghao.tnb.file.app.db.mapper.JobDetailMapper;
import cn.reghao.tnb.file.app.model.constant.JobStatus;

import java.time.LocalDateTime;

/**
 * @author reghao
 * @date 2024-12-04 13:46:25
 */
public class PublishVideoTask implements Runnable {
    private final long jobId;
    private final JobDetailMapper jobDetailMapper;
    private final UserContentService userContentService;
    private final String videoId;

    public PublishVideoTask(long jobId, JobDetailMapper jobDetailMapper, UserContentService userContentService, String videoId) {
        this.jobId = jobId;
        this.jobDetailMapper = jobDetailMapper;
        this.userContentService = userContentService;
        this.videoId = videoId;
    }

    @Override
    public void run() {
        String status = JobStatus.Success.getDesc();
        try {
            Result result = userContentService.setVideoPostPublish(videoId);
            if (result.getCode() != ResultStatus.SUCCESS.getCode()) {
                status = JobStatus.Fail.getDesc();
            }
        } catch (Exception e) {
            e.printStackTrace();
            status = JobStatus.Fail.getDesc();
        }

        LocalDateTime current = LocalDateTime.now();
        jobDetailMapper.updateSetEnd(jobId, status, current);
    }
}
