package backend.service.impl;

import backend.dao.impl.QuestionsDao;
import backend.model.Questions;
import backend.model.enums.GlobalData;

import java.util.List;
import java.util.Vector;

public class QuestionsService {
    private QuestionsDao questionsDao = new QuestionsDao();
    private Vector<Vector<String>> commonHandler(List<Questions> questionsList){
        //加工数据 把数据库查到的List转成Vector<Vector<>>
        Vector<Vector<String>> questionsData = new Vector<>();
        if(questionsList != null) {
            for (Questions questionsPo : questionsList) {
                Vector<String> row = new Vector<>();
                row.add(String.valueOf(questionsPo.getId()));
                row.add(questionsPo.getName());
                row.add(questionsPo.getDescription());
                row.add(questionsPo.getAnswer());
                row.add(GlobalData.FREQUENCY_MAP.get(questionsPo.getFrequency()));
                questionsData.add(row);
            }
        }
        return questionsData;
    }
    public Vector<Vector<String>> getAll(){
        //从数据库中获取所有单词
        List<Questions> questionsList = questionsDao.getAll();
        //把List转成表格所需的格式
        return commonHandler(questionsList);
        //返回给前端
    }

    public Vector<Vector<String>> getByCondition(Integer frequency,String key){
        //从数据库中获取所有单词
        List<Questions> wordPoList = questionsDao.getByCondition(frequency,key);
        //把List转成表格所需的格式
        return commonHandler(wordPoList);
    }
    public boolean insert(Questions question){
        return questionsDao.insert(question);
    }
    public void delete(Integer id){
        questionsDao.delete(id);
    }
}
