Commit 224e10de authored by hcing's avatar hcing

feat:统计服务,大屏综合统计查询接口--人员统计

parent 7204576c
......@@ -197,6 +197,16 @@ public class ComprehensiveStatisticalAnalysisController extends BaseController {
}
/**
* 大屏综合统计--人员统计
*/
@TycloudOperation(ApiLevel = UserType.AGENCY)
@PostMapping(value = "/dp/statistics/person")
@ApiOperation(httpMethod = "POST", value = "大屏综合统计查询接口--人员统计", notes = "大屏综合统计查询接口--人员统计")
public ResponseModel<JSONObject> queryDpStatisticsForPerson(@RequestBody Map<String, Object> map) {
return ResponseHelper.buildResponse(statisticalAnalysisService.queryDpStatisticsForPerson(new JSONObject(map)));
}
/**
* 综合统计分析接口-导出
*
* @param map 入参
......
......@@ -43,11 +43,9 @@ import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.NestedQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.nested.ParsedNested;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
......@@ -2689,6 +2687,8 @@ public class ComprehensiveStatisticalAnalysisServiceImpl {
private List<Map<String, Object>> queryDpPersonStatistics(JSONObject filter, SearchRequest request, SearchSourceBuilder builder,
BoolQueryBuilder boolMust, Map<String, Object> tabTotalMap) {
request.indices(StatisticalAnalysisEnum.person.getKey());
JSONObject filterParams = JSONObject.parseObject(JSONObject.toJSONString(filter.get("filterParams")));
String filterType = "advanced";
// 组装人员过滤条件
......@@ -2869,4 +2869,184 @@ public class ComprehensiveStatisticalAnalysisServiceImpl {
throw new RuntimeException(e);
}
}
/**
* 大屏综合统计查询接口--人员统计
*
* @param filter 过滤条件
* @return 按照 【资质状态】 【人员类型】分组统计返回
*/
public JSONObject queryDpStatisticsForPerson(JSONObject filter) {
JSONObject result = new JSONObject();
String businessType = filter.getString("businessType");
if (ObjectUtils.isEmpty(businessType)) {
return result;
}
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
String orgCode = filter.getString("orgCode");
boolQuery.must(QueryBuilders.prefixQuery("superviseOrgCode", orgCode));
JSONObject filterParams = JSONObject.parseObject(JSONObject.toJSONString(filter.get("filterParams")));
String filterType = "advanced";
// 组装人员过滤条件
this.getPersonBoolQueryBuilder(filterParams, boolQuery, filterType);
// 查询
JSONObject expiryDateStatus = this.getExpiryDateStatusGroupStatistics(boolQuery);
JSONObject personType = this.getPersonTypeGroupStatistics(boolQuery);
result.put("tabTotalMap",new JSONObject()
.fluentPut("expiryDateStatusTotal",this.sumWithJSONObjectValues(expiryDateStatus))
.fluentPut("personTypeTotal",this.sumWithJSONObjectValues(personType)));
result.put("datas", new JSONObject()
.fluentPut("expiryDateStatus", expiryDateStatus)
.fluentPut("personType", personType));
return result;
}
/**
* 对JSONObject类型的所有values求和
* @param jsonObject
* @return
*/
private long sumWithJSONObjectValues(JSONObject jsonObject) {
long result = 0L;
for (String key : jsonObject.keySet()) {
long longValue = Long.parseLong(jsonObject.getString(key));
result += longValue;
}
return result;
}
/**
* 大屏综合统计查询接口--人员统计 【资质状态】分组统计
*/
private JSONObject getExpiryDateStatusGroupStatistics(BoolQueryBuilder boolQuery) {
JSONObject result = new JSONObject();
String path = "licenses";
String nestedField = path + ".expiryDate";
// 资质正常
BoolQueryBuilder normalBoolQuery = QueryBuilderUtils.copyBoolQuery(boolQuery);
NestedQueryBuilder normalNestedQuery = QueryBuilders.nestedQuery(
path,
QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery(nestedField).gt(LocalDate.now().plusDays(30).format(formatter))),
ScoreMode.None
);
normalBoolQuery.must(normalNestedQuery);
Long normalCount = getStatisticCount(normalBoolQuery, StatisticalAnalysisEnum.person.getKey());
result.put("资质正常", normalCount);
// 资质临期
BoolQueryBuilder nearBoolQuery = QueryBuilderUtils.copyBoolQuery(boolQuery);
NestedQueryBuilder nearNestedQuery = QueryBuilders.nestedQuery(
path,
QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery(nestedField).lte(LocalDate.now().plusDays(30).format(formatter)).gte(LocalDate.now().format(formatter))),
ScoreMode.None
);
nearBoolQuery.must(nearNestedQuery);
Long nearCount = getStatisticCount(nearBoolQuery, StatisticalAnalysisEnum.person.getKey());
result.put("资质临期", nearCount);
// 资质超期
BoolQueryBuilder overBoolQuery = QueryBuilderUtils.copyBoolQuery(boolQuery);
NestedQueryBuilder overNestedQuery = QueryBuilders.nestedQuery(
path,
QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery(nestedField).lte(LocalDate.now().format(formatter))),
ScoreMode.None
);
overBoolQuery.must(overNestedQuery);
Long overCount = getStatisticCount(overBoolQuery, StatisticalAnalysisEnum.person.getKey());
result.put("资质超期", overCount);
// 无有效期
BoolQueryBuilder nothingBoolQuery = QueryBuilderUtils.copyBoolQuery(boolQuery);
NestedQueryBuilder nothingNestedQuery = QueryBuilders.nestedQuery(
path,
QueryBuilders.boolQuery().must(QueryBuilders.existsQuery(nestedField)),
ScoreMode.None
);
nothingBoolQuery.must(nothingNestedQuery);
Long nothingCount = getStatisticCount(nothingBoolQuery, StatisticalAnalysisEnum.person.getKey());
result.put("无有效期", nothingCount);
// 无资质
BoolQueryBuilder notLicensesBoolQuery = QueryBuilderUtils.copyBoolQuery(boolQuery);
DynamicQueryBuilder mainBuilder = new DynamicQueryBuilder();
EnhancedDynamicQueryBuilder enhancedDynamicQueryBuilder = new EnhancedDynamicQueryBuilder();
NestedQueryBuilder nestedQuery = QueryBuilders.nestedQuery(
path,
existsQuery(nestedField),
ScoreMode.None
);
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
.should(QueryBuilders.wildcardQuery("newPost", "*66151*"))
.should(QueryBuilders.wildcardQuery("newPost", "*66152*"))
.should(QueryBuilders.wildcardQuery("newPost", "*6552*"))
.should(QueryBuilders.wildcardQuery("newPost", "*6763*"))
.should(QueryBuilders.wildcardQuery("subPost", "*6713*"))
.should(QueryBuilders.wildcardQuery("subPost", "*6764*"))
.should(QueryBuilders.wildcardQuery("subPost", "*6765*"))
.minimumShouldMatch(1);
EnhancedDynamicQueryBuilder noLicensesQuery = new EnhancedDynamicQueryBuilder();
noLicensesQuery.add(QueryBuilders.boolQuery().mustNot(nestedQuery), and);
noLicensesQuery.add(queryBuilder, and);
enhancedDynamicQueryBuilder.add(noLicensesQuery.build(), "AND");
mainBuilder.and(enhancedDynamicQueryBuilder.build());
notLicensesBoolQuery.must(mainBuilder.build());
Long otLicensesCount = getStatisticCount(notLicensesBoolQuery, StatisticalAnalysisEnum.person.getKey());
result.put("无资质", otLicensesCount);
return result;
}
/**
* 大屏综合统计查询接口--人员统计 【人员类型】分组统计
*/
private JSONObject getPersonTypeGroupStatistics(BoolQueryBuilder boolQuery) {
JSONObject result = new JSONObject();
// copy一份,避免后续两部分逻辑不一致
BoolQueryBuilder boolQueryCopy = QueryBuilderUtils.copyBoolQuery(boolQuery);
// 除作业人员外的所有
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.trackTotalHits(true);
searchSourceBuilder.size(0);// 不返回任何文档,只返回聚合结果
SearchRequest searchRequest = new SearchRequest();
String aggName = "personTypeGroupAgg";
QueryBuilderUtils.buildSplitFieldAggCondition(searchSourceBuilder, "postName.keyword", ",", aggName);
searchSourceBuilder.query(boolQuery);
searchRequest.source(searchSourceBuilder);
try {
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
Terms personTypeGroupAgg = searchResponse.getAggregations().get(aggName);
if (personTypeGroupAgg != null) {
for (Terms.Bucket bucket : personTypeGroupAgg.getBuckets()) {
result.put(bucket.getKeyAsString(), bucket.getDocCount());
}
}
result.remove("作业人员");
} catch (Exception ex) {
throw new RuntimeException(ex);
}
// 作业人员取 : 仅持证人员
CountRequest countRequest = new CountRequest();
countRequest.indices(StatisticalAnalysisEnum.person.getKey());
boolQueryCopy.must(QueryBuilders.matchQuery("subPostName", "持证人员").operator(Operator.AND));
countRequest.query(boolQueryCopy);
CountResponse countResponse;
try {
countResponse = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT);
} catch (Exception exception) {
throw new RuntimeException(exception);
}
result.put("作业人员(仅持证人员)", ObjectUtils.isEmpty(countResponse) ? 0L : countResponse.getCount());
return result;
}
}
\ No newline at end of file
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