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

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.themis.business.cache.bean.ExamActivityCacheBean;
import com.qmth.themis.business.dao.TEExamActivityMapper;
import com.qmth.themis.business.dto.response.TEExamActivityDto;
import com.qmth.themis.business.dto.response.TEExamActivityQueryDto;
import com.qmth.themis.business.entity.TEExamActivity;
import com.qmth.themis.business.service.TEExamActivityService;

/**
 * @Description: 考试场次 服务实现类
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/6/25
 */
@Service
public class TEExamActivityServiceImpl extends ServiceImpl<TEExamActivityMapper, TEExamActivity> implements TEExamActivityService {

    @Resource
    TEExamActivityMapper teExamActivityMapper;

    /**
     * 表是否存在
     *
     * @param tableName
     * @param schema
     * @return
     */
    @Override
    public Integer existTable(String tableName, String schema) {
        return teExamActivityMapper.existTable(tableName, schema);
    }

    /**
     * 删除表
     *
     * @param tableName
     */
    @Override
    public void dropTable(String tableName) {
        teExamActivityMapper.dropTable(tableName);
    }

    /**
     * 创建表
     *
     * @param tableName
     */
    @Override
    public void createNewTable(String tableName) {
        teExamActivityMapper.createNewTable(tableName);
    }

    /**
     * 新插数据
     *
     * @param tableName
     * @param teExamActivity
     */
    @Override
    public void insertInfo(String tableName, TEExamActivity teExamActivity) {
        teExamActivityMapper.insertInfo(tableName, teExamActivity);
    }

    /**
     * 查询所有数据
     *
     * @param iPage
     * @param tableName
     * @return
     */
    @Override
    public IPage<TEExamActivity> selectListPage(IPage<Map> iPage, String tableName) {
        return teExamActivityMapper.selectListPage(iPage, tableName);
    }

    /**
     * 查询考场场次信息
     *
     * @param iPage
     * @param examId
     * @param code
     * @param startDate
     * @param finishDate
     * @return
     */
    @Override
    public IPage<TEExamActivityQueryDto> examActivityQuery(IPage<Map> iPage, Long id, Long examId, String code, Long startDate, Long finishDate) {
        return teExamActivityMapper.examActivityQuery(iPage, id, examId, code, startDate, finishDate);
    }

    /**
     * 获取考试待考列表
     *
     * @param studentId
     * @param examActivityIds
     * @param mode
     * @return
     */
    @Override
    public List<TEExamActivityDto> getWaitingExam(Long studentId, Set<Long> examActivityIds, String mode) {
        return teExamActivityMapper.getWaitingExam(studentId, examActivityIds, mode);
    }

    @Cacheable(value = "exam_activity", key = "#examActivityId", unless = "#result == null")
    @Override
    public ExamActivityCacheBean getExamActivityCacheBean(Long examActivityId) {
        return cacheOperation(examActivityId);
    }

    /**
     * 更新场次缓存
     *
     * @param examActivityId
     * @return
     */
    @Override
    @CachePut(value = "exam_activity", key = "#examActivityId", condition = "#result != null")
    public ExamActivityCacheBean updateExamActivityCacheBean(Long examActivityId) {
        return cacheOperation(examActivityId);
    }

    /**
     * 删除场次缓存
     *
     * @param examActivityId
     */
    @Override
    @CacheEvict(value = "exam_activity", key = "#examActivityId")
    public void deleteExamActivityCacheBean(Long examActivityId) {

    }

    /**
     * 缓存操作
     *
     * @param examActivityId
     * @return
     */
    public ExamActivityCacheBean cacheOperation(Long examActivityId) {
        ExamActivityCacheBean ret = null;
        TEExamActivity ac = getById(examActivityId);
        if (ac == null) {
            return ret;
        }
        if (ac.getEnable() != null && ac.getEnable().intValue() == 0) {
            return ret;
        }
        ret = new ExamActivityCacheBean();
        ret.setId(ac.getId());
        ret.setCode(ac.getCode());
        ret.setExamId(ac.getExamId());
        ret.setPrepareSeconds(ac.getPrepareSeconds());
        ret.setMaxDurationSeconds(ac.getMaxDurationSeconds());
        ret.setOpeningSeconds(ac.getOpeningSeconds());
        ret.setStartTime(ac.getStartTime());
        ret.setFinishTime(ac.getFinishTime());
        ret.setEnable(ac.getEnable());
        return ret;
    }
}
