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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.themis.business.cache.bean.ExamActivityCacheBean;
import com.qmth.themis.business.dao.TEExamSummaryMapper;
import com.qmth.themis.business.entity.TEExamSummary;
import com.qmth.themis.business.service.*;
import com.qmth.themis.business.util.UidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Set;

/**
 * <p>
 * 考试信息统计表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2023-10-27
 */
@Service
public class TEExamSummaryServiceImpl extends ServiceImpl<TEExamSummaryMapper, TEExamSummary> implements TEExamSummaryService {
    private final static Logger log = LoggerFactory.getLogger(TEExamSummaryServiceImpl.class);

    @Resource
    TEExamActivityService teExamActivityService;

    @Resource
    ThemisCacheService themisCacheService;

    @Resource
    TEExamStudentService teExamStudentService;

    @Resource
    TOeExamRecordService tOeExamRecordService;

    /**
     * 统计考试信息
     *
     * @param examId
     * @param examActivityId
     * @param roomCodeSet
     * @param freshen
     * @return
     */
    @Override
    public boolean examSummary(Long examId, Long examActivityId, Set<String> roomCodeSet, boolean freshen) {
        boolean updateCache = false;
        if (!CollectionUtils.isEmpty(roomCodeSet)) {
            for (String s : roomCodeSet) {
                Long l = themisCacheService.getExamRecordChangeCache(examActivityId + "_" + s);
                if ((Objects.nonNull(l) && System.currentTimeMillis() - l.longValue() <= 1000 * 60 * 1) || freshen) {
                    //根据考试批次、场次、考场编码统计信息
                    TEExamSummary teExamSummary = this.baseMapper.examSummary(examId, examActivityId, s);
                    this.examSummaryCommon(examId, examActivityId, s, teExamSummary, freshen);
                    updateCache = true;
                } else {
                    if (freshen) {
                        themisCacheService.updateExamSummaryCache(examId, examActivityId, s);
                    } else {
                        themisCacheService.getExamSummaryCache(examId, examActivityId, s);
                    }
                }
            }
            if (updateCache || freshen) {
                themisCacheService.updateExamSummaryCache(examId);
                themisCacheService.updateExamSummaryCache(examId, examActivityId);
            }
        }
        return updateCache;
    }

    /**
     * 更新考试完成数
     *
     * @param examId
     * @param examActivityId
     * @param roomCode
     */
    @Override
    public void updateExamSummaryFinishCount(Long examId, Long examActivityId, String roomCode) {
        this.baseMapper.updateExamSummaryFinishCount(examId, examActivityId, roomCode);
    }

    /**
     * 更新预警未读数
     *
     * @param examId
     * @param examActivityId
     * @param roomCode
     */
    @Override
    public void updateExamSummaryWarningUnread(Long examId, Long examActivityId, String roomCode) {
        this.baseMapper.updateExamSummaryWarningUnread(examId, examActivityId, roomCode);
    }

    /**
     * 考试计算公用
     *
     * @param examId
     * @param examActivityId
     * @param s
     * @param teExamSummary
     * @param freshen
     */
    protected void examSummaryCommon(Long examId, Long examActivityId, String s, TEExamSummary teExamSummary, boolean freshen) {
        if (Objects.nonNull(teExamSummary) && teExamSummary.getTotalCount().intValue() > 0) {
            teExamSummary.setExamId(examId);
            teExamSummary.setExamActivityId(examActivityId);
            teExamSummary.setRoomCode(s);
            TEExamSummary teExamSummaryDb = this.getOne(new QueryWrapper<TEExamSummary>().lambda()
                    .eq(TEExamSummary::getExamId, examId)
                    .eq(TEExamSummary::getExamActivityId, examActivityId)
                    .eq(TEExamSummary::getRoomCode, s));
            if (Objects.isNull(teExamSummaryDb)) {
                teExamSummaryDb = new TEExamSummary();
                BeanUtils.copyProperties(teExamSummary, teExamSummaryDb);
                teExamSummaryDb.setId(UidUtil.nextId());
                Integer finishCount = tOeExamRecordService.findExamRecordByFinish(examId, examActivityId, s);
                teExamSummary.setFinishCount(finishCount);
            } else {
                Long id = teExamSummaryDb.getId();
                Integer finishCount = null;
                if (freshen) {
                    finishCount = tOeExamRecordService.findExamRecordByFinish(examId, examActivityId, s);
                } else {
                    finishCount = teExamSummaryDb.getFinishCount();
                }
                BeanUtils.copyProperties(teExamSummary, teExamSummaryDb);
                teExamSummaryDb.setId(id);
                teExamSummaryDb.setFinishCount(finishCount);
            }
            ExamActivityCacheBean ac = teExamActivityService.getExamActivityCacheBean(examActivityId);
            long timestamp = System.currentTimeMillis();
            if (ac.getFinishTime() <= timestamp) {//当考试场次结束时间已过，缺考=全部应考-已完成考试
                teExamSummaryDb.setAbsentCount(teExamSummaryDb.getTotalCount() - teExamSummaryDb.getFinishCount());
            } else {
                Integer absentCount = teExamStudentService.getAbsentCount(examId, examActivityId, s);
                teExamSummaryDb.setAbsentCount(Objects.nonNull(absentCount) ? absentCount : 0);
            }
            this.saveOrUpdate(teExamSummaryDb);
            themisCacheService.updateExamSummaryCache(examId, examActivityId, s);
        }
    }
}
