fix: 加盟商看板新增用户改为全平台注册口径
新增用户不再取 t_partner_app_user 绑定时间,改为按 t_app_user.create_time 全平台统计
所选区间内新注册用户(跨库按日聚合);充电用户仍为加盟商站点口径。
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
| | |
| | | import com.ruoyi.chargingPile.api.model.TChargingGun; |
| | | import com.ruoyi.chargingPile.api.model.TChargingPile; |
| | | import com.ruoyi.chargingPile.api.model.TParkingRecord; |
| | | import com.ruoyi.chargingPile.api.model.TPartnerAppUser; |
| | | import com.ruoyi.chargingPile.api.vo.GetChargingGunByChargingPileIdsVo; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.dto.ChargingOrderGroup; |
| | |
| | | } |
| | | chargingUsersByDay.computeIfAbsent(agg.getStatDay(), key -> new HashSet<>()).add(agg.getAppUserId()); |
| | | } |
| | | List<TPartnerAppUser> relations = Collections.emptyList(); |
| | | if (partner != null && partner.getId() != null) { |
| | | R<List<TPartnerAppUser>> relationResult = partnerClient.getBoundAppUserRelations(partner.getId()); |
| | | if (relationResult != null && relationResult.getData() != null) { |
| | | relations = relationResult.getData(); |
| | | Map<LocalDate, Long> newUsersByDay = new LinkedHashMap<>(); |
| | | if (range != null && range[0] != null && range[1] != null) { |
| | | List<FranchiseeNewUserStat> newUserStats = chargingOrderMapper.franchiseeNewUserStats(range[0], range[1]); |
| | | if (newUserStats != null) { |
| | | for (FranchiseeNewUserStat stat : newUserStats) { |
| | | if (stat != null && stat.getStatDay() != null && stat.getUserCount() != null) { |
| | | newUsersByDay.put(stat.getStatDay(), stat.getUserCount()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | FranchiseeUserStatistics.Result userStatistics = FranchiseeUserStatistics.calculateByUserDays( |
| | | chargingUsersByDay, relations, range[0], range[1]); |
| | | chargingUsersByDay, newUsersByDay, range[0], range[1]); |
| | | |
| | | Set<Long> users = aggs.stream().map(FranchiseeUserAgg::getAppUserId).filter(Objects::nonNull).collect(Collectors.toSet()); |
| | | List<TAppUser> appUsers = Collections.emptyList(); |
| New file |
| | |
| | | package com.ruoyi.order.dto; |
| | | |
| | | import lombok.Data; |
| | | |
| | | import java.time.LocalDate; |
| | | |
| | | /** |
| | | * 平台新增用户按注册日聚合行(t_app_user.create_time,全平台口径)。 |
| | | * 供加盟商看板"新增用户"统计使用,与"充电用户"(加盟商站点口径)分离。 |
| | | */ |
| | | @Data |
| | | public class FranchiseeNewUserStat { |
| | | |
| | | /** 注册日 */ |
| | | private LocalDate statDay; |
| | | |
| | | /** 该日平台新增注册用户数 */ |
| | | private Long userCount; |
| | | } |
| | |
| | | @Param("end") LocalDateTime end); |
| | | |
| | | /** |
| | | * 平台新增用户按注册日聚合(全平台口径,t_app_user.create_time,跨库)。 |
| | | */ |
| | | List<FranchiseeNewUserStat> franchiseeNewUserStats(@Param("start") LocalDateTime start, |
| | | @Param("end") LocalDateTime end); |
| | | |
| | | /** |
| | | * 流量来源统计(按 tripartite_platform_name 分组,仅 status=5 已结束订单)。 |
| | | */ |
| | | List<FranchiseeSourceStat> franchiseeTrafficStats(@Param("siteIds") List<Integer> siteIds, |
| | |
| | | package com.ruoyi.order.support; |
| | | |
| | | import com.ruoyi.chargingPile.api.model.TPartnerAppUser; |
| | | import com.ruoyi.order.api.model.TChargingOrder; |
| | | |
| | | import java.time.LocalDate; |
| | |
| | | |
| | | /** |
| | | * 旧入口:由订单列表推导"每日充电用户集合"后计算。 |
| | | * 新增用户为全平台口径(platformNewUsersByDay 为 t_app_user 注册日聚合),与加盟商绑定无关。 |
| | | */ |
| | | public static Result calculate(List<TChargingOrder> orders, |
| | | List<TPartnerAppUser> relations, |
| | | Map<LocalDate, Long> platformNewUsersByDay, |
| | | LocalDateTime start, |
| | | LocalDateTime end) { |
| | | List<TChargingOrder> safeOrders = orders == null ? Collections.emptyList() : orders; |
| | |
| | | chargingUsersByDay.computeIfAbsent(order.getEndTime().toLocalDate(), key -> new HashSet<>()) |
| | | .add(order.getAppUserId()); |
| | | } |
| | | return calculateByUserDays(chargingUsersByDay, relations, start, end); |
| | | return calculateByUserDays(chargingUsersByDay, platformNewUsersByDay, start, end); |
| | | } |
| | | |
| | | /** |
| | | * 加盟商看板入口:每日去重充电用户集合已由 SQL 聚合得到 |
| | | * (t_charging_order GROUP BY app_user_id, DATE(end_time),避免全量订单拉回 JVM)。 |
| | | * 加盟商看板入口: |
| | | * 充电用户由每日去重充电用户集合得到(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, |
| | | List<TPartnerAppUser> relations, |
| | | Map<LocalDate, Long> platformNewUsersByDay, |
| | | LocalDateTime start, |
| | | LocalDateTime end) { |
| | | Map<LocalDate, Set<Long>> safeByDay = chargingUsersByDay == null ? Collections.emptyMap() : chargingUsersByDay; |
| | | List<TPartnerAppUser> safeRelations = relations == null ? Collections.emptyList() : relations; |
| | | Map<LocalDate, Long> safeNewByDay = platformNewUsersByDay == null ? Collections.emptyMap() : platformNewUsersByDay; |
| | | |
| | | Set<Long> chargingUsers = new HashSet<>(); |
| | | safeByDay.values().forEach(chargingUsers::addAll); |
| | | |
| | | Map<Long, LocalDateTime> firstBindingByUser = new HashMap<>(); |
| | | for (TPartnerAppUser relation : safeRelations) { |
| | | if (relation == null || relation.getAppUserId() == null || relation.getCreateTime() == null) { |
| | | continue; |
| | | long newUserCount = 0; |
| | | for (Map.Entry<LocalDate, Long> entry : safeNewByDay.entrySet()) { |
| | | if (entry.getValue() != null && inDayRange(entry.getKey(), start, end)) { |
| | | newUserCount += entry.getValue(); |
| | | } |
| | | 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()); |
| | | summary.put("newUserCount", newUserCount); |
| | | |
| | | LocalDate firstDay = start == null ? LocalDate.now() : start.toLocalDate(); |
| | | LocalDate exclusiveEndDay = end == null ? firstDay.plusDays(1) : end.toLocalDate(); |
| | |
| | | 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((long) newUsersByDay.getOrDefault(day, Collections.emptySet()).size()); |
| | | newSeries.add(safeNewByDay.getOrDefault(day, 0L)); |
| | | } |
| | | |
| | | Map<String, Object> trend = new LinkedHashMap<>(); |
| | |
| | | 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)) |
| | |
| | | GROUP BY 1, 2, 3 |
| | | </select> |
| | | |
| | | <!-- 平台新增用户按注册日聚合(全平台口径,跨库 t_app_user;GROUP BY 序号防 Calcite 崩溃) --> |
| | | <select id="franchiseeNewUserStats" resultType="com.ruoyi.order.dto.FranchiseeNewUserStat"> |
| | | SELECT DATE(create_time) AS statDay, |
| | | COUNT(*) AS userCount |
| | | FROM charging_pile_account.t_app_user |
| | | WHERE del_flag = 0 |
| | | AND create_time >= #{start} |
| | | AND create_time < #{end} |
| | | GROUP BY 1 |
| | | ORDER BY 1 |
| | | </select> |
| | | |
| | | <!-- 流量来源统计(仅 status=5 已结束订单,空值归"明星充电"由服务端处理) --> |
| | | <select id="franchiseeTrafficStats" resultType="com.ruoyi.order.dto.FranchiseeSourceStat"> |
| | | SELECT IFNULL(tripartite_platform_name, '') AS source, |
| | |
| | | package com.ruoyi.order.support; |
| | | |
| | | import com.ruoyi.chargingPile.api.model.TPartnerAppUser; |
| | | import com.ruoyi.order.api.model.TChargingOrder; |
| | | import org.junit.jupiter.api.Test; |
| | | |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.util.Arrays; |
| | | import java.util.Collections; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | |
| | | private static final LocalDateTime END = LocalDateTime.of(2026, 7, 4, 0, 0); |
| | | |
| | | @Test |
| | | void countsDistinctChargingUsersAndBucketsBindingsOnTheirExactDate() { |
| | | void countsDistinctChargingUsersAndSumsPlatformNewUsersByRegistrationDay() { |
| | | List<TChargingOrder> orders = Arrays.asList( |
| | | order(1L, LocalDateTime.of(2026, 7, 1, 8, 0)), |
| | | order(1L, LocalDateTime.of(2026, 7, 1, 18, 0)), |
| | | order(1L, LocalDateTime.of(2026, 7, 2, 8, 0)), |
| | | order(2L, LocalDateTime.of(2026, 7, 2, 9, 0)) |
| | | ); |
| | | List<TPartnerAppUser> relations = Arrays.asList( |
| | | relation(1L, LocalDateTime.of(2026, 6, 30, 23, 59)), |
| | | relation(2L, LocalDateTime.of(2026, 7, 2, 10, 0)), |
| | | relation(3L, LocalDateTime.of(2026, 7, 2, 11, 0)), |
| | | relation(3L, LocalDateTime.of(2026, 7, 2, 12, 0)) |
| | | ); |
| | | Map<LocalDate, Long> newUsersByDay = new LinkedHashMap<>(); |
| | | newUsersByDay.put(LocalDate.of(2026, 7, 1), 3L); |
| | | newUsersByDay.put(LocalDate.of(2026, 7, 2), 2L); |
| | | |
| | | FranchiseeUserStatistics.Result result = FranchiseeUserStatistics.calculate(orders, relations, START, END); |
| | | FranchiseeUserStatistics.Result result = FranchiseeUserStatistics.calculate(orders, newUsersByDay, START, END); |
| | | |
| | | assertEquals(2L, result.getSummary().get("chargeUserCount")); |
| | | assertEquals(2L, result.getSummary().get("newUserCount")); |
| | | assertEquals(5L, result.getSummary().get("newUserCount")); |
| | | assertEquals(Arrays.asList("07-01", "07-02", "07-03"), result.getTrend().get("categories")); |
| | | assertEquals(Arrays.asList(1L, 2L, 0L), seriesData(result.getTrend(), 0)); |
| | | assertEquals(Arrays.asList(0L, 2L, 0L), seriesData(result.getTrend(), 1)); |
| | | assertEquals(Arrays.asList(3L, 2L, 0L), seriesData(result.getTrend(), 1)); |
| | | } |
| | | |
| | | @Test |
| | | void ignoresPlatformRegistrationsOutsideTheRange() { |
| | | Map<LocalDate, Long> newUsersByDay = new LinkedHashMap<>(); |
| | | newUsersByDay.put(LocalDate.of(2026, 6, 30), 99L); // 区间外,不计入 |
| | | newUsersByDay.put(LocalDate.of(2026, 7, 1), 1L); |
| | | |
| | | FranchiseeUserStatistics.Result result = FranchiseeUserStatistics.calculateByUserDays( |
| | | Collections.emptyMap(), newUsersByDay, START, END); |
| | | |
| | | assertEquals(1L, result.getSummary().get("newUserCount")); |
| | | assertEquals(Arrays.asList(1L, 0L, 0L), seriesData(result.getTrend(), 1)); |
| | | } |
| | | |
| | | @Test |
| | | void returnsZeroFilledSeriesWhenThereIsNoActivity() { |
| | | FranchiseeUserStatistics.Result result = FranchiseeUserStatistics.calculate( |
| | | Collections.emptyList(), Collections.emptyList(), START, END); |
| | | Collections.emptyList(), Collections.emptyMap(), START, END); |
| | | |
| | | assertEquals(0L, result.getSummary().get("chargeUserCount")); |
| | | assertEquals(0L, result.getSummary().get("newUserCount")); |
| | |
| | | order.setAppUserId(appUserId); |
| | | order.setEndTime(endTime); |
| | | return order; |
| | | } |
| | | |
| | | private TPartnerAppUser relation(Long appUserId, LocalDateTime createTime) { |
| | | TPartnerAppUser relation = new TPartnerAppUser(); |
| | | relation.setAppUserId(appUserId); |
| | | relation.setCreateTime(createTime); |
| | | return relation; |
| | | } |
| | | } |