package com.xhkjedu.smarking.listener;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.smarking.mapper.paper.MsPaperFileMapper;
import com.xhkjedu.smarking.mapper.stupaper.MsPaperStudentQuestionMapper;
import com.xhkjedu.smarking.model.paper.MsPaperFile;
import com.xhkjedu.smarking.model.stupaper.MsPaperStudentQuestion;
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.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 消息到达消费者监听类
 */
@Component
@Slf4j
public class MessageConsumer {
    @Resource
    private MsPaperStudentQuestionMapper msPaperStudentQuestionMapper;
    @Resource
    private MsPaperFileMapper msPaperFileMapper;


    /*
     * @Description 学生试题答案图片合并完处理转换状态
     * @Date 2024/10/29 14:43:39
     * @Author WN
     * @Param [message]
     * @Return void
     **/
    @RabbitHandler
    @RabbitListener(queues = "${rabbitmq.markingImgMergeHandleQueue}")
    public void imgMergeHandleQueue(Message message) {
        try {
            String msg = new String(message.getBody(), "UTF-8");
            System.out.println(LocalDateTime.now() + "学生试题答案合并接收消息内容：" + msg);
            MsPaperStudentQuestion psq = JSON.parseObject(msg, MsPaperStudentQuestion.class);
            if (psq.getConverted() != 2) {//合并失败不修改学生答案：是否转换0默认1待转换2转换成功3转换失败
                psq.setStuanswer(null);
            } else {
                psq.setStuanswer(JSON.toJSONString(new String[]{psq.getStuanswer()}));
            }
            msPaperStudentQuestionMapper.updateByPrimaryKeySelective(psq);
        } catch (Exception e) {
            log.error("学生试题答案合并失败：" + e.getMessage());
        }
    }

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

    @RabbitHandler
    @RabbitListener(queues = "${rabbitmq.markingHtmlToPdfHandleQueue}")
    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());
        // }
    }
}
