package com.ruoyi.test.seriver.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.test.domain.DO.QuestionbankDO;
import com.ruoyi.test.domain.DO.QuestionbankDO1;
import com.ruoyi.test.domain.Questionbank;
import com.ruoyi.test.mapper.QuestionbankMapper;
import com.ruoyi.test.seriver.IQuestionTypeService;
import com.ruoyi.test.seriver.IQuestionbankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class QuestionbankServiceImpl extends ServiceImpl<QuestionbankMapper, Questionbank> implements IQuestionbankService {

    @Autowired
    private IQuestionTypeService iQuestionTypeService;

    @Autowired
    private QuestionbankMapper questionbankMapper;

    @Override
    public QuestionbankDO1 selectQuestionBankById(Long id) {
        LambdaQueryWrapper<Questionbank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Questionbank::getId,Questionbank::getQuestiontype,Questionbank::getContent);
        queryWrapper.eq(Questionbank::getId,id);
        Questionbank questionbank = questionbankMapper.selectOne(queryWrapper);
        QuestionbankDO1 questionbankDO1 = new QuestionbankDO1();
        questionbankDO1.setId(questionbank.getId());
        questionbankDO1.setContent(questionbank.getContent());
        int questiontype = questionbank.getQuestiontype();
        questionbankDO1.setQuestiontype(iQuestionTypeService.getQuestiontype(questiontype));
        return questionbankDO1;
    }

    @Override
    public List<QuestionbankDO1> selectQuestionBankByQuestionType(String type) {
        Long id = iQuestionTypeService.getId(type);
        LambdaQueryWrapper<Questionbank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Questionbank::getId,Questionbank::getQuestiontype,Questionbank::getContent);
        queryWrapper.eq(Questionbank::getQuestiontype,id);
        List<Questionbank> list = questionbankMapper.selectList(queryWrapper);
        List<QuestionbankDO1> list1 = new ArrayList<>();
        for(Questionbank questionbank : list){
            QuestionbankDO1 questionbankDO1 = new QuestionbankDO1();
            questionbankDO1.setId(questionbank.getId());
            questionbankDO1.setQuestiontype(type);
            questionbankDO1.setContent(questionbank.getContent());
            list1.add(questionbankDO1);
        }
        return list1;
    }

    @Override
    public Questionbank selectById(Long id) {
        LambdaQueryWrapper<Questionbank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Questionbank::getId,id);
        return questionbankMapper.selectOne(queryWrapper);
    }

    @Override
    public List<QuestionbankDO1> selectQuestionBankByIdAndQuestionType(Long id, String type) {
        LambdaQueryWrapper<Questionbank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Questionbank::getId,Questionbank::getQuestiontype,Questionbank::getContent);
        if(type!=null){
            Long id1 = iQuestionTypeService.getId(type);
            queryWrapper.eq(Questionbank::getQuestiontype,id1);
        }
        if(id!=null){
            queryWrapper.eq(Questionbank::getId,id);
        }
        List<Questionbank> list = questionbankMapper.selectList(queryWrapper);
        if(list.size()==1 && type==null){
            type = iQuestionTypeService.getQuestiontype(list.get(0).getQuestiontype());
        }
        List<QuestionbankDO1> list1 = new ArrayList<>();
        for(Questionbank questionbank : list){
            QuestionbankDO1 questionbankDO1 = new QuestionbankDO1();
            questionbankDO1.setId(questionbank.getId());
            questionbankDO1.setQuestiontype(type);
            questionbankDO1.setContent(questionbank.getContent());
            list1.add(questionbankDO1);
        }
        return list1;
    }
}
