package com.ruoyi.test.seriver.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ruoyi.test.domain.DO.ExamDO;
import com.ruoyi.test.domain.DO.SingleChoiceDO;
import com.ruoyi.test.domain.ExamCreate;
import com.ruoyi.test.domain.ExamPaper;
import com.ruoyi.test.domain.QuestionType;
import com.ruoyi.test.domain.Questionbank;
import com.ruoyi.test.domain.Vo.ExamCreateVo;
import com.ruoyi.test.domain.Vo.QuestionanswerVo;
import com.ruoyi.test.domain.Vo.QuestionbankVo;
import com.ruoyi.test.domain.Vo.QuestionbankVo1;
import com.ruoyi.test.mapper.*;
import com.ruoyi.test.seriver.IExamDOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ExamDOServiceImpl extends ServiceImpl<ExamDOMapper, ExamDO> implements IExamDOService {

    @Autowired
    private ExamDOMapper examDOMapper;

    @Autowired
    private QuestionTypeMapper questionTypeMapper;

    @Autowired
    private ExamPaperMapper examPaperMapper;

    @Autowired
    private ExamCreateMapper examCreateMapper;

    @Autowired
    private QuestionbankMapper questionbankMapper;


    public ExamPaper selectExamPaper(Long pid){
        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(ExamPaper::getId,ExamPaper::getSubject,ExamPaper::getGrade);
        queryWrapper.eq(ExamPaper::getId,pid);
        return examPaperMapper.selectOne(queryWrapper);
    }

    public QuestionType selectQuestionType(int questiontype){
        LambdaQueryWrapper<QuestionType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(QuestionType::getId,questiontype);
        return questionTypeMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<QuestionbankVo> selectExamDoList(int pagenum,int pagesize) {
        Page<Questionbank> page = new Page<>(pagenum,pagesize);
        LambdaQueryWrapper<Questionbank> queryWrapper = new LambdaQueryWrapper<>();
        IPage<Questionbank> iPage = questionbankMapper.selectPage(page,queryWrapper);
        List<QuestionbankVo> list = new ArrayList<>();
        List<Questionbank> questionbankList = page.getRecords();
        for(Questionbank questionbank : questionbankList){
            QuestionbankVo questionbankVo = new QuestionbankVo();
            questionbankVo.setId(questionbank.getId());
            questionbankVo.setQuestiontype(questionbank.getQuestiontype());
            questionbankVo.setContent(questionbank.getContent());
            String chance = questionbank.getChance();
            Gson gson = new Gson();
            // 将 JSON 字符串转换为 List<Map<String, String>>
            List<Map<String, String>> listMap = gson.fromJson(chance, List.class);
            questionbankVo.setChance(listMap);
            questionbankVo.setAnswer(questionbank.getAnswer());
            questionbankVo.setScore(questionbank.getScore());
            questionbankVo.setAnalysis(questionbank.getAnalysis());
            questionbankVo.setDifficulty(questionbank.getDifficulty());
            questionbankVo.setCreatetime(questionbank.getCreatetime());
            list.add(questionbankVo);
        }
        IPage<QuestionbankVo> pageVo = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        pageVo.setRecords(list);
        return pageVo;
    }

    @Override
    public QuestionbankVo1 preview(Long id) {
        LambdaQueryWrapper<Questionbank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Questionbank::getContent,Questionbank::getChance);
        queryWrapper.eq(Questionbank::getId,id);
        Questionbank questionbank = questionbankMapper.selectOne(queryWrapper);

        String chance = questionbank.getChance();
        Gson gson = new Gson();
        // 将 JSON 字符串转换为 List<Map<String, String>>
        List<Map<String, String>> listMap = gson.fromJson(chance, List.class);
        QuestionbankVo1 questionbankVo = new QuestionbankVo1();
        questionbankVo.setContent(questionbank.getContent());
        questionbankVo.setChance(listMap);
        questionbankVo.setId(id);
        return questionbankVo;
    }

    @Override
    public int addSingleChoice(Questionbank questionbank) {
        return questionbankMapper.addExamCreate(questionbank);
    }

    @Override
    public int addtfQuestions(Questionbank questionbank) {
        return questionbankMapper.addExamCreate(questionbank);
    }

    @Override
    public int addMSingleChoice(Questionbank questionbank) {
        return questionbankMapper.addExamCreate(questionbank);
    }

    @Override
    public int addGapFilling(Questionbank questionbank) {
        return questionbankMapper.addExamCreate(questionbank);
    }

    @Override
    public int addfrQuestions(Questionbank questionbank) {
        return questionbankMapper.addExamCreate(questionbank);
    }

}
