package com.xhkjedu.sstudy.listener;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.sstudy.mapper.paperstudent.PaperStudentQuestionMapper;
import com.xhkjedu.sstudy.mapper.paperstudent.PaperStudentScantronMapper;
import com.xhkjedu.sstudy.mapper.question.QuestionExplainMapper;
import com.xhkjedu.sstudy.model.paperstudent.TPaperStudentQuestion;
import com.xhkjedu.sstudy.model.paperstudent.TPaperStudentScantron;
import com.xhkjedu.sstudy.model.question.TQuestionExplain;
import com.xhkjedu.sstudy.model.resource.TResource;
import com.xhkjedu.sstudy.service.resource.ResourceService;
import com.xhkjedu.sstudy.vo.resource.ResourceVo;
import com.xhkjedu.utils.N_Utils;
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 ResourceService resourceService;
    @Resource
    private QuestionExplainMapper questionExplainMapper;
    @Resource
    private PaperStudentQuestionMapper paperStudentQuestionMapper;
    @Resource
    private PaperStudentScantronMapper paperStudentScantronMapper;


    //资源转换完处理资源文件
    @RabbitHandler
    @RabbitListener(queues = "${rabbitmq.resourceHandleQueue}")
    public void resourceHandleQueue(Message message) {
        try {
            String msg = new String(message.getBody(), "UTF-8");
            System.out.println(LocalDateTime.now() + " 资源处理接收消息内容：" + msg);
            TResource resource = JSON.parseObject(msg, TResource.class);
            String resourceid = resource.getResourceid();
            String resourceurl = resource.getResourceurl();
            String mp4code = resource.getMp4code();
            String source = resource.getSource();
            if ("explain".equals(source)) {
                TQuestionExplain explain = new TQuestionExplain();
                explain.setQcpid(N_Utils.str2Int(resourceid));
                explain.setFilepath(resourceurl);
                explain.setMp4code(mp4code);
                questionExplainMapper.updateMp4code(explain);
            } else {
                ResourceVo vo = resourceService.findById2(resourceid);
                //已转换的不在处理
                if (vo.getConverted() != 0) return;
                Integer resourceclass = resource.getResourceclass();
                if (resourceclass == 7) {//压缩包
                    resourceService.saveFiles(resourceid, resource.getFiles());
                }
                resource.setSource(null);
                resourceService.update(resource);
            }
        } catch (Exception e) {
            log.error("资源转换失败：" + e.getMessage());
        }
    }

    /**
     * 学生答案图片合并完处理转换状态
     *
     * @param [message]
     * @return void
     * @author ywx
     * @date 2022/5/9 13:44
     */
    @RabbitHandler
    @RabbitListener(queues = "${rabbitmq.imgMergeHandleQueue}")
    public void imgMergeHandleQueue(Message message) {
        try {
            String msg = new String(message.getBody(), "UTF-8");
            System.out.println(LocalDateTime.now() + " 学生答案合并接收消息内容：" + msg);
            TPaperStudentQuestion psq = JSON.parseObject(msg, TPaperStudentQuestion.class);
            Integer papertype = psq.getPapertype();
            if (papertype == 1 || papertype == 3) {//题库作业
                if (psq.getConverted() != 1) {//合并失败不修改学生答案
                    psq.setUseranswer(null);
                } else {
                    psq.setUseranswer(JSON.toJSONString(new String[]{psq.getUseranswer()}));
                }
                paperStudentQuestionMapper.updateByPrimaryKeySelective(psq);
            } else {//附件作业
                TPaperStudentScantron pss = new TPaperStudentScantron();
                pss.setPssid(psq.getPsqid());
                if (psq.getConverted() == 1) {//合并成功修改学生答案
                    pss.setUseranswer(JSON.toJSONString(new String[]{psq.getUseranswer()}));
                }
                pss.setConverted(psq.getConverted());
                paperStudentScantronMapper.updateByPrimaryKeySelective(pss);
            }
        } catch (Exception e) {
            log.error("学生答案合并失败：" + e.getMessage());
        }
    }
}
