package com.changhong.epc.form.service.field.impl; import com.alibaba.fastjson.TypeReference; import com.changhong.epc.bean.form.FormBaseEntity; import com.changhong.epc.bean.form.FormKey; import com.changhong.epc.bean.parsing.submit.FormulaParam; import com.changhong.epc.bean.parsing.submit.FormulaResult; import com.changhong.epc.constter.base.BaseConst; import com.changhong.epc.constter.base.Context; import com.changhong.epc.constter.system.prop.ChangHongProperties; import com.changhong.epc.form.service.field.FormFieldService; import com.changhong.epc.form.tool.HttpClientTool; import com.iemsoft.framework.cloud.core.base.ResMsg; import com.iemsoft.framework.cloud.core.thread.Keys; import com.iemsoft.framework.cloud.core.thread.ThreadData; import com.iemsoft.framework.cloud.core.tools.JSONTool; import com.iemsoft.framework.cloud.core.tools.http.Http; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.TimeoutException; @Service("formFieldService") @SuppressWarnings({ "unchecked", "rawtypes" }) public class FormFieldServiceImpl implements FormFieldService{ private static final Map ipMap = new HashMap<>(); static { ipMap.put("getFormInfo", "http://%s:%s/autoForm/formbase/getFormInfoService"); ipMap.put("updateFormInfo", "http://%s:%s/autoForm/datamanagement/updateFormDataService"); ipMap.put("getFormHaveSub", "http://%s:%s/autoForm/formbase/getSubFormAlias"); ipMap.put("getAssFormId", "http://%s:%s/autoForm/formbase/getAssFormId"); ipMap.put("getAssociateData", "http://%s:%s/autoForm/epc/getAssociateData"); ipMap.put("getFormula", "http://%s:%s/autoForm/epc/getFormula"); ipMap.put("getAutoNumber", "http://%s:%s/autoForm/datamanagement/getAotoNumber"); } @Resource public ChangHongProperties changHongProperties; @Override public FormBaseEntity selectFormFieldProperty(FormKey formKey) { // HttpClientTool hc = new HttpClientTool(String.format(ipMap.get("getFormInfo"), "116.62.198.226", "3389") + "?formID=" + formKey.getFormId() +"&tenantID=" + formKey.getTenantId()).setResultClazz(Map.class); // HttpClientTool hc = new HttpClientTool(String.format(ipMap.get("getFormInfo"), "116.62.198.226", "3389") + "?formID=" + formKey.getFormId() +"&tenantID=" + formKey.getTenantId()).setResultClazz(Map.class); String format = String.format(ipMap.get("getFormInfo"), changHongProperties.getFormServiceUrl(), changHongProperties.getFormServicePost()); return (FormBaseEntity) Http.get(format, ResMsg.class) .addUrlParam("formID", formKey.getFormId()) .addUrlParam("tenantID", formKey.getTenantId()) .addHeader("Cookie", mapToStrByCookie(getCookies())) .execute(new TypeReference>(){}.getType()) .map(ResMsg::getData) .orElse(new FormBaseEntity()); } private String mapToStrByCookie(Map cookies){ StringBuilder str = new StringBuilder(); for (Map.Entry eachCookie : cookies.entrySet()) { if(str.length() == 0){ str.append(eachCookie.getKey()).append('=').append( eachCookie.getValue() instanceof String ? eachCookie.getValue() : JSONTool.toJson(eachCookie.getValue())); }else{ str.append(';').append(eachCookie.getKey()).append('=').append( eachCookie.getValue() instanceof String ? eachCookie.getValue() : JSONTool.toJson(eachCookie.getValue())); } } return str.toString(); } @Override public Object updateFormFieldProperty(Map data) { String format = String.format(ipMap.get("updateFormInfo"), changHongProperties.getFormServiceUrl(), changHongProperties.getFormServicePost()); HttpClientTool hc = new HttpClientTool(format).setResultClazz(Map.class); hc.setBodyInfos(data); hc.setCookie(getCookies()); Object object = null; try { object = hc.sendPost().get("data"); } catch (TimeoutException e) { return false; } return true; } @Override public List> getFormHaveSub(Map param) { String format = String.format(ipMap.get("getFormHaveSub"), changHongProperties.getFormServiceUrl(), changHongProperties.getFormServicePost()); HttpClientTool> hc = new HttpClientTool(format).setResultClazz(Map.class); hc.setBodyInfos(param); hc.setCookie(getCookies()); try{ return (List>) hc.sendPost().get("data"); }catch (TimeoutException e) { // System.err.println(e); } return null; } @Override public String getAssFormId(Map param) { String format = String.format(ipMap.get("getAssFormId"), changHongProperties.getFormServiceUrl(), changHongProperties.getFormServicePost()); HttpClientTool hc = new HttpClientTool(format).setResultClazz(Map.class); hc.setBodyInfos(param); hc.setCookie(getCookies()); try{ return (String) hc.sendPost().get("data"); }catch (TimeoutException e) { // System.err.println(e); } return null; } @Override public List> getAssociateData(List> param, String str, String str1) { String format = String.format(ipMap.get("getAssociateData"), changHongProperties.getFormServiceUrl(), changHongProperties.getFormServicePost()); Map map = new HashMap(); map.put("associateData", param); HttpClientTool>>> hc = new HttpClientTool(format+ "?formID=" + str + "&tenantID=" + str1+"&companyId="+ThreadData.get(Keys.COMPANY_ID)).setResultClazz(Map.class); hc.setBodyInfos(map); hc.setCookie(getCookies()); try{ return (List>) hc.sendPost().get("data"); }catch (TimeoutException e) { // System.err.println(e); } return null; } @Override public FormulaResult getFormula(FormulaParam formulaParam) { String format = String.format(ipMap.get("getFormula"), changHongProperties.getFormServiceUrl(), changHongProperties.getFormServicePost()); HttpClientTool hc = new HttpClientTool(format).setResultClazz(Map.class); hc.setBodyInfos(formulaParam); hc.setCookie(getCookies()); try{ return (FormulaResult) hc.sendPost().get("data"); }catch (TimeoutException e) { // System.err.println(e.getMessage()); } return null; } @Override public String getAutoNumber(Map param) { String format = String.format(ipMap.get("getAutoNumber"), changHongProperties.getFormServiceUrl(), changHongProperties.getFormServicePost()); HttpClientTool hc = new HttpClientTool(format).setResultClazz(Map.class); hc.setBodyInfos(param); hc.setCookie(getCookies()); try{ return (String) hc.sendPost().get("data"); }catch (TimeoutException e) { // System.err.println(e.getMessage()); } return null; } private static final String SY = "epc9031"; public Map getCookies(){ Map map = new HashMap(); map.put("companyId", ThreadData.get(Keys.COMPANY_ID)); // System.out.println("当前token值:"+ThreadData.get(Keys.TOKEN)); // System.out.println("当前contoken值:"+Context.getToken()); if(Context.getToken()!=null && !Objects.equals("",Context.getToken())) { map.put("token", Context.getToken()); }else{ map.put("token", SY); } map.put("tenantId", ThreadData.get(Keys.TENANT_ID)); map.put("openId", ThreadData.get(BaseConst.OPEN_ID)); // System.out.println("Cookie访问业务系统:"+JSONTool.toJson(map)); return map; } }