package com.qmth.themis.business.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.themis.business.bean.admin.OpenExamCourseBean;
import com.qmth.themis.business.cache.bean.ExamCourseCacheBean;
import com.qmth.themis.business.dao.TEExamCourseMapper;
import com.qmth.themis.business.dao.TEExamPaperMapper;
import com.qmth.themis.business.dto.response.TEExamCourseDto;
import com.qmth.themis.business.entity.TEExamCourse;
import com.qmth.themis.business.entity.TEExamPaper;
import com.qmth.themis.business.service.TEExamCourseService;
import com.qmth.themis.common.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 考试科目 服务实现类
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/6/25
 */
@Service
public class TEExamCourseServiceImpl extends ServiceImpl<TEExamCourseMapper, TEExamCourse>
        implements TEExamCourseService {

    @Resource
    TEExamCourseMapper teExamCourseMapper;

    @Resource
    TEExamPaperMapper teExamPaperMapper;

    /**
     * 查询考试科目
     *
     * @param iPage
     * @param id
     * @param examId
     * @param courseCode
     * @param courseName
     * @param hasPaper
     * @return
     */
    @Override
    public IPage<TEExamCourseDto> examCourseQuery(IPage<Map> iPage, Long id, Long examId, String courseCode, String courseName,
                                                  Integer hasPaper) {
        return teExamCourseMapper.examCourseQuery(iPage, id, examId, courseCode, courseName, hasPaper);
    }

    @Override
    public TEExamCourse findByExamIdAndCourseCode(Long examId, String courseCode) {
        if (examId == null) {
            throw new BusinessException("examId is null");
        }
        if (StringUtils.isBlank(courseCode)) {
            throw new BusinessException("courseCode is null");
        }
        return teExamCourseMapper.findByExamIdAndCourseCode(examId, courseCode);
    }

    @Cacheable(value = "exam_course", key = "#examId+'_'+#courseCode", unless = "#result == null")
    @Override
    public ExamCourseCacheBean getExamCourseCacheBean(Long examId, String courseCode) {
        return cacheOperation(examId, courseCode);
    }

    /**
     * 更新科目缓存
     *
     * @param examId
     * @param courseCode
     * @return
     */
    @Override
    @CachePut(value = "exam_course", key = "#examId+'_'+#courseCode", condition = "#result != null")
    public ExamCourseCacheBean updateExamCourseCacheBean(Long examId, String courseCode) {
        return cacheOperation(examId, courseCode);
    }

    /**
     * 删除科目缓存
     *
     * @param examId
     * @param courseCode
     */
    @Override
    @CacheEvict(value = "exam_course", key = "#examId+'_'+#courseCode")
    public void deleteExamCourseCacheBean(Long examId, String courseCode) {

    }

    /**
     * 缓存操作
     *
     * @param examId
     * @param courseCode
     * @return
     */
    public ExamCourseCacheBean cacheOperation(Long examId, String courseCode) {
        ExamCourseCacheBean ret = null;
        TEExamCourse ec = teExamCourseMapper.findByExamIdAndCourseCode(examId, courseCode);
        if (ec == null) {
            return ret;
        }
        ret = new ExamCourseCacheBean();
        ret.setId(ec.getId());
        ret.setExamId(ec.getExamId());
        ret.setCourseCode(ec.getCourseCode());
        ret.setCourseName(ec.getCourseName());
        ret.setObjectiveShuffle(ec.getObjectiveShuffle());
        ret.setOptionShuffle(ec.getOptionShuffle());
        ret.setPaperCount(ec.getPaperCount());
        ret.setHasAnswer(ec.getHasAnswer());
        ret.setAudioPlayCount(ec.getAudioPlayCount());
        ret.setHasAudio(ec.getHasAudio());
        ret.setMobilePhotoUpload(ec.getMobilePhotoUpload());
        List<TEExamPaper> papers = teExamPaperMapper.findListByExamIdAndCourseCode(examId, courseCode);
        if (papers != null && papers.size() > 0) {
            List<Long> paperIds = new ArrayList<Long>();
            List<Double> weight = new ArrayList<Double>();
            for (TEExamPaper paper : papers) {
                paperIds.add(paper.getId());
                weight.add(paper.getWeight());
            }
            ret.setPaperIds(paperIds);
            ret.setPaperWeight(weight);
        }
        return ret;
    }

    @Override
    public IPage<OpenExamCourseBean> examCourseQueryForOpen(Page<OpenExamCourseBean> ipage, Long examId,
                                                            String courseCode, Boolean hasPaper) {
        return teExamCourseMapper.examCourseQueryForOpen(ipage, examId, courseCode, hasPaper);
    }
}
