package com.cico.nest.lcdp.boot.core.qxsd.application.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson2.JSONObject;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.TunnelDetailService;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.common.constatnt.RedisConstants;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.TunnelDetailMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelDetailDO;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 隧道信息 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class TunnelDetailServiceImpl implements TunnelDetailService {

    @Resource
    private TunnelDetailMapper tunnelDetailMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public TunnelDetailDO getTunnelDetail(String id) {
        String redisKey = RedisConstants.REDIS_TUNNEL_DETAIL + id;
        String tunnelDetailJson = stringRedisTemplate.opsForValue().get(redisKey);
        if(StrUtil.isNotEmpty(tunnelDetailJson)){
            return JSONObject.parseObject(tunnelDetailJson,TunnelDetailDO.class);
        }
        TunnelDetailDO tunnelDetailDO = tunnelDetailMapper.selectById(id);
        if(Objects.nonNull(tunnelDetailDO)){
            tunnelDetailJson = JSON.toJSONString(tunnelDetailDO,SerializerFeature.WriteMapNullValue);
            stringRedisTemplate.opsForValue().set(redisKey,tunnelDetailJson,5, TimeUnit.MINUTES);
        }
        return tunnelDetailDO;
    }



}
