package cn.com.qmth.stmms.biz.utils;

import org.springframework.data.redis.core.StringRedisTemplate;

import cn.com.qmth.stmms.biz.exam.model.Marker;
import cn.com.qmth.stmms.common.redis.RedisKeyBuilder;
import org.springframework.data.redis.core.ZSetOperations;

import java.util.Iterator;
import java.util.Set;

public class MarkRedisUtil {

    public static void addCurrentTask(StringRedisTemplate redisTemplate, Marker marker, int libraryId) {
        String key = getKey(marker);
        redisTemplate.opsForZSet().add(key, String.valueOf(libraryId), marker.getId());
        redisTemplate.opsForZSet().add(
                RedisKeyBuilder.getCurrentTaskTimeKey(marker.getExamId(), marker.getSubjectCode(),
                        marker.getGroupNumber()), String.valueOf(libraryId), System.currentTimeMillis());
    }

    public static int countCurrentTask(StringRedisTemplate redisTemplate, int examId, String subjectCode, int number) {
        String key = RedisKeyBuilder.getCurrentTaskKey(examId, subjectCode, number);
        Long count = redisTemplate.opsForZSet().size(key);
        return count != null ? count.intValue() : 0;
    }

    public static int countCurrentTask(StringRedisTemplate redisTemplate, Marker marker) {
        String key = getKey(marker);
        Long count = redisTemplate.opsForZSet().count(key, marker.getId() - 0.5, marker.getId() + 0.5);
        return count != null ? count.intValue() : 0;
    }

    public static void removeCurrentTask(StringRedisTemplate redisTemplate, int examId, String subjectCode, int number,
            int libraryId) {
        String key = RedisKeyBuilder.getCurrentTaskKey(examId, subjectCode, number);
        redisTemplate.opsForZSet().remove(key, String.valueOf(libraryId));
        redisTemplate.opsForZSet().remove(RedisKeyBuilder.getCurrentTaskTimeKey(examId, subjectCode, number),
                String.valueOf(libraryId));
    }

    public static boolean isCurrentTask(StringRedisTemplate redisTemplate, int examId, String subjectCode, int number,
            int libraryId) {
        String key = RedisKeyBuilder.getCurrentTaskKey(examId, subjectCode, number);
        Double score = redisTemplate.opsForZSet().score(key, String.valueOf(libraryId));
        return score != null && score > 0;
    }

    public static void clearCurrentTask(StringRedisTemplate redisTemplate, Marker marker) {
        String key = getKey(marker);
        redisTemplate.opsForZSet().removeRangeByScore(key, marker.getId() - 0.5, marker.getId() + 0.5);
    }

    public static void clearCurrentTask(StringRedisTemplate redisTemplate, int examId, String subjectCode, int number) {
        String key = RedisKeyBuilder.getCurrentTaskKey(examId, subjectCode, number);
        redisTemplate.delete(key);
        redisTemplate.delete(RedisKeyBuilder.getCurrentTaskTimeKey(examId, subjectCode, number));
    }

    private static String getKey(Marker marker) {
        return RedisKeyBuilder.getCurrentTaskKey(marker.getExamId(), marker.getSubjectCode(), marker.getGroupNumber());
    }

    public static void QuartzClearTask(StringRedisTemplate redisTemplate, long cleanMapinterval) {
        Set<String> keys = redisTemplate.keys(RedisKeyBuilder.getCurrentTaskTimePrefix() + "*");
        Iterator<String> iterators = keys.iterator();
        while (iterators.hasNext()) {
            String key = iterators.next();
            Set<ZSetOperations.TypedTuple<String>> rangeWithScores = redisTemplate.opsForZSet().rangeWithScores(key, 0,
                    -1);

            Iterator<ZSetOperations.TypedTuple<String>> iterator = rangeWithScores.iterator();
            while (iterator.hasNext()) {
                ZSetOperations.TypedTuple<String> next = iterator.next();
                if ((System.currentTimeMillis() - next.getScore()) > cleanMapinterval * 1000 * 60) {

                    redisTemplate.opsForZSet().remove(RedisKeyBuilder.getCurrentTaskKeyByTimeKey(key), next.getValue());

                    redisTemplate.opsForZSet().remove(key, next.getValue());
                }
            }

        }

    }
}
