package org.springblade.utils;

import lombok.AllArgsConstructor;
import org.springblade.config.ChinaNetConfig;
import org.springblade.core.log.logger.BladeLogger;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.utils.entity.CustInfoEntity;
import org.springblade.utils.entity.CustInfoResponse;
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.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;

/**
 * Created By lidexi in 2021/1/26
 **/
@RestController
@AllArgsConstructor
@RequestMapping("custInfoVerify")
public class CustInfoVerifyUtil {

	private BladeLogger bladeLogger;
	private ChinaNetConfig chinaNetConfig;

	@PostMapping("/verify")
	public R<Boolean> custInfoVerify(@RequestBody CustInfoEntity custInfoEntity){
		String certNumber = custInfoEntity.getCertNumber();
		String custName = custInfoEntity.getCustName();
		if(StringUtil.isBlank(certNumber) || StringUtil.isBlank(custName)){
			return R.fail("身份证和姓名不能为空");
		}
		Boolean verify = verify(custName, certNumber, custInfoEntity.getCertType());
		if(!verify){
			return R.data(false, "身份证校验失败，请输入正确的身份证信息");
		}else{
			return R.data(true,"身份证校验成功");
		}
	}

	public Boolean verify(String custName, String certNumber, String certType){
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		String uuid = UUID.randomUUID().toString();
		headers.add("X-APP-ID",chinaNetConfig.getCrmCustInfoVerifyAppId());
		headers.add("X-APP-KEY",chinaNetConfig.getCrmCustInfoVerifyAppKey());
		headers.add("X-CTG-Request-ID",uuid);
		headers.add("Content-Type", "application/json");
		Map<String, Object> logMap = new HashMap<>();
		Map<String, Object> map = new HashMap<>();
		map.put("custName", custName);
		map.put("certNumber", certNumber);
		map.put("certType", certType);
		logMap.put("header", headers);
		logMap.put("body", map);
		HttpEntity<String> requestEntity = new HttpEntity<String>(JsonUtil.toJson(map), headers);
		ParameterizedTypeReference<CustInfoResponse> reference = new ParameterizedTypeReference<CustInfoResponse>() {};
		String url = chinaNetConfig.getCrmCustInfoVerifyUrl();
		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());
		}
		bladeLogger.info("custInfoVerify", JsonUtil.toJson(logMap));
		CustInfoResponse custInfoResponse = result.getBody();
		String code = custInfoResponse.getCode();
		if(!"0".equals(code)){
			return false;
		}else{
			return true;
		}
	}
}
