package com.ynfy.buss.exam.paperquestion.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.paperquestion.entity.PaperQuestion;
import com.ynfy.buss.exam.paperquestion.mapper.PaperQuestionMapper;
import com.ynfy.buss.exam.paperquestion.service.IPaperQuestionService;
import com.ynfy.buss.exam.paperquestionanswer.service.IPaperQuestionAnswerService;
import com.ynfy.buss.exam.paperrulegroup.entity.PaperRuleGroup;
import com.ynfy.buss.exam.question.entity.Question;
import com.ynfy.buss.exam.question.enums.QuestionType;
import com.ynfy.buss.exam.userexamquestion.service.IUserExamQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: paper_question
 * @Author: jeecg-boot
 * @Date: 2023-02-23
 * @Version: V1.0
 */
@Service
public class PaperQuestionServiceImpl extends ServiceImpl<PaperQuestionMapper, PaperQuestion> implements IPaperQuestionService {

    @Autowired
    private IUserExamQuestionService userExamQuestionService;

    @Autowired
    private PaperQuestionMapper paperQuestionMapper;

    @Autowired
    private IPaperQuestionAnswerService paperQuestionAnswerService;

    /**
     * 删除已保存
     */
    @Override
    public void remove(String paperId) {
        QueryWrapper<PaperQuestion> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(PaperQuestion::getPaperId, paperId);
        this.remove(wrapper);
    }

    /**
     * 试卷试题排序并保存
     *
     * @param paperQuestions
     */
    @Override
    public List<PaperQuestion> sort(List<PaperQuestion> paperQuestions) {
        if (!CollectionUtils.isEmpty(paperQuestions)) {
            //按题型排序并编号
            paperQuestions = paperQuestions.stream().sorted(Comparator.comparing(PaperQuestion::getQuestionType)).collect(Collectors.toList());
            int sort = 1;
            for (PaperQuestion pq : paperQuestions) {
                pq.setSort(sort);
                sort++;
            }
        }
        return paperQuestions;
    }

    @Override
    public void saveAll(String paperId, List<PaperRuleGroup> groupList) {
        if (!CollectionUtils.isEmpty(groupList)) {
            groupList.stream().forEach(g -> {
                List<PaperQuestion> paperQuestionList = new ArrayList<>();
                List<Question> questionList = g.getQuestionList();
                int sort = 1;
                if (!CollectionUtils.isEmpty(questionList)) {
                    for (Question q : questionList) {
                        //保存漏选选项分值
                        if (QuestionType.MULTI.getCode().equals(q.getType()) && !Objects.isNull(g.getCanMissOption()) && g.getCanMissOption()) {
                            paperQuestionAnswerService.save(paperId, q);
                        } else if (QuestionType.BLANK.getCode().equals(q.getType()) && !Objects.isNull(g.getCanBlankOption()) && g.getCanBlankOption()) {
                            paperQuestionAnswerService.save(paperId, q);
                        }
                        PaperQuestion paperQuestion = new PaperQuestion();
                        paperQuestion.setGroupId(g.getId());
                        paperQuestion.setPaperId(paperId);
                        paperQuestion.setQuestionId(q.getId());
                        paperQuestion.setQuestionType(q.getType());
                        paperQuestion.setQuestionScore(q.getScore());
                        paperQuestion.setSort(sort);
                        paperQuestionList.add(paperQuestion);
                        sort++;
                    }
                }
                saveBatch(paperQuestionList);
            });
        }
    }

    @Override
    public List<PaperQuestion> listByPaperId(String paperId) {
        return paperQuestionMapper.listByPaperId(paperId);
    }

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

    /**
     * 检查题目是否已经被试卷使用
     *
     * @param questionIds
     * @return
     */
    @Override
    public boolean checkIsUsedInPaper(String[] questionIds) {
        LambdaQueryWrapper<PaperQuestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PaperQuestion::getQuestionId, questionIds);
        return this.count(wrapper) > 0 ? true : false;
    }


    /**
     * 检查题目是否已经被使用
     *
     * @param questionIds
     * @return
     */
    @Override
    public boolean checkIsUsed(String[] questionIds) {
        return checkIsUsedInPaper(questionIds) || userExamQuestionService.checkIsUsedInExam(questionIds);
    }
}
