package com.sptg.video.cloud.biz.service.impl;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.permissions.api.entity.Conf;
import com.sptg.video.cloud.api.entity.SysParams;
import com.sptg.video.cloud.api.entity.VCPusherConfig;
import com.sptg.video.cloud.biz.mapper.VCPusherConfigMapper;
import com.sptg.video.cloud.biz.service.VCPusherConfigServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author zwx
 * @create 2020/11/19 12:06
 */
@Service
@RequiredArgsConstructor
public class VCPusherConfigServiceImpl extends ServiceImpl<VCPusherConfigMapper, VCPusherConfig> implements VCPusherConfigServiceInterface {

    /**
     * 菜单缓存名称
     */
    private static final String CONF_CACHE_NAME = "sptg:videoCloud:pusherConfig";

    private final RedisTemplate<String,String> redisTemplate;

    @Override
    public VCPusherConfig findById(Long id) {
        //  查询缓存
        HashOperations<String, String, String> hashOperations = this.redisTemplate.opsForHash();
        String json = hashOperations.get(CONF_CACHE_NAME, id.toString());
        if (StringUtils.isEmpty(json)){
            //  查询数据库
            VCPusherConfig vcPusherConfig = this.selectById(id);
            if (null != vcPusherConfig){
                //  保存到缓存
                json = JsonUtils.objectToJson(vcPusherConfig);
                hashOperations.put(CONF_CACHE_NAME,id.toString(),json);
            }
            return vcPusherConfig;
        }
        return JsonUtils.jsonToPojo(json,VCPusherConfig.class);
    }
}
