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.ruoyi.test.domain.Errorbook;
import com.ruoyi.test.domain.Vo.ErrorbookVo;
import com.ruoyi.test.mapper.ErrorbookMapper;
import com.ruoyi.test.seriver.IErrorbookService;
import com.ruoyi.test.seriver.IQuestionTypeService;
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 ErrorbookServiceImpl extends ServiceImpl<ErrorbookMapper, Errorbook> implements IErrorbookService {

    @Autowired
    private ErrorbookMapper errorbookMapper;

    @Autowired
    private IQuestionTypeService iQuestionTypeService;

    @Override
    public int addErrorBook(Errorbook errorbook) {
        return errorbookMapper.addErrorBook(errorbook);
    }

    @Override
    public IPage<ErrorbookVo> selectErrorBook(int pagenum, String sender) {
        LambdaQueryWrapper<Errorbook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Errorbook::getSender,sender);
        List<Errorbook> list = errorbookMapper.selectList(queryWrapper);
        List<ErrorbookVo> list1 = new ArrayList<>();
        for(Errorbook errorbook : list){
            ErrorbookVo errorbookVo = new ErrorbookVo();
            errorbookVo.setSender(sender);
            errorbookVo.setContent(errorbook.getContent());
            errorbookVo.setDifficulty(errorbook.getDifficulty());
            errorbookVo.setAnswer(errorbook.getAnswer());
            String chance = errorbook.getChance();
            System.out.println(chance);
            Gson gson = new Gson();
            List<Map<String,String>> listMap = gson.fromJson(chance,List.class);
            errorbookVo.setChance(listMap);
            errorbookVo.setAnalysis(errorbook.getAnalysis());
            errorbookVo.setSubject(errorbook.getSubject());
            Integer type = errorbook.getType();
            String type1 = iQuestionTypeService.getQuestiontype(type);
            errorbookVo.setType(type1);
            errorbookVo.setScore(errorbook.getScore());
            list1.add(errorbookVo);
        }
        Page<ErrorbookVo> page = new Page<>(pagenum,10);
        IPage<ErrorbookVo> iPage = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        iPage.setRecords(list1);
        return iPage;
    }

    @Override
    public IPage<ErrorbookVo> selectErrorBookBySubject(int pagenum, String sender, String subject) {
        LambdaQueryWrapper<Errorbook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Errorbook::getSender,sender)
                .eq(Errorbook::getSubject,subject);
        List<Errorbook> list = errorbookMapper.selectList(queryWrapper);
        List<ErrorbookVo> list1 = new ArrayList<>();
        for(Errorbook errorbook : list){
            ErrorbookVo errorbookVo = new ErrorbookVo();
            errorbookVo.setSender(sender);
            errorbookVo.setContent(errorbook.getContent());
            errorbookVo.setDifficulty(errorbook.getDifficulty());
            errorbookVo.setAnswer(errorbook.getAnswer());
            String chance = errorbook.getChance();
            Gson gson = new Gson();
            List<Map<String,String>> listMap = gson.fromJson(chance,List.class);
            errorbookVo.setChance(listMap);
            errorbookVo.setAnalysis(errorbook.getAnalysis());
            errorbookVo.setSubject(errorbook.getSubject());
            Integer type = errorbook.getType();
            String type1 = iQuestionTypeService.getQuestiontype(type);
            errorbookVo.setType(type1);
            errorbookVo.setScore(errorbook.getScore());
            list1.add(errorbookVo);
        }
        Page<ErrorbookVo> page = new Page<>(pagenum,10);
        IPage<ErrorbookVo> iPage = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        iPage.setRecords(list1);
        return iPage;
    }
}
