package com.zd.exam.service.impl;

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.mapper.ElPractiseQuAnswerMapper;
import com.zd.exam.mapper.ElPractiseQuMapper;
import com.zd.exam.service.ElQuService;
import com.zd.exam.service.IElPractiseQuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 练习题Service业务层处理
 *
 * @author liubo
 * @date 2021-12-14
 */
@Service
public class ElPractiseQuServiceImpl implements IElPractiseQuService {
    @Autowired
    private ElPractiseQuMapper elPractiseQuMapper;
    @Autowired
    private ElPractiseQuAnswerMapper practiseQuAnswerMapper;
    @Autowired
    private ElQuService elQuService;

    /**
     * 查询练习题
     *
     * @param id 练习题主键
     * @return 练习题
     */
    @Override
    public ElPractiseQu selectElPractiseQuById(Long id) {
        return elPractiseQuMapper.selectElPractiseQuById(id);
    }

    /**
     * 查询练习题列表
     *
     * @param elPractiseQu 练习题
     * @return 练习题
     */
    @Override
    public List<ElPractiseQu> selectElPractiseQuList(ElPractiseQu elPractiseQu) {
        return elPractiseQuMapper.selectElPractiseQuList(elPractiseQu);
    }

    /**
     * 新增练习题
     *
     * @param elPractiseQu 练习题
     * @return 结果
     */
    @Override
    public int insertElPractiseQu(ElPractiseQu elPractiseQu) {
        //设置其他公共字段
        SaveUtil.setCommonAttr(elPractiseQu);
        return elPractiseQuMapper.insertElPractiseQu(elPractiseQu);
    }

    /**
     * 修改练习题
     *
     * @param elPractiseQu 练习题
     * @return 结果
     */
    @Override
    public int updateElPractiseQu(ElPractiseQu elPractiseQu) {
        elPractiseQu.setUpdateTime(DateUtils.getNowDate());
        elPractiseQu.setUpdateBy(SecurityUtils.getUsername());
        return elPractiseQuMapper.updateElPractiseQu(elPractiseQu);
    }

    /**
     * 批量删除练习题
     *
     * @param ids 需要删除的练习题主键
     * @return 结果
     */
    @Override
    public int deleteElPractiseQuByIds(Long[] ids) {
        return elPractiseQuMapper.deleteElPractiseQuByIds(ids);
    }

    /**
     * 删除练习题信息
     *
     * @param id 练习题主键
     * @return 结果
     */
    @Override
    public int deleteElPractiseQuById(Long id) {
        return elPractiseQuMapper.deleteElPractiseQuById(id);
    }

    @Override
    public ElPractiseQu selectElPractiseQuDetail(ElPractiseQu practiseQu) {
        // 问题
        ElQu qu = elQuService.queryById(practiseQu.getQuId());

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

        ElPractiseQuAnswer elPractiseQuAnswer = new ElPractiseQuAnswer();
        elPractiseQuAnswer.setPractiseId(practiseQu.getPractiseId());
        elPractiseQuAnswer.setQuId(practiseQu.getQuId());
        // 答案列表
        List<ElPractiseQuAnswer> list = practiseQuAnswerMapper.selectElPractiseQuAnswerList(elPractiseQuAnswer);
        practiseQu.setAnswerList(list);

        return practiseQu;
    }
}
