package cn.ey.biz.module.system.dal.redis.codeblock;

import cn.ey.biz.common.util.json.JsonUtils;
import cn.ey.biz.module.system.dal.dataobject.codeblock.CodeBlockDO;
import cn.ey.biz.module.system.dal.redis.RedisKeyConstants;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Repository
public class CodeBlockRedisDAO {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public static final String LEGAL_CODE_BLOCK = "LEGAL_CODE_BLOCK";

    public List<CodeBlockDO> getCodeBlockData() {
        String redisKey = formatCodeBlockDataKey();
        return JsonUtils.parseArray(stringRedisTemplate.opsForValue().get(redisKey), CodeBlockDO.class);
    }

    public void set(List<CodeBlockDO> value) {
        String redisKey = formatCodeBlockDataKey();
        stringRedisTemplate.opsForValue().set(redisKey, JsonUtils.toJsonString(value), 24, TimeUnit.HOURS);
    }

    public void delete() {
        String redisKey = formatCodeBlockDataKey();
        stringRedisTemplate.delete(redisKey);
    }

    private static String formatCodeBlockDataKey() {
        return String.format(RedisKeyConstants.CODE_BLOCK.getKeyTemplate(), LEGAL_CODE_BLOCK);
    }


}
