package com.kym.admin.jobs;

import com.kym.common.utils.HttpUtil;
import com.kym.entity.common.RedisKeys;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author skyline
 * @description EN+ SASS平台token刷新任务
 * @date 2023-09-30 16:15
 */
//@Component
@Slf4j
public class EnPlusSaasTokenJob {

    private final StringRedisTemplate redisTemplate;
    @Value("${en-plus.sass}")
    public String sass;


    public EnPlusSaasTokenJob(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    // 每30分钟执行一次
    @Scheduled(cron = "0 0/30 * * * ?")
    public void execute() {
        var res = HttpUtil.post(sass);
        var json = HttpUtil.parseJson(res);
        if (json.getIntValue("code") == HttpStatus.SC_OK) {
            var token = HttpUtil.parseJson(json.getString("data")).getString("token");
            log.info("EN+ SASS平台token刷新任务执行结果 Token：{}", token);
            redisTemplate.opsForValue().set(RedisKeys.EN_PLUS_SASS_TOKEN, token);
        }
    }
}
