多租户层级查询优化
This commit is contained in:
+17
-61
@@ -471,16 +471,10 @@ public interface DashboardMapper {
|
||||
"<if test='p.deviceSn != null and p.deviceSn != \"\"'>",
|
||||
" AND o.device_sn = #{p.deviceSn}",
|
||||
"</if>",
|
||||
"<choose>",
|
||||
" <when test='p.tourismIds != null and p.tourismIds.size > 0'>",
|
||||
"<if test='p.tourismIds != null and p.tourismIds.size > 0'>",
|
||||
" AND o.tourism_id IN",
|
||||
" <foreach collection='p.tourismIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
" </when>",
|
||||
" <when test='p.tenantIds != null and p.tenantIds.size > 0'>",
|
||||
" AND q.tenant_id IN",
|
||||
" <foreach collection='p.tenantIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
" </when>",
|
||||
"</choose>",
|
||||
"</if>",
|
||||
"</script>"
|
||||
})
|
||||
Long getYesterdayOrders(@Param("p") Map<String, Object> params);
|
||||
@@ -505,16 +499,6 @@ public interface DashboardMapper {
|
||||
"<if test='p.deviceSn != null and p.deviceSn != \"\"'>",
|
||||
" AND o.device_sn = #{p.deviceSn}",
|
||||
"</if>",
|
||||
"<choose>",
|
||||
" <when test='p.tourismIds != null and p.tourismIds.size > 0'>",
|
||||
" AND o.tourism_id IN",
|
||||
" <foreach collection='p.tourismIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
" </when>",
|
||||
" <when test='p.tenantIds != null and p.tenantIds.size > 0'>",
|
||||
" AND q.tenant_id IN",
|
||||
" <foreach collection='p.tenantIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
" </when>",
|
||||
"</choose>",
|
||||
"<if test='p.startTime != null and p.startTime != \"\"'>",
|
||||
" AND o.create_time >= #{p.startTime}",
|
||||
"</if>",
|
||||
@@ -563,16 +547,10 @@ public interface DashboardMapper {
|
||||
"<if test='p.deviceSn != null and p.deviceSn != \"\"'>",
|
||||
" AND o.device_sn = #{p.deviceSn}",
|
||||
"</if>",
|
||||
"<choose>",
|
||||
" <when test='p.tourismIds != null and p.tourismIds.size > 0'>",
|
||||
" AND o.tourism_id IN",
|
||||
" <foreach collection='p.tourismIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
" </when>",
|
||||
" <when test='p.tenantIds != null and p.tenantIds.size > 0'>",
|
||||
" AND q.tenant_id IN",
|
||||
" <foreach collection='p.tenantIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
" </when>",
|
||||
"</choose>",
|
||||
"<if test='p.tourismIds != null and p.tourismIds.size > 0'>",
|
||||
" AND o.tourism_id IN",
|
||||
" <foreach collection='p.tourismIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
"</if>",
|
||||
"GROUP BY ",
|
||||
"<choose>",
|
||||
" <when test=\"p.statType == 'year'\"> DATE_FORMAT(o.create_time, '%Y') </when>",
|
||||
@@ -611,16 +589,10 @@ public interface DashboardMapper {
|
||||
"<if test='p.deviceSn != null and p.deviceSn != \"\"'>",
|
||||
" AND o.device_sn = #{p.deviceSn}",
|
||||
"</if>",
|
||||
"<choose>",
|
||||
" <when test='p.tourismIds != null and p.tourismIds.size > 0'>",
|
||||
" AND o.tourism_id IN",
|
||||
" <foreach collection='p.tourismIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
" </when>",
|
||||
" <when test='p.tenantIds != null and p.tenantIds.size > 0'>",
|
||||
" AND q.tenant_id IN",
|
||||
" <foreach collection='p.tenantIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
" </when>",
|
||||
"</choose>",
|
||||
"<if test='p.tourismIds != null and p.tourismIds.size > 0'>",
|
||||
" AND o.tourism_id IN",
|
||||
" <foreach collection='p.tourismIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
"</if>",
|
||||
"GROUP BY ",
|
||||
"<choose>",
|
||||
" <when test=\"p.statType == 'year'\"> DATE_FORMAT(o.create_time, '%Y') </when>",
|
||||
@@ -658,17 +630,10 @@ public interface DashboardMapper {
|
||||
"<if test='p.deviceSn != null and p.deviceSn != \"\"'>",
|
||||
" AND o.device_sn = #{p.deviceSn}",
|
||||
"</if>",
|
||||
"<choose>",
|
||||
" <when test='p.tourismIds != null and p.tourismIds.size > 0'>",
|
||||
" AND o.tourism_id IN",
|
||||
" <foreach collection='p.tourismIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
" </when>",
|
||||
" <when test='p.tenantIds != null and p.tenantIds.size > 0'>",
|
||||
" AND q.tenant_id IN",
|
||||
" <foreach collection='p.tenantIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
" </when>",
|
||||
"</choose>",
|
||||
|
||||
"<if test='p.tourismIds != null and p.tourismIds.size > 0'>",
|
||||
" AND o.tourism_id IN",
|
||||
" <foreach collection='p.tourismIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
"</if>",
|
||||
"GROUP BY ",
|
||||
"<choose>",
|
||||
" <when test=\"p.statType == 'year'\"> DATE_FORMAT(o.create_time, '%Y') </when>",
|
||||
@@ -706,11 +671,10 @@ public interface DashboardMapper {
|
||||
"<if test='p.deviceSn != null and p.deviceSn != \"\"'>",
|
||||
" AND o.device_sn = #{p.deviceSn}",
|
||||
"</if>",
|
||||
"<if test='p.tenantIds != null and p.tenantIds.size > 0'>",
|
||||
" AND q.tenant_id IN",
|
||||
" <foreach collection='p.tenantIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
"<if test='p.tourismIds != null and p.tourismIds.size > 0'>",
|
||||
" AND o.tourism_id IN",
|
||||
" <foreach collection='p.tourismIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
"</if>",
|
||||
|
||||
"GROUP BY ",
|
||||
"<choose>",
|
||||
" <when test=\"p.statType == 'year'\"> DATE_FORMAT(o.create_time, '%Y') </when>",
|
||||
@@ -755,10 +719,6 @@ public interface DashboardMapper {
|
||||
"<if test='p.deviceSn != null and p.deviceSn != \"\"'>",
|
||||
" AND o.device_sn = #{p.deviceSn}",
|
||||
"</if>",
|
||||
"<if test='p.tenantIds != null and p.tenantIds.size > 0'>",
|
||||
" AND q.tenant_id IN",
|
||||
" <foreach collection='p.tenantIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
"</if>",
|
||||
"<if test='p.startTime != null and p.startTime != \"\"'>",
|
||||
" AND o.create_time >= #{p.startTime}",
|
||||
"</if>",
|
||||
@@ -795,10 +755,6 @@ public interface DashboardMapper {
|
||||
"<if test='p.deviceSn != null and p.deviceSn != \"\"'>",
|
||||
" AND o.device_sn = #{p.deviceSn}",
|
||||
"</if>",
|
||||
"<if test='p.tenantIds != null and p.tenantIds.size > 0'>",
|
||||
" AND q.tenant_id IN",
|
||||
" <foreach collection='p.tenantIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>",
|
||||
"</if>",
|
||||
"<if test='p.startTime != null and p.startTime != \"\"'>",
|
||||
" AND o.create_time >= #{p.startTime}",
|
||||
"</if>",
|
||||
|
||||
+12
-1
@@ -1,11 +1,13 @@
|
||||
package com.central.order.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.common.datascope.annotation.IgnoreTenantScope;
|
||||
import com.central.db.mapper.SuperMapper;
|
||||
import com.central.order.model.Tourism;
|
||||
import com.central.order.model.TourismWithDistance;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
@@ -85,4 +87,13 @@ public interface TourismMapper extends SuperMapper<Tourism> {
|
||||
*/
|
||||
List<Long> findTourismIdsByTenantIdsFromQrcodes(@Param("tenantIds") List<String> tenantIds);
|
||||
|
||||
}
|
||||
/**
|
||||
* 获取全部景区ID(用于webApp统计全库时的范围构建)
|
||||
*/
|
||||
List<Long> findAllTourismIds();
|
||||
|
||||
/**
|
||||
* 根据单个租户ID查询景区ID集合(XML映射)
|
||||
*/
|
||||
List<Long> findTourismIdsByTenantId(@Param("tenantId") String tenantId);
|
||||
}
|
||||
+121
-142
@@ -1,15 +1,11 @@
|
||||
package com.central.order.service.impl;
|
||||
|
||||
import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.common.context.TenantQueryOverrideContext;
|
||||
import com.central.common.datascope.annotation.IgnoreTenantScope;
|
||||
import com.central.common.feign.UserService;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.service.ITenantHierarchyService;
|
||||
import com.central.common.utils.SpringContextHolder;
|
||||
import com.central.order.mapper.DashboardMapper;
|
||||
import com.central.order.mapper.TourismMapper;
|
||||
import com.central.order.model.Order;
|
||||
import com.central.order.model.OrderStatistics;
|
||||
import com.central.order.model.vo.*;
|
||||
import com.central.order.service.IDashboardService;
|
||||
import com.central.order.service.IOrderService;
|
||||
@@ -36,137 +32,104 @@ public class DashboardServiceImpl implements IDashboardService {
|
||||
@Override
|
||||
@IgnoreTenantScope
|
||||
public DashboardStatisticsVO getDashboardStatistics(Map<String, Object> params) {
|
||||
// 依据租户层级组装景区ID集合,作为统计范围(支持 includeChildren=false 仅当前租户)
|
||||
// 核心流程:租户 → 子孙租户 → 景区 → 订单统计
|
||||
try {
|
||||
// 获取前端传来的tenantId,如果没有则使用当前登录用户的租户
|
||||
// Step 1: 获取当前租户ID
|
||||
String tenantId = params != null && params.get("tenantId") != null ?
|
||||
String.valueOf(params.get("tenantId")) : null;
|
||||
String.valueOf(params.get("tenantId")) : null;
|
||||
|
||||
boolean includeChildren = true;
|
||||
try {
|
||||
Object inc = params != null ? params.get("includeChildren") : null;
|
||||
if (inc != null) {
|
||||
if (inc instanceof Boolean) {
|
||||
includeChildren = (Boolean) inc;
|
||||
} else {
|
||||
String s = String.valueOf(inc);
|
||||
includeChildren = !("false".equalsIgnoreCase(s) || "0".equals(s));
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
// Step 2: 获取自己及所有子孙租户(始终包含子租户,确保数据汇总正确)
|
||||
List<String> tenantIds = tenantResolverService.resolveTenantIds(tenantId, true);
|
||||
|
||||
// 处理时间维度参数,如果没有传时间范围,根据statType自动生成
|
||||
String statType = params != null && params.get("statType") != null ?
|
||||
String.valueOf(params.get("statType")) : "day";
|
||||
|
||||
if (params != null && (params.get("startTime") == null || "".equals(params.get("startTime")))) {
|
||||
java.time.LocalDate now = java.time.LocalDate.now();
|
||||
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
|
||||
switch (statType) {
|
||||
case "year":
|
||||
// 最近5年
|
||||
params.put("startTime", now.withDayOfYear(1).format(formatter));
|
||||
params.put("endTime", now.format(formatter) + " 23:59:59");
|
||||
break;
|
||||
case "month":
|
||||
// 最近12个月
|
||||
params.put("startTime", now.withDayOfMonth(1).format(formatter));
|
||||
params.put("endTime", now.format(formatter) + " 23:59:59");
|
||||
break;
|
||||
case "day":
|
||||
default:
|
||||
// 最近7天
|
||||
params.put("startTime", now.minusDays(6).format(formatter));
|
||||
params.put("endTime", now.format(formatter) + " 23:59:59");
|
||||
break;
|
||||
}
|
||||
if (tenantIds == null || tenantIds.isEmpty()) {
|
||||
log.warn("无法获取租户列表,tenantId: {}", tenantId);
|
||||
// 返回空数据
|
||||
DashboardStatisticsVO emptyStats = new DashboardStatisticsVO();
|
||||
emptyStats.setOrderStats(new OrderStatisticsVO());
|
||||
emptyStats.setChartStats(new ChartStatistics());
|
||||
emptyStats.setFilterOptions(new FilterOptions());
|
||||
return emptyStats;
|
||||
}
|
||||
|
||||
List<String> tenantIds = tenantResolverService.resolveTenantIds(tenantId, includeChildren);
|
||||
log.info("租户 {} 获取到 {} 个租户(含自己及子孙)", tenantId, tenantIds.size());
|
||||
params.put("tenantIds", tenantIds);
|
||||
|
||||
// 特殊处理:如果是webApp且没有获取到租户列表,则查询所有景区
|
||||
if ("webApp".equals(tenantId) && (tenantIds == null || tenantIds.isEmpty())) {
|
||||
// 不设置tourismIds,让查询返回所有景区的数据
|
||||
log.info("webApp租户统计所有数据,时间范围: {} 到 {}",
|
||||
params.get("startTime"), params.get("endTime"));
|
||||
} else if (tenantIds != null && !tenantIds.isEmpty()) {
|
||||
List<Long> tourismIds = resolveTourismIdsByTenants(tenantIds);
|
||||
if (tourismIds != null && !tourismIds.isEmpty()) {
|
||||
params.put("tourismIds", tourismIds);
|
||||
log.info("统计范围 - 租户IDs: {} 个租户, 景区IDs: {} 个景区, 时间范围: {} 到 {}",
|
||||
tenantIds.size(), tourismIds.size(), params.get("startTime"), params.get("endTime"));
|
||||
} else {
|
||||
// 没有找到景区,设置一个不存在的ID,避免查询到所有数据
|
||||
log.warn("租户 {} 没有关联的景区", tenantIds);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("解析租户/景区范围失败,使用默认上下文: {}", e.getMessage());
|
||||
}
|
||||
DashboardStatisticsVO statistics = new DashboardStatisticsVO();
|
||||
// Step 3: 根据租户集合查询所有关联的景
|
||||
List<Long> tourismIds = resolveTourismIdsByTenants(tenantIds);
|
||||
|
||||
if (tourismIds == null || tourismIds.isEmpty()) {
|
||||
log.warn("租户集合无关联景区,tenantIds: {}", tenantIds);
|
||||
// 设置空的景区列表,确保查询不会返回任何数据
|
||||
tourismIds = new ArrayList<>();
|
||||
tourismIds.add(-1L); // 添加一个不存在的ID,确保查询结果为空
|
||||
}
|
||||
|
||||
params.put("tourismIds", tourismIds);
|
||||
log.info("租户 {} 关联 {} 个景区,开始统计订单数据", tenantId, tourismIds.size());
|
||||
|
||||
|
||||
// 保留必要的查询条件,不要删除
|
||||
// 时间条件等其他参数应该保留,让前端或调用方控制
|
||||
} catch (Exception e) {
|
||||
log.error("解析租户/景区范围失败: {}", e.getMessage(), e);
|
||||
}
|
||||
|
||||
DashboardStatisticsVO statistics = new DashboardStatisticsVO();
|
||||
try {
|
||||
// 1. 获取订单统计(包含对账数据)
|
||||
statistics.setOrderStats(getOrderStatistics(params));
|
||||
|
||||
// 2. 获取图表统计
|
||||
statistics.setChartStats(getChartStatistics(params));
|
||||
|
||||
// 3. 获取筛选选项
|
||||
statistics.setFilterOptions(getFilterOptions(params));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("获取仪表盘统计数据失败", e);
|
||||
}
|
||||
|
||||
return statistics;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<Order> getDashboardOrders(Map<String, Object> params) {
|
||||
try {
|
||||
// 与统计接口一致:解析租户与景区范围(含子孙租户)
|
||||
try {
|
||||
String tenantId = params != null && params.get("tenantId") != null ? String.valueOf(params.get("tenantId")) : null;
|
||||
boolean includeChildren = true;
|
||||
try {
|
||||
Object inc = params != null ? params.get("includeChildren") : null;
|
||||
if (inc != null) {
|
||||
if (inc instanceof Boolean) {
|
||||
includeChildren = (Boolean) inc;
|
||||
} else {
|
||||
String s = String.valueOf(inc);
|
||||
includeChildren = !("false".equalsIgnoreCase(s) || "0".equals(s));
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
// 与统计接口保持一致的逻辑
|
||||
// Step 1: 获取当前租户ID
|
||||
String tenantId = params != null && params.get("tenantId") != null ?
|
||||
String.valueOf(params.get("tenantId")) : null;
|
||||
|
||||
List<String> tenantIds = tenantResolverService.resolveTenantIds(tenantId, includeChildren);
|
||||
params.put("tenantIds", tenantIds);
|
||||
// Step 2: 获取自己及所有子孙租户
|
||||
List<String> tenantIds = tenantResolverService.resolveTenantIds(tenantId, true);
|
||||
|
||||
if (!"webApp".equals(tenantId) && tenantIds != null && !tenantIds.isEmpty()) {
|
||||
List<Long> tourismIds = resolveTourismIdsByTenants(tenantIds);
|
||||
if (tourismIds != null && !tourismIds.isEmpty()) {
|
||||
params.put("tourismIds", tourismIds);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("解析订单列表租户/景区范围失败:{}", e.getMessage());
|
||||
if (tenantIds == null || tenantIds.isEmpty()) {
|
||||
log.warn("无法获取租户列表,tenantId: {}", tenantId);
|
||||
return PageResult.<Order>builder()
|
||||
.data(new ArrayList<>())
|
||||
.count(0L)
|
||||
.resp_code(0)
|
||||
.build();
|
||||
}
|
||||
|
||||
// 获取订单列表
|
||||
PageResult<Order> result = orderService.findList(params);
|
||||
|
||||
// 添加调试日志
|
||||
log.info("获取仪表盘订单列表 - 参数: {}, 结果数量: {}", params, result.getCount());
|
||||
|
||||
return result;
|
||||
params.put("tenantIds", tenantIds);
|
||||
|
||||
// Step 3: 根据租户集合查询所有关联的景区
|
||||
List<Long> tourismIds = resolveTourismIdsByTenants(tenantIds);
|
||||
|
||||
if (tourismIds != null && !tourismIds.isEmpty()) {
|
||||
params.put("tourismIds", tourismIds);
|
||||
log.info("查询订单列表 - 租户: {}, 关联景区数: {}", tenantId, tourismIds.size());
|
||||
} else {
|
||||
// 无关联景区,设置空结果保护
|
||||
params.put("tourismIds", java.util.Collections.singletonList(-1L));
|
||||
log.warn("租户 {} 无关联景区", tenantId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("获取仪表盘订单列表失败", e);
|
||||
return new PageResult<>();
|
||||
log.error("解析订单列表租户/景区范围失败:{}", e.getMessage(), e);
|
||||
}
|
||||
|
||||
// 获取订单列表
|
||||
PageResult<Order> result = orderService.findList(params);
|
||||
// 添加调试日志
|
||||
log.info("获取仪表盘订单列表 - 参数: {}, 结果数量: {}", params, result.getCount());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,44 +146,41 @@ public class DashboardServiceImpl implements IDashboardService {
|
||||
|
||||
// 计算订单增长率
|
||||
Long yesterdayOrders = dashboardMapper.getYesterdayOrders(params);
|
||||
if (yesterdayOrders > 0) {
|
||||
if (yesterdayOrders != null && yesterdayOrders > 0) {
|
||||
BigDecimal growth = new BigDecimal(stats.getTodayOrders() - yesterdayOrders)
|
||||
.divide(new BigDecimal(yesterdayOrders), 4, RoundingMode.HALF_UP)
|
||||
.multiply(new BigDecimal(100));
|
||||
.divide(new BigDecimal(yesterdayOrders), 4, RoundingMode.HALF_UP)
|
||||
.multiply(new BigDecimal(100));
|
||||
stats.setOrderGrowth(growth);
|
||||
}
|
||||
|
||||
// 计算完成率
|
||||
Long completedOrders = dashboardMapper.getCompletedOrders(params);
|
||||
if (stats.getTotalOrders() > 0) {
|
||||
if (stats.getTotalOrders() != null && stats.getTotalOrders() > 0) {
|
||||
BigDecimal completionRate = new BigDecimal(completedOrders)
|
||||
.divide(new BigDecimal(stats.getTotalOrders()), 4, RoundingMode.HALF_UP)
|
||||
.multiply(new BigDecimal(100));
|
||||
.divide(new BigDecimal(stats.getTotalOrders()), 4, RoundingMode.HALF_UP)
|
||||
.multiply(new BigDecimal(100));
|
||||
stats.setCompletionRate(completionRate);
|
||||
}
|
||||
// 设置总完成订单数
|
||||
stats.setCompletedOrders(completedOrders);
|
||||
|
||||
// 获取总收款订单数量
|
||||
// 获取总收款订单数
|
||||
Long totalPaidOrders = dashboardMapper.getTotalPaidOrders(params);
|
||||
stats.setTotalPaidOrders(totalPaidOrders);
|
||||
|
||||
// 添加调试日志
|
||||
// 调试日志
|
||||
log.info("总收款订单数统计 - 参数: {}, 结果: {}", params, totalPaidOrders);
|
||||
log.info("时间范围详情 - startTime: {}, endTime: {}", params.get("startTime"), params.get("endTime"));
|
||||
|
||||
// 获取今日收款订单数量
|
||||
Long todayPaidOrders = dashboardMapper.getTodayPaidOrders(params);
|
||||
stats.setTodayPaidOrders(todayPaidOrders);
|
||||
|
||||
// 添加调试日志
|
||||
log.info("今日收款订单数统计 - 参数: {}, 结果: {}", params, todayPaidOrders);
|
||||
|
||||
// 获取退款订单数量
|
||||
// 获取退款订单数
|
||||
Long refundedOrders = dashboardMapper.getRefundedOrders(params);
|
||||
stats.setRefundedOrders(refundedOrders);
|
||||
|
||||
// 获取今日退款订单数量
|
||||
// 获取今日退款订单数
|
||||
Long todayRefundedOrders = dashboardMapper.getTodayRefundedOrders(params);
|
||||
stats.setTodayRefundedOrders(todayRefundedOrders);
|
||||
|
||||
@@ -244,21 +204,18 @@ public class DashboardServiceImpl implements IDashboardService {
|
||||
*/
|
||||
private ChartStatistics getChartStatistics(Map<String, Object> params) {
|
||||
ChartStatistics chartStats = new ChartStatistics();
|
||||
|
||||
try {
|
||||
// 获取订单趋势数据
|
||||
List<TrendDataVO> orderTrend = dashboardMapper.getOrderTrend(params);
|
||||
chartStats.setOrderTrend(orderTrend);
|
||||
|
||||
|
||||
// 获取订单状态分布数据
|
||||
List<DistributionDataVO> orderDistribution = dashboardMapper.getOrderDistribution(params);
|
||||
chartStats.setOrderDistribution(orderDistribution);
|
||||
|
||||
|
||||
// 获取收入构成数据
|
||||
List<IncomeDataVO> incomeBreakdown = dashboardMapper.getIncomeBreakdown(params);
|
||||
chartStats.setIncomeBreakdown(incomeBreakdown);
|
||||
|
||||
// 添加收入统计调试日志
|
||||
log.info("收入构成统计 - 参数: {}, 结果数量: {}", params, incomeBreakdown.size());
|
||||
for (IncomeDataVO item : incomeBreakdown) {
|
||||
log.info("收入构成 - 类型: {}, 金额: {}", item.getType(), item.getValue());
|
||||
@@ -267,11 +224,11 @@ public class DashboardServiceImpl implements IDashboardService {
|
||||
// 每日订单总数与总金额(元)
|
||||
List<TrendDataVO> dailySummary = dashboardMapper.getDailyOrderSummary(params);
|
||||
chartStats.setDailySummary(dailySummary);
|
||||
|
||||
|
||||
// 获取二维码订单数量数据
|
||||
List<QrcodeOrderDataVO> qrcodeOrderCount = dashboardMapper.getQrcodeOrderCount(params);
|
||||
chartStats.setQrcodeOrderCount(qrcodeOrderCount);
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("获取图表统计数据失败", e);
|
||||
// 设置空数据避免前端报错
|
||||
@@ -281,7 +238,6 @@ public class DashboardServiceImpl implements IDashboardService {
|
||||
chartStats.setDailySummary(new ArrayList<>());
|
||||
chartStats.setQrcodeOrderCount(new ArrayList<>());
|
||||
}
|
||||
|
||||
return chartStats;
|
||||
}
|
||||
|
||||
@@ -290,18 +246,13 @@ public class DashboardServiceImpl implements IDashboardService {
|
||||
*/
|
||||
private FilterOptions getFilterOptions(Map<String, Object> params) {
|
||||
FilterOptions options = new FilterOptions();
|
||||
|
||||
// 获取景区列表
|
||||
options.setTourismList(dashboardMapper.getTourismOptions());
|
||||
|
||||
// 获取二维码列表
|
||||
options.setQrcodeList(dashboardMapper.getQrcodeOptions(params));
|
||||
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解析当前请求上下文的租户集合(包含子租户)
|
||||
* @deprecated 使用 TenantResolverService.resolveTenantIds 方法替代
|
||||
@@ -316,20 +267,48 @@ public class DashboardServiceImpl implements IDashboardService {
|
||||
*/
|
||||
private List<Long> resolveTourismIdsByTenants(List<String> tenantIds) {
|
||||
try {
|
||||
if (tenantIds == null || tenantIds.isEmpty()) return java.util.Collections.emptyList();
|
||||
// 先按景区表直接匹配租户ID】
|
||||
List<Long> byTourism = tourismMapper.findTourismIdsByTenantIds(tenantIds);
|
||||
java.util.Set<Long> union = new java.util.LinkedHashSet<>();
|
||||
if (byTourism != null) union.addAll(byTourism);
|
||||
// 容错:部分环境景区未直连租户,改用二维码表反查所关联的景区ID
|
||||
if (union.isEmpty()) {
|
||||
List<Long> byQrcodes = tourismMapper.findTourismIdsByTenantIdsFromQrcodes(tenantIds);
|
||||
if (byQrcodes != null) union.addAll(byQrcodes);
|
||||
if (tenantIds == null || tenantIds.isEmpty()) {
|
||||
log.warn("租户集合为空,无法查询景区");
|
||||
return java.util.Collections.emptyList();
|
||||
}
|
||||
return new java.util.ArrayList<>(union);
|
||||
|
||||
log.debug("开始查询 {} 个租户关联的景区", tenantIds.size());
|
||||
|
||||
// 使用Set去重,遍历每个租户查询其景区
|
||||
java.util.Set<Long> allTourismIds = new java.util.LinkedHashSet<>();
|
||||
|
||||
// 遍历每个租户,单独查询其关联的景区
|
||||
for (String tenantId : tenantIds) {
|
||||
// 使用无租户过滤的查询方法
|
||||
List<Long> singleTenantTourisms = queryTourismIdsWithoutTenantFilter(tenantId);
|
||||
|
||||
if (singleTenantTourisms != null && !singleTenantTourisms.isEmpty()) {
|
||||
allTourismIds.addAll(singleTenantTourisms);
|
||||
}
|
||||
}
|
||||
|
||||
if (allTourismIds.isEmpty()) {
|
||||
log.warn("租户集合 {} 没有关联的景区", tenantIds);
|
||||
return java.util.Collections.emptyList();
|
||||
}
|
||||
|
||||
// 转换为List返回
|
||||
List<Long> tourismIds = new java.util.ArrayList<>(allTourismIds);
|
||||
log.debug("从景区表查询到 {} 个景区(去重后)", tourismIds.size());
|
||||
|
||||
return tourismIds;
|
||||
} catch (Exception e) {
|
||||
log.debug("根据租户查询景区失败: {}", e.getMessage());
|
||||
log.error("根据租户查询景区失败: {}", e.getMessage(), e);
|
||||
return java.util.Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 忽略多租户过滤,查询指定租户的景区
|
||||
*/
|
||||
@IgnoreTenantScope
|
||||
public List<Long> queryTourismIdsWithoutTenantFilter(String tenantId) {
|
||||
return tourismMapper.findTourismIdsByTenantId(tenantId) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+21
-7
@@ -43,22 +43,36 @@ public class TenantResolverService {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// webApp是特殊的超级租户,应该返回空列表(查询所有数据)
|
||||
if ("webApp".equals(anchor)) {
|
||||
log.info("webApp租户统计所有数据");
|
||||
return Collections.emptyList(); // 空列表表示不限制租户
|
||||
}
|
||||
|
||||
// 如果不包含子租户,只返回当前租户
|
||||
if (!includeChildren) {
|
||||
log.info("不包含子租户,仅返回当前租户: {}", anchor);
|
||||
return Collections.singletonList(anchor);
|
||||
}
|
||||
|
||||
// 通过Feign调用user-center获取子孙租户
|
||||
// 通过Feign调用user-center获取子孙租户(包括webApp)
|
||||
try {
|
||||
log.info("开始获取租户 {} 的所有子孙租户", anchor);
|
||||
List<String> tenantIds = userCenterService.getSonTenantIds(anchor);
|
||||
|
||||
if (tenantIds == null || tenantIds.isEmpty()) {
|
||||
log.warn("租户 {} 没有子孙租户,返回自身", anchor);
|
||||
return Collections.singletonList(anchor);
|
||||
}
|
||||
|
||||
log.info("租户 {} 获取到 {} 个租户(含自身及子孙): {}",
|
||||
anchor, tenantIds.size(),
|
||||
tenantIds.size() <= 20 ? tenantIds : tenantIds.subList(0, 20) + "...");
|
||||
|
||||
// 特别检查:如果是webApp,验证是否包含了所有预期的租户
|
||||
if ("webApp".equals(anchor)) {
|
||||
String[] expectedTenants = {"xcApp", "ymgApp", "zcapp", "zhapp", "djhApp", "sndApp", "sntApp", "tyApp", "ljtxApp", "yfApp"};
|
||||
for (String expected : expectedTenants) {
|
||||
if (!tenantIds.contains(expected)) {
|
||||
log.error("警告:webApp的子孙租户列表缺少预期的租户: {}", expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tenantIds;
|
||||
} catch (Exception e) {
|
||||
log.error("获取租户层级失败: {}", e.getMessage(), e);
|
||||
|
||||
@@ -130,4 +130,20 @@
|
||||
ORDER BY q.tourism_id DESC
|
||||
</select>
|
||||
|
||||
<!-- 全部景区ID(webApp全库统计使用) -->
|
||||
<select id="findAllTourismIds" resultType="java.lang.Long">
|
||||
SELECT id FROM tourisms WHERE is_del = 0 ORDER BY id DESC
|
||||
</select>
|
||||
|
||||
|
||||
<!-- 根据单个租户ID查询景区ID集合 -->
|
||||
<select id="findTourismIdsByTenantId" resultType="java.lang.Long">
|
||||
SELECT id FROM tourisms
|
||||
WHERE is_del = 0
|
||||
<if test="tenantId != null and tenantId != ''">
|
||||
AND tenant_id = #{tenantId}
|
||||
</if>
|
||||
ORDER BY id DESC
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
package com.central.common.autoconfigure;
|
||||
|
||||
import com.central.common.filter.IncludeChildrenParamFilter;
|
||||
import com.central.common.filter.LoginUserFilter;
|
||||
import com.central.common.filter.TenantFilter;
|
||||
import com.central.common.filter.TenantParamOverrideFilter;
|
||||
import com.central.common.filter.TenantQueryOverrideFilter;
|
||||
import com.central.common.service.UserTenantInitializer;
|
||||
import jakarta.servlet.Filter;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* 多租户相关自动配置
|
||||
* - 注册登录用户与租户解析相关过滤器
|
||||
* - 暴露用户租户初始化器(登录成功后用于计算可访问租户范围)
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
|
||||
@ConditionalOnClass(Filter.class)
|
||||
public class TenantInitializerAutoConfiguration {
|
||||
|
||||
// LoginUserFilter - 提取并缓存当前登录用户
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public LoginUserFilter loginUserFilter() {
|
||||
return new LoginUserFilter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(name = "loginUserFilterRegistration")
|
||||
public FilterRegistrationBean<LoginUserFilter> loginUserFilterRegistration(LoginUserFilter filter) {
|
||||
FilterRegistrationBean<LoginUserFilter> reg = new FilterRegistrationBean<>(filter);
|
||||
reg.setOrder(Ordered.HIGHEST_PRECEDENCE + 10);
|
||||
reg.addUrlPatterns("/*");
|
||||
return reg;
|
||||
}
|
||||
|
||||
// IncludeChildrenParamFilter - 解析 includeChildren 参数
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public IncludeChildrenParamFilter includeChildrenParamFilter() {
|
||||
return new IncludeChildrenParamFilter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(name = "includeChildrenParamFilterRegistration")
|
||||
public FilterRegistrationBean<IncludeChildrenParamFilter> includeChildrenParamFilterRegistration(IncludeChildrenParamFilter filter) {
|
||||
FilterRegistrationBean<IncludeChildrenParamFilter> reg = new FilterRegistrationBean<>(filter);
|
||||
reg.setOrder(Ordered.HIGHEST_PRECEDENCE + 18);
|
||||
reg.addUrlPatterns("/*");
|
||||
return reg;
|
||||
}
|
||||
|
||||
// TenantParamOverrideFilter - 解析前端传入的 tenantId 参数(覆盖查询锚点)
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public TenantParamOverrideFilter tenantParamOverrideFilter() {
|
||||
return new TenantParamOverrideFilter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(name = "tenantParamOverrideFilterRegistration")
|
||||
public FilterRegistrationBean<TenantParamOverrideFilter> tenantParamOverrideFilterRegistration(TenantParamOverrideFilter filter) {
|
||||
FilterRegistrationBean<TenantParamOverrideFilter> reg = new FilterRegistrationBean<>(filter);
|
||||
reg.setOrder(Ordered.HIGHEST_PRECEDENCE + 19);
|
||||
reg.addUrlPatterns("/*");
|
||||
return reg;
|
||||
}
|
||||
|
||||
// TenantQueryOverrideFilter - 校验并应用租户覆盖锚点
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public TenantQueryOverrideFilter tenantQueryOverrideFilter() {
|
||||
return new TenantQueryOverrideFilter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(name = "tenantQueryOverrideFilterRegistration")
|
||||
public FilterRegistrationBean<TenantQueryOverrideFilter> tenantQueryOverrideFilterRegistration(TenantQueryOverrideFilter filter) {
|
||||
FilterRegistrationBean<TenantQueryOverrideFilter> reg = new FilterRegistrationBean<>(filter);
|
||||
reg.setOrder(Ordered.HIGHEST_PRECEDENCE + 20);
|
||||
reg.addUrlPatterns("/*");
|
||||
return reg;
|
||||
}
|
||||
|
||||
// TenantFilter - 兼容旧的 tenantId 参数(可选)
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public TenantFilter tenantFilter() {
|
||||
return new TenantFilter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(name = "tenantFilterRegistration")
|
||||
public FilterRegistrationBean<TenantFilter> tenantFilterRegistration(TenantFilter filter) {
|
||||
FilterRegistrationBean<TenantFilter> reg = new FilterRegistrationBean<>(filter);
|
||||
reg.setOrder(Ordered.HIGHEST_PRECEDENCE + 50);
|
||||
reg.addUrlPatterns("/*");
|
||||
return reg;
|
||||
}
|
||||
|
||||
// 用户租户信息初始化器(若业务侧未显式定义 Bean,则由此提供)
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public UserTenantInitializer userTenantInitializer() {
|
||||
return new UserTenantInitializer();
|
||||
}
|
||||
}
|
||||
|
||||
-5
@@ -1,6 +1 @@
|
||||
com.central.common.filter.TenantFilter
|
||||
com.central.common.filter.LoginUserFilter
|
||||
com.central.common.filter.TenantParamOverrideFilter
|
||||
com.central.common.filter.TenantQueryOverrideFilter
|
||||
com.central.common.filter.IncludeChildrenParamFilter
|
||||
com.central.common.autoconfigure.TenantInitializerAutoConfiguration
|
||||
|
||||
Reference in New Issue
Block a user