package com.sptg.permissions.biz.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.permissions.api.entity.ApiDockingConf;
import com.sptg.permissions.biz.mapper.ApiDockingConfMapper;
import com.sptg.permissions.biz.service.ApiDockingConfInterface;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * title: ConfServiceImpl
 * package: com.sptg.permissions.biz.security.impl
 * description:
 *
 *               
 *                
 */
@Service
@RequiredArgsConstructor
public class ApiDockingConfServiceImpl extends ServiceImpl<ApiDockingConfMapper, ApiDockingConf> implements ApiDockingConfInterface {

    /**
     * 菜单缓存名称
     */
    private static final String API_CONF_CACHE_NAME = "sptg:permissions:apiDockingConf";

    private final RedisTemplate<String,String> redisTemplate;

    @Override
    public ApiDockingConf selectEntityById(Long orgId) {
        //  查询缓存
        HashOperations<String, String, String> hashOperations = this.redisTemplate.opsForHash();
        String hashKey = orgId.toString();
        String json = hashOperations.get(API_CONF_CACHE_NAME,hashKey);
        if (StringUtils.isEmpty(json)) {
            ApiDockingConf conf = this.selectById(orgId);
            if (null != conf) {
                //  保存到缓存中
                json = JsonUtils.objectToJson(conf);
                hashOperations.put(API_CONF_CACHE_NAME,hashKey,json);
            }
            return conf;
        }
        return JsonUtils.jsonToPojo(json,ApiDockingConf.class);
    }
}
