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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.themis.business.cache.bean.ExamStudentCacheBean;
import com.qmth.themis.business.dao.TEExamStudentMapper;
import com.qmth.themis.business.dto.response.RoomCodeQueryDto;
import com.qmth.themis.business.dto.response.TEExamStudentDto;
import com.qmth.themis.business.entity.TEExamStudent;
import com.qmth.themis.business.service.TEExamStudentService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description: 考生库 服务实现类
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/6/25
 */
@Service
public class TEExamStudentServiceImpl extends ServiceImpl<TEExamStudentMapper, TEExamStudent> implements TEExamStudentService {

    @Resource
    TEExamStudentMapper teExamStudentMapper;

    /**
     * 查询考生信息
     *
     * @param iPage
     * @param examId
     * @param activityId
     * @param identity
     * @param name
     * @param roomCode
     * @param courseCode
     * @param grade
     * @param enable
     * @param classNo
     * @return
     */
    @Override
    public IPage<TEExamStudentDto> examStudentQuery(IPage<Map> iPage, Long examId, Long activityId, String identity, String name, String roomCode, String courseCode, String grade, Integer enable, String classNo, Integer hasPhoto) {
        return teExamStudentMapper.examStudentQuery(iPage, examId, activityId, identity, name, roomCode, courseCode, grade, enable, classNo, hasPhoto);
    }

    @Cacheable(value = "exam_student", key = "#examStudentId", unless = "#result == null")
    @Override
    public ExamStudentCacheBean getExamStudentCacheBean(Long examStudentId) {
        return cacheOperation(examStudentId);
    }

    /**
     * 更新考生缓存
     *
     * @param examStudentId
     * @return
     */
    @Override
    @CachePut(value = "exam_student", key = "#examStudentId", condition = "#result != null")
    public ExamStudentCacheBean updateExamStudentCacheBean(Long examStudentId) {
        return cacheOperation(examStudentId);
    }

    /**
     * 删除考生缓存
     *
     * @param examStudentId
     */
    @Override
    @CacheEvict(value = "exam_student", key = "#examStudentId")
    public void deleteExamStudentCacheBean(Long examStudentId) {

    }

    /**
     * 考场查询
     *
     * @param roomName
     * @return
     */
    @Override
    public List<RoomCodeQueryDto> examRoomQuery(String roomName) {
        return teExamStudentMapper.examRoomQuery(roomName);
    }

    /**
     * 缓存操作
     *
     * @param examStudentId
     * @return
     */
    public ExamStudentCacheBean cacheOperation(Long examStudentId) {
        ExamStudentCacheBean ret = null;
        TEExamStudent es = getById(examStudentId);
        if (es == null) {
            return ret;
        }
        if (es.getEnable() != null && es.getEnable().intValue() == 0) {
            return ret;
        }
        ret = new ExamStudentCacheBean();
        ret.setId(es.getId());
        ret.setExamId(es.getExamId());
        ret.setExamActivityId(es.getExamActivityId());
        ret.setStudentId(es.getStudentId());
        ret.setCourseCode(es.getCourseCode());
        ret.setCourseName(es.getCourseName());
        ret.setRoomCode(es.getRoomCode());
        ret.setRoomName(es.getRoomName());
        ret.setAlreadyExamCount(es.getAlreadyExamCount());
        ret.setCurrentRecordId(es.getCurrentRecordId());
        ret.setIdentity(es.getIdentity());
        ret.setName(es.getName());
        ret.setEnable(es.getEnable());
        return ret;
    }

    @Override
    public List<TEExamStudentDto> examStudentList(Long examId, Long activityId, String identity, String name,
                                                  String roomCode, String courseCode, String grade, Integer enable, String classNo, Integer hasPhoto) {
        return teExamStudentMapper.examStudentList(examId, activityId, identity, name, roomCode, courseCode, grade, enable, classNo, hasPhoto);
    }

    @Transactional
    @Override
    public void updateByMqMsg(Map<String, Object> param) {
        Long examStudentId = (Long) param.get("examStudentId");
        Integer alreadyExamCount = (Integer) param.get("alreadyExamCount");
        Long currentRecordId = (Long) param.get("currentRecordId");
        if (alreadyExamCount != null) {
            teExamStudentMapper.updateAlreadyExamCount(examStudentId, alreadyExamCount);
        }
        if (currentRecordId != null) {
            teExamStudentMapper.updateCurrentRecordId(examStudentId, currentRecordId);
        }
    }
}
