/*
 * Copyright (c) 京北方信息技术股份有限公司 Corporation 2021 . All rights reserved.
 *
 */
package net.northking.cctp.common.cache;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * 实体名称缓存--redis实现类
 *
 * @author : xiusong.xie
 * @version : 1.0
 */
public class EntityNameRedisRepository implements EntityNameCache
{

  /**
   * Redis工具类
   */
  private final RedisTemplate<String, Object> redisTemplate;

  public EntityNameRedisRepository(RedisTemplate<String, Object> redisTemplate)
  {
    this.redisTemplate = redisTemplate;
  }

  @Override
  public String translateById(String catalogCode, String id)
  {
    if (StringUtils.hasText(id))
    {
      return (String) redisTemplate.opsForHash().get(createCatalogKey(catalogCode), id);
    } else
    {
      return "";
    }
  }

  @Override
  public void put(String catalogCode, Map<String, String> dataMap)
  {
    redisTemplate.opsForHash().putAll(createCatalogKey(catalogCode), dataMap);
  }

  @Override
  public void put(String catalogCode, String id, String name)
  {
    redisTemplate.opsForHash().put(createCatalogKey(catalogCode), id, name);
  }

  private String createCatalogKey(String catalogCode)
  {
    return catalogCode;
  }
}
