package com.xhkjedu.sstudy.service.resource;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.sstudy.config.ConfigKey;
import com.xhkjedu.sstudy.mapper.resource.ResourceMapper;
import com.xhkjedu.sstudy.model.resource.TResource;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description：flv非h264格式视频转码
 * @Author: WN
 * @Date: 2024/9/14 16:16:02
 **/
@Slf4j
@Service
public class FlvUpdateResService {

    @Resource
    ResourceMapper resourceMapper;

    /*
     * @Description 如果flv编码不是h264格式，则设置资源带转换
     * @Date 2024/9/14 16:33:23
     * @Author WN
     * @Param []
     * @Return void
     **/
    public int hadnleFlvResource() {
        List<TResource> resourceList = resourceMapper.listFlvResources();
        int count = 0;
        for (TResource resource : resourceList) {
            String resourceid = resource.getResourceid();

            String params = "filepath=" + resource.getResourceurl();
            String fileInfoStr = N_Utils.sendPost(ConfigKey.fileapi + "video/vc", params);

            ResultVo fileResult = JSON.parseObject(fileInfoStr, ResultVo.class);
            if (fileResult.getCode() == 0 && N_Utils.isNotEmpty(fileResult.getObj())) {
                String flvCode = fileResult.getObj().toString();
                if (!"h264".equals(flvCode)) {
                    resourceMapper.updateFlvResconverted(resourceid, flvCode);
                    count++;
                }else{
                    resourceMapper.updateFlvMp4code(resourceid, "h264");
                }
            }
        }
        return count;
    }

}
