package com.xhkjedu.sexam.listener;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
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
@Slf4j
public class MessageSender {
    @Autowired
    private AmqpTemplate rabbitTemplate;
    //负载一生产者mq
    @Value("${rabbitmq.examImgMergeQueue}")
    private String examImgMergeQueue;
    @Value("${rabbitmq.examBaseToPdfQueue}")
    private String examBaseToPdfQueue;
    @Value("${rabbitmq.examBaseToScanImgQueue}")
    private String examBaseToScanImgQueue;
    @Value("${rabbitmq.examImgToPdfQueue}")
    private String examImgToPdfQueue;
    @Value("${rabbitmq.examHtmlToPdfQueue}")
    private String examHtmlToPdfQueue;
    //负载二生产者mq
    @Value("${rabbitmq.examImgMergeQueue2}")
    private String examImgMergeQueue2;
    @Value("${rabbitmq.examBaseToPdfQueue2}")
    private String examBaseToPdfQueue2;
    @Value("${rabbitmq.examBaseToScanImgQueue2}")
    private String examBaseToScanImgQueue2;
    @Value("${rabbitmq.examImgToPdfQueue2}")
    private String examImgToPdfQueue2;
    //负载二节点名称
    private final String node2="node02/";

    /**
     * @Description 学生答案合并
     * @Param [epsqid, userAnswers, device]
     * @Return void
     * @Author wn
     * @Date 2022/7/28 16:46
     **/
    public void imgMerge(Integer epsqid, List<String> userAnswers, Integer device) {
        Map map = new HashMap();
        map.put("epsqid", epsqid);
        map.put("stuanswers", userAnswers);
        map.put("device", device);
        if (userAnswers.toString().contains(node2)) {
            rabbitTemplate.convertAndSend(examImgMergeQueue2, JSON.toJSONString(map));
        } else {
            rabbitTemplate.convertAndSend(examImgMergeQueue, JSON.toJSONString(map));
        }
    }

    /**
     * @Description 答题卡图片合并为pdf
     * @Param [epid, imgs]
     * @Return void
     * @Author wn
     * @Date 2022/8/2 13:57
     **/
    public void base64ToPdf(Integer examid,Integer epid, List<String> imgs,String savefolder,Integer scantroncol,String filename) {
        Map map = new HashMap();
        map.put("examid", examid);
        map.put("epid",epid);
        map.put("imgs", imgs);
        map.put("scantroncol", scantroncol);
        map.put("savefolder",savefolder);
        map.put("filename",filename);
        if (imgs.toString().contains(node2)) {
            rabbitTemplate.convertAndSend(examBaseToPdfQueue2, JSON.toJSONString(map));
        } else {
            rabbitTemplate.convertAndSend(examBaseToPdfQueue, JSON.toJSONString(map));
        }
    }

    /**
     * @Description  扫码出错图片base64转图片
     * @Param [serrorid, imgs, savefolder]
     * @Return void
     * @Author wn
     * @Date 2022/9/19 9:40
     **/
    public void base64ToScanImg(Integer serrorid,Integer examid,List<String> imgs,String savefolder) {
        Map map = new HashMap();
        map.put("serrorid", serrorid);
        map.put("examid", examid);
        map.put("imgs", imgs);
        map.put("savefolder",savefolder);
        if (imgs.toString().contains(node2)) {
            rabbitTemplate.convertAndSend(examBaseToScanImgQueue2, JSON.toJSONString(map));
        } else {
            rabbitTemplate.convertAndSend(examBaseToScanImgQueue, JSON.toJSONString(map));
        }
    }

    /**
     * @Description 考试附件试卷图片转pdf
     * @Param [imgs, epid]
     * @Return void
     * @Author wn
     * @Date 2022/11/1 10:43
     **/
    public void ImgToPdf(List<String> imgs,Integer epid) {
        Map map = new HashMap();
        map.put("epid",epid);
        map.put("imgs", imgs);
        if (imgs.toString().contains(node2)) {
            rabbitTemplate.convertAndSend(examImgToPdfQueue2, JSON.toJSONString(map));
        } else {
            rabbitTemplate.convertAndSend(examImgToPdfQueue, JSON.toJSONString(map));
        }
    }

    /**
     * @Description 考试报告html转pdf
     * @Param [map]
     * @Author ywx
     * @Date 2023/2/14 11:24
     * @return void
     **/
    public void htmlToPdf(Map map) {
        rabbitTemplate.convertAndSend(examHtmlToPdfQueue, JSON.toJSONString(map));
    }
}
