package org.springblade.healthcode.controller;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.core.tool.api.R;
import org.springblade.healthcode.Dto.HealthCodeDto;
import org.springblade.healthcode.vo.HealthCodeVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/health")
@Api(tags = "全国健康码")
public class HealthCodeController {

	@Value("${healthcode.productId}")
	private  String  PRODUCT_ID;		//平台服务id
	@Value("${healthcode.productSecret}")
	private  String  PRODUCT_SECRET;		//平台服务秘钥

	private  String CODE_URL = "http://120.92.153.151:3000/hio/api/health_code/info";

	@PostMapping("/getCode")
	@ApiOperation("获取健康码")
	public R getHealthCode(@RequestBody HealthCodeDto healthCodeDto){
		//发送API请求
		String result = HttpRequest.get(CODE_URL)
			.header(Header.CONTENT_TYPE, "application/json")
			.header("ProductKey",PRODUCT_ID)
			.header("ProductSecret", PRODUCT_SECRET)
			.form("name",healthCodeDto.getUserName())
			.form("id_card",healthCodeDto.getIdCardNumber())
			.execute().body();

		//解析响应报文
		JSONObject rsObject = JSON.parseObject(result);
		return "ok".equals(rsObject.getString("status"))?
			R.data(
				new HealthCodeVO.HealthCodeVOBuilder()
				.withCode(rsObject.getJSONObject("data").getString("image_url"))
				.withColor(rsObject.getJSONObject("data").getString("color"))
				.build()
			)
			:R.fail(rsObject.getJSONObject("error").getString("message"));
	}

/*	public static void main(String[] args) {
		String result = HttpRequest.get(CODE_URL)
			.header(Header.CONTENT_TYPE, "application/json")
			.header("ProductKey","19fccd26af234791" )
			.header("ProductSecret", "bb21153d067446d5")
			.form("name","梁汉强")
			.form("id_card","440981199612222852")
			.execute().body();
		System.out.println(result);
	}*/
}
