package com.ruoyi.school.paper.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.school.paper.domain.DbTestPaperRecordDetail;
import com.ruoyi.school.paper.domain.bo.DbTestPaperRecordBo;
import com.ruoyi.school.paper.domain.bo.TestPaperAnswerBo;
import com.ruoyi.school.paper.domain.bo.TestPaperAnswerItemBo;
import com.ruoyi.school.paper.domain.vo.DbTestPaperVo;
import com.ruoyi.school.paper.domain.vo.TestPaperVo;
import com.ruoyi.school.paper.domain.vo.TestQuestionsVo;
import com.ruoyi.school.paper.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>created in  2023/7/21 17:03
 *
 * @author WangMin
 * @version 1.0
 */
@Service
@RequiredArgsConstructor
public class TestPaperServiceImpl implements ITestPaperService {

    private final IDbTestPaperService testPaperService;

    private final IDbTestPaperRecordService paperRecordService;

    private final IDbQuestionBankTestPaperService bankTestPaperService;

    private final IDbTestPaperRecordDetailService paperRecordDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TestPaperVo generateTestPaper(Long userId, Long paperId) {
        TestPaperVo result = new TestPaperVo();
        // 获取到对应试卷信息
        DbTestPaperVo dbTestPaperVo = testPaperService.queryById(paperId);
        Objects.requireNonNull(dbTestPaperVo, "未检查到目标试卷");
        result.setTestName(dbTestPaperVo.getTestName());
        // 插入学生-试卷考试记录信息
        DbTestPaperRecordBo dbTestPaperRecord = new DbTestPaperRecordBo();
        dbTestPaperRecord.setTestPaperId(paperId);
        dbTestPaperRecord.setUserId(userId);
        dbTestPaperRecord.setStatus(0);
        boolean recordInsertFlag = paperRecordService.insertByBo(dbTestPaperRecord);
        if (!recordInsertFlag) {
            throw new ServiceException("考试记录生成失败");
        }
        result.setRecordId(dbTestPaperRecord.getId());
        // 查询试卷试题
        List<TestQuestionsVo> testPaperQuestion = bankTestPaperService.getTestPaperQuestion(paperId);
        result.setQuestions(testPaperQuestion);
        return result;
    }

    @Override
    public boolean paperTestEnd(TestPaperAnswerBo bo) {
        List<TestPaperAnswerItemBo> answers = bo.getAnswers();
        if (CollUtil.isEmpty(answers)) {
            return true;
        }
        List<DbTestPaperRecordDetail> insertPaperRecords = new ArrayList<>(answers.size());
        for (TestPaperAnswerItemBo answer : answers) {
            DbTestPaperRecordDetail buffer = new DbTestPaperRecordDetail();
            buffer.setRecordId(bo.getRecordId());
            buffer.setQuestionBankId(answer.getId());
            buffer.setAnswer(answer.getAnswer());
            insertPaperRecords.add(buffer);
        }
        if (CollUtil.isNotEmpty(insertPaperRecords)) {
            paperRecordDetailService.saveBatch(insertPaperRecords);
            return true;
        }
        return false;
    }
}
