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;
|
}
|
}
|
}
|