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.QuestionbankDO;
import com.ruoyi.test.domain.ExamCreate;
import com.ruoyi.test.domain.ExamPaper;
import com.ruoyi.test.domain.Questionanswer;
import com.ruoyi.test.domain.Questionbank;
import com.ruoyi.test.mapper.ExamCreateMapper;
import com.ruoyi.test.mapper.QuestionbankMapper;
import com.ruoyi.test.seriver.IExamCreateService;
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 ExamCreateServiceImpl extends ServiceImpl<ExamCreateMapper, ExamCreate> implements IExamCreateService {

    @Autowired
    private ExamCreateMapper examCreateMapper;
    @Autowired
    private QuestionbankMapper questionbankMapper;

    @Override
    public List<Questionbank> selectByPid(Long pid) {
        LambdaQueryWrapper<ExamCreate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamCreate::getPid,pid);
        List<ExamCreate> list = examCreateMapper.selectList(queryWrapper);
        List<Questionbank> list1 = new ArrayList<>();
        for(ExamCreate examCreate : list){
            LambdaQueryWrapper<Questionbank> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Questionbank::getId,examCreate.getId());
            list1.add(questionbankMapper.selectOne(queryWrapper1));
        }
        return list1;
    }

    @Override
    public List<Questionbank> selectListByPid(Long pid) {
        LambdaQueryWrapper<ExamCreate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamCreate::getPid,pid);
        List<ExamCreate> list = examCreateMapper.selectList(queryWrapper);
        List<Questionbank> list1 = new ArrayList<>();
        for(ExamCreate examCreate : list){
            LambdaQueryWrapper<Questionbank> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Questionbank::getId,examCreate.getId());
            list1.add(questionbankMapper.selectOne(queryWrapper1));
        }
        return list1;
    }

    @Override
    public int addPaperCreate(Long id, Long pid) {
        return examCreateMapper.addPaperCreate(id,pid);
    }

    @Override
    public int deletePaperCreate(Long id, Long pid) {
        return examCreateMapper.deletePaperCreate(id,pid);
    }

    @Override
    public List<QuestionbankDO> selectPaperCreate(Long pid) {
        LambdaQueryWrapper<ExamCreate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamCreate::getPid,pid);
        List<ExamCreate> list = examCreateMapper.selectList(queryWrapper);
        List<QuestionbankDO> list1 = 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.setContent(questionbank.getContent());
            String chance = questionbank.getChance();
            List<Map<String,String>> listMap = gson.fromJson(chance,List.class);
            questionbankDO.setChance(listMap);
            list1.add(questionbankDO);
        }
        return list1;
    }

    @Override
    public Integer total(Long pid) {
        LambdaQueryWrapper<ExamCreate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamCreate::getPid,pid);
        return examCreateMapper.selectList(queryWrapper).size();
    }

//    @Override
//    public Integer total(List<ExamPaper> list) {
//        Integer total = 0;
//        for (ExamPaper examPaper : list){
//            LambdaQueryWrapper<ExamCreate> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(ExamCreate::getPid,examPaper.getId());
//            total += examCreateMapper.selectList(queryWrapper).size();
//        }
//        return total;
//    }
}
