kimi42345
2020-03-17 6c6fdb4db59a2a2343e43ffd73a07f17b057c4fa
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.highdatas.mdm.util;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.highdatas.mdm.entity.SysField;
import com.highdatas.mdm.pojo.CodeMsg;
import com.highdatas.mdm.pojo.Result;
import com.highdatas.mdm.service.ISysFieldService;
import org.jcodings.util.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
 
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
@ConfigurationProperties(prefix = "rule")
@Component
public class RuleClient {
 
    @Value("${rule.url}")
    String url;
    @Autowired
    ISysFieldService fieldService;
    String prefix ;
 
    private RuleClient() {
 
//        this.prefix = "/quality/";
        this.prefix = "/api/datacvg/quality/";
    }
    public HashMap<String,Boolean> execuImmeForCollect(String tableName,String userId) {
        //TODO
        String url = this.url + prefix + "execuImmeForCollect";
        HashMap<String, String> params = new HashMap<>();
        params.put("tableName",tableName);
        params.put("createUserId",tableName);
        String s = HttpRestClient(url, HttpMethod.POST, params, null, null);
        JSONObject result = (JSONObject) JSON.parse(s);
        String sucess = result.getString(Constant.Success);
        JSONObject dataObject = result.getJSONObject(Constant.Data);
        JSONArray jsonArray = dataObject.getJSONArray(Constant.ResultList);
        HashMap<String, Boolean> resultMap = new HashMap<>();
 
        for (Object o : jsonArray) {
            JSONObject oneResult = (JSONObject) o;
            String fieldName = oneResult.getString(Constant.fieldName);
            Boolean checked;
            Integer sucPercent = oneResult.getInteger(Constant.sucPercent);
            if (sucPercent != null && sucPercent == 100) {
                checked = true;
            }else {
                checked = false;
            }
            if (resultMap.containsKey(fieldName)  && !checked) {
                Boolean preChecked = resultMap.get(fieldName);
                if (preChecked) {
                    resultMap.put(fieldName, checked);
                }
            }else {
                resultMap.put(fieldName, checked);
            }
        }
        return resultMap;
    }
 
    public  boolean getExecHistoryByTableName(String tableName, Integer pageNo, Integer pageSize) {
        //TODO
        String url = this.url + prefix + "getExecHistoryByTableName";
        HashMap<String, String> params = new HashMap<>();
        params.put("tableName",tableName);
        params.put("pageNum",pageNo.toString());
        params.put("pageSize",pageSize.toString());
        String s = HttpRestClient(url, HttpMethod.POST, params, null, null);
        JSONObject result = (JSONObject) JSON.parse(s);
        String sucess = result.getString(Constant.Success);
        if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
            return false;
        }else {
            return true;
        }
    }
 
    public Result getRuleByTable(String tableName, String isTemp, String maintainId) {
        String url =  this.url + prefix + "getRuleByTable";
        HashMap<String, String> params = new HashMap<>();
        params.put("tableName",tableName);
        params.put("isTemp",isTemp);
 
        String s = HttpRestClient(url, HttpMethod.POST, params,null, MediaType.APPLICATION_JSON);
        JSONObject result = (JSONObject) JSON.parse(s);
        boolean sucess = result.getBoolean(Constant.Success);
        if (!sucess) {
            return Result.error(CodeMsg.Client_fail);
        }
        JSONArray jsonArray = result.getJSONArray(Constant.Data);
        List<SysField> fields = fieldService.getFieldByTable(tableName);
        HashMap<String, String> fieldMap = new HashMap<>();
        for (SysField field : fields) {
            String alias = field.getAlias();
            String code = field.getField();
            fieldMap.put(code,alias);
        }
        for (Object o : jsonArray) {
            JSONObject one = (JSONObject) o;
            String field = one.getString(Constant.fieldName);
            String alias = fieldMap.get(field);
            if (!StringUtils.isEmpty(alias)){
                one.fluentPut(Constant.fieldName, alias);
            }
        }
        return Result.success(jsonArray);
    }
 
 
    public String HttpRestClient(String url, HttpMethod method, HashMap<String, String> formParams, String getParams,  MediaType mediaType) {
        if (!StringUtils.isEmpty(getParams)) {
            url = url + Constant.QUESTION + getParams;
        }
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(15*1000);
        requestFactory.setReadTimeout(15*1000);
        RestTemplate client = new RestTemplate(requestFactory);
        client.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpHeaders headers = new HttpHeaders();
        if (mediaType == null){
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        }else {
            headers.setContentType(mediaType);
        }
 
        HttpEntity<HashMap<String, String>> requestEntity = new HttpEntity<HashMap<String, String>>(formParams, headers);
        //  执行HTTP请求
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
 
        return response.getBody();
    }
}