package com.ruoyi.order.support;
|
|
import com.ruoyi.order.api.model.TChargingOrder;
|
|
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.Collections;
|
import java.util.HashMap;
|
import java.util.HashSet;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Objects;
|
import java.util.Set;
|
|
/** 加盟商看板用户统计的纯计算逻辑。 */
|
public final class FranchiseeUserStatistics {
|
|
private FranchiseeUserStatistics() {
|
}
|
|
/**
|
* 旧入口:由订单列表推导"每日充电用户集合"后计算。
|
* 新增用户为全平台口径(platformNewUsersByDay 为 t_app_user 注册日聚合),与加盟商绑定无关。
|
*/
|
public static Result calculate(List<TChargingOrder> orders,
|
Map<LocalDate, Long> platformNewUsersByDay,
|
LocalDateTime start,
|
LocalDateTime end) {
|
List<TChargingOrder> safeOrders = orders == null ? Collections.emptyList() : orders;
|
Map<LocalDate, Set<Long>> chargingUsersByDay = new HashMap<>();
|
for (TChargingOrder order : safeOrders) {
|
if (order == null || order.getAppUserId() == null || !inRange(order.getEndTime(), start, end)) {
|
continue;
|
}
|
chargingUsersByDay.computeIfAbsent(order.getEndTime().toLocalDate(), key -> new HashSet<>())
|
.add(order.getAppUserId());
|
}
|
return calculateByUserDays(chargingUsersByDay, platformNewUsersByDay, start, end);
|
}
|
|
/**
|
* 加盟商看板入口:
|
* 充电用户由每日去重充电用户集合得到(t_charging_order GROUP BY app_user_id, DATE(end_time),
|
* 避免全量订单拉回 JVM,仅加盟商站点范围);
|
* 新增用户为全平台口径(t_app_user.create_time 按日聚合),与加盟商站点/绑定无关。
|
*/
|
public static Result calculateByUserDays(Map<LocalDate, Set<Long>> chargingUsersByDay,
|
Map<LocalDate, Long> platformNewUsersByDay,
|
LocalDateTime start,
|
LocalDateTime end) {
|
Map<LocalDate, Set<Long>> safeByDay = chargingUsersByDay == null ? Collections.emptyMap() : chargingUsersByDay;
|
Map<LocalDate, Long> safeNewByDay = platformNewUsersByDay == null ? Collections.emptyMap() : platformNewUsersByDay;
|
|
Set<Long> chargingUsers = new HashSet<>();
|
safeByDay.values().forEach(chargingUsers::addAll);
|
|
long newUserCount = 0;
|
for (Map.Entry<LocalDate, Long> entry : safeNewByDay.entrySet()) {
|
if (entry.getValue() != null && inDayRange(entry.getKey(), start, end)) {
|
newUserCount += entry.getValue();
|
}
|
}
|
|
Map<String, Object> summary = new LinkedHashMap<>();
|
summary.put("chargeUserCount", (long) chargingUsers.size());
|
summary.put("newUserCount", newUserCount);
|
|
LocalDate firstDay = start == null ? LocalDate.now() : start.toLocalDate();
|
LocalDate exclusiveEndDay = end == null ? firstDay.plusDays(1) : end.toLocalDate();
|
List<String> categories = new ArrayList<>();
|
List<Long> chargingSeries = new ArrayList<>();
|
List<Long> newSeries = new ArrayList<>();
|
for (LocalDate day = firstDay; day.isBefore(exclusiveEndDay); day = day.plusDays(1)) {
|
categories.add(String.format("%02d-%02d", day.getMonthValue(), day.getDayOfMonth()));
|
chargingSeries.add((long) safeByDay.getOrDefault(day, Collections.emptySet()).size());
|
newSeries.add(safeNewByDay.getOrDefault(day, 0L));
|
}
|
|
Map<String, Object> trend = new LinkedHashMap<>();
|
trend.put("categories", categories);
|
trend.put("series", Arrays.asList(series("充电用户", chargingSeries), series("新增用户", newSeries)));
|
return new Result(summary, trend);
|
}
|
|
private static boolean inDayRange(LocalDate day, LocalDateTime start, LocalDateTime end) {
|
return day != null
|
&& (start == null || !day.isBefore(start.toLocalDate()))
|
&& (end == null || day.isBefore(end.toLocalDate()));
|
}
|
|
private static boolean inRange(LocalDateTime time, LocalDateTime start, LocalDateTime end) {
|
return time != null
|
&& (start == null || !time.isBefore(start))
|
&& (end == null || time.isBefore(end));
|
}
|
|
private static Map<String, Object> series(String name, List<Long> data) {
|
Map<String, Object> series = new LinkedHashMap<>();
|
series.put("name", name);
|
series.put("data", data);
|
return series;
|
}
|
|
public static final class Result {
|
private final Map<String, Object> summary;
|
private final Map<String, Object> trend;
|
|
private Result(Map<String, Object> summary, Map<String, Object> trend) {
|
this.summary = Objects.requireNonNull(summary);
|
this.trend = Objects.requireNonNull(trend);
|
}
|
|
public Map<String, Object> getSummary() {
|
return summary;
|
}
|
|
public Map<String, Object> getTrend() {
|
return trend;
|
}
|
}
|
}
|