package com.ruoyi.test.seriver.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.ruoyi.test.domain.DO.AnswerSheetDO;
import com.ruoyi.test.domain.DO.ExamCreateDO;
import com.ruoyi.test.domain.DO.QuestionbankDO;
import com.ruoyi.test.domain.ExamCreate;
import com.ruoyi.test.domain.Questionbank;
import com.ruoyi.test.domain.Vo.ExamCreateVo;
import com.ruoyi.test.domain.Vo.QuestionbankVo;
import com.ruoyi.test.mapper.*;
import com.ruoyi.test.seriver.IAnswerSheetDOService;
import com.ruoyi.test.seriver.IExamCreateService;
import com.ruoyi.test.seriver.IExamDOService;
import com.ruoyi.test.seriver.IExamPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AnswerSheetDOServiceImpl extends ServiceImpl<AnswerSheetDOMapper, AnswerSheetDO> implements IAnswerSheetDOService {

    @Autowired
    private AnswerSheetDOMapper answerSheetDOMapper;

    @Autowired
    private QuestionbankMapper questionbankMapper;

    @Autowired
    private ExamCreateMapper examCreateMapper;

    @Autowired
    private IExamPaperService iExamPaperService;

    @Autowired
    private IExamCreateService iExamCreateService;

    @Autowired
    private IExamDOService iExamDOService;

    @Override
    public AnswerSheetDO testPaper(String subject, String grade, String name) {
        Long pid = iExamPaperService.selectIdBySubjectAndGradeAndName(subject,grade,name);
        LambdaQueryWrapper<ExamCreate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamCreate::getPid,pid);
        List<ExamCreate> list = examCreateMapper.selectList(queryWrapper);
        List<QuestionbankDO> l = new ArrayList<>();
        Gson gson = new Gson();
        for(ExamCreate examCreate : list){
            LambdaQueryWrapper<Questionbank> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Questionbank::getId,examCreate.getId());
            Questionbank questionbank = questionbankMapper.selectOne(queryWrapper1);
            QuestionbankDO questionbankDO = new QuestionbankDO();
            questionbankDO.setId(questionbank.getId());
            questionbankDO.setContent(questionbank.getContent());
            String chance = questionbank.getChance();
            List<Map<String,String>> listMap = gson.fromJson(chance,List.class);
            questionbankDO.setChance(listMap);
            l.add(questionbankDO);
        }

        AnswerSheetDO answerSheetDO = new AnswerSheetDO();
        answerSheetDO.setGrade(grade);
        answerSheetDO.setSubject(subject);
        answerSheetDO.setName(name);
        answerSheetDO.setList(l);
        return answerSheetDO;
    }
}
