package com.xhkjedu.sstudy.listener;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.sstudy.vo.resource.ResourceVo;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ywx
 * @className MessageSender
 * @description
 * @date 2021/10/25 14:28
 **/
@Component
public class MessageSender {
    @Autowired
    private AmqpTemplate rabbitTemplate;
    @Value("${rabbitmq.resourceToPdfQueue}")
    private String resourceToPdfQueue;
    @Value("${rabbitmq.resourceTransferQueue}")
    private String resourceTransferQueue;
    @Value("${rabbitmq.resourceUnZipQueue}")
    private String resourceUnZipQueue;
    @Value("${rabbitmq.resourceDelQueue}")
    private String resourceDelQueue;
    @Value("${rabbitmq.imgMergeQueue}")
    private String imgMergeQueue;

    //资源转换mq
    public String convertResource(ResourceVo resource) {
        Integer resourceclass = resource.getResourceclass();
        Map map = new HashMap();
        map.put("resourceid", resource.getResourceid());
        map.put("resourceclass", resourceclass);
        map.put("resourceurl", resource.getResourceurl());
        map.put("suffix", resource.getSuffix());
        String mqKey;
        if (resourceclass == 5) {
            map.put("source", "resource");
            mqKey = resourceTransferQueue;
        } else if (resourceclass == 7) {
            mqKey = resourceUnZipQueue;
        } else {
            mqKey = resourceToPdfQueue;
        }
        rabbitTemplate.convertAndSend(mqKey, JSON.toJSONString(map));
        return null;
    }

    //教研、错题讲解视频转换mq
    public void convertVideo(Integer resourceid, String resourceurl, String source) {
        Map map = new HashMap();
        map.put("resourceid", resourceid);
        map.put("resourceclass", 5);
        map.put("resourceurl", resourceurl);
        map.put("suffix", "mp4");
        map.put("source", source);
        rabbitTemplate.convertAndSend(resourceTransferQueue, JSON.toJSONString(map));
    }

    //资源删除mq
    public void resourceDel(List<String> resourceurls) {
        rabbitTemplate.convertAndSend(resourceDelQueue, JSON.toJSONString(resourceurls));
    }

    /**
     * 学生答案图片合并mq
     *
     * @param [psqid, userAnswers, device(作业类型1题库作业2附件作业)]
     * @return void
     * @author ywx
     * @date 2022/5/9 13:40
     */
    public void imgMerge(Integer psqid, List<String> userAnswers, Integer device) {
        Map map = new HashMap();
        map.put("psqid", psqid);
        map.put("userAnswers", userAnswers);
        map.put("device", device);
        rabbitTemplate.convertAndSend(imgMergeQueue, JSON.toJSONString(map));
    }
}
