|
|
|
@ -13,7 +13,6 @@ import java.util.Map; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
|
|
|
|
import com.inspect.agg.config.AggProperties; |
|
|
|
import com.inspect.agg.entity.AggAnalysisDefectMonth; |
|
|
|
import com.inspect.agg.entity.AggStation; |
|
|
|
import com.inspect.agg.enums.AlgCategory; |
|
|
|
@ -31,39 +30,49 @@ public class AggStatisticService { |
|
|
|
private static final String MONTH = "month"; |
|
|
|
private static final String QUARTER = "quarter"; |
|
|
|
private static final String YEAR = "year"; |
|
|
|
/** 统计窗口下限:最早只能查 2026-10(季度下限即 2026-Q4 的首月)。 */ |
|
|
|
private static final YearMonth MIN_STAT_MONTH = YearMonth.of(2026, 10); |
|
|
|
private static final YearMonth MIN_STAT_QUARTER_START = YearMonth.of(2026, 10); |
|
|
|
|
|
|
|
private final AggAnalysisDefectMonthMapper defectMonthMapper; |
|
|
|
private final AggPatrolTaskStatusMapper taskStatusMapper; |
|
|
|
private final AggStationMapper stationMapper; |
|
|
|
private final AggProperties props; |
|
|
|
|
|
|
|
public List<Map<String, Object>> defectStatistic(String stationCode, String algCategory, |
|
|
|
String dateType) { |
|
|
|
List<AlgCategory> categories = parseCategories(algCategory); |
|
|
|
String normalizedType = normalizeDateType(dateType); |
|
|
|
YearMonth current = YearMonth.now(); |
|
|
|
LocalDate begin; |
|
|
|
LocalDate endExclusive; |
|
|
|
if (QUARTER.equals(normalizedType)) { |
|
|
|
YearMonth currentQuarter = quarterStart(current); |
|
|
|
begin = currentQuarter.minusMonths(9).atDay(1); |
|
|
|
endExclusive = currentQuarter.plusMonths(3).atDay(1); |
|
|
|
} else if (YEAR.equals(normalizedType)) { |
|
|
|
begin = LocalDate.of(current.getYear() - 2, 1, 1); |
|
|
|
endExclusive = LocalDate.of(current.getYear() + 1, 1, 1); |
|
|
|
|
|
|
|
// 固定长度窗口:月 12 个、季 4 个、年 3 个;右端对齐当前周期, |
|
|
|
// 若左端早于统计下限(2026-10),整体右移,窗口长度不变。 |
|
|
|
YearMonth windowStart; |
|
|
|
int windowMonths; |
|
|
|
if (YEAR.equals(normalizedType)) { |
|
|
|
int startYear = Math.max(current.getYear() - 2, MIN_STAT_MONTH.getYear()); |
|
|
|
windowStart = YearMonth.of(startYear, 1); |
|
|
|
windowMonths = 36; |
|
|
|
} else if (QUARTER.equals(normalizedType)) { |
|
|
|
YearMonth start = quarterStart(current).minusMonths(9); |
|
|
|
windowStart = start.isBefore(MIN_STAT_QUARTER_START) ? MIN_STAT_QUARTER_START : start; |
|
|
|
windowMonths = 12; |
|
|
|
} else { |
|
|
|
begin = current.minusMonths(11).atDay(1); |
|
|
|
endExclusive = current.plusMonths(1).atDay(1); |
|
|
|
YearMonth start = current.minusMonths(11); |
|
|
|
windowStart = start.isBefore(MIN_STAT_MONTH) ? MIN_STAT_MONTH : start; |
|
|
|
windowMonths = 12; |
|
|
|
} |
|
|
|
|
|
|
|
LocalDate begin = windowStart.atDay(1); |
|
|
|
LocalDate endExclusive = windowStart.plusMonths(windowMonths).atDay(1); |
|
|
|
List<AggAnalysisDefectMonth> rows = defectMonthMapper.selectRange(stationCode, begin, endExclusive); |
|
|
|
Map<YearMonth, Counts> monthlyMap = toMonthlyMap(rows, categories); |
|
|
|
|
|
|
|
String stationName = stationDisplayName(stationCode); |
|
|
|
List<Map<String, Object>> result = new ArrayList<>(); |
|
|
|
if (YEAR.equals(normalizedType)) { |
|
|
|
int currentYear = current.getYear(); |
|
|
|
for (int year = currentYear - 2; year <= currentYear; year++) { |
|
|
|
int startYear = windowStart.getYear(); |
|
|
|
for (int i = 0; i < 3; i++) { |
|
|
|
int year = startYear + i; |
|
|
|
Counts counts = new Counts(); |
|
|
|
for (int monthOffset = 0; monthOffset < 12; monthOffset++) { |
|
|
|
counts.add(monthlyMap.get(YearMonth.of(year, 1).plusMonths(monthOffset))); |
|
|
|
@ -75,9 +84,8 @@ public class AggStatisticService { |
|
|
|
} |
|
|
|
|
|
|
|
if (QUARTER.equals(normalizedType)) { |
|
|
|
YearMonth firstQuarter = quarterStart(current).minusMonths(9); |
|
|
|
for (int i = 0; i < 4; i++) { |
|
|
|
YearMonth quarter = firstQuarter.plusMonths(i * 3L); |
|
|
|
YearMonth quarter = windowStart.plusMonths(i * 3L); |
|
|
|
Counts counts = new Counts(); |
|
|
|
for (int monthOffset = 0; monthOffset < 3; monthOffset++) { |
|
|
|
counts.add(monthlyMap.get(quarter.plusMonths(monthOffset))); |
|
|
|
@ -89,9 +97,8 @@ public class AggStatisticService { |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
YearMonth first = current.minusMonths(11); |
|
|
|
for (int i = 0; i < 12; i++) { |
|
|
|
YearMonth month = first.plusMonths(i); |
|
|
|
YearMonth month = windowStart.plusMonths(i); |
|
|
|
Counts counts = monthlyMap.getOrDefault(month, new Counts()); |
|
|
|
result.add(toPeriodResult(counts, month.format(MONTH_FORMAT), normalizedType, algCategory, |
|
|
|
stationCode, stationName)); |
|
|
|
@ -161,7 +168,6 @@ public class AggStatisticService { |
|
|
|
item.put("stationCode", stationCode); |
|
|
|
item.put("stationName", stationName != null ? stationName : stationCode); |
|
|
|
} |
|
|
|
item.put("minorCount", minorCount(counts)); |
|
|
|
item.put("generalCount", counts.general); |
|
|
|
item.put("seriousCount", counts.serious); |
|
|
|
item.put("urgentCount", counts.urgent); |
|
|
|
@ -182,21 +188,16 @@ public class AggStatisticService { |
|
|
|
result.put("inspectedTaskCount", number(taskStats, "inspected_task_count")); |
|
|
|
result.put("completedTaskCount", number(taskStats, "completed_task_count")); |
|
|
|
result.put("defectCount", defectCount(defectStats)); |
|
|
|
result.put("minorCount", minorCount(defectStats)); |
|
|
|
result.put("generalCount", defectStats.general); |
|
|
|
result.put("seriousCount", defectStats.serious); |
|
|
|
result.put("urgentCount", defectStats.urgent); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
private int minorCount(Counts counts) { |
|
|
|
return props.getStatistic().isMinorDefectEnabled() ? counts.minor : 0; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 缺陷总数只统计一般及以上缺陷,轻微缺陷不计入。 |
|
|
|
*/ |
|
|
|
private long defectCount(Counts counts) { |
|
|
|
if (props.getStatistic().isMinorDefectEnabled()) { |
|
|
|
return counts.defectCount(); |
|
|
|
} |
|
|
|
return (long) counts.general + counts.serious + counts.urgent; |
|
|
|
} |
|
|
|
|
|
|
|
@ -262,7 +263,6 @@ public class AggStatisticService { |
|
|
|
} |
|
|
|
|
|
|
|
private static class Counts { |
|
|
|
private int minor; |
|
|
|
private int general; |
|
|
|
private int serious; |
|
|
|
private int urgent; |
|
|
|
@ -272,7 +272,6 @@ public class AggStatisticService { |
|
|
|
if (row == null) { |
|
|
|
return; |
|
|
|
} |
|
|
|
minor += nz(row.getMinorCount()); |
|
|
|
general += nz(row.getGeneralCount()); |
|
|
|
serious += nz(row.getSeriousCount()); |
|
|
|
urgent += nz(row.getUrgentCount()); |
|
|
|
@ -283,17 +282,12 @@ public class AggStatisticService { |
|
|
|
if (other == null) { |
|
|
|
return; |
|
|
|
} |
|
|
|
minor += other.minor; |
|
|
|
general += other.general; |
|
|
|
serious += other.serious; |
|
|
|
urgent += other.urgent; |
|
|
|
normal += other.normal; |
|
|
|
} |
|
|
|
|
|
|
|
private long defectCount() { |
|
|
|
return minor + general + serious + urgent; |
|
|
|
} |
|
|
|
|
|
|
|
private static int nz(Integer value) { |
|
|
|
return value == null ? 0 : value; |
|
|
|
} |
|
|
|
|