package thyyxxk.webserver.scheduled;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * <p>
 * 描述： 获取体检token
 * </p>
 *
 * @author xc
 * @date 2021-07-13 17:09
 */
@Slf4j
@Service
public class SiTjToken {
    @Value("${si-tj-url}")
    private String siTjUrl;
    private volatile String token;

//    @Scheduled(fixedRate = 1000 * 60 * 60 * 23)
    public void getToken() {
        RestTemplate restTemplate = new RestTemplate();
        JSONObject result = restTemplate.getForObject(siTjUrl +
                "/token?username=43010150145&password=150145", JSONObject.class);
        if (null == result) {
            token = null;
            return;
        }
        if (result.getIntValue("code") != 0) {
            token = null;
            return;
        }
        token = result.getJSONObject("data").getString("token");
    }

    public String getSiTjToken() {
        if (token == null) {
            getToken();
        }
        return token;
    }
}
