longz
2026-08-05 3d00292d6c3ee0215a37cd66d79769d77ce7d652
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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;
        }
    }
}