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;
    //负载一生产者mq
    @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
    @Value("${rabbitmq.resourceToPdfQueue2}")
    private String resourceToPdfQueue2;
    @Value("${rabbitmq.resourceTransferQueue2}")
    private String resourceTransferQueue2;
    @Value("${rabbitmq.resourceUnZipQueue2}")
    private String resourceUnZipQueue2;
    @Value("${rabbitmq.resourceDelQueue2}")
    private String resourceDelQueue2;
    @Value("${rabbitmq.imgMergeQueue2}")
    private String imgMergeQueue2;
    //负载二节点名称
    private final String node2="node02/";

    //资源转换mq
    public String convertResource(ResourceVo resource) {
        Integer resourceclass = resource.getResourceclass();
        String resourceurl = resource.getResourceurl();
        Map map = new HashMap();
        map.put("resourceid", resource.getResourceid());
        map.put("resourceclass", resourceclass);
        map.put("resourceurl", resourceurl);
        map.put("suffix", resource.getSuffix());
        String mqKey;
        if (resourceclass == 5) {
            map.put("source", "resource");
            if (resourceurl.contains(node2)) {
                mqKey = resourceTransferQueue2;
            } else {
                mqKey = resourceTransferQueue;
            }
        } else if (resourceclass == 7) {
            if (resourceurl.contains(node2)) {
                mqKey = resourceUnZipQueue2;
            } else {
                mqKey = resourceUnZipQueue;
            }
        } else {
            if (resourceurl.contains(node2)) {
                mqKey = resourceToPdfQueue2;
            } 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);
        if (resourceurl.contains(node2)) {
            rabbitTemplate.convertAndSend(resourceTransferQueue2, JSON.toJSONString(map));
        } else {
            rabbitTemplate.convertAndSend(resourceTransferQueue, JSON.toJSONString(map));
        }
    }

    //资源删除mq
    public void resourceDel(List<String> resourceurls) {
        if (resourceurls.toString().contains(node2)) {
            rabbitTemplate.convertAndSend(resourceDelQueue2, JSON.toJSONString(resourceurls));
        } else {
            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 papertype) {
        Map map = new HashMap();
        map.put("psqid", psqid);
        map.put("userAnswers", userAnswers);
        map.put("papertype", papertype);
        if (userAnswers.toString().contains(node2)) {
            rabbitTemplate.convertAndSend(imgMergeQueue2, JSON.toJSONString(map));
        } else {
            rabbitTemplate.convertAndSend(imgMergeQueue, JSON.toJSONString(map));
        }
    }
}
