package com.xhkjedu.sstudy.utils;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.sstudy.listener.MessageSender;
import com.xhkjedu.sstudy.mapper.resource.ResourceFileMapper;
import com.xhkjedu.sstudy.model.resource.TResource;
import com.xhkjedu.sstudy.vo.resource.ResourceVo;
import com.xhkjedu.utils.JedisUtil;
import com.xhkjedu.utils.N_Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @author ywx
 * @classname ConvertUtil
 * @description 文件转换工具类
 * @date 2021/10/27 9:32
 **/
@Component
public class ConvertUtil extends JedisUtil {
    @Resource
    private ResourceFileMapper resourceFileMapper;
    @Autowired
    private MessageSender messageSender;
    private static Integer[] CLASS = {1,2,5,7};//资源分类1word,2ppt,3pdf,4图片,5视频,6音频,7压缩包,10其他
    private static Integer redisdatatime = 180;//有效期3分钟

    //资源转换（word或ppt转为pdf，视频非h264编码转码，压缩包解压）
    public void convertResource(TResource model) {
        Integer resourceclass = model.getResourceclass();
        //flv的视频不转换
        // if ("flv".equals(model.getSuffix())) return;
        if (Arrays.asList(CLASS).contains(resourceclass) && model.getConverted() == 0) {
            if (redisHandle(model.getResourceurl(), model.getResourceid())) return;
            ResourceVo resource = new ResourceVo();
            resource.setResourceid(model.getResourceid());
            resource.setResourceclass(resourceclass);
            resource.setResourceurl(model.getResourceurl());
            resource.setSuffix(model.getSuffix());
            messageSender.convertResource(resource);
        }
    }

    //redis操作
    private boolean redisHandle(String resourceurl, String resourceid) {
        String[] strings = resourceurl.split("/");
        String str1 = strings[0];
        String cloudcode;
        if (str1.startsWith("node")) {
            cloudcode = strings[1];
        } else {
            cloudcode = str1;
        }
        String key = cloudcode + "_" + resourceid;
        String redis_value = get(key);
        if (N_Utils.isNotEmpty(redis_value)) return true;
        set(key, resourceurl, redisdatatime);
        return false;
    }

    //获取资源预览路径
    public synchronized String getResourceUrl(ResourceVo resource) {
        String pdfurl = resource.getPdfurl();
        Integer resourceclass = resource.getResourceclass();
        Integer converted = resource.getConverted();
        if ((resourceclass == 1 || resourceclass == 2) && converted == 0) {
            if (redisHandle(resource.getResourceurl(), resource.getResourceid())) return null;
            //需要把word或ppt转为pdf
            pdfurl = messageSender.convertResource(resource);
        } else if (resourceclass == 5) {
            pdfurl = videoTransfer(resource);
        } else if (resourceclass == 7) {
            pdfurl = unzip(resource);
        }
        resource.setPdfurl(pdfurl);
        return pdfurl;
    }

    //视频转换
    public String videoTransfer(ResourceVo resource) {
        String resourceurl = resource.getResourceurl();
        String suffix = resource.getSuffix();
        if (!"h264".equals(resource.getMp4code())) {
            if (redisHandle(resource.getResourceurl(), resource.getResourceid())) return null;
            resourceurl = messageSender.convertResource(resource);
        }
        return resourceurl;
    }

    //解压zip
    public String unzip(ResourceVo resource) {
        String resourceid = resource.getResourceid();
        String pdfurl = null;
        Integer converted = resource.getConverted();
        if (converted == 0) {
            if (redisHandle(resource.getResourceurl(), resourceid)) return null;
            //获取压缩包文件
            pdfurl = messageSender.convertResource(resource);
        } else {
            if (resource.getBelong().equals(10)) resourceid = resource.getResourcepid();
            List<Map> files = resourceFileMapper.listFile(resourceid);
            pdfurl = JSON.toJSONString(files);
        }
        return pdfurl;
    }

    /**
     * 学生答案图片合并mq
     *
     * @param [psqid, userAnswers, papertype(作业类型1题库作业2附件作业)]
     * @return void
     * @author ywx
     * @date 2022/5/9 14:57
     */
    public void imgMerge(Integer psqid, List<String> userAnswers, Integer papertype) {
        messageSender.imgMerge(psqid, userAnswers, papertype);
    }

}
