package com.yifu.cloud.plus.v1.yifu.insurances.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.TInsuranceSettlePushParam;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.File;

/**
 * @author zhaji
 * @description EKP交互工具类
 *
 * @date 2022-08-05 09:42:33
 */
public class EKPUtil {

	/**
	 * 多层级的VO对象，且包含上传功能的样例
	 * 注意key的书写格式,类似EL表达式的方式，属性关系用'.', 列表和数组关系用[]，Map关系用["xxx"]
	 */
	public static void testAddNewsInRestTemplate() throws Exception{
		RestTemplate yourRestTemplate = new RestTemplate();
		TInsuranceSettlePushParam formValues = new TInsuranceSettlePushParam();
		String jsonObject = new ObjectMapper().writeValueAsString(formValues);
		//指向EKP的接口url
		String url = "http://119.96.227.251:8080/api/sys-modeling/appModelRestService/addModel";
		//把ModelingAppModelParameterAddForm转换成MultiValueMap
		MultiValueMap<String,Object> wholeForm = new LinkedMultiValueMap<>();
		wholeForm.add("docSubject", new String("接口发起流程".getBytes("UTF-8"),"ISO-8859-1") );
		wholeForm.add("docCreator", "{\"LoginName\":\"admin\"}");
		wholeForm.add("docStatus", 20);
		wholeForm.add("fdModelId", "181d73279372e5a55438a47d7436ab7e");
		wholeForm.add("fdFlowId", "18267f206233f29cbd3c5ee425c9408a");
		wholeForm.add("formValues", new String("{\"fd_3adfe6af71a1cc\":\"王五\", \"fd_3adfe658c6229e\":\"2019-03-26\", \"fd_3adfe6592b4158\":\"这里内容\"}".getBytes("UTF-8"),"ISO-8859-1") );
		//wholeForm.add("formValues", new String("{\"fd_3adfe6af71a1cc\":\"王五\", \"fd_3adfe658c6229e\":\"2019-03-26\", \"fd_3adfe6592b4158\":\"这里内容\"}".getBytes("UTF-8"),"ISO-8859-1") );

		HttpHeaders headers = new HttpHeaders();
		//如果EKP对该接口启用了Basic认证，那么客户端需要加入
		//addAuth(headers,"yourAccount"+":"+"yourPassword");是VO，则使用APPLICATION_JSON
		headers.setContentType(MediaType.MULTIPART_FORM_DATA);
		//必须设置上传类型，如果入参是字符串，使用MediaType.TEXT_PLAIN；如果
		HttpEntity<MultiValueMap<String,Object>> entity = new HttpEntity<MultiValueMap<String,Object>>(wholeForm,headers);

		//有返回值的情况 VO可以替换成具体的JavaBean
		ResponseEntity<String> obj = yourRestTemplate.exchange(url, HttpMethod.POST, entity, String.class);
		String body = obj.getBody();
		System.out.println(body);
	}

}
