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

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;

import cn.com.qmth.stmms.common.redis.RedisKeyBuilder;

public class BlockTaskCountUtil {

    public static void incrBlockTotalCount(StringRedisTemplate redisTemplate, final int blockId) {
        redisTemplate.execute(new RedisCallback<Long>() {

            @Override
            public Long doInRedis(RedisConnection conn) throws DataAccessException {
                try {
                    conn.incr(RedisKeyBuilder.getBlockTotalCountKey(blockId).getBytes("UTF-8"));
                } catch (Exception e) {
                }
                return 0l;
            }
        });
    }

    public static void addBlockMarkedTask(StringRedisTemplate redisTemplate, final int blockId, final int libraryId) {
        redisTemplate.opsForSet().add(RedisKeyBuilder.getBlockMarkedTaskKey(blockId), String.valueOf(libraryId));
    }

    public static int getBlockMarkedTaskCount(StringRedisTemplate redisTemplate, final int blockId) {
        Long count = redisTemplate.opsForSet().size(RedisKeyBuilder.getBlockMarkedTaskKey(blockId));
        return count != null ? count.intValue() : 0;
    }
}
