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 execuImmeForCollect(String tableName,String userId) { //TODO String url = this.url + prefix + "execuImmeForCollect"; HashMap 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 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 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 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 fields = fieldService.getFieldByTable(tableName); HashMap 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 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> requestEntity = new HttpEntity>(formParams, headers); // 执行HTTP请求 ResponseEntity response = client.exchange(url, method, requestEntity, String.class); return response.getBody(); } }