package com.ynfy.buss.exam.questionanswer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynfy.buss.exam.question.enums.QuestionType;
import com.ynfy.buss.exam.questionanswer.entity.QuestionAnswer;
import com.ynfy.buss.exam.questionanswer.mapper.QuestionAnswerMapper;
import com.ynfy.buss.exam.questionanswer.service.IQuestionAnswerService;
import com.ynfy.common.utils.CharUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * @Description: question_answer
 * @Author: jeecg-boot
 * @Date: 2023-02-22
 * @Version: V1.0
 */
@Service
public class QuestionAnswerServiceImpl extends ServiceImpl<QuestionAnswerMapper, QuestionAnswer> implements IQuestionAnswerService {


    /**
     * 查找已存在的列表
     *
     * @param quId
     * @return
     */
    public List<String> findExistsList(String quId) {
        //返回结果
        List<String> ids = new ArrayList<>();
        QueryWrapper<QuestionAnswer> wrapper = new QueryWrapper();
        wrapper.lambda().eq(QuestionAnswer::getQuestionId, quId);
        List<QuestionAnswer> list = this.list(wrapper);
        if (!CollectionUtils.isEmpty(list)) {
            for (QuestionAnswer item : list) {
                ids.add(item.getId());
            }
        }
        return ids;
    }

    /**
     * 保存试题选项
     *
     * @param questionId
     * @param list
     */
    @Override
    public void saveAll(String questionId, Integer questionType, List<QuestionAnswer> list) {
        remove(questionId);
        if (!CollectionUtils.isEmpty(list)) {
            int sort = 1;
            for (QuestionAnswer item : list) {
                item.setId(null);
                item.setQuestionId(questionId);
                //填空题不生成tag
                if (!QuestionType.BLANK.getCode().equals(questionType)) {
                    item.setTag(CharUtil.getZm(sort - 1));
                }
                item.setSort(sort);
                sort++;
            }
            saveBatch(list);
        }
    }

    /**
     * 删除
     *
     * @param questionId
     */
    @Override
    public void remove(String questionId) {
        QueryWrapper<QuestionAnswer> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(QuestionAnswer::getQuestionId, questionId);
        this.remove(wrapper);
    }


    /**
     * 查询试题选项
     *
     * @param questionId
     */
    @Override
    public List<QuestionAnswer> listAnswerByQuestionId(String questionId) {
        LambdaQueryWrapper<QuestionAnswer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QuestionAnswer::getQuestionId, questionId);
        wrapper.orderByAsc(QuestionAnswer::getSort);
        return this.list(wrapper);
    }

    @Override
    public void removeByQuestionIds(String[] questionIds) {
        QueryWrapper<QuestionAnswer> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(QuestionAnswer::getQuestionId, questionIds);
        this.remove(wrapper);
    }

    /**
     * 查询试题集合答案
     *
     * @param questionIds
     */
    @Override
    public List<QuestionAnswer> listAnswerByQuestionIds(List<String> questionIds) {
        LambdaQueryWrapper<QuestionAnswer> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(QuestionAnswer::getQuestionId, questionIds);
        return this.list(wrapper);
    }

}
