package com.zd.exam.service.impl;

import java.util.List;

import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.StringUtils;
import com.zd.exam.domain.ElPaperQu;
import com.zd.exam.domain.ElPaperQuAnswer;
import com.zd.exam.domain.ElQu;
import com.zd.exam.mapper.ElPaperQuAnswerMapper;
import com.zd.exam.service.ElQuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import org.springframework.transaction.annotation.Transactional;
import com.zd.exam.mapper.ElPaperQuMapper;
import com.zd.exam.service.IElPaperQuService;
import org.springframework.util.CollectionUtils;

/**
 * 试卷考题Service业务层处理
 *
 * @author zd
 * @date 2021-08-31
 */
@Service
public class ElPaperQuServiceImpl implements IElPaperQuService {
    @Autowired
    private ElPaperQuMapper elPaperQuMapper;
    @Autowired
    private ElQuService elQuService;
    @Autowired
    private ElPaperQuAnswerMapper elPaperQuAnswerMapper;

    /**
     * 查询试卷考题
     *
     * @param id 试卷考题主键
     * @return 试卷考题
     */
    @Override
    public ElPaperQu selectElPaperQuById(Long id) {
        return elPaperQuMapper.selectElPaperQuById(id);
    }

    /**
     * 查询试卷考题列表
     *
     * @param elPaperQu 试卷考题
     * @return 试卷考题
     */
    @Override
    public List<ElPaperQu> selectElPaperQuList(ElPaperQu elPaperQu) {
        return elPaperQuMapper.selectElPaperQuList(elPaperQu);
    }

    /**
     * 新增试卷考题
     *
     * @param elPaperQu 试卷考题
     * @return 结果
     */
    @Transactional
    @Override
    public int insertElPaperQu(ElPaperQu elPaperQu) {
        elPaperQu.setCreateTime(DateUtils.getNowDate());
        int rows = elPaperQuMapper.insertElPaperQu(elPaperQu);
        insertElPaperQuAnswer(elPaperQu);
        return rows;
    }

    /**
     * 修改试卷考题
     *
     * @param elPaperQu 试卷考题
     * @return 结果
     */
    @Transactional
    @Override
    public int updateElPaperQu(ElPaperQu elPaperQu) {
        elPaperQu.setUpdateTime(DateUtils.getNowDate());
        elPaperQuMapper.deleteElPaperQuAnswerByQuId(elPaperQu.getId());
        insertElPaperQuAnswer(elPaperQu);
        return elPaperQuMapper.updateElPaperQu(elPaperQu);
    }

    /**
     * 批量删除试卷考题
     *
     * @param ids 需要删除的试卷考题主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteElPaperQuByIds(Long[] ids) {
        elPaperQuMapper.deleteElPaperQuAnswerByQuIds(ids);
        return elPaperQuMapper.deleteElPaperQuByIds(ids);
    }

    /**
     * 删除试卷考题信息
     *
     * @param id 试卷考题主键
     * @return 结果
     */
    @Override
    public int deleteElPaperQuById(Long id) {
        elPaperQuMapper.deleteElPaperQuAnswerByQuId(id);
        return elPaperQuMapper.deleteElPaperQuById(id);
    }

    /**
     * 答题过程根据 paperId 和 quId 查询问题和选项
     */
    @Override
    public ElPaperQu queryQuDetail(ElPaperQu paperQu) {
        // 问题
        ElQu qu = elQuService.queryById(paperQu.getQuId());

        // 基本信息
        List<ElPaperQu> paperQus = elPaperQuMapper.selectElPaperQuList(paperQu);
        // 同时根据 paperId 和 quId 查询应该只有一条数据 （即 一个试卷中同样的题只能加一个）
        if (paperQus.size() != 1) {
            throw new ServiceException("数据异常");
        }
        paperQu = paperQus.get(0);
        paperQu.setContent(qu.getContent());
        paperQu.setAnalysis(qu.getAnalysis());

        // 答案列表
        List<ElPaperQuAnswer> list = elPaperQuAnswerMapper.selectByPaperQuId(paperQu);
        paperQu.setElPaperQuAnswerList(list);

        return paperQu;
    }

    /**
     * 统计分数
     */
    @Override
    public int sumObjective(Long paperId) {
        return elPaperQuMapper.sumObjective(paperId);
    }

    /**
     * 填充答案 下一题实时保存上题答案
     */
    @Override
    public void fillAnswer(ElPaperQu paperQu) {
        List<ElPaperQuAnswer> elPaperQuAnswerList = paperQu.getElPaperQuAnswerList();
        if (CollectionUtils.isEmpty(elPaperQuAnswerList)
                && StringUtils.isBlank(paperQu.getAnswer())) {
            // 未作答
            return;
        }

        paperQu = elPaperQuMapper.selectOneByPaperQuId(paperQu);
        if (paperQu == null) {
            throw new ServiceException("参数无效！");
        }
        //查找答案列表
        List<ElPaperQuAnswer> list = elPaperQuAnswerMapper.selectByPaperQuId(paperQu);

        //是否正确
        boolean right = true;

        //更新正确答案
        for (ElPaperQuAnswer item : list) {

            // 保存答案，先初始化，防止多选题重复修改，选项不一致情况
            item.setChecked(0);
            for (ElPaperQuAnswer elPaperQuAnswer : elPaperQuAnswerList) {
                if (elPaperQuAnswer.getId().equals(item.getId())) {
                    item.setChecked(1);
                    break;
                }
            }

            //有一个对不上就是错的
            if (item.getIsRight() != null && !item.getIsRight().equals(item.getChecked())) {
                right = false;
            }
            elPaperQuAnswerMapper.updateElPaperQuAnswer(item);
        }

        //修改为已回答
        paperQu.setIsRight(right ? 1 : 0);
        // paperQu.setAnswer(paperQu.getAnswer());
        paperQu.setAnswered(1);

        elPaperQuMapper.updateElPaperQu(paperQu);
    }

    /**
     * 新增试卷考题备选答案信息
     *
     * @param elPaperQu 试卷考题对象
     */
    public void insertElPaperQuAnswer(ElPaperQu elPaperQu) {
        List<ElPaperQuAnswer> elPaperQuAnswerList = elPaperQu.getElPaperQuAnswerList();
        Long id = elPaperQu.getId();
        if (StringUtils.isNotNull(elPaperQuAnswerList)) {
            List<ElPaperQuAnswer> list = new ArrayList<ElPaperQuAnswer>();
            for (ElPaperQuAnswer elPaperQuAnswer : elPaperQuAnswerList) {
                elPaperQuAnswer.setQuId(id);
                list.add(elPaperQuAnswer);
            }
            if (list.size() > 0) {
                elPaperQuMapper.batchElPaperQuAnswer(list);
            }
        }
    }
}
