package ink.wgink.module.examine.service.question.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.module.examine.dao.question.IQuestionTypeQuestionDao;
import ink.wgink.module.examine.pojo.dtos.question.QuestionTypeQuestionDTO;
import ink.wgink.module.examine.service.question.IQuestionTypeQuestionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 试题类型与试题
 * @Author: WenG
 * @Date: 2022/5/15 17:29
 * @Version: 1.0
 **/
@Service
public class QuestionTypeQuestionServiceImpl extends DefaultBaseService implements IQuestionTypeQuestionService {

    @Autowired
    private IQuestionTypeQuestionDao questionTypeQuestionDao;

    @Override
    public void save(String questionId, String questionTypeId) {
        Map<String, Object> params = getHashMap(4);
        params.put("questionTypeId", questionTypeId);
        params.put("questionId", questionId);
        questionTypeQuestionDao.save(params);
    }

    @Override
    public void save(String questionId, List<String> questionTypeIds) {
        if (questionTypeIds.isEmpty()) {
            return;
        }
        // 删除已有类型
        deleteByQuestionId(questionId);
        for (String questionTypeId : questionTypeIds) {
            if(StringUtils.isBlank(questionTypeId)) {
                continue;
            }
            save(questionId, questionTypeId);
        }
    }

    @Override
    public void deleteByQuestionId(String questionId) {
        Map<String, Object> params = getHashMap(2);
        params.put("questionIds", Arrays.asList(questionId));
        questionTypeQuestionDao.delete(params);
    }

    @Override
    public void deleteByQuestionIds(List<String> questionIds) {
        Map<String, Object> params = getHashMap(2);
        params.put("questionIds", questionIds);
        questionTypeQuestionDao.delete(params);
    }

    @Override
    public void deleteByQuestionTypeId(String questionTypeId) {
        Map<String, Object> params = getHashMap(2);
        params.put("questionTypeIds", Arrays.asList(questionTypeId));
        questionTypeQuestionDao.delete(params);
    }

    @Override
    public void deleteByQuestionTypeIds(List<String> questionTypeIds) {
        Map<String, Object> params = getHashMap(2);
        params.put("questionTypeIds", questionTypeIds);
        questionTypeQuestionDao.delete(params);
    }

    @Override
    public List<String> listQuestionTypeIdByQuestionId(String questionId) {
        Map<String, Object> params = getHashMap(2);
        params.put("questionId", questionId);
        return questionTypeQuestionDao.listQuestionTypeId(params);
    }

    @Override
    public List<String> listQuestionTypeIdByQuestionIds(List<String> questionIds) {
        Map<String, Object> params = getHashMap(2);
        params.put("questionIds", questionIds);
        return questionTypeQuestionDao.listQuestionTypeId(params);
    }

    @Override
    public List<QuestionTypeQuestionDTO> listByQuestionId(String questionId) {
        Map<String, Object> params = getHashMap(2);
        params.put("questionId", questionId);
        return questionTypeQuestionDao.list(params);
    }

    @Override
    public List<QuestionTypeQuestionDTO> listByQuestionIds(List<String> questionIds) {
        Map<String, Object> params = getHashMap(2);
        params.put("questionIds", questionIds);
        return questionTypeQuestionDao.list(params);
    }

}
