|
@@ -2,8 +2,12 @@ package com.jkcredit.query.record.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.ciphergateway.ciphersuite.CipherSuiteException;
|
|
|
+import com.ciphergateway.ciphersuite.CipherSuiteMacException;
|
|
|
+import com.ciphergateway.ciphersuite.CipherSuiteUtils;
|
|
|
import com.jkcredit.query.record.constant.CommonConstant;
|
|
|
import com.jkcredit.query.record.model.CommonResponseObject;
|
|
|
+import com.jkcredit.query.record.model.LogObject;
|
|
|
import com.jkcredit.query.record.model.MonthResult;
|
|
|
import com.jkcredit.query.record.service.QueryRecordService;
|
|
|
import com.jkcredit.query.record.util.IndexUtil;
|
|
@@ -23,9 +27,12 @@ import org.elasticsearch.search.SearchHit;
|
|
|
import org.elasticsearch.search.SearchHits;
|
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.xml.bind.DatatypeConverter;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.time.LocalDate;
|
|
@@ -41,10 +48,25 @@ import java.util.concurrent.TimeUnit;
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
public class QueryRecordServiceImpl implements QueryRecordService {
|
|
|
+ @Value("${recordsEncrypt.keyId}")
|
|
|
+ private String keyId;
|
|
|
+ @Value("${recordsEncrypt.metadata}")
|
|
|
+ private String metadata;
|
|
|
+ @Value("${recordsEncrypt.ivStr}")
|
|
|
+ private String ivStr;
|
|
|
+ @Value("${recordsEncrypt.algorithm}")
|
|
|
+ private String algorithm;
|
|
|
+
|
|
|
@Autowired
|
|
|
private RestHighLevelClient esRestClient;
|
|
|
private static SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据月份,车牌号查询
|
|
|
+ * @param plateNumber 车牌号
|
|
|
+ * @param month 月份
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
@Override
|
|
|
public CommonResponseObject monthRecords(String plateNumber, String month) {
|
|
|
String[] indices = {CommonConstant.ES_INDEX + month};
|
|
@@ -52,6 +74,11 @@ public class QueryRecordServiceImpl implements QueryRecordService {
|
|
|
return new CommonResponseObject(CommonConstant.SUCCESS_CODE, JSON.toJSONString(results));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据车牌号,查询12个月的结果
|
|
|
+ * @param plateNumber 车牌号
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
@Override
|
|
|
public CommonResponseObject yearRecords(String plateNumber) {
|
|
|
String[] indices = new String[12];
|
|
@@ -65,6 +92,11 @@ public class QueryRecordServiceImpl implements QueryRecordService {
|
|
|
return new CommonResponseObject(CommonConstant.SUCCESS_CODE, JSON.toJSONString(results));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 查询索引中结果总数
|
|
|
+ * @param month 月份
|
|
|
+ * @return 结果总数
|
|
|
+ */
|
|
|
@Override
|
|
|
public CommonResponseObject countByMonth(String month) {
|
|
|
String index = CommonConstant.ES_INDEX + month;
|
|
@@ -87,6 +119,13 @@ public class QueryRecordServiceImpl implements QueryRecordService {
|
|
|
return new CommonResponseObject(CommonConstant.SUCCESS_CODE, String.valueOf(count));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据车牌号,查询多月结果(小于12个月)
|
|
|
+ * @param plateNumber 车牌号
|
|
|
+ * @param startMonth 起始月份
|
|
|
+ * @param endMonth 结束月份
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
@Override
|
|
|
public CommonResponseObject multipleMonthRecords(String plateNumber, String startMonth, String endMonth) {
|
|
|
List<String> monthList = getMonthBetween(startMonth, endMonth);
|
|
@@ -101,46 +140,90 @@ public class QueryRecordServiceImpl implements QueryRecordService {
|
|
|
return new CommonResponseObject(CommonConstant.SUCCESS_CODE, JSON.toJSONString(results));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据车牌号,索引查结果
|
|
|
+ * @param plateNumber 车牌号
|
|
|
+ * @param indices 索引
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
private List<MonthResult> find(String plateNumber, String[] indices) {
|
|
|
+ // 打印入参出参日志
|
|
|
+ LogObject logObject = new LogObject();
|
|
|
+ logObject.setPlateNumber(plateNumber);
|
|
|
+ logObject.setIndices(indices);
|
|
|
+
|
|
|
+ // 结果集合
|
|
|
+ List<MonthResult> results = new ArrayList<>();
|
|
|
+
|
|
|
+ // 判断索引是否存在
|
|
|
List<String> existIndices = new ArrayList<>();
|
|
|
for (String index : indices) {
|
|
|
if (isExistIndex(index)) {
|
|
|
existIndices.add(index);
|
|
|
}
|
|
|
}
|
|
|
- //设置es的索引index和类型他type
|
|
|
+ // 索引不存在,返回空结果
|
|
|
+ if (existIndices.size() == 0) {
|
|
|
+ logObject.setSuccess(false);
|
|
|
+ logObject.setMessage(CommonConstant.INDEX_NOT_FIND);
|
|
|
+ log.info(JSON.toJSONString(logObject));
|
|
|
+ return results;
|
|
|
+ }
|
|
|
+ // 车牌号加密
|
|
|
+ String encryptPlateNumber = vehicleIdEncrypt(plateNumber);
|
|
|
+ if (StringUtils.isBlank(encryptPlateNumber)) {
|
|
|
+ logObject.setSuccess(false);
|
|
|
+ logObject.setMessage(CommonConstant.ENCRYPT_ERROR);
|
|
|
+ log.info(JSON.toJSONString(logObject));
|
|
|
+ log.error("encryptPlateNumberError:{}", plateNumber);
|
|
|
+ return results;
|
|
|
+ }
|
|
|
+ // 设置es的索引index
|
|
|
SearchRequest searchRequest = new SearchRequest();
|
|
|
searchRequest.indices(existIndices.toArray(new String[0]));
|
|
|
- //bool工厂,相当于sql中where条件,where之后的条件都写在这里
|
|
|
+ // bool工厂,相当于sql中where条件,where之后的条件都写在这里
|
|
|
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
|
|
|
- queryBuilder.must(QueryBuilders.matchQuery("vehicleid.keyword", plateNumber));
|
|
|
- //将where条件放入工厂
|
|
|
+ queryBuilder.must(QueryBuilders.matchQuery("vehicleid.keyword", encryptPlateNumber));
|
|
|
+ // 将where条件放入工厂
|
|
|
searchSourceBuilder.timeout(new TimeValue(CommonConstant.OUT_TIME, TimeUnit.MILLISECONDS));
|
|
|
searchSourceBuilder.query(queryBuilder);
|
|
|
searchSourceBuilder.size(CommonConstant.MAX_RESULT_SIZE);
|
|
|
searchRequest.source(searchSourceBuilder);
|
|
|
SearchResponse searchResponse = null;
|
|
|
+
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
try {
|
|
|
- //执行请求
|
|
|
+ // 执行请求
|
|
|
searchResponse = esRestClient.search(searchRequest, RequestOptions.DEFAULT);
|
|
|
} catch (IOException e) {
|
|
|
+ logObject.setSuccess(false);
|
|
|
+ logObject.setMessage(CommonConstant.QUERY_ERROR);
|
|
|
+ log.info(JSON.toJSONString(logObject));
|
|
|
+ log.error("encryptPlateNumberError:{}", plateNumber);
|
|
|
log.error("queryRecordError:", e);
|
|
|
}
|
|
|
- //返回查询到的具体数据
|
|
|
+ long endTime = System.currentTimeMillis();
|
|
|
+
|
|
|
+ // 返回查询到的具体数据
|
|
|
SearchHits searchHits = searchResponse.getHits();
|
|
|
- List<MonthResult> results = new ArrayList<>();
|
|
|
for (SearchHit hit : searchHits.getHits()) {
|
|
|
MonthResult monthResult = JSON.toJavaObject(JSON.parseObject(hit.getSourceAsString()), MonthResult.class);
|
|
|
results.add(monthResult);
|
|
|
}
|
|
|
- JSONObject logObject = new JSONObject();
|
|
|
- logObject.put("plateNumber", plateNumber);
|
|
|
- logObject.put("indices", indices);
|
|
|
- logObject.put("result", results);
|
|
|
- log.info("queryRecordSuccess:{}", logObject.toJSONString());
|
|
|
+
|
|
|
+ logObject.setSuccess(true);
|
|
|
+ logObject.setMessage(CommonConstant.QUERY_SUCCESS);
|
|
|
+ logObject.setResult(results);
|
|
|
+ logObject.setQueryRecordSuccess(endTime-startTime);
|
|
|
+ log.info(JSON.toJSONString(logObject));
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 判断索引是否存在
|
|
|
+ * @param index 索引
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
private boolean isExistIndex(String index) {
|
|
|
boolean isExistIndex = false;
|
|
|
String indexInMap = IndexUtil.INSTANCE.getByIndex(index);
|
|
@@ -161,6 +244,12 @@ public class QueryRecordServiceImpl implements QueryRecordService {
|
|
|
return isExistIndex;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取月份区间
|
|
|
+ * @param minDate 开始月
|
|
|
+ * @param maxDate 结束月
|
|
|
+ * @return 月份集合
|
|
|
+ */
|
|
|
private List<String> getMonthBetween(String minDate, String maxDate) {
|
|
|
ArrayList<String> result = new ArrayList<String>();
|
|
|
try {
|
|
@@ -184,4 +273,35 @@ public class QueryRecordServiceImpl implements QueryRecordService {
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对vehicle_id字段进行加密
|
|
|
+ * @param vehicleId 待加密vehicleId
|
|
|
+ * @return 加密后vehicleId
|
|
|
+ */
|
|
|
+ private String vehicleIdEncrypt(String vehicleId) {
|
|
|
+ try {
|
|
|
+ byte[] plainData = vehicleId.getBytes("UTF-8");
|
|
|
+ byte[] iv = ivStr.getBytes("UTF-8");
|
|
|
+ Long startTime = System.currentTimeMillis();
|
|
|
+ // 加密
|
|
|
+ byte[] cipherData = CipherSuiteUtils.encrypt(plainData, algorithm, keyId, metadata, iv);
|
|
|
+ return DatatypeConverter.printHexBinary(cipherData);
|
|
|
+ } catch (UnsupportedEncodingException ue) {
|
|
|
+ log.error("UnsupportedEncodingException:", ue);
|
|
|
+ return "";
|
|
|
+ } catch (CipherSuiteException cse) {
|
|
|
+ log.error("CipherSuiteException:", cse);
|
|
|
+ return "";
|
|
|
+ } catch (CipherSuiteMacException csme) {
|
|
|
+ log.error("CipherSuiteMacException:", csme);
|
|
|
+ return "";
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Exception:", e);
|
|
|
+ return "";
|
|
|
+ } catch (Error error) {
|
|
|
+ log.error("Error:", error);
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|