package cn.autoform.web.client.impl; import java.util.*; import java.util.concurrent.TimeoutException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.alibaba.fastjson.TypeReference; import cn.autoform.bean.CodeAndValueBean; import cn.autoform.db.entity.IpAndPortEntity; import cn.autoform.db.entity.MasterDefineEntity; import cn.autoform.db.entity.MasterElementEntity; import cn.autoform.db.entity.MasterValueEntity; import cn.autoform.db.exten.MasterData; import cn.autoform.db.exten.MasterValue; import cn.autoform.fw.exception.FormClientException; import cn.autoform.fw.exception.RestResult; import cn.autoform.util.thread.Keys; import cn.autoform.util.thread.ThreadData; import cn.autoform.util.tool.GetFormClientResult; import cn.autoform.util.tool.JSONTool; import cn.autoform.web.client.MasterClient; import cn.autoform.web.client.util.HttpClientTool; import cn.autoform.web.client.util.IpAndPortTool; import cn.autoform.web.mapper.masterdata.MasterDataMapper; @Service public class MasterClientImpl implements MasterClient { private static final Map ipMap = new HashMap<>(); @Value("${autoForm.businessSystem}") private String autoFormClient; static { ipMap.put("selectAll", "%s/epc/epc-form"+FormClientImpl.CLIENT_CONTEXT_PATH+"/define/selectAll"); ipMap.put("search", "%s/epc/epc-form"+FormClientImpl.CLIENT_CONTEXT_PATH+"/define/search"); ipMap.put("mainElements", "%s/epc/epc-form"+FormClientImpl.CLIENT_CONTEXT_PATH+"/select/mainElements"); ipMap.put("codeToValue", "%s/epc/epc-form"+FormClientImpl.CLIENT_CONTEXT_PATH+"/select/codeToValue"); // ipMap.put("selectAll", "%s/define/selectAll"); // ipMap.put("search", "%s/define/search"); // ipMap.put("mainElements", "%s/select/mainElements"); } @Autowired MasterDataMapper masterDataMapper = null; @Autowired IpAndPortTool ipAndPortTool; @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public List queryClientMasterDefines(MasterData masterData) throws TimeoutException, FormClientException { System.err.println(JSONTool.toJson(masterData)); IpAndPortEntity ipAndPortEntity = ipAndPortTool.queryIpAndPortByServiceName(autoFormClient); System.err.println(ipAndPortEntity.getIp() + ":" + ipAndPortEntity.getPort()); HttpClientTool>> hc = new HttpClientTool(String.format(ipMap.get("selectAll"), ipAndPortEntity.getIp())); hc.setBodyInfos(masterData); RestResult> result = hc.sendPost( j->JSONTool.toObj(j, new TypeReference>>(){})); return GetFormClientResult.getRightResult(result); } @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public List queryClientMasterElements(MasterData masterData) throws Exception { IpAndPortEntity ipAndPortEntity = ipAndPortTool.queryIpAndPortByServiceName(autoFormClient); HttpClientTool>> hc = new HttpClientTool(String.format(ipMap.get("search"), ipAndPortEntity.getIp())); hc.setBodyInfos(masterData); RestResult> result = hc.sendPost( j->JSONTool.toObj(j, new TypeReference>>(){})); return GetFormClientResult.getRightResult(result); } @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public List queryClientMasterValues(MasterValue masterValue) throws Exception { System.err.println(JSONTool.toJson(masterValue)); System.out.println("公司ID"+ThreadData.get(Keys.COMPANY_ID)); IpAndPortEntity ipAndPortEntity = ipAndPortTool.queryIpAndPortByServiceName(autoFormClient); HttpClientTool>> hc = new HttpClientTool(String.format(ipMap.get("mainElements"), ipAndPortEntity.getIp())); hc.setBodyInfos(masterValue); Map m = getCookies(); System.out.println("heard:"+JSONTool.toJson(m)); hc.setCookie(m); RestResult> result = hc.sendPost( j->JSONTool.toObj(j, new TypeReference>>(){}.getType())); return (List)Optional.ofNullable(GetFormClientResult.getRightResult(result)).orElse(new ArrayList<>()); } @Override public CodeAndValueBean masterValueCodeToName(MasterValue masterValue) throws Exception { System.err.println(JSONTool.toJson(masterValue)); IpAndPortEntity ipAndPortEntity = ipAndPortTool.queryIpAndPortByServiceName(autoFormClient); System.out.println("ip:"+JSONTool.toJson(ipAndPortEntity)); HttpClientTool> hc = new HttpClientTool>(String.format(ipMap.get("codeToValue"), ipAndPortEntity.getIp())); hc.setBodyInfos(masterValue); RestResult result = hc.sendPost( j->JSONTool.toObj(j, new TypeReference>(){})); return GetFormClientResult.getRightResult(result); } //下面是之前的方法 // public List queryClientMasterValues(MasterValue masterValue) throws Exception { // System.err.println(JSONTool.toJson(masterValue)); // IpAndPortEntity ipAndPortEntity = ipAndPortTool.queryIpAndPortByServiceName(autoFormClient); // HttpClientTool>> hc = new HttpClientTool(String.format(ipMap.get("mainElements"), ipAndPortEntity.getIp())); // hc.setBodyInfos(masterValue); // RestResult> result = hc.sendPost( // j->JSONTool.toObj(j, new TypeReference>>(){})); // return GetFormClientResult.getRightResult(result); // // } public Map getCookies(){ Map map = new HashMap(); map.put("companyId", ThreadData.get(Keys.COMPANY_ID)); map.put("openId", ThreadData.get(Keys.OPEN_ID)); map.put("token", ThreadData.get(Keys.TOKEN)); map.put("tenantId", ThreadData.get(Keys.TENANT_ID)); System.out.println("Cookie访问业务系统:"+JSONTool.toJson(map)); return map; } }