package com.dxhy.core.job.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.core.job.service.TokenService;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author user
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@Service("tokenService")
@Slf4j
public class TokenServiceImpl implements TokenService {

    @Value("${jxjk.dataPlatformUrl}")
    private String url;

    /**
     * 获取数据平台token
     */
    @Override
    public String getToken(String clientId, String secret) {
        log.info("获取数据平台token开始");
        // 拼接请求参数
        JSONObject json = new JSONObject();
        json.put("clientId", clientId);
        json.put("secret", secret);
        String params = json.toJSONString();
        try {
            String responseJson = HttpUtil.post(url, params, 30000);
            if (responseJson != null) {
                JSONObject tokenJson = JSONObject.parseObject(responseJson);
                log.info("获取数据平台token结束:" + tokenJson.toJSONString());
                if (tokenJson.containsKey("access_token")) {
                    return tokenJson.getString("access_token");
                } else {
                    return null;
                }
            }
        } catch (Exception e) {
            log.error("获取数据平台token异常");
           log.info("业务处理异常：{}", e);
            return null;
        }
        return null;
    }
}
