kimi
2020-05-27 c007f0ca1785db093d48f4846cda82fe8e955765
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.highdatas.mdm.util;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.datacvg.common.DesensitizationUtils;
import com.highdatas.mdm.entity.AntianaphylaxisResult;
import com.highdatas.mdm.entity.AntianaphylaxisType;
import com.highdatas.mdm.entity.BlurryType;
import com.highdatas.mdm.entity.SysField;
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.StringUtils;
import org.springframework.web.client.RestTemplate;
 
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
 
@ConfigurationProperties(prefix = "antianaphy")
@Component
public class AntianaphylaxisClient {
 
    @Value("${antianaphy.url}")
    String url;
 
    String prefix;
 
    private AntianaphylaxisClient() {
        this.prefix = "/api/datacvg/desensitization/";
    }
 
    public boolean putRedisVal(String key, String value) {
        String url = this.url + prefix + "put";
        Map<String, String> params = new LinkedHashMap<>();
        params.put("key", key);
        params.put("value", value);
        params.put("type", Constant.MainData);
        String s = HttpUtils.HttpRestClient(url, HttpMethod.POST, params);
        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 void fixMasterData(List<Map<String, Object>> datas, Map<String, AntianaphylaxisResult> fixField) {
        if (fixField == null) {
            return;
        }
        for (Map<String, Object> data : datas) {
            fixOneData(data, fixField);
        }
    }
 
    public void fixOneData(Map<String, Object> data, Map<String, AntianaphylaxisResult> fixField) {
        Set<String> keySet = data.keySet();
        Set<String> fixKeys = fixField.keySet();
        com.datacvg.common.DesensitizationUtils desensitizationUtils = new DesensitizationUtils();
        for (String field : keySet) {
            if (!fixKeys.contains(field)) {
                continue;
            }
            Object o = data.get(field);
            AntianaphylaxisResult result = fixField.get(field);
            AntianaphylaxisType type = result.getType();
            Object changed = null;
            try {
                if (AntianaphylaxisType.Blurry.equals(type)) {
                    BlurryType initialDirection = result.getInitialDirection();
                    if (BlurryType.left.equals(initialDirection)) {
                        changed = desensitizationUtils.leftBlurry(o.toString(), Constant.Star, result.getNumber());
                    } else if (BlurryType.right.equals(initialDirection)) {
                        changed = desensitizationUtils.leftBlurry(o.toString(), Constant.Star, result.getNumber());
                    } else {
                        changed = desensitizationUtils.middleBlurry(o.toString(), result.getInitialPosition(), result.getNumber(), Constant.Star);
                    }
                } else if (AntianaphylaxisType.MD5.equals(type)) {
                    changed = desensitizationUtils.md5(o.toString());
 
                } else {
                    changed = o;
                }
            } catch (Exception e) {
                e.printStackTrace();
                changed = o;
            }
 
            data.put(field, changed);
        }
    }
 
    public Map<String, AntianaphylaxisResult> getHelpfulField(String fields, String tableName) {
        try {
            List<String> fieldList = DbUtils.split(fields, Constant.COMMA);
            List<AntianaphylaxisResult> antainList = getAntainList(tableName);
            if (antainList == null) {
                return null;
            }
            return antainList.stream().filter(antianaphylaxisResult -> fieldList.contains(antianaphylaxisResult.getFieldName())).collect(Collectors.toMap(one -> one.getFieldName(), one -> one));
 
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    public Map<String, AntianaphylaxisResult> getHelpfulField(List<String> fieldList, String tableName) {
        List<AntianaphylaxisResult> antainList = getAntainList(tableName);
        return antainList.stream().filter(antianaphylaxisResult -> fieldList.contains(antianaphylaxisResult.getFieldName())).collect(Collectors.toMap(one -> one.getFieldName(), one -> one));
    }
 
    public void getHelpfulFieldBySysField(List<SysField> fieldList, String tableName) {
        List<AntianaphylaxisResult> antainList = getAntainList(tableName);
        if (antainList == null) {
            return;
        }
        for (SysField sysField : fieldList) {
            String field = sysField.getField();
 
            List<AntianaphylaxisResult> collect = antainList.stream()
                    .filter(one -> !StringUtils.isEmpty(one.getFieldName()))
                    .filter(one -> one.getFieldName().equalsIgnoreCase(field)).collect(Collectors.toList());
            if (collect.isEmpty()) {
                continue;
            }
            AntianaphylaxisResult result = collect.get(0);
            sysField.setDesensitizationType(AntianaphylaxisType.parse(result.getDesensitizationType()));
            sysField.setNumber(result.getNumber());
            sysField.setInitialDirection(result.getInitialDirection());
            sysField.setInitialPosition(result.getInitialPosition());
        }
    }
 
    public List<AntianaphylaxisResult> getAntainList(String tableName) {
        String url = this.url + prefix + "getRuleNameAndId";
        HashMap<String, Object> params = new LinkedHashMap<>();
        params.put("tableName", tableName);
 
        String s = HttpRestClient(url, HttpMethod.POST, params, null, MediaType.APPLICATION_JSON);
        JSONObject result = (JSONObject) JSON.parse(s);
        String sucess = result.getString(Constant.Success);
        if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
            return null;
        } else {
 
            return JSONObject.parseArray(result.getString(Constant.Data), AntianaphylaxisResult.class);
        }
 
    }
 
    public String HttpRestClient(String url, HttpMethod method, HashMap<String, Object> formParams, String getParams, MediaType mediaType) {
        if (!StringUtils.isEmpty(getParams)) {
            url = url + Constant.QUESTION + getParams;
        }
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(10 * 1000);
        requestFactory.setReadTimeout(10 * 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, Object>> requestEntity = new HttpEntity<HashMap<String, Object>>(formParams, headers);
        //  执行HTTP请求
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
 
        return response.getBody();
    }
}