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

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynfy.buss.exam.exam.service.IExamService;
import com.ynfy.buss.exam.paperquestionanswer.entity.PaperQuestionAnswer;
import com.ynfy.buss.exam.paperquestionanswer.mapper.PaperQuestionAnswerMapper;
import com.ynfy.buss.exam.paperquestionanswer.service.IPaperQuestionAnswerService;
import com.ynfy.buss.exam.question.entity.Question;
import com.ynfy.buss.exam.question.enums.QuestionType;
import com.ynfy.buss.exam.questionanswer.entity.QuestionAnswer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * @Description: 试卷试题答案
 * @Author: jeecg-boot
 * @Date: 2023-04-05
 * @Version: V1.0
 */
@Service
public class PaperQuestionAnswerServiceImpl extends ServiceImpl<PaperQuestionAnswerMapper, PaperQuestionAnswer> implements IPaperQuestionAnswerService {

    @Lazy
    @Autowired
    private IExamService examService;

    @Override
    public List<PaperQuestionAnswer> listByPaperId(String paperId) {
        LambdaQueryWrapper<PaperQuestionAnswer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaperQuestionAnswer::getPaperId, paperId);
        return this.list(wrapper);
    }

    @Override
    public void remove(String paperId) {
        LambdaQueryWrapper<PaperQuestionAnswer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaperQuestionAnswer::getPaperId, paperId);
        this.remove(wrapper);
    }

    @Override
    public void save(String paperId, Question question, Integer groupQuestionType) {
        List<QuestionAnswer> answerList = question.getAnswerList();
        if (!CollectionUtils.isEmpty(answerList)) {
            List<PaperQuestionAnswer> paperQuestionAnswerList = new ArrayList<>();
            if (QuestionType.COMBINATION.getCode().equals(groupQuestionType)) {//组合题
                if (QuestionType.MULTI.getCode().equals(question.getType())) {
                    //找出正确答案
                    answerList = examService.findRightList(answerList);
                    double pathScore = NumberUtil.div(question.getScore().doubleValue(), answerList.size(), 2);
                    answerList.stream().forEach(answer ->
                            //生成试卷试题答案
                            paperQuestionAnswerList.add(generatePaperQuestionAnswer(paperId, question.getId(), answer.getId(), pathScore)));
                } else if (QuestionType.BLANK.getCode().equals(question.getType())) {
                    double pathScore = NumberUtil.div(question.getScore().doubleValue(), answerList.size(), 2);
                    answerList.stream().forEach(answer ->
                            paperQuestionAnswerList.add(generatePaperQuestionAnswer(paperId, question.getId(), answer.getId(), pathScore)));
                }

            } else {
                List<QuestionAnswer> pathScoreList = answerList.stream().filter(o ->
                        !Objects.isNull(o.getPathScore()) && o.getPathScore() > 0).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(pathScoreList)) {
                    pathScoreList.stream().forEach(ps ->
                            //生成试卷试题答案
                            paperQuestionAnswerList.add(generatePaperQuestionAnswer(paperId, question.getId(),
                                    ps.getId(), ps.getPathScore())));
                }
            }
            if (!CollectionUtils.isEmpty(paperQuestionAnswerList)) {
                saveBatch(paperQuestionAnswerList);
            }
        }
    }

    /**
     * 生成试卷试题答案
     *
     * @param paperId
     * @param question
     * @param answerId
     * @param pathScore
     * @return
     */
    public PaperQuestionAnswer generatePaperQuestionAnswer(String paperId, String questionId,
                                                           String answerId, Double pathScore) {
        PaperQuestionAnswer pqa = new PaperQuestionAnswer();
        pqa.setPaperId(paperId);
        pqa.setQuestionId(questionId);
        pqa.setAnswerId(answerId);
        pqa.setPathScore(pathScore);
        return pqa;
    }
}
