package com.sptg.permissions.biz.controller;

import cn.hutool.json.JSONUtil;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.permissions.api.conf.CustInfoVerifyConfig;
import com.sptg.permissions.api.entity.CustInfoResponse;
import com.sptg.permissions.api.entity.ApiLog;
import com.sptg.permissions.api.entity.CustInfoVerify;
import com.sptg.permissions.api.feigns.SysApiLogFeign;
import com.sptg.permissions.biz.service.SysApiLogServiceInterface;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * title: SysApiLogController
 * package: com.sptg.permissions.biz.controller
 * description: 系统接口调用日志-控制器
 *
 *                  
 *                
 */
@RestController
@RequiredArgsConstructor
public class SysApiLogController implements SysApiLogFeign {

    private final SysApiLogServiceInterface sysApiLogServiceInterface;

    /* -------------------------------------------------------------------------------------------------------------- */

    @Override
    @PostMapping(value = "/sysApiLog/create")
    public ResultBean<Boolean> create(@RequestBody ApiLog apiLog) {
        boolean insert = sysApiLogServiceInterface.insert(apiLog);
        if(insert){
            return new ResultBean<>(ResultEnum.SUCCESS, true);
        }else{
            return new ResultBean<>(ResultEnum.FAILURE, false);
        }
    }

    @PostMapping(value = "/sysApiLog/custInfoVerify")
    public CustInfoResponse custInfoVerify(@RequestBody CustInfoVerify custInfoVerify){
        String custName = custInfoVerify.getCustName();
        String certNumber = custInfoVerify.getCertNumber();
        if(!StringUtils.hasText(certNumber) || !StringUtils.hasText(custName)){
            Assert.isTrue(false, "身份证和姓名不能为空");
        }
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        String uuid = UUID.randomUUID().toString();
        CustInfoVerifyConfig config = new CustInfoVerifyConfig();
        headers.add("X-APP-ID", config.getAppId());
        headers.add("X-APP-KEY", config.getAppKey());
        headers.add("X-CTG-Request-ID",uuid);
        headers.add("Content-Type", "text/xml;charset=utf-8");
        Map<String, Object> logMap = new HashMap<>();
        logMap.put("header", headers);
        logMap.put("body", JsonUtils.objectToJson(custInfoVerify));
        HttpEntity<String> requestEntity = new HttpEntity<>(JSONUtil.toJsonStr(custInfoVerify), headers);
        ParameterizedTypeReference<CustInfoResponse> reference = new ParameterizedTypeReference<CustInfoResponse>() {};
        String url = config.getHost();
        URI uri = URI.create(url);
        logMap.put("url", uri);
        ResponseEntity<CustInfoResponse> result = null;
        try {
            result = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);
            logMap.put("result", result.getBody());
        }catch (Exception e){
            logMap.put("result", e.getMessage());
        }
        CustInfoResponse response = result.getBody();
//        ApiLog apiLog = new ApiLog("custInfoVerify", "身份证验证", JsonUtils.objectToJson(logMap), "小程序/后台");
//        sysApiLogServiceInterface.insert(apiLog);
        return response;
    }
}
