package com.njcn.auth.controller;

import cn.hutool.json.JSONObject;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Objects;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2022年04月27日 11:22
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api(tags = "校验第三方token")
@RequestMapping("/judgeToken")
public class JudgeThirdToken extends BaseController {

    /**
     * 校验广州超高压token有效性
     *
     * @param token token数据
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/guangZhou")
    @ApiOperation("校验广州超高压token有效性")
    @ApiImplicitParam(name = "token", required = true)
    public HttpResult<Object> guangZhou(String token) {
        RestTemplate restTemplate = new RestTemplate();
        String methodDescribe = getMethodDescribe("guangZhou");
        LogUtil.njcnDebug(log, "{}，token：{}", methodDescribe, token);

        // 请求地址
        String url = "http://10.121.17.9:9080/ehv/auth_valid";

        // 请求头设置,x-www-form-urlencoded格式的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        //提交参数设置
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("token", token);

        // 组装请求体
        HttpEntity<MultiValueMap<String, String>> request =
                new HttpEntity<>(map, headers);

        // 发送post请求，并打印结果，以String类型接收响应结果JSON字符串
        String result = restTemplate.postForObject(url, request, String.class);
        JSONObject resultJson = new JSONObject(result);
        if (Objects.equals(resultJson.getInt("status"), DataStateEnum.ENABLE.getCode())) {
            //成功
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }


    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/heBei")
    @ApiOperation("校验河北token有效性")
    @ApiImplicitParam(name = "token", required = true)
    public HttpResult<Object> heBei(String token) {
        RestTemplate restTemplate = new RestTemplate();
        String methodDescribe = getMethodDescribe("heBei");
        LogUtil.njcnDebug(log, "{}，token：{}", methodDescribe, token);

     /*   // 请求地址
        String url = "http://dwzyywzt-test.com/baseCenter/oauth2/user/token";

        // 请求头设置,x-www-form-urlencoded格式的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        //提交参数设置
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("token", token);

        // 组装请求体
        HttpEntity<MultiValueMap<String, String>> request =
                new HttpEntity<>(map, headers);

        // 发送post请求，并打印结果，以String类型接收响应结果JSON字符串
        String result = restTemplate.postForObject(url, request, String.class);
        JSONObject resultJson = new JSONObject(result);
        if (Objects.equals(resultJson.getInt("status"), DataStateEnum.ENABLE.getCode())) {
            //成功
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }*/


        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "HE_DNZLJCYW", methodDescribe);

    }

}
