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

import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.EKPInteractiveParam;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.TInsuranceSettlePushParam;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;


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

	@Getter
	public static String url;

	@Getter
	public static String LoginName;

	@Getter
	private static String fdModelId;

	@Getter
	private static String fdFlowId;

	@Getter
	private static String docStatus;

	@Getter
	private static String docSubject;

	@Value("${ekp.url}")
	public void setUrl(String db) {
		url = db;
	}

	@Value("${ekp.LoginName}")
	public void setLoginName(String db) {
		LoginName = db;
	}

	@Value("${ekp.fdModelId}")
	public void setFdModelId(String db) {
		fdModelId = db;
	}

	@Value("${ekp.fdFlowId}")
	public void setFdFlowId(String db) {
		fdFlowId = db;
	}

	@Value("${ekp.docStatus}")
	public void setDocStatus(String db) {
		docStatus = db;
	}

	@Value("${ekp.docSubject}")
	public void setDocSubject(String db) {
		docSubject = db;
	}


	/**
	 * 多层级的VO对象，且包含上传功能的样例
	 * 注意key的书写格式,类似EL表达式的方式，属性关系用'.', 列表和数组关系用[]，Map关系用["xxx"]
	 */
	public static String sendToEKP(EKPInteractiveParam param){
		System.out.println("开始推送EKP");
		RestTemplate yourRestTemplate = new RestTemplate();
		TInsuranceSettlePushParam pushParam = InsuranceDetail2PushParam(param);
		try{
			String formValues = new ObjectMapper().writeValueAsString(pushParam);
			//指向EKP的接口url
			//把ModelingAppModelParameterAddForm转换成MultiValueMap
			JSONObject loginName = new JSONObject();
			loginName.append("LoginName",LoginName);
			String loginData = new ObjectMapper().writeValueAsString(loginName);
			MultiValueMap<String,Object> wholeForm = new LinkedMultiValueMap<>();
			//wholeForm.add("docSubject", new String(docSubject.getBytes("UTF-8"),"ISO-8859-1") );
			wholeForm.add("docSubject",docSubject);
			wholeForm.add("docCreator", "{\"LoginName\":\"admin\"}");
			//wholeForm.add("docCreator", loginData);
			wholeForm.add("docStatus", docStatus);
			wholeForm.add("fdModelId", fdModelId);
			wholeForm.add("fdFlowId", fdFlowId);
			//wholeForm.add("formValues", new String(formValues.getBytes("UTF-8"),"ISO-8859-1"));
			wholeForm.add("formValues", formValues);
			//wholeForm.add("formValues", new String("{\"fd_3adfe6af71a1cc\":\"王五\", \"fd_3adfe658c6229e\":\"2019-03-26\", \"fd_3adfe6592b4158\":\"这里内容\"}".getBytes("UTF-8"),"ISO-8859-1") );
			System.out.println("wholeForm:"+wholeForm);
			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
			System.out.println("交易开始");
			ResponseEntity<String> obj = yourRestTemplate.exchange(url, HttpMethod.POST, entity, String.class);
			System.out.println("交易结束");
			System.out.println("obj"+obj);
			String body = obj.getBody();
			if (StringUtils.isBlank(body)){
				System.out.println("交易失败"+body);
				return body;
			}else{
				System.out.println("交易成功"+body);
				return body;
			}
		}catch (Exception e){
			log.info(e.toString());
			return e.getMessage();
		}
	}

	/**
	 * 将类转换成EKP要求的格式
	 *
	 * @author zhaji
	 * @param param 转换类
	 * @return {@link TInsuranceSettlePushParam}
	 */
	public static TInsuranceSettlePushParam InsuranceDetail2PushParam(EKPInteractiveParam param){
			String format = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
			TInsuranceSettlePushParam pushParam = new TInsuranceSettlePushParam();
			//ekpId
			if(null != param.getId() && null != param.getDefaultSettleId()){
				pushParam.setFd_3afa8a70006bea(param.getId()+CommonConstants.DOWN_LINE_STRING+param.getDefaultSettleId());
			}else{
				pushParam.setFd_3afa8a70006bea(CommonConstants.EMPTY_STRING);
			}
			//单据类型
			if(null != param.getSettleType()){
				pushParam.setFd_3adfe6af71a1cc(param.getSettleType());
			}else{
				pushParam.setFd_3adfe6af71a1cc(CommonConstants.ZERO_INT);
			}
			//项目编码
			pushParam.setFd_3adfe658c6229e(param.getDeptNo());
			if(null != param.getDeptNo()){
				pushParam.setFd_3adfe658c6229e(param.getDeptNo());
			}else{
				pushParam.setFd_3adfe658c6229e(CommonConstants.EMPTY_STRING);
			}
			//项目名称
			pushParam.setFd_3adfe6592b4158(param.getDeptName());
			if(null != param.getDeptName()){
				pushParam.setFd_3adfe6592b4158(param.getDeptName());
			}else{
				pushParam.setFd_3adfe6592b4158(CommonConstants.EMPTY_STRING);
			}
			//客户编码
			pushParam.setFd_3adfe6598281e8(param.getCustomerCode());
			if(null != param.getCustomerCode()){
				pushParam.setFd_3adfe6598281e8(param.getCustomerCode());
			}else{
				pushParam.setFd_3adfe6598281e8(CommonConstants.EMPTY_STRING);
			}
			//客户名称
			pushParam.setFd_3adfe7a2688902(param.getCustomerName());
			if(null != param.getCustomerName()){
				pushParam.setFd_3adfe7a2688902(param.getCustomerName());
			}else{
				pushParam.setFd_3adfe7a2688902(CommonConstants.EMPTY_STRING);
			}
			//发生日期
			pushParam.setFd_3adfe67c24dace(format);
			//姓名
			pushParam.setFd_3adfe65d759650(param.getEmpName());
			if(null != param.getEmpName()){
				pushParam.setFd_3adfe65d759650(param.getEmpName());
			}else{
				pushParam.setFd_3adfe65d759650(CommonConstants.EMPTY_STRING);
			}
			//身份证号
			pushParam.setFd_3adfe65dbd9f68(param.getEmpIdcardNo());
			if(null != param.getEmpIdcardNo()){
				pushParam.setFd_3adfe65dbd9f68(param.getEmpIdcardNo());
			}else{
				pushParam.setFd_3adfe65dbd9f68(CommonConstants.EMPTY_STRING);
			}
			//发票号
			if(null != param.getInvoiceNo()){
				pushParam.setFd_3adfe65e0cd094(param.getInvoiceNo());
			}else{
				pushParam.setFd_3adfe65e0cd094(CommonConstants.EMPTY_STRING);
			}
			//险种
			if(null != param.getInsuranceTypeName()){
				pushParam.setFd_3adfe65f6599e4(param.getInsuranceTypeName());
			}else{
				pushParam.setFd_3adfe65f6599e4(CommonConstants.EMPTY_STRING);
			}
			//保险公司
			if(null != param.getInsuranceCompanyName()){
				pushParam.setFd_3adfe65ea04728(param.getInsuranceCompanyName());
			}else{
				pushParam.setFd_3adfe65ea04728(CommonConstants.EMPTY_STRING);
			}
			//保单号
			if(null != param.getPolicyNo()){
				pushParam.setFd_3adfe65e60e110(param.getPolicyNo());
			}else{
				pushParam.setFd_3adfe65e60e110(CommonConstants.EMPTY_STRING);
			}
			//保险开始日期
			if(null != param.getPolicyStart()){
				pushParam.setFd_3adfe6b7e0ede8(param.getPolicyStart().toString());
			}else{
				pushParam.setFd_3adfe6b7e0ede8(CommonConstants.EMPTY_STRING);
			}
			//保险结束日期
			if(null != param.getPolicyEnd()){
				pushParam.setFd_3adfe6b847bfe6(param.getPolicyEnd().toString());
			}else{
				pushParam.setFd_3adfe6b847bfe6(CommonConstants.EMPTY_STRING);
			}
			//购买标准
			pushParam.setFd_3adfe6d55384c6(param.getBuyStandard());
			if(null != param.getBuyStandard()){
				pushParam.setFd_3adfe6d55384c6(param.getBuyStandard());
			}else{
				pushParam.setFd_3adfe6d55384c6(CommonConstants.EMPTY_STRING);
			}
			//实际保费
			if(null != param.getActualPremium()){
				pushParam.setFd_3adfe6610c0d2c(param.getActualPremium().toString());
			}else{
				pushParam.setFd_3adfe6610c0d2c(CommonConstants.EMPTY_STRING);
			}
			//医保
			if(null != param.getMedicalQuota()){
				pushParam.setFd_3adfe66041a996(param.getMedicalQuota());
			}else{
				pushParam.setFd_3adfe66041a996(CommonConstants.EMPTY_STRING);
			}
			//事故或残疾
			if(null != param.getDieDisableQuota()){
				pushParam.setFd_3adfe6609aa810(param.getDieDisableQuota());
			}else{
				pushParam.setFd_3adfe6609aa810(CommonConstants.EMPTY_STRING);
			}
			//预估保费
			if(null != param.getEstimatePremium()){
				pushParam.setFd_3adfe6e30f2a3c(param.getEstimatePremium().toString());
			}else{
				pushParam.setFd_3adfe6e30f2a3c(CommonConstants.EMPTY_STRING);
			}
			//结算月
			if(null != param.getSettleMonth()){
				pushParam.setFd_3aea2f0180eccc(param.getSettleMonth());
			}else{
				pushParam.setFd_3aea2f0180eccc(CommonConstants.EMPTY_STRING);
			}

			//交易类型
			if(null != param.getInteractiveType()){
				pushParam.setFd_3af9197b31071c(param.getInteractiveType());
			}else{
				pushParam.setFd_3af9197b31071c(CommonConstants.ZERO_INT);
			}

		return pushParam;
	}
	}

