package com.yanzhu.exam.questions.service.impl;

import java.util.List;
import java.util.UUID;

import com.yanzhu.exam.questions.domain.ExamQuestionsAnswer;
import com.yanzhu.exam.questions.domain.ExamQuestionsContent;
import com.yanzhu.exam.questions.domain.ExamQuestionsProperty;
import com.yanzhu.exam.questions.mapper.ExamQuestionsAnswerMapper;
import com.yanzhu.exam.questions.mapper.ExamQuestionsContentMapper;
import com.yanzhu.exam.questions.mapper.ExamQuestionsPropertyMapper;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.exam.questions.mapper.ExamQuestionsMapper;
import com.yanzhu.exam.questions.domain.ExamQuestions;
import com.yanzhu.exam.questions.service.IExamQuestionsService;

/**
 * 考试题目Service业务层处理
 * 
 * @author qnsdt
 * @date 2022-12-28
 */
@Service
public class ExamQuestionsServiceImpl implements IExamQuestionsService 
{
    @Autowired
    private ExamQuestionsMapper examQuestionsMapper;

    @Autowired
    private ExamQuestionsPropertyMapper examQuestionsPropertyMapper;

    @Autowired
    private ExamQuestionsContentMapper examQuestionsContentMapper;

    @Autowired
    private ExamQuestionsAnswerMapper examQuestionsAnswerMapper;
    /**
     * 查询考试题目
     * 
     * @param questionsCode 考试题目ID
     * @return 考试题目
     */
    @Override
    public ExamQuestions selectExamQuestionsById(String questionsCode)
    {
        return examQuestionsMapper.selectExamQuestionsById(questionsCode);
    }

    /**
     * 查询考试题目列表
     * 
     * @param examQuestions 考试题目
     * @return 考试题目
     */
    @Override
    public List<ExamQuestions> selectExamQuestionsList(ExamQuestions examQuestions)
    {
        return examQuestionsMapper.selectExamQuestionsList(examQuestions);
    }

    /**
     * 新增考试题目
     * 
     * @param examQuestions 考试题目
     * @return 结果
     */
    @Override
    public int insertExamQuestions(ExamQuestions examQuestions)
    {
        examQuestions.setCreateTime(DateUtils.getNowDate());
        return examQuestionsMapper.insertExamQuestions(examQuestions);
    }

    /**
     * 修改考试题目
     * 
     * @param examQuestions 考试题目
     * @return 结果
     */
    @Override
    public int updateExamQuestions(ExamQuestions examQuestions)
    {
        return examQuestionsMapper.updateExamQuestions(examQuestions);
    }

    /**
     * 批量删除考试题目
     * 
     * @param questionsCodes 需要删除的考试题目ID
     * @return 结果
     */
    @Override
    public int deleteExamQuestionsByIds(String[] questionsCodes)
    {
        return examQuestionsMapper.deleteExamQuestionsByIds(questionsCodes);
    }

    /**
     * 删除考试题目信息
     * 
     * @param questionsCode 考试题目ID
     * @return 结果
     */
    @Override
    public int deleteExamQuestionsById(String questionsCode)
    {
        return examQuestionsMapper.deleteExamQuestionsById(questionsCode);
    }

    /**
     * 根据题目标题查询考试题目
     * 
     * @param questionsTitle 题目标题
     * @return 考试题目
     */
    @Override
    public ExamQuestions selectExamQuestionsByTitle(String questionsTitle)
    {
        return examQuestionsMapper.selectExamQuestionsByTitle(questionsTitle);
    }

    /**
     * 导入考试题目数据
     * 
     * @param examQuestionsList 考试题目数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importExamQuestions(List<ExamQuestions> examQuestionsList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(examQuestionsList) || examQuestionsList.size() == 0) {
            throw new CustomException("导入考试题目数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ExamQuestions examQuestions : examQuestionsList) {

            try {
                ExamQuestionsProperty examProperty = new ExamQuestionsProperty();
                examProperty.setBankCode(examQuestions.getBankCode());
                examProperty.setQuestionsCode(examQuestions.getQuestionsCode());
                examProperty.setQuestionsTitle(examQuestions.getQuestionsTitle());
                examProperty.setQuestionsScore(examQuestions.getQuestionsScore());
                examProperty.setQuestionsType(examQuestions.getQuestionsType());
                examProperty.setRateNumber(examQuestions.getRateNumber());
                examProperty.setAnswerAnalyse(examQuestions.getAnswerAnalyse());
                examProperty.setRightAnswer(examQuestions.getRightAnswer());
                examProperty.setStatus(0);
                examProperty.setCreateTime(DateUtils.getNowDate());
                examProperty.setCreateBy(examQuestions.getCreateBy());
                examQuestionsPropertyMapper.insertExamQuestionsProperty(examProperty);
                ExamQuestionsContent examContent = new ExamQuestionsContent(
                        examQuestions.getQuestionsCode(), examQuestions.getQuestionsContent()
                );
                examQuestionsContentMapper.insertExamQuestionsContent(examContent);

                ExamQuestionsAnswer answer = new ExamQuestionsAnswer();
                answer.setQuestionsCode(examQuestions.getQuestionsCode());
                int idx = -1;
                String opt = "ABCDEFGHIJKLMN";

                String code = "";
                String rightAnswer = examQuestions.getRightAnswer();
                if (StringUtils.isEmpty(rightAnswer)) {
                    rightAnswer = "";
                }
                String[] tmps = new String[]{
                        examQuestions.getAnswerA(),
                        examQuestions.getAnswerB(),
                        examQuestions.getAnswerC(),
                        examQuestions.getAnswerD(),
                        examQuestions.getAnswerE(),
                        examQuestions.getAnswerF(),
                        examQuestions.getAnswerG(),
                        examQuestions.getAnswerH()
                };
                for (String tmp : tmps) {
                    if (StringUtils.isNotEmpty(tmp)) {
                        idx++;
                        code = "" + opt.charAt(idx);
                        answer.setOptionCode(code);
                        answer.setOptionDescribe(tmp);
                        answer.setIsRight(rightAnswer.contains(code) ? 0 : 1);
                        examQuestionsAnswerMapper.insertExamQuestionsAnswer(answer);
                    }
                }
                successNum++;
            }catch (Exception e){
                failureNum++;
            }

        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
