package com.zd.exam.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.security.utils.SaveUtil;

import com.zd.exam.domain.*;
import com.zd.exam.enums.QuType;
import com.zd.exam.mapper.ElCourseChapterQuAnswerMapper;
import com.zd.exam.service.ElQuAnswerService;
import com.zd.exam.service.ElQuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.exam.mapper.ElCourseChapterQuMapper;
import com.zd.exam.service.IElCourseChapterQuService;
import org.springframework.util.CollectionUtils;

/**
 * 章节考核自选考题Service业务层处理
 *
 * @author liubo
 * @date 2022-01-05
 */
@Service
public class ElCourseChapterQuServiceImpl implements IElCourseChapterQuService {
    @Autowired
    private ElCourseChapterQuMapper elCourseChapterQuMapper;
    @Autowired
    private ElCourseChapterQuAnswerMapper elCourseChapterQuAnswerMapper;
    @Autowired
    private ElQuService quService;
    @Autowired
    private ElQuAnswerService elQuAnswerService;

    /**
     * 查询章节考核自选考题
     *
     * @param id 章节考核自选考题主键
     * @return 章节考核自选考题
     */
    @Override
    public ElCourseChapterQu selectElCourseChapterQuById(Long id) {
        return elCourseChapterQuMapper.selectElCourseChapterQuById(id);
    }

    /**
     * 查询章节考核自选考题列表
     *
     * @param elCourseChapterQu 章节考核自选考题
     * @return 章节考核自选考题
     */
    @Override
    public List<ElCourseChapterQu> selectElCourseChapterQuList(ElCourseChapterQu elCourseChapterQu) {
        return elCourseChapterQuMapper.selectElCourseChapterQuList(elCourseChapterQu);
    }

    /**
     * 新增章节考核自选考题
     *
     * @param elCourseChapterQu 章节考核自选考题
     * @return 结果
     */
    @Override
    public int insertElCourseChapterQu(ElCourseChapterQu elCourseChapterQu) {
        //设置其他公共字段
        SaveUtil.setCommonAttr(elCourseChapterQu);
        return elCourseChapterQuMapper.insertElCourseChapterQu(elCourseChapterQu);
    }

    /**
     * 修改章节考核自选考题
     *
     * @param elCourseChapterQu 章节考核自选考题
     * @return 结果
     */
    @Override
    public int updateElCourseChapterQu(ElCourseChapterQu elCourseChapterQu) {
        elCourseChapterQu.setUpdateTime(DateUtils.getNowDate());
        elCourseChapterQu.setUpdateBy(SecurityUtils.getUsername());
        return elCourseChapterQuMapper.updateElCourseChapterQu(elCourseChapterQu);
    }

    /**
     * 批量删除章节考核自选考题
     *
     * @param ids 需要删除的章节考核自选考题主键
     * @return 结果
     */
    @Override
    public int deleteElCourseChapterQuByIds(Long[] ids) {
        return elCourseChapterQuMapper.deleteElCourseChapterQuByIds(ids);
    }

    /**
     * 删除章节考核自选考题信息
     *
     * @param id 章节考核自选考题主键
     * @return 结果
     */
    @Override
    public int deleteElCourseChapterQuById(Long id) {
        return elCourseChapterQuMapper.deleteElCourseChapterQuById(id);
    }

    @Override
    public void insertListByChapterId(long chapterId, List<ElCourseChapterQu> chapterQuList) {
        // 先删除

        elCourseChapterQuMapper.deleteByChapterId(chapterId);
        elCourseChapterQuAnswerMapper.deleteByChapterId(chapterId);

        List<ElCourseChapterQu> list = new ArrayList<>();

        int sort = 0;
        ElQu qu;
        ElCourseChapterQu courseChapterQu;
        for (ElCourseChapterQu item : chapterQuList) {

            qu = quService.queryById(item.getQuId());
            if(qu == null){
                throw new ServiceException("题目ID错误！");
            }

            courseChapterQu = new ElCourseChapterQu();

            courseChapterQu.setQuId(qu.getId());
            courseChapterQu.setAnswered(0);
            courseChapterQu.setIsRight(0);
            courseChapterQu.setCourseChapterId(chapterId);
            courseChapterQu.setSort(sort);
            courseChapterQu.setQuType(qu.getQuType());
            // 考核得分目前是根据答对数量校验
            courseChapterQu.setScore(-1);
            courseChapterQu.setActualScore(-1);


            list.add(courseChapterQu);
            sort++;
        }

        //添加问题
        elCourseChapterQuMapper.insertBatch(list);

        List<ElCourseChapterQuAnswer> batchAnswerList = new ArrayList<>();
        ElCourseChapterQuAnswer chapterQuAnswer;
        for (ElCourseChapterQu chapterQu : list) {
            //回答列表
            List<ElQuAnswer> answerList = elQuAnswerService.queryByQuId(chapterQu.getQuId());

            if (!CollectionUtils.isEmpty(answerList)) {

                int ii = 0;
                for (ElQuAnswer answer : answerList) {
                    chapterQuAnswer = new ElCourseChapterQuAnswer();
                    chapterQuAnswer.setCourseChapterId(chapterId);
                    chapterQuAnswer.setCourseChapterQuId(chapterQu.getId());

                    chapterQuAnswer.setQuId(answer.getQuId());
                    chapterQuAnswer.setAnswerId(answer.getId());
                    chapterQuAnswer.setChecked(0);
                    chapterQuAnswer.setSort(ii);
                    chapterQuAnswer.setAbc(QuType.ABC.get(ii));
                    chapterQuAnswer.setIsRight(answer.getIsRight());
                    ii++;
                    batchAnswerList.add(chapterQuAnswer);
                }
            }
        }

        //批量添加问题答案
        elCourseChapterQuAnswerMapper.addBatchQuAnswer(batchAnswerList);
    }

    @Override
    public List<ElCourseChapterQu> queryQuByChapterId(Long courseChapterId) {
        return elCourseChapterQuMapper.queryQuByChapterId(courseChapterId);
    }
}
