Commit 4a57667d authored by tianyiming's avatar tianyiming

refactor: 增加对压力管道长度的统计并优化代码逻辑

parent 88de4d5f
...@@ -76,6 +76,7 @@ import java.util.concurrent.CompletableFuture; ...@@ -76,6 +76,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
...@@ -938,7 +939,7 @@ public class ComprehensiveStatisticalAnalysisServiceImpl { ...@@ -938,7 +939,7 @@ public class ComprehensiveStatisticalAnalysisServiceImpl {
String inspectStatus = inspectionStatusMap.get(none); String inspectStatus = inspectionStatusMap.get(none);
if (dto.containsKey("NEXT_INSPECT_DATE")) { if (dto.containsKey("NEXT_INSPECT_DATE")) {
String nextInspectDate = dto.getString("NEXT_INSPECT_DATE"); String nextInspectDate = dto.getString("NEXT_INSPECT_DATE");
if(!ObjectUtils.isEmpty(nextInspectDate) && !"".equals(nextInspectDate)){ if (!ObjectUtils.isEmpty(nextInspectDate) && !"".equals(nextInspectDate)) {
long daysBetween = ChronoUnit.DAYS.between(LocalDate.now(), LocalDate.parse(nextInspectDate, formatter)); long daysBetween = ChronoUnit.DAYS.between(LocalDate.now(), LocalDate.parse(nextInspectDate, formatter));
if (daysBetween <= 0) { if (daysBetween <= 0) {
inspectStatus = inspectionStatusMap.get(overdue); inspectStatus = inspectionStatusMap.get(overdue);
...@@ -980,7 +981,7 @@ public class ComprehensiveStatisticalAnalysisServiceImpl { ...@@ -980,7 +981,7 @@ public class ComprehensiveStatisticalAnalysisServiceImpl {
JSONArray equList = (JSONArray) filterParams.get("EQU_LIST"); JSONArray equList = (JSONArray) filterParams.get("EQU_LIST");
if (ObjectUtils.isEmpty(equList) || equList.contains("8000") || equList.contains("all")) { if (ObjectUtils.isEmpty(equList) || equList.contains("8000") || equList.contains("all")) {
// 获取压力管道长度 // 获取压力管道长度
BigDecimal pipeLength = getPipeLength(boolMust, builder); BigDecimal pipeLength = getPipeLength(boolMust, builder, "pipeLength");
Map<String, Object> pipeMap = new HashMap<>(); Map<String, Object> pipeMap = new HashMap<>();
pipeMap.put("name", "压力管道(千米)"); pipeMap.put("name", "压力管道(千米)");
pipeMap.put("value", pipeLength.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP)); pipeMap.put("value", pipeLength.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
...@@ -1280,7 +1281,7 @@ public class ComprehensiveStatisticalAnalysisServiceImpl { ...@@ -1280,7 +1281,7 @@ public class ComprehensiveStatisticalAnalysisServiceImpl {
return ObjectUtils.isEmpty(response) ? 0 : response.getCount(); return ObjectUtils.isEmpty(response) ? 0 : response.getCount();
} }
private BigDecimal getPipeLength(BoolQueryBuilder boolMust, SearchSourceBuilder builder) { private BigDecimal getPipeLength(BoolQueryBuilder boolMust, SearchSourceBuilder builder, String countField) {
SearchRequest request = new SearchRequest(); SearchRequest request = new SearchRequest();
request.indices(StatisticalAnalysisEnum.equip.getKey()); request.indices(StatisticalAnalysisEnum.equip.getKey());
BigDecimal pipeLong; BigDecimal pipeLong;
...@@ -1288,7 +1289,7 @@ public class ComprehensiveStatisticalAnalysisServiceImpl { ...@@ -1288,7 +1289,7 @@ public class ComprehensiveStatisticalAnalysisServiceImpl {
QueryBuilders.boolQuery().must(QueryBuilders.termsQuery("techParams.paramKey", "pipeLength")), QueryBuilders.boolQuery().must(QueryBuilders.termsQuery("techParams.paramKey", "pipeLength")),
ScoreMode.None); ScoreMode.None);
boolMust.must(nestedQueryBuilder); boolMust.must(nestedQueryBuilder);
builder.query(boolMust).aggregation(AggregationBuilders.nested("longPipe", "techParams").subAggregation( builder.query(boolMust).aggregation(AggregationBuilders.nested(countField, "techParams").subAggregation(
AggregationBuilders.sum("pipeLength").field("techParams.doubleValue") AggregationBuilders.sum("pipeLength").field("techParams.doubleValue")
) )
); );
...@@ -1296,7 +1297,7 @@ public class ComprehensiveStatisticalAnalysisServiceImpl { ...@@ -1296,7 +1297,7 @@ public class ComprehensiveStatisticalAnalysisServiceImpl {
try { try {
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT); SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
ParsedSum sumAgg = ((ParsedNested) response.getAggregations() ParsedSum sumAgg = ((ParsedNested) response.getAggregations()
.get("longPipe")) .get(countField))
.getAggregations() .getAggregations()
.get("pipeLength"); .get("pipeLength");
double value = sumAgg.getValue(); double value = sumAgg.getValue();
...@@ -3018,31 +3019,48 @@ public class ComprehensiveStatisticalAnalysisServiceImpl { ...@@ -3018,31 +3019,48 @@ public class ComprehensiveStatisticalAnalysisServiceImpl {
} }
boolMust.mustNot(QueryBuilders.termsQuery("STATUS", Arrays.asList("草稿", "已拒领", "待认领"))); boolMust.mustNot(QueryBuilders.termsQuery("STATUS", Arrays.asList("草稿", "已拒领", "待认领")));
Map<String, Long> staticCountByGroup = new HashMap<>(); Map<String, Object> staticCountByGroup = new HashMap<>();
boolMust.mustNot(QueryBuilders.matchQuery("EQU_CATEGORY_CODE", "2300"));
getStatisticCountByGroup(request, builder, boolMust, groupField, staticCountByGroup); getStatisticCountByGroup(request, builder, boolMust, groupField, staticCountByGroup);
Long equipCount = staticCountByGroup.values().stream().reduce(0L, Long::sum);
tabTotalMap.put(StatisticalAnalysisEnum.equip.getCode(), equipCount);
AtomicReference<Long> otherCount = new AtomicReference<>(0L);
staticCountByGroup.forEach((key, value) -> {
if (!key.startsWith("8")) {
otherCount.updateAndGet(v -> v + Long.valueOf(value.toString()));
}
});
tabTotalMap.put(StatisticalAnalysisEnum.equip.getCode(), otherCount.toString());
// 组装数据 // 组装数据
List<Map<String, Object>> dataMapList = new ArrayList<>(); List<Map<String, Object>> dataMapList = new ArrayList<>();
List<String> codes = new ArrayList<>();
BoolQueryBuilder pipeBoolMust = QueryBuilderUtils.copyBoolQuery(boolMust);
for (Map<String, Object> categoryMap : staticCountByGroupMap) { for (Map<String, Object> categoryMap : staticCountByGroupMap) {
BoolQueryBuilder queryBuilder = QueryBuilderUtils.copyBoolQuery(pipeBoolMust);
String code = categoryMap.get("value").toString(); String code = categoryMap.get("value").toString();
codes.add(code);
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("key", categoryMap.get("key")); map.put("key", categoryMap.get("key"));
map.put("label", categoryMap.get("label")); map.put("label", categoryMap.get("label"));
map.put("code", categoryMap.get("value")); map.put("code", categoryMap.get("value"));
map.put("fieldKey", groupField.replace("_CODE", "")); map.put("fieldKey", groupField.replace("_CODE", ""));
map.put("value", staticCountByGroup.containsKey(code) ? staticCountByGroup.get(code) : 0L); Object value = staticCountByGroup.containsKey(code) ? staticCountByGroup.get(code) : 0L;
if (code.startsWith("8")) {
queryBuilder.must(QueryBuilders.matchQuery(groupField, code));
BigDecimal pipeLength = getPipeLength(queryBuilder, builder, code);
value = pipeLength.divide(new BigDecimal(1000), 4, BigDecimal.ROUND_HALF_UP);
}
map.put("value", value);
map.put("img", dpEquipPhotoPrefix + code + dpEquipPhotoSuffix); map.put("img", dpEquipPhotoPrefix + code + dpEquipPhotoSuffix);
Integer count = tzsCustomFilterMapper.selectEquipmentCategoryCountByParentId(categoryMap.get("key").toString()); Integer count = tzsCustomFilterMapper.selectEquipmentCategoryCountByParentId(categoryMap.get("key").toString());
map.put("noChild", 0 == count ? Boolean.TRUE : Boolean.FALSE); map.put("noChild", 0 == count ? Boolean.TRUE : Boolean.FALSE);
map.put("measurementUnit", "台套"); map.put("measurementUnit", code.startsWith("8") ? "千米" : "台套");
map.put("noBottom", code.startsWith("8") ? Boolean.TRUE : Boolean.FALSE);
// 统计该分类下的数据完整性的分组数量 DATA_QUALITY_SCORE // 统计该分类下的数据完整性的分组数量 DATA_QUALITY_SCORE
BoolQueryBuilder scoreQuery = QueryBuilderUtils.copyBoolQuery(boolMust); BoolQueryBuilder scoreQuery = QueryBuilderUtils.copyBoolQuery(boolMust);
scoreQuery.must(QueryBuilders.termQuery(groupField, code)); scoreQuery.must(QueryBuilders.termQuery(groupField, code));
SearchSourceBuilder scoreBuilder = new SearchSourceBuilder(); SearchSourceBuilder scoreBuilder = new SearchSourceBuilder();
Map<String, Long> scoreCountByGroup = new HashMap<>(); Map<String, Object> scoreCountByGroup = new HashMap<>();
getStatisticCountByGroup(request, scoreBuilder, scoreQuery, "DATA_QUALITY_SCORE", scoreCountByGroup); getStatisticCountByGroup(request, scoreBuilder, scoreQuery, "DATA_QUALITY_SCORE", scoreCountByGroup);
map.put("DATA_QUALITY_SCORE", scoreCountByGroup); map.put("DATA_QUALITY_SCORE", scoreCountByGroup);
// 统计该分类下红黄绿码分别的分组数量 NEXT_INSPECT_DATE // 统计该分类下红黄绿码分别的分组数量 NEXT_INSPECT_DATE
...@@ -3055,20 +3073,45 @@ public class ComprehensiveStatisticalAnalysisServiceImpl { ...@@ -3055,20 +3073,45 @@ public class ComprehensiveStatisticalAnalysisServiceImpl {
dataMapList.add(map); dataMapList.add(map);
} }
BigDecimal otherPipeLength = new BigDecimal(0);
// 其他种类的
if (!groupField.equals("EQU_LIST_CODE") && !ObjectUtils.isEmpty(equipCode) && equipCode.startsWith("8")) {
if (groupField.equals("EQU_CATEGORY_CODE")) {
pipeBoolMust.mustNot(QueryBuilders.termsQuery("EQU_CATEGORY_CODE", codes));
}
if (groupField.equals("EQU_DEFINE_CODE")) {
pipeBoolMust.mustNot(QueryBuilders.termsQuery("EQU_DEFINE_CODE", codes));
}
BigDecimal pipeLength = getPipeLength(pipeBoolMust, builder, "other");
BigDecimal divide = pipeLength.divide(new BigDecimal(1000), 4, BigDecimal.ROUND_HALF_UP);
otherPipeLength = otherPipeLength.add(divide);
staticCountByGroup.put("N/A", otherPipeLength);
}
// 统计种类/类别/品种数据不对的数据字段 // 统计种类/类别/品种数据不对的数据字段
if (!ObjectUtils.isEmpty(staticCountByGroup)) { if (!ObjectUtils.isEmpty(staticCountByGroup)) {
statisticOtherCount(request, dataMapList, staticCountByGroup, boolMust, groupField); statisticOtherCount(request, dataMapList, staticCountByGroup, boolMust, groupField, equipCode);
} }
if (!ObjectUtils.isEmpty(equipCode)) { if (!ObjectUtils.isEmpty(equipCode)) {
tabTotalMap.put(equipCode, dataMapList.stream().mapToLong(map -> (Long) map.get("value")).sum()); if (equipCode.startsWith("8")) {
BigDecimal pipeLengthCount = new BigDecimal(0);
for (Map<String, Object> map : dataMapList) {
BigDecimal value = (BigDecimal) map.get("value");
pipeLengthCount = pipeLengthCount.add(value);
}
tabTotalMap.put(equipCode, pipeLengthCount);
} else {
tabTotalMap.put(equipCode, dataMapList.stream().mapToLong(map -> (Long) map.get("value")).sum());
}
} }
return dataMapList; return dataMapList;
} }
private void statisticOtherCount(SearchRequest request, List<Map<String, Object>> dataMapList, Map<String, Long> staticCountByGroup, BoolQueryBuilder boolMust, String groupField) { private void statisticOtherCount(SearchRequest request, List<Map<String, Object>> dataMapList, Map<String, Object> staticCountByGroup, BoolQueryBuilder boolMust, String groupField, String equipCode) {
String type = "other"; String type = "other";
Long otherCount = 0L; Long otherCount = 0L;
BigDecimal otherPipeLength = new BigDecimal(0);
Long oneCount = 0L; Long oneCount = 0L;
Long twoCount = 0L; Long twoCount = 0L;
Long threeCount = 0L; Long threeCount = 0L;
...@@ -3077,8 +3120,13 @@ public class ComprehensiveStatisticalAnalysisServiceImpl { ...@@ -3077,8 +3120,13 @@ public class ComprehensiveStatisticalAnalysisServiceImpl {
Long greenCount = 0L; Long greenCount = 0L;
Long grayCount = 0L; Long grayCount = 0L;
Boolean isOther = false;
for (String key : staticCountByGroup.keySet()) { for (String key : staticCountByGroup.keySet()) {
otherCount += staticCountByGroup.get(key); otherCount += staticCountByGroup.get(key) instanceof Long ? (Long) staticCountByGroup.get(key) : 0L;
if (staticCountByGroup.get(key) instanceof BigDecimal) {
isOther = true;
otherPipeLength = otherPipeLength.add((BigDecimal) staticCountByGroup.get(key));
}
// 统计该分类下的数据完整性的分组数量 DATA_QUALITY_SCORE // 统计该分类下的数据完整性的分组数量 DATA_QUALITY_SCORE
BoolQueryBuilder scoreQuery = QueryBuilderUtils.copyBoolQuery(boolMust); BoolQueryBuilder scoreQuery = QueryBuilderUtils.copyBoolQuery(boolMust);
if (key.equals("NA")) { if (key.equals("NA")) {
...@@ -3087,11 +3135,11 @@ public class ComprehensiveStatisticalAnalysisServiceImpl { ...@@ -3087,11 +3135,11 @@ public class ComprehensiveStatisticalAnalysisServiceImpl {
scoreQuery.must(QueryBuilders.termQuery(groupField, key)); scoreQuery.must(QueryBuilders.termQuery(groupField, key));
} }
SearchSourceBuilder scoreBuilder = new SearchSourceBuilder(); SearchSourceBuilder scoreBuilder = new SearchSourceBuilder();
Map<String, Long> scoreCountByGroup = new HashMap<>(); Map<String, Object> scoreCountByGroup = new HashMap<>();
getStatisticCountByGroup(request, scoreBuilder, scoreQuery, "DATA_QUALITY_SCORE", scoreCountByGroup); getStatisticCountByGroup(request, scoreBuilder, scoreQuery, "DATA_QUALITY_SCORE", scoreCountByGroup);
oneCount += scoreCountByGroup.containsKey("1") ? scoreCountByGroup.get("1") : 0L; oneCount += scoreCountByGroup.containsKey("1") ? (Long) scoreCountByGroup.get("1") : 0L;
twoCount += scoreCountByGroup.containsKey("2") ? scoreCountByGroup.get("2") : 0L; twoCount += scoreCountByGroup.containsKey("2") ? (Long) scoreCountByGroup.get("2") : 0L;
threeCount += scoreCountByGroup.containsKey("3") ? scoreCountByGroup.get("3") : 0L; threeCount += scoreCountByGroup.containsKey("3") ? (Long) scoreCountByGroup.get("3") : 0L;
// 统计该分类下红黄绿码分别的分组数量 NEXT_INSPECT_DATE // 统计该分类下红黄绿码分别的分组数量 NEXT_INSPECT_DATE
Map<String, Long> nextInspectCountByGroup = new HashMap<>(); Map<String, Long> nextInspectCountByGroup = new HashMap<>();
...@@ -3113,9 +3161,10 @@ public class ComprehensiveStatisticalAnalysisServiceImpl { ...@@ -3113,9 +3161,10 @@ public class ComprehensiveStatisticalAnalysisServiceImpl {
map.put("code", type); map.put("code", type);
map.put("fieldKey", type); map.put("fieldKey", type);
map.put("noChild", Boolean.TRUE); map.put("noChild", Boolean.TRUE);
map.put("value", otherCount); map.put("value", isOther ? otherPipeLength : otherCount);
map.put("img", dpEquipPhotoPrefix + type + dpEquipPhotoSuffix); map.put("img", dpEquipPhotoPrefix + type + dpEquipPhotoSuffix);
map.put("measurementUnit", "台套"); map.put("measurementUnit", equipCode!= null && equipCode.startsWith("8") ? "千米" : "台套");
map.put("noBottom", equipCode!= null && equipCode.startsWith("8") ? Boolean.TRUE : Boolean.FALSE);
Map<String, Long> scoreCount = new HashMap<>(); Map<String, Long> scoreCount = new HashMap<>();
scoreCount.put("1", oneCount); scoreCount.put("1", oneCount);
scoreCount.put("2", twoCount); scoreCount.put("2", twoCount);
...@@ -3153,7 +3202,7 @@ public class ComprehensiveStatisticalAnalysisServiceImpl { ...@@ -3153,7 +3202,7 @@ public class ComprehensiveStatisticalAnalysisServiceImpl {
nextInspectCountByGroup.put("gray", noFieldCount); nextInspectCountByGroup.put("gray", noFieldCount);
} }
private void getStatisticCountByGroup(SearchRequest request, SearchSourceBuilder builder, BoolQueryBuilder boolMust, String groupField, Map<String, Long> staticCountByGroup) { private void getStatisticCountByGroup(SearchRequest request, SearchSourceBuilder builder, BoolQueryBuilder boolMust, String groupField, Map<String, Object> staticCountByGroup) {
builder.query(boolMust); builder.query(boolMust);
if (groupField.equals("DATA_QUALITY_SCORE")) { if (groupField.equals("DATA_QUALITY_SCORE")) {
builder.aggregation(AggregationBuilders.terms("groupCount").field(groupField).missing(99).size(50)); builder.aggregation(AggregationBuilders.terms("groupCount").field(groupField).missing(99).size(50));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment