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

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.SaveException;
import ink.wgink.module.examine.dao.paper.question.IPaperQuestionDao;
import ink.wgink.module.examine.enums.question.QuestionChoiceTypeEnum;
import ink.wgink.module.examine.enums.question.QuestionTypeEnum;
import ink.wgink.module.examine.pojo.dtos.paper.PaperDTO;
import ink.wgink.module.examine.pojo.dtos.paper.question.PaperQuestionDTO;
import ink.wgink.module.examine.pojo.dtos.question.QuestionDTO;
import ink.wgink.module.examine.pojo.vos.paper.question.PaperQuestionRandomSaveVO;
import ink.wgink.module.examine.service.paper.IPaperService;
import ink.wgink.module.examine.service.paper.question.IPaperQuestionService;
import ink.wgink.module.examine.service.question.IQuestionService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Description: 试卷试题
 * @Author: WenG
 * @Date: 2022/5/23 23:44
 * @Version: 1.0
 **/
@Service
public class PaperQuestionServiceImpl extends DefaultBaseService implements IPaperQuestionService {

    @Autowired
    private IPaperQuestionDao paperQuestionDao;
    @Autowired
    private IPaperService paperService;
    @Autowired
    private IQuestionService questionService;

    @Override
    public void save(String paperId, List<String> questionIds) {
        Map<String, Object> params = getHashMap(8);
        params.put("paperId", paperId);
        params.put("questionSort", "ZZZ-000");
        for (String questionId : questionIds) {
            params.put("paperQuestionId", UUIDUtil.getUUID());
            params.put("questionId", questionId);
            paperQuestionDao.save(params);
        }
    }

    @Override
    public void saveRandom(String paperId, PaperQuestionRandomSaveVO paperQuestionRandomSaveVO) {
        PaperDTO paperDTO = paperService.getById(paperId);
        QuestionTypeEnum questionTypeEnum = null;
        QuestionChoiceTypeEnum questionChoiceTypeEnum = null;
        int randomCount = 0;
        // 单选
        if (StringUtils.equals(paperQuestionRandomSaveVO.getQuestionType(), QuestionTypeEnum.CHOICE.getValue()) &&
                StringUtils.equals(paperQuestionRandomSaveVO.getQuestionChoiceType(), QuestionChoiceTypeEnum.SINGLE.getValue())) {
            questionTypeEnum = QuestionTypeEnum.CHOICE;
            questionChoiceTypeEnum = QuestionChoiceTypeEnum.SINGLE;
            randomCount = paperDTO.getChoiceSingleCount();
        }
        // 多选
        if (StringUtils.equals(paperQuestionRandomSaveVO.getQuestionType(), QuestionTypeEnum.CHOICE.getValue()) &&
                StringUtils.equals(paperQuestionRandomSaveVO.getQuestionChoiceType(), QuestionChoiceTypeEnum.MULTIPLE.getValue())) {
            questionTypeEnum = QuestionTypeEnum.CHOICE;
            questionChoiceTypeEnum = QuestionChoiceTypeEnum.MULTIPLE;
            randomCount = paperDTO.getChoiceMultipleCount();
        }
        // 判断
        if (StringUtils.equals(paperQuestionRandomSaveVO.getQuestionType(), QuestionTypeEnum.TURE_OR_FALSE.getValue())) {
            questionTypeEnum = QuestionTypeEnum.TURE_OR_FALSE;
            randomCount = paperDTO.getTureOrFalseCount();
        }
        // 填空
        if (StringUtils.equals(paperQuestionRandomSaveVO.getQuestionType(), QuestionTypeEnum.FILL_IN_THE_BLANKS.getValue())) {
            questionTypeEnum = QuestionTypeEnum.FILL_IN_THE_BLANKS;
            randomCount = paperDTO.getFillInTheBlanksCount();
        }
        // 解答
        if (StringUtils.equals(paperQuestionRandomSaveVO.getQuestionType(), QuestionTypeEnum.ANSWER.getValue())) {
            questionTypeEnum = QuestionTypeEnum.ANSWER;
            randomCount = paperDTO.getAnswerCount();
        }
        // 随机抽题
        List<String> randomQuestionIds = questionService.listRandomIdByRandomCountAndTypeAndChoiceType(randomCount, questionTypeEnum, questionChoiceTypeEnum);
        if (randomQuestionIds.isEmpty()) {
            throw new SaveException("没有试题可以随机");
        }

        // 删除对应的试题
        Map<String, Object> params = getHashMap(6);
        params.put("questionType", questionTypeEnum.getValue());
        params.put("questionChoiceType", questionChoiceTypeEnum == null ? null : questionChoiceTypeEnum.getValue());
        params.put("paperId", paperId);
        List<String> wantDeleteIds = paperQuestionDao.listId(params);

        if (!wantDeleteIds.isEmpty()) {
            // 删除
            params.clear();
            params.put("paperQuestionIds", wantDeleteIds);
            paperQuestionDao.delete(params);
        }

        // 保存试题
        save(paperId, randomQuestionIds);
    }

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

    @Override
    public List<PaperQuestionDTO> list(String paperId, Map<String, Object> params) {
        params = params == null ? getHashMap(0) : params;
        return paperQuestionDao.list(params);
    }

    @Override
    public SuccessResultList<List<PaperQuestionDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<PaperQuestionDTO> paperQuestionDTOS = paperQuestionDao.list(page.getParams());
        PageInfo<PaperQuestionDTO> pageInfo = new PageInfo<>(paperQuestionDTOS);
        return new SuccessResultList<>(paperQuestionDTOS, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public SuccessResultList<List<PaperQuestionDTO>> listPage(String paperId, ListPage page) {
        page.getParams().put("paperId", paperId);
        return listPage(page);
    }

    @Override
    public SuccessResultList<List<QuestionDTO>> listPageQuestionByExcludePaperId(String excludePaperId, ListPage page) {
        page.getParams().put("excludePaperId", excludePaperId);
        PageHelper.startPage(page.getPage(), page.getRows());
        List<QuestionDTO> questionDTOS = paperQuestionDao.listQuestion(page.getParams());
        PageInfo<QuestionDTO> pageInfo = new PageInfo<>(questionDTOS);
        return new SuccessResultList<>(questionDTOS, pageInfo.getPageNum(), pageInfo.getTotal());
    }
}
