package com.tuoheng.admin.tzhl.service.token;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.tzhl.config.TZHLConfig;
import com.tuoheng.admin.tzhl.constant.TZHLConstant;
import com.tuoheng.admin.tzhl.request.TZHLGetTokenRequest;
import com.tuoheng.admin.tzhl.response.TZHLTokenResponse;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.RedisUtils;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service
public class TZHLGetTokenService {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    @Qualifier("restTemplate")
    private RestTemplate restTemplate;

    public String getToken() {
        TZHLTokenResponse tokenResponse = (TZHLTokenResponse) redisUtils.get(TZHLConstant.HL_TOKEN_KEY);
        if (ObjectUtil.isNotNull(tokenResponse)) {
            return tokenResponse.getToken();
        }
        log.info("redis中不存在泰州海陵区城管token，重新获取");
        tokenResponse = this.getTokenResponse();
        redisUtils.set(TZHLConstant.HL_TOKEN_KEY, tokenResponse, 1500); // 过期时间为半小时1800，为防止延迟，缩小一点25分钟失效
        return tokenResponse.getToken();
    }

    public TZHLTokenResponse getTokenResponse() {
        TZHLGetTokenRequest request = new TZHLGetTokenRequest();
        request.setTicket(TZHLConfig.tzhlTicket);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity httpEntity = new HttpEntity(request, headers);
        String url = TZHLConfig.tzhlURL + TZHLConstant.TIAN_YI_API_TICKET_LOGIN;
        ResponseEntity<TZHLTokenResponse> response;
        try {
            response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, TZHLTokenResponse.class);
        } catch (Exception e) {
            log.info("泰州海陵区城管，获取token接口异常, url:{}", url);
            log.info("泰州海陵区城管，获取token接口异常, httpEntity:{}", httpEntity);
            throw new ServiceException("南京海事局，获取token接口异常");
        }
        if (null == response || !response.hasBody()) {
            log.info("泰州海陵区城管，获取token接口失败，response为空");
            log.info("泰州海陵区城管，获取token接口失败, url:{}", url);
            log.info("泰州海陵区城管，获取token接口失败, httpEntity:{}", httpEntity);
            throw new ServiceException("泰州海陵区城管，获取token接口失败");
        }
        TZHLTokenResponse tokenResponse = JSONObject.parseObject(JSONObject.toJSONString(response.getBody()), TZHLTokenResponse.class);
        if (!TZHLConstant.RESPONSE_SUCCESS_CODE.equals(tokenResponse.getCode())) {
            log.error("泰州海陵区城管，获取token接口失败,{}", response.getBody());
            throw new ServiceException("泰州海陵区城管，获取token接口失败");
        }
        return tokenResponse;
    }

}
