fix: 优化逻辑

This commit is contained in:
tangwei 2026-07-13 11:30:06 +08:00
parent 6bf379f7d4
commit e9633595b7

View File

@ -1,5 +1,6 @@
package com.yfd.platform.qgc_env.warn.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
@ -28,10 +29,13 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Service
public class WarnDataServiceImpl implements WarnDataService {
@ -264,6 +268,7 @@ public class WarnDataServiceImpl implements WarnDataService {
@Cacheable(cacheNames = "warn#3600", keyGenerator = "cacheKeyGenerator")
public DataSourceResult<AlarmPointVo> getAlarmPointKendoListCust(DataSourceRequest dataSourceRequest) {
DataSourceLoadOptionsBase loadOptions = dataSourceRequest == null ? null : dataSourceRequest.toDevRequest();
GroupingInfo[] groupInfos = loadOptions == null ? null : loadOptions.getGroup();
String startTimeText = loadOptions == null ? null : QgcQueryWrapperUtil.getFilterFieldValue(loadOptions, "startTime");
String endTimeText = loadOptions == null ? null : QgcQueryWrapperUtil.getFilterFieldValue(loadOptions, "endTime");
@ -295,10 +300,23 @@ public class WarnDataServiceImpl implements WarnDataService {
List<Map<String, Object>> expressionRows = microservicDynamicSQLMapper.getAllList(buildAlarmPointLegendSql(), new HashMap<>());
List<AlarmPointVo> resultList = mergeAlarmPointData(points, qecCountMap, wqCountMap, expressionRows, yyyy);
List<AlarmPointVo> filteredList = applyAlarmPointFilter(
resultList,
dataSourceRequest == null ? null : dataSourceRequest.getFilter()
);
applyAlarmPointSort(filteredList, dataSourceRequest == null ? null : dataSourceRequest.getSort());
if (CollUtil.isNotEmpty(dataSourceRequest == null ? null : dataSourceRequest.getGroup())) {
List<Map<String, Object>> rows = buildAlarmPointGroupRows(filteredList);
return QgcQueryWrapperUtil.buildGroupResult(dataSourceRequest, rows);
}
Page<?> page = loadOptions == null ? null : QgcQueryWrapperUtil.buildPage(loadOptions, loadOptions.getSkip(), loadOptions.getTake());
List<AlarmPointVo> pageList = applyAlarmPointPage(filteredList, loadOptions);
DataSourceResult<AlarmPointVo> result = new DataSourceResult<>();
result.setData(resultList);
result.setTotal(resultList.size());
result.setData(pageList);
result.setTotal(page != null ? (long) filteredList.size() : (long) filteredList.size());
result.setAggregates(new HashMap<>());
return result;
}
@ -1383,12 +1401,13 @@ public class WarnDataServiceImpl implements WarnDataService {
if (point == null || !isAlarmPointVisible(point.getAnchoPointState())) {
continue;
}
point.setAnchoPointState(normalizeAlarmPointState(point.getAnchoPointState()) + "_ALARM");
point.setAnchoPointState(normalizeAlarmPointState(point.getAnchoPointState()));
point.setStqCount(qecCountMap.getOrDefault(point.getStcd(), 0));
point.setWqCount(wqCountMap.getOrDefault(point.getStcd(), 0));
point.setZCount(0);
point.setWtCount(0);
point.setYyyy(yyyy);
point.setSttpMap(point.getSttpMap() + "_ALARM");
point.setSttpCode("ENG");
int total = point.getStqCount() + point.getWqCount();
applyAlarmPointRange(point, total, expressionRows);
@ -1397,6 +1416,291 @@ public class WarnDataServiceImpl implements WarnDataService {
return result;
}
private List<AlarmPointVo> applyAlarmPointFilter(List<AlarmPointVo> source, DataSourceRequest.FilterDescriptor filter) {
if (CollUtil.isEmpty(source) || filter == null) {
return source;
}
List<AlarmPointVo> result = new ArrayList<>();
for (AlarmPointVo item : source) {
if (matchesAlarmPointFilter(item, filter)) {
result.add(item);
}
}
return result;
}
private boolean matchesAlarmPointFilter(AlarmPointVo item, DataSourceRequest.FilterDescriptor filter) {
if (item == null || filter == null) {
return true;
}
if (CollUtil.isNotEmpty(filter.getFilters())) {
String logic = StrUtil.blankToDefault(filter.getLogic(), "and");
if ("or".equalsIgnoreCase(logic)) {
for (DataSourceRequest.FilterDescriptor child : filter.getFilters()) {
if (matchesAlarmPointFilter(item, child)) {
return true;
}
}
return false;
}
for (DataSourceRequest.FilterDescriptor child : filter.getFilters()) {
if (!matchesAlarmPointFilter(item, child)) {
return false;
}
}
return true;
}
if (StrUtil.isBlank(filter.getField()) || StrUtil.isBlank(filter.getOperator())) {
return true;
}
if (isAlarmPointMetaField(filter.getField())) {
return true;
}
Object actualValue = getAlarmPointFieldValue(item, filter.getField());
return compareAlarmPointField(actualValue, filter.getValue(), filter.getOperator(), filter.isIgnoreCase());
}
private boolean isAlarmPointMetaField(String field) {
return "startTime".equals(field) || "endTime".equals(field);
}
private boolean compareAlarmPointField(Object actualValue, Object expectedValue, String operator, boolean ignoreCase) {
if ("isblank".equalsIgnoreCase(operator)) {
return actualValue == null || StrUtil.isBlank(String.valueOf(actualValue));
}
if ("isnotblank".equalsIgnoreCase(operator)) {
return actualValue != null && StrUtil.isNotBlank(String.valueOf(actualValue));
}
if (actualValue == null) {
return expectedValue == null && ("eq".equalsIgnoreCase(operator) || "=".equals(operator));
}
String actualText = String.valueOf(actualValue);
String expectedText = expectedValue == null ? null : String.valueOf(expectedValue);
String left = ignoreCase && actualText != null ? actualText.toLowerCase() : actualText;
String right = ignoreCase && expectedText != null ? expectedText.toLowerCase() : expectedText;
switch (operator.toLowerCase()) {
case "eq":
case "=":
return Objects.equals(normalizeAlarmPointCompareValue(actualValue), normalizeAlarmPointCompareValue(expectedValue))
|| Objects.equals(left, right);
case "neq":
case "<>":
return !Objects.equals(normalizeAlarmPointCompareValue(actualValue), normalizeAlarmPointCompareValue(expectedValue))
&& !Objects.equals(left, right);
case "contains":
return right != null && left != null && left.contains(right);
case "doesnotcontain":
return right == null || left == null || !left.contains(right);
case "startswith":
return right != null && left != null && left.startsWith(right);
case "endswith":
return right != null && left != null && left.endsWith(right);
case "gt":
return compareAlarmPointComparable(actualValue, expectedValue) > 0;
case "gte":
case "ge":
return compareAlarmPointComparable(actualValue, expectedValue) >= 0;
case "lt":
return compareAlarmPointComparable(actualValue, expectedValue) < 0;
case "lte":
case "le":
return compareAlarmPointComparable(actualValue, expectedValue) <= 0;
default:
return true;
}
}
private Object normalizeAlarmPointCompareValue(Object value) {
if (value == null) {
return null;
}
if (value instanceof Number) {
return new BigDecimal(value.toString());
}
if (value instanceof Date) {
return ((Date) value).getTime();
}
String text = String.valueOf(value).trim();
if (StrUtil.isBlank(text)) {
return text;
}
if (StrUtil.isNumeric(text)) {
return new BigDecimal(text);
}
return text;
}
private int compareAlarmPointComparable(Object actualValue, Object expectedValue) {
Object left = normalizeAlarmPointCompareValue(actualValue);
Object right = normalizeAlarmPointCompareValue(expectedValue);
if (left == null && right == null) {
return 0;
}
if (left == null) {
return -1;
}
if (right == null) {
return 1;
}
if (left instanceof BigDecimal && right instanceof BigDecimal) {
return ((BigDecimal) left).compareTo((BigDecimal) right);
}
if (left instanceof Long && right instanceof Long) {
return Long.compare((Long) left, (Long) right);
}
return String.valueOf(left).compareTo(String.valueOf(right));
}
private void applyAlarmPointSort(List<AlarmPointVo> list, List<DataSourceRequest.SortDescriptor> sorts) {
if (CollUtil.isEmpty(list) || CollUtil.isEmpty(sorts)) {
return;
}
Comparator<AlarmPointVo> comparator = null;
for (DataSourceRequest.SortDescriptor sort : sorts) {
if (sort == null || StrUtil.isBlank(sort.getField()) || Boolean.FALSE.equals(sort.getNeedSortFlag())) {
continue;
}
Comparator<AlarmPointVo> current = (left, right) -> compareAlarmPointSortValue(
buildAlarmPointSortValue(getAlarmPointFieldValue(left, sort.getField())),
buildAlarmPointSortValue(getAlarmPointFieldValue(right, sort.getField()))
);
if ("desc".equalsIgnoreCase(sort.getDir()) || "des".equalsIgnoreCase(sort.getDir())) {
current = current.reversed();
}
comparator = comparator == null ? current : comparator.thenComparing(current);
}
if (comparator != null) {
list.sort(comparator);
}
}
private Comparable<?> buildAlarmPointSortValue(Object value) {
if (value == null) {
return null;
}
if (value instanceof BigDecimal) {
return (BigDecimal) value;
}
if (value instanceof Integer) {
return (Integer) value;
}
if (value instanceof Long) {
return (Long) value;
}
if (value instanceof Date) {
return ((Date) value).getTime();
}
return String.valueOf(value);
}
private int compareAlarmPointSortValue(Comparable<?> left, Comparable<?> right) {
if (left == null && right == null) {
return 0;
}
if (left == null) {
return 1;
}
if (right == null) {
return -1;
}
return String.valueOf(left).compareTo(String.valueOf(right));
}
private List<AlarmPointVo> applyAlarmPointPage(List<AlarmPointVo> source, DataSourceLoadOptionsBase loadOptions) {
if (CollUtil.isEmpty(source) || loadOptions == null || loadOptions.getTake() == null || loadOptions.getTake() <= 0) {
return source;
}
int skip = loadOptions.getSkip() == null ? 0 : Math.max(loadOptions.getSkip(), 0);
if (skip >= source.size()) {
return new ArrayList<>();
}
int endIndex = Math.min(skip + loadOptions.getTake(), source.size());
return new ArrayList<>(source.subList(skip, endIndex));
}
private List<Map<String, Object>> buildAlarmPointGroupRows(List<AlarmPointVo> source) {
List<Map<String, Object>> rows = new ArrayList<>();
if (CollUtil.isEmpty(source)) {
return rows;
}
for (AlarmPointVo item : source) {
rows.add(new LinkedHashMap<>(BeanUtil.beanToMap(item)));
}
return rows;
}
private Object getAlarmPointFieldValue(AlarmPointVo item, String field) {
if (item == null || StrUtil.isBlank(field)) {
return null;
}
switch (field) {
case "sttp":
return item.getSttp();
case "stcd":
return item.getStcd();
case "lgtd":
return item.getLgtd();
case "lttd":
return item.getLttd();
case "dtmel":
return item.getDtmel();
case "anchoPointState":
return item.getAnchoPointState();
case "sttpMap":
return item.getSttpMap();
case "titleName":
return item.getTitleName();
case "distance":
return item.getDistance();
case "rvcd":
return item.getRvcd();
case "addvcd":
return item.getAddvcd();
case "baseId":
return item.getBaseId();
case "rstcds":
return item.getRstcds();
case "sttpCode":
return item.getSttpCode();
case "ennm":
return item.getEnnm();
case "rvnm":
return item.getRvnm();
case "endInstalledType":
return item.getEndInstalledType();
case "stqCount":
return item.getStqCount();
case "wqCount":
return item.getWqCount();
case "zCount":
return item.getZCount();
case "wtCount":
return item.getWtCount();
case "yyyy":
return item.getYyyy();
case "range":
return item.getRange();
case "ttpwr":
return item.getTtpwr();
case "baseName":
return item.getBaseName();
case "rstcd":
return item.getRstcd();
case "rvcdStepSort":
return item.getRvcdStepSort();
case "rstcdStepSort":
return item.getRstcdStepSort();
case "siteStepSort":
return item.getSiteStepSort();
case "logo":
return item.getLogo();
default:
return null;
}
}
private boolean isAlarmPointVisible(String state) {
return "large_eng_built".equals(state)
|| "mid_eng_built".equals(state)