提交 d4e0aa78 作者: 925993793@qq.com

平台专题:统战部风险分析页面接口封装

上级 91213800
......@@ -23,6 +23,7 @@ public class Constants {
public static final String SUBJECT_INDEX = "subjectdatabase";
//es 采集库索引
public static final String COLLECT_INDEX = "basedata";
public static final String COLLECT_MIDDLE_INDEX = "middlesubjectdatabase";
//专题事件脉络展示 伪事件脉络 的资讯数量阈值
public static final int FAKE_NUM = 6;
......
package com.zzsn.event.controller.common;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
......@@ -9,6 +10,7 @@ import com.zzsn.event.entity.Subject;
import com.zzsn.event.entity.SysDictItem;
import com.zzsn.event.es.EsService;
import com.zzsn.event.service.*;
import com.zzsn.event.util.CalculateUtil;
import com.zzsn.event.util.DateUtil;
import com.zzsn.event.vo.*;
import com.zzsn.event.vo.es.Label;
......@@ -18,10 +20,9 @@ import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
/**
......@@ -46,6 +47,8 @@ public class StatisticalAnalysisController {
private ClbLabelService clbLabelService;
@Autowired
private InformationService informationService;
@Autowired
private CommonService commonService;
/**
* 概述分析
......@@ -92,7 +95,7 @@ public class StatisticalAnalysisController {
}
}
//补充数据
supplyData(countList,bindLabelList,labelMap);
supplyData(countList, bindLabelList, labelMap);
overviewAnalysisVO.setInfoCount(totalCount);
overviewAnalysisVO.setCountList(countList);
return Result.OK(overviewAnalysisVO);
......@@ -201,10 +204,10 @@ public class StatisticalAnalysisController {
public Result<?> dataSource(@RequestBody InfoDataSearchCondition searchCondition) {
List<CountVO> dataList = new ArrayList<>();
List<Label> bindLabelList = bindLabelList(searchCondition.getSubjectId());
Map<String, Label> labelMap = bindLabelList.stream().collect(Collectors.toMap(label-> label.getLabelMark() + "-"+label.getRelationId(), label -> label));
Map<String, Label> labelMap = bindLabelList.stream().collect(Collectors.toMap(label -> label.getLabelMark() + "-" + label.getRelationId(), label -> label));
if (CollectionUtils.isEmpty(searchCondition.getIds()) && searchCondition.getNum() == null) {
List<String> includeValues = new ArrayList<>();
bindLabelList.forEach(e -> includeValues.add(e.getLabelMark() + "-" +e.getRelationId()));
bindLabelList.forEach(e -> includeValues.add(e.getLabelMark() + "-" + e.getRelationId()));
searchCondition.setIncludeValues(includeValues.toArray(new String[0]));
dataList = esService.groupByInfoSourceTag(searchCondition);
} else {
......@@ -226,8 +229,6 @@ public class StatisticalAnalysisController {
}
/**
* 资讯数量top的信息源统计
*
......@@ -375,4 +376,104 @@ public class StatisticalAnalysisController {
}
}
}
/*--------------------------------------------以下为【统战部】风险评估页面接口---------------------------------------------------------*/
/**
* 单个国家统计
*
* @param subjectId 专题id
* @param country 国家名称
* @param startTime 开始时间
* @param endTime 结束时间
* @author lkg
* @date 2025/6/7
*/
@GetMapping("/singleCcountryStatistic")
public Result<?> singleCountryStatistic(@RequestParam String subjectId, @RequestParam String country,
@RequestParam(required = false) String startTime,
@RequestParam(required = false) String endTime) {
Map<String, Object> map = esService.singleCountryStatistic(subjectId, country, startTime, endTime);
return Result.OK(map);
}
/**
* 按国家统计
*
* @param subjectId 专题id
* @param startTime 开始时间
* @param endTime 结束时间
* @author lkg
* @date 2025/6/7
*/
@GetMapping("/countryStatistic")
public Result<?> countryStatistic(@RequestParam String subjectId,
@RequestParam(required = false) String startTime,
@RequestParam(required = false) String endTime) {
List<CountVO> countVOS = esService.countryStatistic(subjectId, startTime, endTime);
return Result.OK(countVOS);
}
/**
* 按风险统计
*
* @param subjectId 专题id
* @param startTime 开始时间
* @param endTime 结束时间
* @author lkg
* @date 2025/6/7
*/
@GetMapping("/riskTypeStatistic")
public Result<?> riskTypeStatistic(@RequestParam String subjectId,
@RequestParam(required = false) String startTime,
@RequestParam(required = false) String endTime) {
List<CountVO> countVOS = esService.riskTypeStatistic(subjectId, startTime, endTime);
return Result.OK(countVOS);
}
@GetMapping("/monthStatistic")
public Result<?> monthStatistic(@RequestParam String subjectId) {
DateTime startDateTime = cn.hutool.core.date.DateUtil.offsetMonth(new Date(), -13);
String startTime = startDateTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().withDayOfMonth(1).toLocalDate().atStartOfDay().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String endTime = cn.hutool.core.date.DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
List<CountVO> countVOS = esService.groupByMonth(subjectId, startTime, endTime);
List<String> months = DateUtil.getMonthsBetween(startTime, endTime);
for (String month : months) {
boolean exist = countVOS.stream().anyMatch(e -> month.equals(e.getName()));
if (!exist) {
CountVO countVO = new CountVO();
countVO.setName(month);
countVO.setValue(0L);
countVOS.add(countVO);
}
}
countVOS.sort(Comparator.comparing(CountVO::getName));
for (int i = 1; i < countVOS.size(); i++) {
CountVO before = countVOS.get(i - 1);
CountVO current = countVOS.get(i);
if (before.getValue() == 0) {
current.setPercentage("0");
} else {
long subtract = current.getValue() - before.getValue();
String divide = CalculateUtil.divide(String.valueOf(subtract), String.valueOf(before.getValue()), 2);
current.setPercentage(divide);
}
}
countVOS.remove(0);
return Result.OK(countVOS);
}
@GetMapping("/sourceStatistic")
public Result<?> sourceStatistic(@RequestParam String subjectId) {
Map<String, Integer> map = new HashMap<>();
map.put("sourceType", 4);
List<String> subjectIds = new ArrayList<>();
subjectIds.add(subjectId);
Map<String, Integer> sourceCountMap = commonService.bindSourceCount(subjectIds);
map.put("sourceNum", sourceCountMap.get(subjectId));
int collectCount = esService.currentCollectCount(subjectId);
map.put("collectNum", collectCount);
return Result.OK(map);
}
}
package com.zzsn.event.es;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.net.URLDecoder;
import cn.hutool.json.JSONUtil;
......@@ -9,10 +10,12 @@ import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cronutils.model.field.expression.On;
import com.google.common.base.Charsets;
import com.zzsn.event.constant.Constants;
import com.zzsn.event.entity.Event;
import com.zzsn.event.entity.Subject;
import com.zzsn.event.entity.SysDictItem;
import com.zzsn.event.service.*;
import com.zzsn.event.util.*;
import com.zzsn.event.util.tree.Node;
......@@ -94,6 +97,8 @@ public class EsService {
private CommonService commonService;
@Autowired
private EsOpUtil esOpUtil;
@Autowired
private SysDictItemService sysDictItemService;
/**
* 获取专题下的资讯
......@@ -869,6 +874,8 @@ public class EsService {
for (SearchHit hit : searchHits) {
String queryInfo = hit.getSourceAsString();
SpecialInformation info = JSONUtil.toBean(queryInfo, SpecialInformation.class);
info.setPublishDate(EsDateUtil.esFieldDateMapping(info.getPublishDate()));
info.setCreateDate(EsDateUtil.esFieldDateMapping(info.getCreateDate()));
info.setDbIndex(hit.getIndex());
info.setIndex(hit.getIndex());
list.add(info);
......@@ -2188,7 +2195,7 @@ public class EsService {
boolQuery.mustNot(QueryBuilders.termQuery("deleteFlag", 1));
}
}
} else if (status == 1) {
} else if (status == 1) {
boolQuery.must(QueryBuilders.termQuery("isFreeCheck", 1));
if (CollectionUtils.isNotEmpty(excludeStatus)) {
if (excludeStatus.contains(2)) {
......@@ -2820,4 +2827,250 @@ public class EsService {
jsonObject.put("updateDate", EsDateUtil.esFieldDateFormat(DateUtils.formatDateTime()));
esOpUtil.docUpdateById(index, id, jsonObject.toString());
}
public Map<String,Object> singleCountryStatistic(String subjectId,String country, String startTime, String endTime) {
Map<String, Object> map = new HashMap<>();
//根据地区的id从ES查询相关的记录
SearchRequest searchRequest = new SearchRequest(Constants.SUBJECT_INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.trackTotalHits(true);
searchSourceBuilder.size(0);
//创建查询对象
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("subjectId.keyword", subjectId));
List<SysDictItem> countryItemList = sysDictItemService.listByDictCode("article_tzb_guojia");
Optional<SysDictItem> first = countryItemList.stream().filter(item -> item.getItemText().equals(country)).findFirst();
if (first.isPresent()) {
String countryId = first.get().getId();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("labels.relationId", countryId);
boolQuery.must(QueryBuilders.nestedQuery("labels", termQueryBuilder, ScoreMode.None));
} else {
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("labels.relationName", country);
boolQuery.must(QueryBuilders.nestedQuery("labels", matchQueryBuilder, ScoreMode.None));
}
//统计没有删除的
boolQuery.mustNot(QueryBuilders.matchQuery("deleteFlag", "1"));
if (StringUtils.isNotEmpty(startTime) || StringUtils.isNotEmpty(endTime)) {
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("publishDate");
if (StringUtils.isNotEmpty(startTime)) {
rangeQueryBuilder.gte(EsDateUtil.esFieldDateFormat(startTime));
}
if (StringUtils.isNotEmpty(endTime)) {
rangeQueryBuilder.lte(EsDateUtil.esFieldDateFormat(endTime));
}
boolQuery.filter(rangeQueryBuilder);
}
searchSourceBuilder.query(boolQuery);
List<SysDictItem> riskTypeItemList = sysDictItemService.listByDictCode("article_risk_type");
Map<String, String> riskTypeMap = riskTypeItemList.stream().collect(Collectors.toMap(SysDictItem::getId, SysDictItem::getItemText));
NestedAggregationBuilder nestedAggregationBuilder = AggregationBuilders.nested("labels", "labels")
.subAggregation(AggregationBuilders.terms("group_risk")
.field("labels.relationId")
.size(1)
.order(BucketOrder.count(false))
.includeExclude(new IncludeExclude(riskTypeItemList.stream().map(SysDictItem::getId).toArray(String[]::new), null)));
searchSourceBuilder.aggregation(nestedAggregationBuilder);
searchRequest.source(searchSourceBuilder);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
long totalCount = searchResponse.getHits().getTotalHits().value;
Aggregations aggregations = searchResponse.getAggregations();
ParsedNested labels = aggregations.get("labels");
Aggregations labelsAggregations = labels.getAggregations();
Terms groupCountry = labelsAggregations.get("group_risk");
List<? extends Terms.Bucket> buckets = groupCountry.getBuckets();
String mainRiskType = riskTypeMap.get(buckets.get(0).getKeyAsString());
map.put("mainRiskType", mainRiskType);
map.put("totalCount", totalCount);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
public List<CountVO> countryStatistic(String subjectId, String startTime, String endTime) {
List<CountVO> countList = new ArrayList<>();
//根据地区的id从ES查询相关的记录
SearchRequest searchRequest = new SearchRequest(Constants.SUBJECT_INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.trackTotalHits(true);
searchSourceBuilder.size(0);
//创建查询对象
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("subjectId.keyword", subjectId));
//统计没有删除的
boolQuery.mustNot(QueryBuilders.matchQuery("deleteFlag", "1"));
if (StringUtils.isNotEmpty(startTime) || StringUtils.isNotEmpty(endTime)) {
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("publishDate");
if (StringUtils.isNotEmpty(startTime)) {
rangeQueryBuilder.gte(EsDateUtil.esFieldDateFormat(startTime));
}
if (StringUtils.isNotEmpty(endTime)) {
rangeQueryBuilder.lte(EsDateUtil.esFieldDateFormat(endTime));
}
boolQuery.filter(rangeQueryBuilder);
}
searchSourceBuilder.query(boolQuery);
List<SysDictItem> countryItemList = sysDictItemService.listByDictCode("article_tzb_guojia");
Map<String, String> countryMap = countryItemList.stream().collect(Collectors.toMap(SysDictItem::getId, SysDictItem::getItemText));
NestedAggregationBuilder nestedAggregationBuilder = AggregationBuilders.nested("labels", "labels")
.subAggregation(AggregationBuilders.terms("group_country")
.field("labels.relationId")
.size(countryItemList.size())
.includeExclude(new IncludeExclude(countryItemList.stream().map(SysDictItem::getId).toArray(String[]::new), null)));
searchSourceBuilder.aggregation(nestedAggregationBuilder);
searchRequest.source(searchSourceBuilder);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
Aggregations aggregations = searchResponse.getAggregations();
ParsedNested labels = aggregations.get("labels");
Aggregations labelsAggregations = labels.getAggregations();
Terms groupCountry = labelsAggregations.get("group_country");
List<? extends Terms.Bucket> buckets = groupCountry.getBuckets();
for (Terms.Bucket bucket : buckets) {
CountVO countVO = new CountVO();
String key = bucket.getKeyAsString();
countVO.setName(countryMap.get(key));
countVO.setValue(bucket.getDocCount());
countList.add(countVO);
}
} catch (Exception e) {
e.printStackTrace();
}
return countList;
}
public List<CountVO> riskTypeStatistic(String subjectId, String startTime, String endTime) {
List<CountVO> countList = new ArrayList<>();
//根据地区的id从ES查询相关的记录
SearchRequest searchRequest = new SearchRequest(Constants.SUBJECT_INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.trackTotalHits(true);
searchSourceBuilder.size(0);
//创建查询对象
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("subjectId.keyword", subjectId));
//统计没有删除的
boolQuery.mustNot(QueryBuilders.matchQuery("deleteFlag", "1"));
if (StringUtils.isNotEmpty(startTime) || StringUtils.isNotEmpty(endTime)) {
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("publishDate");
if (StringUtils.isNotEmpty(startTime)) {
rangeQueryBuilder.gte(EsDateUtil.esFieldDateFormat(startTime));
}
if (StringUtils.isNotEmpty(endTime)) {
rangeQueryBuilder.lte(EsDateUtil.esFieldDateFormat(endTime));
}
boolQuery.filter(rangeQueryBuilder);
}
searchSourceBuilder.query(boolQuery);
List<SysDictItem> riskTypeItemList = sysDictItemService.listByDictCode("article_risk_type");
Map<String, String> map = riskTypeItemList.stream().collect(Collectors.toMap(SysDictItem::getId, SysDictItem::getItemText));
NestedAggregationBuilder nestedAggregationBuilder = AggregationBuilders.nested("labels", "labels")
.subAggregation(AggregationBuilders.terms("group_risk")
.field("labels.relationId")
.size(riskTypeItemList.size())
.includeExclude(new IncludeExclude(riskTypeItemList.stream().map(SysDictItem::getId).toArray(String[]::new), null))
);
searchSourceBuilder.aggregation(nestedAggregationBuilder);
searchRequest.source(searchSourceBuilder);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
Aggregations aggregations = searchResponse.getAggregations();
ParsedNested labels = aggregations.get("labels");
Aggregations labelsAggregations = labels.getAggregations();
Terms groupRisk = labelsAggregations.get("group_risk");
List<? extends Terms.Bucket> buckets = groupRisk.getBuckets();
for (Terms.Bucket bucket : buckets) {
CountVO countVO = new CountVO();
String key = bucket.getKeyAsString();
countVO.setName(map.get(key));
countVO.setValue(bucket.getDocCount());
countList.add(countVO);
}
} catch (Exception e) {
e.printStackTrace();
}
return countList;
}
public List<CountVO> groupByMonth(String subjectId, String startTime, String endTime) {
List<CountVO> countList = new ArrayList<>();
//根据地区的id从ES查询相关的记录
SearchRequest searchRequest = new SearchRequest(Constants.SUBJECT_INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.trackTotalHits(true);
searchSourceBuilder.size(0);
//创建查询对象
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("subjectId.keyword", subjectId));
//统计没有删除的
boolQuery.mustNot(QueryBuilders.matchQuery("deleteFlag", "1"));
if (StringUtils.isNotEmpty(startTime) || StringUtils.isNotEmpty(endTime)) {
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("publishDate");
if (StringUtils.isNotEmpty(startTime)) {
rangeQueryBuilder.gte(EsDateUtil.esFieldDateFormat(startTime));
}
if (StringUtils.isNotEmpty(endTime)) {
rangeQueryBuilder.lte(EsDateUtil.esFieldDateFormat(endTime));
}
boolQuery.filter(rangeQueryBuilder);
}
searchSourceBuilder.query(boolQuery);
DateHistogramAggregationBuilder aggregationBuilder = AggregationBuilders.dateHistogram("groupMonth")
.field("publishDate")
.calendarInterval(DateHistogramInterval.MONTH)
.format("yyyy-MM");
searchSourceBuilder.aggregation(aggregationBuilder);
searchRequest.source(searchSourceBuilder);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
Aggregations aggregations = searchResponse.getAggregations();
ParsedDateHistogram parsedDateHistogram = aggregations.get("groupMonth");
List<? extends Histogram.Bucket> buckets = parsedDateHistogram.getBuckets();
for (Histogram.Bucket bucket : buckets) {
CountVO countVO = new CountVO();
countVO.setName(bucket.getKeyAsString());
countVO.setValue(bucket.getDocCount());
countList.add(countVO);
}
} catch (Exception e) {
e.printStackTrace();
}
return countList;
}
/**
* 今日采集量
*
* @param subjectId 专题id
* @author lkg
* @date 2025/6/7
*/
public int currentCollectCount(String subjectId) {
long count = 0L;
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//创建查询对象
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("subjectIds", subjectId));
DateTime beginOfDay = cn.hutool.core.date.DateUtil.beginOfDay(new Date());
boolQuery.filter(QueryBuilders.rangeQuery("createDate").gte(EsDateUtil.esFieldDateFormat(DateUtil.format(beginOfDay, "yyyy-MM-dd HH:mm:ss"))));
SearchRequest searchRequest = new SearchRequest(EsIndexUtil.getIndexYear(Constants.COLLECT_MIDDLE_INDEX));
//默认最大数量是10000,设置为true后,显示准确数量
searchSourceBuilder.trackTotalHits(true);
searchSourceBuilder.query(boolQuery);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse;
try {
searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
count = searchResponse.getHits().getTotalHits().value;
} catch (Exception e) {
log.info("获取专题-{}-下资讯,查询es库失败", subjectId);
e.printStackTrace();
}
return (int) count;
}
}
......@@ -2,6 +2,7 @@ package com.zzsn.event.util;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
......@@ -135,6 +136,27 @@ public class DateUtil {
return dateList;
}
public static List<String> getMonthsBetween(String startDateStr, String endDateStr) {
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
LocalDate startDate = LocalDate.parse(startDateStr, dateFormatter);
LocalDate endDate = LocalDate.parse(endDateStr, dateFormatter);
List<String> months = new ArrayList<>();
YearMonth startYearMonth = YearMonth.from(startDate);
YearMonth endYearMonth = YearMonth.from(endDate);
for (YearMonth yearMonth = startYearMonth;
!yearMonth.isAfter(endYearMonth);
yearMonth = yearMonth.plusMonths(1)) {
months.add(yearMonth.format(monthFormatter));
}
return months;
}
public static String format(Date d, String format){
if (d == null) {
return "";
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论