refactor(statistics): 切换数据查询服务并优化扫码统计功能

- 将 BrokerBiz 和 OrderBiz 中的数据查询从 StatisticsMapper 切换到 StatsQueryService
- 更新 StatisticsServiceImpl 使用 StatsQueryService 进行数据查询
- 添加订单数据合并功能到扫码统计图表中
- 重构扫码统计查询逻辑以支持统计数据和订单数据的合并显示
- 新增按小时和按日期统计订单数据的查询方法
- 优化 SQL 查询以分离统计数据和订单数据的查询逻辑
This commit is contained in:
2026-01-12 18:30:27 +08:00
parent 3bd658cc1f
commit f8c6604a8a
6 changed files with 165 additions and 50 deletions

View File

@@ -1,6 +1,7 @@
package com.ycwl.basic.biz;
import cn.hutool.core.date.DateUtil;
import com.ycwl.basic.clickhouse.service.StatsQueryService;
import com.ycwl.basic.mapper.BrokerMapper;
import com.ycwl.basic.mapper.BrokerRecordMapper;
import com.ycwl.basic.mapper.StatisticsMapper;
@@ -34,7 +35,7 @@ public class BrokerBiz {
@Autowired
private ScenicRepository scenicRepository;
@Autowired
private StatisticsMapper statisticsMapper;
private StatsQueryService statsQueryService;
public void processOrder(Long orderId) {
log.info("开始处理订单分佣,订单ID:{}", orderId);
@@ -52,7 +53,7 @@ public class BrokerBiz {
if (scenicConfig.getInteger("sample_store_day") != null) {
expireDay = scenicConfig.getInteger("sample_store_day");
}
List<Long> brokerIdList = statisticsMapper.getBrokerIdListForUser(order.getMemberId(), DateUtil.offsetDay(DateUtil.beginOfDay(order.getCreateAt()), -expireDay), order.getCreateAt());
List<Long> brokerIdList = statsQueryService.getBrokerIdListForUser(order.getMemberId(), DateUtil.offsetDay(DateUtil.beginOfDay(order.getCreateAt()), -expireDay), order.getCreateAt());
if (brokerIdList == null || brokerIdList.isEmpty()) {
log.info("用户与推客无关,订单ID:{}", orderId);
return;

View File

@@ -1,5 +1,6 @@
package com.ycwl.basic.biz;
import com.ycwl.basic.clickhouse.service.StatsQueryService;
import com.ycwl.basic.enums.StatisticEnum;
import com.ycwl.basic.integration.common.manager.ScenicConfigManager;
import com.ycwl.basic.mapper.OrderMapper;
@@ -66,6 +67,8 @@ public class OrderBiz {
private PrinterService printerService;
@Autowired
private IPriceCalculationService iPriceCalculationService;
@Autowired
private StatsQueryService statsQueryService;
public PriceObj queryPrice(Long scenicId, Long memberId, int goodsType, Long goodsId) {
PriceObj priceObj = new PriceObj();
@@ -254,7 +257,7 @@ public class OrderBiz {
orderRepository.clearOrderCache(orderId); // 更新完了,清理下
StatisticsRecordAddReq statisticsRecordAddReq = new StatisticsRecordAddReq();
statisticsRecordAddReq.setMemberId(order.getMemberId());
Long enterType = statisticsMapper.getUserRecentEnterType(order.getMemberId(), order.getCreateAt());
Long enterType = statsQueryService.getUserRecentEnterType(order.getMemberId(), order.getCreateAt());
if(!Long.valueOf(1014).equals(enterType)){//
statisticsRecordAddReq.setType(StatisticEnum.ON_SITE_PAYMENT.code);
}else {

View File

@@ -79,12 +79,14 @@ public interface StatsQueryService {
List<HashMap<String, Object>> getDailyScanStats(Long brokerId, Date startTime, Date endTime);
/**
* 按小时统计扫码人数
* 按小时统计扫码人数(仅返回统计数据,不含订单)
* 返回格式: [{t: "MM-dd HH", count: "xxx"}, ...]
*/
List<HashMap<String, String>> scanCodeMemberChartByHour(CommonQueryReq query);
/**
* 按日期统计扫码人数
* 按日期统计扫码人数(仅返回统计数据,不含订单)
* 返回格式: [{t: "MM-dd", count: "xxx"}, ...]
*/
List<HashMap<String, String>> scanCodeMemberChartByDate(CommonQueryReq query);
}

View File

@@ -105,10 +105,26 @@ public interface StatisticsMapper {
List<HashMap<String, String>> orderChartByHour(CommonQueryReq query);
/**
* 按小时统计扫码人数(仅统计数据,不含订单)
*/
List<HashMap<String, String>> scanCodeMemberChartByHour(CommonQueryReq query);
/**
* 按日期统计扫码人数(仅统计数据,不含订单)
*/
List<HashMap<String, String>> scanCodeMemberChartByDate(CommonQueryReq query);
/**
* 按小时统计订单数据
*/
List<HashMap<String, String>> orderChartByHourForMerge(CommonQueryReq query);
/**
* 按日期统计订单数据
*/
List<HashMap<String, String>> orderChartByDateForMerge(CommonQueryReq query);
/**
* 统计分销员扫码次数
*/

View File

@@ -1,5 +1,6 @@
package com.ycwl.basic.service.pc.impl;
import com.ycwl.basic.clickhouse.service.StatsQueryService;
import com.ycwl.basic.mapper.StatisticsMapper;
import com.ycwl.basic.model.mobile.statistic.req.CommonQueryReq;
import com.ycwl.basic.model.pc.statistics.resp.OrderStatisticsResp;
@@ -13,6 +14,8 @@ import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @Author:longbinbin
@@ -21,17 +24,11 @@ import java.util.List;
@Service
public class StatisticsServiceImpl implements StatisticsService {
@Autowired
private StatsQueryService statsQueryService;
@Autowired
private StatisticsMapper statisticsMapper;
List<HashMap<String, String>> getScanCodeMemberChartByHour(CommonQueryReq query) {
return statisticsMapper.scanCodeMemberChartByHour(query);
}
List<HashMap<String, String>> getScanCodeMemberChartByDate(CommonQueryReq query) {
return statisticsMapper.scanCodeMemberChartByDate(query);
}
@Override
public List<HashMap<String, String>> getScanCodeMemberChartAuto(CommonQueryReq query) {
// 检查时间范围
@@ -40,34 +37,34 @@ public class StatisticsServiceImpl implements StatisticsService {
// 如果开始和结束时间均为空,则默认为当天0点到23时59分59秒
// 如果只有一个为空,则往前或往后推24小时
LocalDateTime now = LocalDateTime.now();
if (query.getStartTime() == null && query.getEndTime() == null) {
// 都为空,设置为当天0点到23:59:59
LocalDateTime startOfDay = now.withHour(0).withMinute(0).withSecond(0).withNano(0);
LocalDateTime endOfDay = now.withHour(23).withMinute(59).withSecond(59).withNano(0);
query.setStartTime(Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant()));
query.setEndTime(Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant()));
} else if (query.getStartTime() == null) {
// 开始时间为空,结束时间不为空,往前推24小时
LocalDateTime endDateTime = query.getEndTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
LocalDateTime startDateTime = endDateTime.minusHours(24);
query.setStartTime(Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant()));
} else {
// 结束时间为空,开始时间不为空,往后推24小时
LocalDateTime startDateTime = query.getStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
LocalDateTime endDateTime = startDateTime.plusHours(24);
query.setEndTime(Date.from(endDateTime.atZone(ZoneId.systemDefault()).toInstant()));
}
}
// 计算时间差(天数)
LocalDateTime startDateTime = query.getStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
LocalDateTime endDateTime = query.getEndTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
long daysBetween = ChronoUnit.DAYS.between(startDateTime, endDateTime);
// 超过7天使用日期级别,否则使用小时级别
if (daysBetween > 7) {
return getScanCodeMemberChartByDate(query);
@@ -76,6 +73,68 @@ public class StatisticsServiceImpl implements StatisticsService {
}
}
/**
* 按小时统计扫码人数(合并统计数据和订单数据)
*/
private List<HashMap<String, String>> getScanCodeMemberChartByHour(CommonQueryReq query) {
// 1. 从 StatsQueryService 获取统计数据(根据配置走 ClickHouse 或 MySQL)
List<HashMap<String, String>> statsData = statsQueryService.scanCodeMemberChartByHour(query);
// 2. 从 MySQL 获取订单数据
List<HashMap<String, String>> orderData = statisticsMapper.orderChartByHourForMerge(query);
// 3. 合并数据
return mergeChartData(statsData, orderData);
}
/**
* 按日期统计扫码人数(合并统计数据和订单数据)
*/
private List<HashMap<String, String>> getScanCodeMemberChartByDate(CommonQueryReq query) {
// 1. 从 StatsQueryService 获取统计数据(根据配置走 ClickHouse 或 MySQL)
List<HashMap<String, String>> statsData = statsQueryService.scanCodeMemberChartByDate(query);
// 2. 从 MySQL 获取订单数据
List<HashMap<String, String>> orderData = statisticsMapper.orderChartByDateForMerge(query);
// 3. 合并数据
return mergeChartData(statsData, orderData);
}
/**
* 合并统计数据和订单数据
* 统计数据包含: t, count
* 订单数据包含: t, orderCount, orderAmount
* 合并结果: t, count, orderCount, orderAmount
*/
private List<HashMap<String, String>> mergeChartData(
List<HashMap<String, String>> statsData,
List<HashMap<String, String>> orderData) {
// 将订单数据转为 Map 以便快速查找
Map<String, HashMap<String, String>> orderMap = orderData.stream()
.collect(Collectors.toMap(
m -> m.get("t"),
m -> m,
(existing, replacement) -> existing
));
// 合并数据
for (HashMap<String, String> stat : statsData) {
String timeKey = stat.get("t");
HashMap<String, String> order = orderMap.get(timeKey);
if (order != null) {
stat.put("orderCount", order.get("orderCount"));
stat.put("orderAmount", order.get("orderAmount"));
} else {
stat.put("orderCount", "0");
stat.put("orderAmount", "0");
}
}
return statsData;
}
@Override
public OrderStatisticsResp getOrderStatistics(CommonQueryReq query) {
// 时间参数兜底与规范化