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 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> datas, Map fixField) { if (fixField == null) { return; } for (Map data : datas) { fixOneData(data, fixField); } } public void fixOneData(Map data, Map fixField) { Set keySet = data.keySet(); Set 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 getHelpfulField(String fields, String tableName) { try { List fieldList = DbUtils.split(fields, Constant.COMMA); List 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 getHelpfulField(List fieldList, String tableName) { List antainList = getAntainList(tableName); return antainList.stream().filter(antianaphylaxisResult -> fieldList.contains(antianaphylaxisResult.getFieldName())).collect(Collectors.toMap(one -> one.getFieldName(), one -> one)); } public void getHelpfulFieldBySysField(List fieldList, String tableName) { List antainList = getAntainList(tableName); if (antainList == null) { return; } for (SysField sysField : fieldList) { String field = sysField.getField(); List 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 getAntainList(String tableName) { String url = this.url + prefix + "getRuleNameAndId"; HashMap 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 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> requestEntity = new HttpEntity>(formParams, headers); // 执行HTTP请求 ResponseEntity response = client.exchange(url, method, requestEntity, String.class); return response.getBody(); } }