longz
2026-07-28 cd4e26608b01cbc376760d96e2eab5d0a796f0b0
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
package com.ruoyi.order.support;
 
import com.ruoyi.chargingPile.api.model.TPartnerAppUser;
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() {
    }
 
    public static Result calculate(List<TChargingOrder> orders,
                                   List<TPartnerAppUser> relations,
                                   LocalDateTime start,
                                   LocalDateTime end) {
        List<TChargingOrder> safeOrders = orders == null ? Collections.emptyList() : orders;
        List<TPartnerAppUser> safeRelations = relations == null ? Collections.emptyList() : relations;
 
        Map<LocalDate, Set<Long>> chargingUsersByDay = new HashMap<>();
        Set<Long> chargingUsers = new HashSet<>();
        for (TChargingOrder order : safeOrders) {
            if (order == null || order.getAppUserId() == null || !inRange(order.getEndTime(), start, end)) {
                continue;
            }
            chargingUsers.add(order.getAppUserId());
            chargingUsersByDay.computeIfAbsent(order.getEndTime().toLocalDate(), key -> new HashSet<>())
                    .add(order.getAppUserId());
        }
 
        Map<Long, LocalDateTime> firstBindingByUser = new HashMap<>();
        for (TPartnerAppUser relation : safeRelations) {
            if (relation == null || relation.getAppUserId() == null || relation.getCreateTime() == null) {
                continue;
            }
            firstBindingByUser.merge(relation.getAppUserId(), relation.getCreateTime(),
                    (left, right) -> left.isBefore(right) ? left : right);
        }
        Map<LocalDate, Set<Long>> newUsersByDay = new HashMap<>();
        Set<Long> newUsers = new HashSet<>();
        firstBindingByUser.forEach((appUserId, createTime) -> {
            if (inRange(createTime, start, end)) {
                newUsers.add(appUserId);
                newUsersByDay.computeIfAbsent(createTime.toLocalDate(), key -> new HashSet<>()).add(appUserId);
            }
        });
 
        Map<String, Object> summary = new LinkedHashMap<>();
        summary.put("chargeUserCount", (long) chargingUsers.size());
        summary.put("newUserCount", (long) newUsers.size());
 
        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) chargingUsersByDay.getOrDefault(day, Collections.emptySet()).size());
            newSeries.add((long) newUsersByDay.getOrDefault(day, Collections.emptySet()).size());
        }
 
        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 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;
        }
    }
}