package com.xhkjedu.sexam.listener;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.sexam.mapper.exam.EBaseMapper;
import com.xhkjedu.sexam.mapper.paper.EPaperFileMapper;
import com.xhkjedu.sexam.mapper.paper.EPaperMapper;
import com.xhkjedu.sexam.mapper.paperstudent.EPaperStudentQuestionMapper;
import com.xhkjedu.sexam.mapper.paperstudent.EScanerrorMapper;
import com.xhkjedu.sexam.mapper.report.EReportGenerateMapper;
import com.xhkjedu.sexam.model.paper.EPaper;
import com.xhkjedu.sexam.model.paper.EPaperFile;
import com.xhkjedu.sexam.model.paperstudent.EPaperStudentQuestion;
import com.xhkjedu.sexam.model.paperstudent.EScanerror;
import com.xhkjedu.sexam.utils.ConvertUtil;
import com.xhkjedu.sexam.vo.report.EReportVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 消息到达消费者监听类
 */
@Component
@Slf4j
public class MessageConsumer {

    @Resource
    private EPaperStudentQuestionMapper ePaperStudentQuestionMapper;
    @Resource
    private EPaperMapper ePaperMapper;
    @Resource
    private EScanerrorMapper eScanerrorMapper;
    @Resource
    private EPaperFileMapper ePaperFileMapper;
    @Resource
    private EReportGenerateMapper eReportGenerateMapper;
    @Resource
    private EBaseMapper eBaseMapper;
    @Autowired
    private ConvertUtil convertUtil;


    /*
     * 学生答案图片合并完处理转换状态
     *
     * @param [message]
     * @return void
     * @author ywx
     * @date 2022/5/9 13:44
     */
    @RabbitHandler
    @RabbitListener(queues = "${rabbitmq.examImgMergeHandleQueue}")
    public void imgMergeHandleQueue(Message message) {
        try {
            String msg = new String(message.getBody(), "UTF-8");
            System.out.println(LocalDateTime.now() + "学生试题答案合并接收消息内容：" + msg);
            EPaperStudentQuestion psq = JSON.parseObject(msg, EPaperStudentQuestion.class);
            if (psq.getConverted() != 1) {//合并失败不修改学生答案
                psq.setStuanswer(null);
            } else {
                psq.setStuanswer(JSON.toJSONString(new String[]{psq.getStuanswer()}));
            }
            ePaperStudentQuestionMapper.updateByPrimaryKeySelective(psq);
        } catch (Exception e) {
            log.error("学生试题答案合并失败：" + e.getMessage());
        }
    }

    /**
     * @Description 修改试卷答题卡pdf
     * @Param [message]
     * @Return void
     * @Author wn
     * @Date 2022/8/2 14:00
     **/
    @RabbitHandler
    @RabbitListener(queues = "${rabbitmq.examBaseToPdfHandleQueue}")
    public void baseToPdfHandleQueue(Message message) {
        try {
            String msg = new String(message.getBody(), "UTF-8");
            System.out.println(LocalDateTime.now() + "答题卡图片合并接收消息内容：" + msg);
            EPaper paper = JSON.parseObject(msg, EPaper.class);
            ePaperMapper.updatePaperScantronpdf(paper);
        } catch (Exception e) {
            log.error("答题卡图片合并失败：" + e.getMessage());
        }
    }

    /**
     * @Description 扫码出错答题卡base64转图片
     * @Param [message]
     * @Return void
     * @Author wn
     * @Date 2022/9/19 9:46
     **/
    @RabbitHandler
    @RabbitListener(queues = "${rabbitmq.examBaseToScanImgHandleQueue}")
    public void baseToScanImgHandleQueue(Message message) {
        try {
            String msg = new String(message.getBody(), "UTF-8");
            System.out.println(LocalDateTime.now() + "扫码出错答题卡base64转图片接收消息内容：" + msg);
            EScanerror eScanerror = JSON.parseObject(msg, EScanerror.class);
            if (eScanerror.getConverted() != null && eScanerror.getConverted() == 1) {
                eScanerrorMapper.updateErrorStupic(eScanerror.getSerrorid(), eScanerror.getStupic());
            }
        } catch (Exception e) {
            log.error("扫码出错答题卡base64转图片失败：" + e.getMessage());
        }
    }

    /**
     * @Description 考试附件试卷图片转pdf
     * @Param [message]
     * @Return void
     * @Author wn
     * @Date 2022/11/1 10:45
     **/
    @RabbitHandler
    @RabbitListener(queues = "${rabbitmq.examImgToPdfHandleQueue}")
    public void examImageToPdfHandleQueue(Message message) {
        try {
            String msg = new String(message.getBody(), "UTF-8");
            System.out.println(LocalDateTime.now() + "考试附件试卷图片转pdf接收消息内容：" + msg);
            EPaperFile paperFile = JSON.parseObject(msg, EPaperFile.class);
            if (paperFile.getConverted() != null && paperFile.getConverted() == 1) {
                ePaperFileMapper.updateSourcepathByEpid(paperFile.getEpid(),paperFile.getSourcepath());
            }
        } catch (Exception e) {
            log.error("考试附件试卷图片转pdf失败：" + e.getMessage());
        }
    }

    @RabbitHandler
    @RabbitListener(queues = "${rabbitmq.examHtmlToPdfHandleQueue}")
    public void examHtmlToPdfHandleQueue(Message message) {
        try {
            String msg = new String(message.getBody(), "UTF-8");
            System.out.println(LocalDateTime.now() + "考试报告html转pdf接收消息内容：" + msg);
            EReportVo report = JSON.parseObject(msg, EReportVo.class);
            if (report.getConverted() != null && report.getConverted() != 1) return;
            eReportGenerateMapper.updatePdf(report);//修改考试pdf地址
            Integer examid = report.getExamid();
            Integer type = report.getType();
            if (3 == type) {
                Integer classid = report.getClassid();
                Integer num = eReportGenerateMapper.getNoPdfNumByExamAndClassId(examid, classid);//考试班级未生成pdf的数量
                if (0 == num) {
                    Map map = new HashMap();
                    String savefolder = eReportGenerateMapper.getSaveFolder(examid);
                    map.put("savefolder", savefolder + "/" +  + classid);
                    map.put("examid", examid);
                    map.put("classid", classid);
                    map.put("type", 4);
                    convertUtil.htmlToPdf(map);
                }
            } else if (4 == type) {
                Integer num = eReportGenerateMapper.getNoPdfNumByExamId(examid);//考试未生成pdf的数量
                if (0 == num) {
                    eBaseMapper.updateExamReportState(examid, 2);
                }
            }
        } catch (Exception e) {
            log.error("考试报告html转pdf失败：" + e.getMessage());
        }
    }
}
