zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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;
    }
}