首页统计
This commit is contained in:
+16
-11
@@ -34,6 +34,9 @@ public class DashboardController {
|
||||
@Operation(summary = "获取首页统计数据")
|
||||
@GetMapping("/statistics")
|
||||
public Result<DashboardStatisticsVO> getStatistics(
|
||||
@Parameter(description = "租户ID") @RequestParam(required = false) String tenantId,
|
||||
@Parameter(description = "是否包含子租户") @RequestParam(required = false) Boolean includeChildren,
|
||||
@Parameter(description = "统计类型(day/month/year)") @RequestParam(required = false, defaultValue = "day") String statType,
|
||||
@Parameter(description = "景区ID") @RequestParam(required = false) Long tourismId,
|
||||
@Parameter(description = "二维码ID") @RequestParam(required = false) String qrcodeId,
|
||||
@Parameter(description = "设备编号") @RequestParam(required = false) String deviceSn,
|
||||
@@ -42,19 +45,21 @@ public class DashboardController {
|
||||
@Parameter(description = "是否包含测试二维码") @RequestParam(required = false, defaultValue = "false") Boolean includeTestQrcodes) {
|
||||
try {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
// 追加统计粒度(day/month/year),从请求参数读取
|
||||
// 规范化租户与子租户范围
|
||||
String anchor = tenantId;
|
||||
try {
|
||||
org.springframework.web.context.request.ServletRequestAttributes attrs = (org.springframework.web.context.request.ServletRequestAttributes) org.springframework.web.context.request.RequestContextHolder.getRequestAttributes();
|
||||
jakarta.servlet.http.HttpServletRequest req = attrs != null ? attrs.getRequest() : null;
|
||||
String statType = req != null ? req.getParameter("statType") : null;
|
||||
if (statType != null && !statType.isEmpty()) {
|
||||
params.put("statType", statType);
|
||||
if (anchor == null || anchor.isEmpty()) {
|
||||
anchor = com.central.common.context.TenantQueryOverrideContext.getTenantId();
|
||||
if (anchor == null || anchor.isEmpty()) {
|
||||
anchor = com.central.common.context.LoginUserContextHolder.getCurrentTenantId();
|
||||
}
|
||||
}
|
||||
String incStr = req != null ? req.getParameter("includeChildren") : null;
|
||||
if (incStr != null && !incStr.isEmpty()) {
|
||||
params.put("includeChildren", incStr);
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
} catch (Exception ignored) { }
|
||||
boolean withChildren = (includeChildren == null) || Boolean.TRUE.equals(includeChildren);
|
||||
params.put("tenantId", anchor);
|
||||
params.put("includeChildren", withChildren);
|
||||
// 其他筛选参数
|
||||
params.put("statType", statType);
|
||||
params.put("tourismId", tourismId);
|
||||
params.put("qrcodeId", qrcodeId);
|
||||
params.put("deviceSn", deviceSn);
|
||||
|
||||
+3
-1
@@ -32,7 +32,9 @@ public class UserCenterServiceFallbackFactory implements FallbackFactory<UserCen
|
||||
|
||||
@Override
|
||||
public Result<List<String>> getDescendantTenantIds(String tenantId) {
|
||||
return null;
|
||||
log.warn("获取子孙租户失败,降级返回仅当前租户: {}, error: {}", tenantId, cause.getMessage());
|
||||
// 降级策略:返回仅包含当前租户的列表
|
||||
return Result.succeed(java.util.Collections.singletonList(tenantId));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -242,6 +242,10 @@ public interface DashboardMapper {
|
||||
"<if test='p.endTime != null and p.endTime != \"\"'>",
|
||||
" AND o.create_time <= #{p.endTime}",
|
||||
"</if>",
|
||||
"<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>",
|
||||
"</script>"
|
||||
})
|
||||
Long getTotalAmount(@Param("p") Map<String, Object> params);
|
||||
@@ -272,6 +276,10 @@ public interface DashboardMapper {
|
||||
"<if test='p.endTime != null and p.endTime != \"\"'>",
|
||||
" AND o.create_time <= #{p.endTime}",
|
||||
"</if>",
|
||||
"<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>",
|
||||
"</script>"
|
||||
})
|
||||
Long getRefundAmount(@Param("p") Map<String, Object> params);
|
||||
|
||||
@@ -79,4 +79,10 @@ public interface TourismMapper extends SuperMapper<Tourism> {
|
||||
*/
|
||||
List<Long> findTourismIdsByTenantIds(@Param("tenantIds") List<String> tenantIds);
|
||||
|
||||
/**
|
||||
* 根据租户ID集合,基于二维码表(qrcodes)反查关联的景区ID集合
|
||||
* 适用于景区记录未直接维护 tenant_id,或存在历史数据不一致的情况
|
||||
*/
|
||||
List<Long> findTourismIdsByTenantIdsFromQrcodes(@Param("tenantIds") List<String> tenantIds);
|
||||
|
||||
}
|
||||
|
||||
+144
-11
@@ -30,26 +30,75 @@ public class DashboardServiceImpl implements IDashboardService {
|
||||
private final DashboardMapper dashboardMapper;
|
||||
private final IOrderService orderService;
|
||||
private final TourismMapper tourismMapper;
|
||||
private final TenantResolverService tenantResolverService;
|
||||
|
||||
@Override
|
||||
@IgnoreTenantScope
|
||||
public DashboardStatisticsVO getDashboardStatistics(Map<String, Object> params) {
|
||||
// 依据租户层级组装景区ID集合,作为统计范围(支持 includeChildren=false 仅当前租户)
|
||||
try {
|
||||
// 获取前端传来的tenantId,如果没有则使用当前登录用户的租户
|
||||
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) {
|
||||
String s = String.valueOf(inc);
|
||||
includeChildren = !("false".equalsIgnoreCase(s) || "0".equals(s));
|
||||
if (inc instanceof Boolean) {
|
||||
includeChildren = (Boolean) inc;
|
||||
} else {
|
||||
String s = String.valueOf(inc);
|
||||
includeChildren = !("false".equalsIgnoreCase(s) || "0".equals(s));
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
|
||||
List<String> tenantIds = resolveTenantIdsFromContext(includeChildren);
|
||||
if (tenantIds != null && !tenantIds.isEmpty()) {
|
||||
// 处理时间维度参数,如果没有传时间范围,根据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.minusYears(1).withDayOfYear(1).format(formatter));
|
||||
params.put("endTime", now.format(formatter) + " 23:59:59");
|
||||
break;
|
||||
case "month":
|
||||
// 最近12个月
|
||||
params.put("startTime", now.minusMonths(1).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;
|
||||
}
|
||||
}
|
||||
|
||||
List<String> tenantIds = tenantResolverService.resolveTenantIds(tenantId, includeChildren);
|
||||
|
||||
// 特殊处理:如果是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,避免查询到所有数据
|
||||
params.put("tourismIds", java.util.Collections.singletonList(-1L));
|
||||
log.warn("租户 {} 没有关联的景区", tenantIds);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -218,34 +267,118 @@ public class DashboardServiceImpl implements IDashboardService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析当前请求上下文的租户集合(包含子租户)
|
||||
* 解析租户ID集合(包含子租户)
|
||||
* @deprecated 已迁移到TenantResolverService
|
||||
*/
|
||||
private List<String> resolveTenantIdsFromContext(boolean includeChildren) {
|
||||
String anchor = TenantQueryOverrideContext.getTenantId();
|
||||
@Deprecated
|
||||
private List<String> resolveTenantIds_old(String tenantId, boolean includeChildren) {
|
||||
// 优先使用前端传来的tenantId,否则从上下文获取
|
||||
String anchor = tenantId;
|
||||
if (anchor == null || anchor.isEmpty()) {
|
||||
anchor = TenantQueryOverrideContext.getTenantId();
|
||||
}
|
||||
if (anchor == null || anchor.isEmpty()) {
|
||||
anchor = LoginUserContextHolder.getCurrentTenantId();
|
||||
}
|
||||
if (anchor == null || anchor.isEmpty()) {
|
||||
return java.util.Collections.emptyList();
|
||||
}
|
||||
|
||||
// webApp是特殊的超级租户,应该返回所有租户数据
|
||||
if ("webApp".equals(anchor)) {
|
||||
// 查询所有租户ID
|
||||
try {
|
||||
ITenantHierarchyService tenantService = SpringContextHolder.getBean(ITenantHierarchyService.class);
|
||||
if (tenantService != null) {
|
||||
// 获取所有顶级租户及其子孙
|
||||
List<String> allTenants = new ArrayList<>();
|
||||
List<com.central.common.model.TenantInfo> topTenants = tenantService.getTenantList(null, null, null, null, null).getData();
|
||||
if (topTenants != null) {
|
||||
for (com.central.common.model.TenantInfo tenant : topTenants) {
|
||||
if (tenant.getTenantId() != null && !"webApp".equals(tenant.getTenantId())) {
|
||||
List<String> subTenants = tenantService.getAllSubTenantIds(tenant.getTenantId());
|
||||
if (subTenants != null && !subTenants.isEmpty()) {
|
||||
allTenants.addAll(subTenants);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!allTenants.isEmpty()) {
|
||||
log.info("webApp租户统计所有租户: {}", allTenants);
|
||||
return allTenants;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("webApp获取所有租户失败: {}", e.getMessage());
|
||||
}
|
||||
// 如果失败了,返回空列表(将查询所有数据)
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 如果不包含子租户,只返回当前租户
|
||||
if (!includeChildren) {
|
||||
return java.util.Collections.singletonList(anchor);
|
||||
}
|
||||
|
||||
// 获取当前租户及其所有子孙租户
|
||||
try {
|
||||
// 优先尝试使用本地的ITenantHierarchyService
|
||||
ITenantHierarchyService tenantService = SpringContextHolder.getBean(ITenantHierarchyService.class);
|
||||
List<String> all = tenantService != null ? tenantService.getAllSubTenantIds(anchor) : null;
|
||||
if (all != null && !all.isEmpty()) return all;
|
||||
} catch (Exception ignored) { }
|
||||
if (tenantService != null) {
|
||||
List<String> all = tenantService.getAllSubTenantIds(anchor);
|
||||
if (all != null && !all.isEmpty()) {
|
||||
log.info("本地获取租户层级成功: {} -> {}", anchor, all);
|
||||
return all;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.debug("本地获取租户层级失败,尝试远程调用: {}", e.getMessage());
|
||||
}
|
||||
|
||||
// 如果本地服务不可用,尝试远程调用
|
||||
try {
|
||||
com.central.order.feign.UserCenterService userCenterService = SpringContextHolder.getBean(com.central.order.feign.UserCenterService.class);
|
||||
if (userCenterService != null) {
|
||||
com.central.common.model.Result<List<String>> result = userCenterService.getDescendantTenantIds(anchor);
|
||||
if (result != null && result.getResp_code() == 0 && result.getData() != null) {
|
||||
log.info("远程获取租户层级成功: {} -> {}", anchor, result.getData());
|
||||
return result.getData();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("远程获取租户层级失败: {}", e.getMessage());
|
||||
}
|
||||
|
||||
// 如果都失败了,只返回当前租户
|
||||
log.warn("获取租户层级失败,降级为仅当前租户: {}", anchor);
|
||||
return java.util.Collections.singletonList(anchor);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析当前请求上下文的租户集合(包含子租户)
|
||||
* @deprecated 使用 TenantResolverService.resolveTenantIds 方法替代
|
||||
*/
|
||||
@Deprecated
|
||||
private List<String> resolveTenantIdsFromContext(boolean includeChildren) {
|
||||
return tenantResolverService.resolveTenantIds(null, includeChildren);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据租户集合查询景区ID集合
|
||||
*/
|
||||
private List<Long> resolveTourismIdsByTenants(List<String> tenantIds) {
|
||||
try {
|
||||
if (tenantIds == null || tenantIds.isEmpty()) return java.util.Collections.emptyList();
|
||||
return tourismMapper.findTourismIdsByTenantIds(tenantIds);
|
||||
// 先按景区表直接匹配租户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);
|
||||
}
|
||||
return new java.util.ArrayList<>(union);
|
||||
} catch (Exception e) {
|
||||
log.debug("根据租户查询景区失败: {}", e.getMessage());
|
||||
return java.util.Collections.emptyList();
|
||||
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
package com.central.order.service.impl;
|
||||
|
||||
import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.common.context.TenantQueryOverrideContext;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.order.feign.UserCenterService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 租户解析服务
|
||||
* 专门处理租户层级关系的获取
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class TenantResolverService {
|
||||
|
||||
private final UserCenterService userCenterService;
|
||||
|
||||
/**
|
||||
* 解析租户ID集合(包含子租户)
|
||||
* @param tenantId 指定的租户ID,null时使用当前上下文
|
||||
* @param includeChildren 是否包含子租户
|
||||
* @return 租户ID列表
|
||||
*/
|
||||
public List<String> resolveTenantIds(String tenantId, boolean includeChildren) {
|
||||
// 优先使用前端传来的tenantId,否则从上下文获取
|
||||
String anchor = tenantId;
|
||||
if (anchor == null || anchor.isEmpty()) {
|
||||
anchor = TenantQueryOverrideContext.getTenantId();
|
||||
}
|
||||
if (anchor == null || anchor.isEmpty()) {
|
||||
anchor = LoginUserContextHolder.getCurrentTenantId();
|
||||
}
|
||||
if (anchor == null || anchor.isEmpty()) {
|
||||
log.warn("无法获取租户ID,返回空列表");
|
||||
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获取子孙租户
|
||||
try {
|
||||
log.info("开始获取租户 {} 的所有子孙租户", anchor);
|
||||
Result<List<String>> result = userCenterService.getDescendantTenantIds(anchor);
|
||||
|
||||
if (result != null) {
|
||||
if (result.getResp_code() == 0 && result.getData() != null && !result.getData().isEmpty()) {
|
||||
List<String> tenantIds = result.getData();
|
||||
log.info("成功获取租户层级: {} -> {} 个租户: {}", anchor, tenantIds.size(), tenantIds);
|
||||
return tenantIds;
|
||||
} else {
|
||||
log.warn("获取租户层级返回异常: code={}, msg={}", result.getResp_code(), result.getResp_msg());
|
||||
}
|
||||
} else {
|
||||
log.warn("获取租户层级返回null");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("获取租户层级失败: {}", e.getMessage(), e);
|
||||
}
|
||||
|
||||
// 降级:只返回当前租户
|
||||
log.warn("获取子孙租户失败,降级为仅当前租户: {}", anchor);
|
||||
return Collections.singletonList(anchor);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有租户(用于webApp超级管理员)
|
||||
*/
|
||||
public List<String> getAllTenantIds() {
|
||||
// TODO: 实现获取所有租户的逻辑
|
||||
// 暂时返回空,表示不限制
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
@@ -115,4 +115,19 @@
|
||||
ORDER BY id DESC
|
||||
</select>
|
||||
|
||||
<!-- 基于二维码表按租户反查景区ID(容错方案) -->
|
||||
<select id="findTourismIdsByTenantIdsFromQrcodes" resultType="java.lang.Long">
|
||||
SELECT DISTINCT q.tourism_id
|
||||
FROM qrcodes q
|
||||
WHERE q.is_del = 0
|
||||
AND q.tourism_id IS NOT NULL
|
||||
<if test="tenantIds != null and tenantIds.size > 0">
|
||||
AND q.tenant_id IN
|
||||
<foreach collection="tenantIds" item="tid" open="(" separator="," close=")">
|
||||
#{tid}
|
||||
</foreach>
|
||||
</if>
|
||||
ORDER BY q.tourism_id DESC
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
||||
Reference in New Issue
Block a user