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<String,String> 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<MasterDefineEntity> 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<RestResult<List<MasterDefineEntity>>> hc = new HttpClientTool(String.format(ipMap.get("selectAll"), ipAndPortEntity.getIp()));
|
hc.setBodyInfos(masterData);
|
RestResult<List<MasterDefineEntity>> result = hc.sendPost(
|
j->JSONTool.toObj(j, new TypeReference<RestResult<List<MasterDefineEntity>>>(){}));
|
return GetFormClientResult.getRightResult(result);
|
}
|
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
@Override
|
public List<MasterElementEntity> queryClientMasterElements(MasterData masterData) throws Exception {
|
IpAndPortEntity ipAndPortEntity = ipAndPortTool.queryIpAndPortByServiceName(autoFormClient);
|
HttpClientTool<RestResult<List<MasterElementEntity>>> hc = new HttpClientTool(String.format(ipMap.get("search"), ipAndPortEntity.getIp()));
|
hc.setBodyInfos(masterData);
|
RestResult<List<MasterElementEntity>> result = hc.sendPost(
|
j->JSONTool.toObj(j, new TypeReference<RestResult<List<MasterElementEntity>>>(){}));
|
return GetFormClientResult.getRightResult(result);
|
}
|
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
@Override
|
public List<CodeAndValueBean> 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<RestResult<List<CodeAndValueBean>>> hc = new HttpClientTool(String.format(ipMap.get("mainElements"), ipAndPortEntity.getIp()));
|
hc.setBodyInfos(masterValue);
|
Map<String,Object> m = getCookies();
|
System.out.println("heard:"+JSONTool.toJson(m));
|
hc.setCookie(m);
|
RestResult<List<CodeAndValueBean>> result = hc.sendPost(
|
j->JSONTool.toObj(j, new TypeReference<RestResult<List<CodeAndValueBean>>>(){}.getType()));
|
return (List<CodeAndValueBean>)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<RestResult<CodeAndValueBean>> hc = new HttpClientTool<RestResult<CodeAndValueBean>>(String.format(ipMap.get("codeToValue"), ipAndPortEntity.getIp()));
|
hc.setBodyInfos(masterValue);
|
RestResult<CodeAndValueBean> result = hc.sendPost(
|
j->JSONTool.toObj(j, new TypeReference<RestResult<CodeAndValueBean>>(){}));
|
return GetFormClientResult.getRightResult(result);
|
}
|
|
//下面是之前的方法
|
// public List<MasterValueEntity> queryClientMasterValues(MasterValue masterValue) throws Exception {
|
// System.err.println(JSONTool.toJson(masterValue));
|
// IpAndPortEntity ipAndPortEntity = ipAndPortTool.queryIpAndPortByServiceName(autoFormClient);
|
// HttpClientTool<RestResult<List<MasterValueEntity>>> hc = new HttpClientTool(String.format(ipMap.get("mainElements"), ipAndPortEntity.getIp()));
|
// hc.setBodyInfos(masterValue);
|
// RestResult<List<MasterValueEntity>> result = hc.sendPost(
|
// j->JSONTool.toObj(j, new TypeReference<RestResult<List<MasterValueEntity>>>(){}));
|
// return GetFormClientResult.getRightResult(result);
|
//
|
// }
|
|
public Map<String,Object> getCookies(){
|
Map<String,Object> map = new HashMap<String,Object>();
|
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;
|
}
|
}
|