package com.education.business.message.mq;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.education.business.message.param.ExamMessage;
import com.education.business.service.education.ExamInfoService;
import com.education.business.service.education.StudentQuestionAnswerService;
import com.education.business.service.education.StudentWrongBookService;
import com.education.business.service.education.TestPaperInfoService;
import com.education.business.service.system.SystemMessageLogService;
import com.education.common.constants.SystemConstants;
import com.education.model.entity.ExamInfo;
import com.education.model.entity.MessageLog;
import com.education.model.entity.StudentQuestionAnswer;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
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.util.List;


/**
 * @author zjt
 * @create_at 2022年11月21日 0021 17:20
 * @since 1.0.6
 */
@Component
public class ExamMqMessageListener extends AbstractMqMessageListener<ExamMessage> {

    @Resource
    private ExamInfoService examInfoService;
    @Resource
    private StudentWrongBookService studentWrongBookService;
    @Resource
    private StudentQuestionAnswerService studentQuestionAnswerService;
    @Resource
    private TestPaperInfoService testPaperInfoService;

    @Override
//    @RabbitListener(queues = "${rabbitmq.exam.queue}")
    public void onMessage(Message message, Channel channel) {
        super.onMessage(message, channel);
    }

    @Override
    void doBizMessage(ExamMessage examMessage) {
        // 保存考试记录
        ExamInfo examInfo = examMessage.getExamInfo();
        examInfoService.save(examInfo);
        // 批量保存学员错题
        if (examMessage.getStudentWrongBookList() != null) {
            studentWrongBookService.saveBatch(examMessage.getStudentWrongBookList());
        }
        // 保存学员答题记录
        List<StudentQuestionAnswer> studentQuestionAnswerList = examMessage.getStudentQuestionAnswerList();
        studentQuestionAnswerList.stream().forEach(item -> item.setExamInfoId(examInfo.getId()));

        studentQuestionAnswerService.saveBatch(studentQuestionAnswerList);
        // 更新试卷参考人数
        testPaperInfoService.updateExamNumber(examInfo.getTestPaperInfoId());
    }
}
