多租户层级查询优化

This commit is contained in:
2025-10-09 14:33:15 +08:00
parent da9bf97667
commit 6bfc01d2fc
25 changed files with 581 additions and 309 deletions
@@ -1,11 +1,11 @@
package com.central.order.controller;
import com.central.common.feign.UserService;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.common.model.SysUser;
import com.central.order.constants.EditTaskStatus;
import com.central.order.enums.OrderStatus;
import com.central.order.feign.UserCenterService;
import com.central.order.model.*;
import com.central.order.model.dto.CallbackDTO;
import com.central.order.model.params.EditTaskQueryParams;
@@ -46,7 +46,7 @@ public class AutoCutVideoController {
private WxMiniSubscribeService wxMiniSubscribeService;
@Autowired
private UserCenterService userCenterService;
private UserService userCenterService;
@Autowired
private ITourismService tourismService;
@@ -190,6 +190,8 @@ public class DashboardController {
@Operation(summary = "获取首页订单列表")
@GetMapping("/orders")
public Result<PageResult<Order>> getDashboardOrders(
@Parameter(description = "租户ID") @RequestParam(required = false) String tenantId,
@Parameter(description = "是否包含子租户") @RequestParam(required = false) Boolean includeChildren,
@Parameter(description = "景区ID") @RequestParam(required = false) Long tourismId,
@Parameter(description = "二维码ID") @RequestParam(required = false) String qrcodeId,
@Parameter(description = "设备编号") @RequestParam(required = false) String deviceSn,
@@ -200,6 +202,19 @@ public class DashboardController {
@Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer limit) {
try {
Map<String, Object> params = new HashMap<>();
// 规范化租户与子租户范围
String anchor = tenantId;
try {
if (anchor == null || anchor.isEmpty()) {
anchor = com.central.common.context.TenantQueryOverrideContext.getTenantId();
if (anchor == null || anchor.isEmpty()) {
anchor = com.central.common.context.LoginUserContextHolder.getCurrentTenantId();
}
}
} catch (Exception ignored) { }
boolean withChildren = (includeChildren == null) || Boolean.TRUE.equals(includeChildren);
params.put("tenantId", anchor);
params.put("includeChildren", withChildren);
params.put("tourismId", tourismId);
params.put("qrcodeId", qrcodeId);
params.put("deviceSn", deviceSn);
@@ -4,6 +4,7 @@ import com.alibaba.csp.sentinel.util.StringUtil;
import com.central.common.constant.CommonConstant;
import com.central.common.context.LoginUserContextHolder;
import com.central.common.context.TenantContextHolder;
import com.central.common.feign.UserService;
import com.central.common.model.LoginAppUser;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
@@ -13,7 +14,6 @@ import com.central.order.enums.CommonStatus;
import com.central.order.enums.OrderStatus;
import com.central.order.feign.DeviceCenterService;
import com.central.order.feign.FileCenterService;
import com.central.order.feign.UserCenterService;
import com.central.order.model.*;
import com.central.order.service.*;
import com.central.wechat.config.WxPayConfigFactory;
@@ -75,7 +75,7 @@ public class OrderController {
private ITourismFileService tourismFileService;
@Autowired
private UserCenterService userCenterService;
private UserService userCenterService;
@Autowired
private FileCenterService fileCenterService;
@@ -2,11 +2,11 @@ package com.central.order.controller;
import com.central.common.context.LoginUserContextHolder;
import com.central.common.context.TenantContextHolder;
import com.central.common.feign.UserService;
import com.central.common.model.*;
import com.central.order.constants.EditTaskStatus;
import com.central.order.enums.OrderStatus;
import com.central.order.feign.FileCenterService;
import com.central.order.feign.UserCenterService;
import com.central.order.model.*;
import com.central.order.service.*;
import com.central.order.util.DateUtils;
@@ -71,7 +71,7 @@ public class OrderFileController {
private String cdnUrl;
@Autowired
private UserCenterService userCenterService;
private UserService userCenterService;
@Autowired
private WxMiniSubscribeService wxMiniSubscribeService;
@@ -1,12 +1,12 @@
package com.central.order.controller;
import com.central.common.context.TenantContextHolder;
import com.central.common.feign.UserService;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.common.model.SysUser;
import com.central.order.enums.OrderStatus;
import com.central.order.enums.RefundState;
import com.central.order.feign.UserCenterService;
import com.central.order.model.Order;
import com.central.order.model.QrCode;
import com.central.order.service.IOrderService;
@@ -53,7 +53,7 @@ public class RefundController {
private IWxTemplateMessageService templateMessageService;
@Autowired
private UserCenterService userCenterService;
private UserService userCenterService;
@Autowired
private WechatConfigService wechatConfigService;
@@ -1,42 +0,0 @@
package com.central.order.feign;
import com.central.common.constant.ServiceNameConstants;
import com.central.common.model.Result;
import com.central.common.model.SysUser;
import com.central.order.feign.fallback.UserCenterServiceFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.Map;
/**
* user-center模块远程调用
*
* @author lsl
* @version 1.0
* @description user-center模块远程调用
* @create 2025/1/13
**/
@FeignClient(name = ServiceNameConstants.USER_SERVICE, fallbackFactory = UserCenterServiceFallbackFactory.class, dismiss404 = true)
public interface UserCenterService {
/**
* 通过ID查询用户信息
*
* @param id 用户ID
* @return
*/
@GetMapping("/users/{id}")
SysUser findUserById(@PathVariable("id") Long id);
/**
* 获取指定租户(含自身)及其所有子孙租户ID
* @param tenantId 锚点租户ID
*/
@GetMapping("/tenant/hierarchy/{tenantId}")
Result<List<String>> getDescendantTenantIds(@PathVariable("tenantId") String tenantId) throws Exception;
}
@@ -1,41 +0,0 @@
package com.central.order.feign.fallback;
import com.central.common.model.Result;
import com.central.common.model.SysUser;
import com.central.order.feign.UserCenterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* 用户中心调用失败回调
*
* @author caiji
* @version 1.0
* @description 用户中心调用失败回调
* @create 2024/8/6 11:33
**/
@Slf4j
@Component
public class UserCenterServiceFallbackFactory implements FallbackFactory<UserCenterService> {
@Override
public UserCenterService create(Throwable cause) {
return new UserCenterService() {
@Override
public SysUser findUserById(Long id) {
return null;
}
@Override
public Result<List<String>> getDescendantTenantIds(String tenantId) {
log.warn("获取子孙租户失败,降级返回仅当前租户: {}, error: {}", tenantId, cause.getMessage());
// 降级策略:返回仅包含当前租户的列表
return Result.succeed(java.util.Collections.singletonList(tenantId));
}
};
}
}
@@ -1,12 +1,12 @@
package com.central.order.listener;
import com.alibaba.fastjson.JSON;
import com.central.common.feign.UserService;
import com.central.common.model.SysUser;
import com.central.order.enums.MessageTemplateEnum;
import com.central.order.enums.OrderStatus;
import com.central.order.enums.OrderStatusChangeEnum;
import com.central.order.event.OrderStatusChangeEvent;
import com.central.order.feign.UserCenterService;
import com.central.order.model.Message;
import com.central.order.model.Order;
import com.central.order.model.QrCode;
@@ -37,7 +37,7 @@ public class OrderStatusChangeListener {
@Autowired
private IOrderService orderService;
@Autowired
private UserCenterService userCenterService;
private UserService userCenterService;
@Autowired
private WxMiniSubscribeService wxMiniSubscribeService;
@@ -471,6 +471,16 @@ 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>",
"</script>"
})
Long getYesterdayOrders(@Param("p") Map<String, Object> params);
@@ -495,16 +505,223 @@ 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>",
"<if test='p.endTime != null and p.endTime != \"\"'>",
" AND o.create_time &lt;= #{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 getCompletedOrders(@Param("p") Map<String, Object> params);
// 每日订单总数、订单总金额(元)、退款订单数、退款金额(元)趋势
@Select({
"<script>",
"SELECT",
" <choose>",
" <when test=\"p.statType == 'year'\"> DATE_FORMAT(o.create_time, '%Y') as date, </when>",
" <when test=\"p.statType == 'month'\"> DATE_FORMAT(o.create_time, '%Y-%m') as date, </when>",
" <otherwise> DATE_FORMAT(o.create_time, '%Y-%m-%d') as date, </otherwise>",
" </choose>",
" '订单数' as type,",
" COUNT(*) as value",
"FROM orders o",
"JOIN qrcodes q ON o.code_id = q.code_id",
"WHERE 1=1",
"<if test='p.includeTestQrcodes == null or p.includeTestQrcodes == false'>",
" AND q.is_test = 0",
"</if>",
"AND o.order_status IN (1, 3, 4, 5, 7, 8, 9)",
"AND (o.pay_type = 1 OR o.pay_type IS NULL)",
"<if test='p.startTime != null and p.startTime != \"\"'>",
" AND o.create_time &gt;= #{p.startTime}",
"</if>",
"<if test='p.endTime != null and p.endTime != \"\"'>",
" AND o.create_time &lt;= #{p.endTime}",
"</if>",
"<if test='p.tourismId != null'>",
" AND o.tourism_id = #{p.tourismId}",
"</if>",
"<if test='p.qrcodeId != null and p.qrcodeId != \"\"'>",
" AND o.code_id = #{p.qrcodeId}",
"</if>",
"<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>",
"GROUP BY ",
"<choose>",
" <when test=\"p.statType == 'year'\"> DATE_FORMAT(o.create_time, '%Y') </when>",
" <when test=\"p.statType == 'month'\"> DATE_FORMAT(o.create_time, '%Y-%m') </when>",
" <otherwise> DATE_FORMAT(o.create_time, '%Y-%m-%d') </otherwise>",
"</choose>",
"UNION ALL",
"SELECT",
" <choose>",
" <when test=\"p.statType == 'year'\"> DATE_FORMAT(o.create_time, '%Y') as date, </when>",
" <when test=\"p.statType == 'month'\"> DATE_FORMAT(o.create_time, '%Y-%m') as date, </when>",
" <otherwise> DATE_FORMAT(o.create_time, '%Y-%m-%d') as date, </otherwise>",
" </choose>",
" '订单金额(元)' as type,",
" COALESCE(SUM(o.order_amount)/100, 0) as value",
"FROM orders o",
"JOIN qrcodes q ON o.code_id = q.code_id",
"WHERE 1=1",
"<if test='p.includeTestQrcodes == null or p.includeTestQrcodes == false'>",
" AND q.is_test = 0",
"</if>",
"AND o.order_status IN (1, 3, 4, 5, 6, 7, 8, 9)",
"AND (o.pay_type = 1 OR o.pay_type IS NULL)",
"<if test='p.startTime != null and p.startTime != \"\"'>",
" AND o.create_time &gt;= #{p.startTime}",
"</if>",
"<if test='p.endTime != null and p.endTime != \"\"'>",
" AND o.create_time &lt;= #{p.endTime}",
"</if>",
"<if test='p.tourismId != null'>",
" AND o.tourism_id = #{p.tourismId}",
"</if>",
"<if test='p.qrcodeId != null and p.qrcodeId != \"\"'>",
" AND o.code_id = #{p.qrcodeId}",
"</if>",
"<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>",
"GROUP BY ",
"<choose>",
" <when test=\"p.statType == 'year'\"> DATE_FORMAT(o.create_time, '%Y') </when>",
" <when test=\"p.statType == 'month'\"> DATE_FORMAT(o.create_time, '%Y-%m') </when>",
" <otherwise> DATE_FORMAT(o.create_time, '%Y-%m-%d') </otherwise>",
"</choose>",
"UNION ALL",
"SELECT",
" <choose>",
" <when test=\"p.statType == 'year'\"> DATE_FORMAT(o.create_time, '%Y') as date, </when>",
" <when test=\"p.statType == 'month'\"> DATE_FORMAT(o.create_time, '%Y-%m') as date, </when>",
" <otherwise> DATE_FORMAT(o.create_time, '%Y-%m-%d') as date, </otherwise>",
" </choose>",
" '退款订单数' as type,",
" SUM(CASE WHEN o.order_status = 8 THEN 1 ELSE 0 END) as value",
"FROM orders o",
"JOIN qrcodes q ON o.code_id = q.code_id",
"WHERE 1=1",
"<if test='p.includeTestQrcodes == null or p.includeTestQrcodes == false'>",
" AND q.is_test = 0",
"</if>",
"AND (o.pay_type = 1 OR o.pay_type IS NULL)",
"<if test='p.startTime != null and p.startTime != \"\"'>",
" AND o.create_time &gt;= #{p.startTime}",
"</if>",
"<if test='p.endTime != null and p.endTime != \"\"'>",
" AND o.create_time &lt;= #{p.endTime}",
"</if>",
"<if test='p.tourismId != null'>",
" AND o.tourism_id = #{p.tourismId}",
"</if>",
"<if test='p.qrcodeId != null and p.qrcodeId != \"\"'>",
" AND o.code_id = #{p.qrcodeId}",
"</if>",
"<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>",
"GROUP BY ",
"<choose>",
" <when test=\"p.statType == 'year'\"> DATE_FORMAT(o.create_time, '%Y') </when>",
" <when test=\"p.statType == 'month'\"> DATE_FORMAT(o.create_time, '%Y-%m') </when>",
" <otherwise> DATE_FORMAT(o.create_time, '%Y-%m-%d') </otherwise>",
"</choose>",
"UNION ALL",
"SELECT",
" <choose>",
" <when test=\"p.statType == 'year'\"> DATE_FORMAT(o.create_time, '%Y') as date, </when>",
" <when test=\"p.statType == 'month'\"> DATE_FORMAT(o.create_time, '%Y-%m') as date, </when>",
" <otherwise> DATE_FORMAT(o.create_time, '%Y-%m-%d') as date, </otherwise>",
" </choose>",
" '退款金额(元)' as type,",
" COALESCE(SUM(CASE WHEN o.order_status = 8 THEN o.order_amount ELSE 0 END)/100, 0) as value",
"FROM orders o",
"JOIN qrcodes q ON o.code_id = q.code_id",
"WHERE 1=1",
"<if test='p.includeTestQrcodes == null or p.includeTestQrcodes == false'>",
" AND q.is_test = 0",
"</if>",
"AND (o.pay_type = 1 OR o.pay_type IS NULL)",
"<if test='p.startTime != null and p.startTime != \"\"'>",
" AND o.create_time &gt;= #{p.startTime}",
"</if>",
"<if test='p.endTime != null and p.endTime != \"\"'>",
" AND o.create_time &lt;= #{p.endTime}",
"</if>",
"<if test='p.tourismId != null'>",
" AND o.tourism_id = #{p.tourismId}",
"</if>",
"<if test='p.qrcodeId != null and p.qrcodeId != \"\"'>",
" AND o.code_id = #{p.qrcodeId}",
"</if>",
"<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>",
"GROUP BY ",
"<choose>",
" <when test=\"p.statType == 'year'\"> DATE_FORMAT(o.create_time, '%Y') </when>",
" <when test=\"p.statType == 'month'\"> DATE_FORMAT(o.create_time, '%Y-%m') </when>",
" <otherwise> DATE_FORMAT(o.create_time, '%Y-%m-%d') </otherwise>",
"</choose>",
"ORDER BY date",
"</script>"
})
List<TrendDataVO> getDailyOrderSummary(@Param("p") Map<String, Object> params);
@Select({
"<script>",
"SELECT",
@@ -538,6 +755,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>",
"<if test='p.startTime != null and p.startTime != \"\"'>",
" AND o.create_time >= #{p.startTime}",
"</if>",
@@ -574,6 +795,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>",
"<if test='p.startTime != null and p.startTime != \"\"'>",
" AND o.create_time >= #{p.startTime}",
"</if>",
@@ -10,13 +10,17 @@ import java.util.List;
public class ChartStatistics {
@Schema(description = "订单趋势")
private List<TrendDataVO> orderTrend;
@Schema(description = "订单状态分布")
private List<DistributionDataVO> orderDistribution;
@Schema(description = "收入构成")
private List<IncomeDataVO> incomeBreakdown;
@Schema(description = "二维码订单数量")
private List<QrcodeOrderDataVO> qrcodeOrderCount;
}
@Schema(description = "每日订单总数与订单总金额(元)")
private List<TrendDataVO> dailySummary;
}
@@ -3,6 +3,7 @@ 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;
@@ -65,12 +66,12 @@ public class DashboardServiceImpl implements IDashboardService {
switch (statType) {
case "year":
// 最近5年
params.put("startTime", now.minusYears(1).withDayOfYear(1).format(formatter));
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.minusMonths(1).withDayOfMonth(1).format(formatter));
params.put("startTime", now.withDayOfMonth(1).format(formatter));
params.put("endTime", now.format(formatter) + " 23:59:59");
break;
case "day":
@@ -83,6 +84,7 @@ public class DashboardServiceImpl implements IDashboardService {
}
List<String> tenantIds = tenantResolverService.resolveTenantIds(tenantId, includeChildren);
params.put("tenantIds", tenantIds);
// 特殊处理:如果是webApp且没有获取到租户列表,则查询所有景区
if ("webApp".equals(tenantId) && (tenantIds == null || tenantIds.isEmpty())) {
@@ -97,7 +99,6 @@ public class DashboardServiceImpl implements IDashboardService {
tenantIds.size(), tourismIds.size(), params.get("startTime"), params.get("endTime"));
} else {
// 没有找到景区,设置一个不存在的ID,避免查询到所有数据
params.put("tourismIds", java.util.Collections.singletonList(-1L));
log.warn("租户 {} 没有关联的景区", tenantIds);
}
}
@@ -126,6 +127,35 @@ public class DashboardServiceImpl implements IDashboardService {
@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) {}
List<String> tenantIds = tenantResolverService.resolveTenantIds(tenantId, includeChildren);
params.put("tenantIds", tenantIds);
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());
}
// 获取订单列表
PageResult<Order> result = orderService.findList(params);
@@ -224,15 +254,19 @@ public class DashboardServiceImpl implements IDashboardService {
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());
}
// 获取收入构成数据
List<IncomeDataVO> incomeBreakdown = dashboardMapper.getIncomeBreakdown(params);
chartStats.setIncomeBreakdown(incomeBreakdown);
// 添加收入统计调试日志
log.info("收入构成统计 - 参数: {}, 结果数量: {}", params, incomeBreakdown.size());
for (IncomeDataVO item : incomeBreakdown) {
log.info("收入构成 - 类型: {}, 金额: {}", item.getType(), item.getValue());
}
// 每日订单总数与总金额(元)
List<TrendDataVO> dailySummary = dashboardMapper.getDailyOrderSummary(params);
chartStats.setDailySummary(dailySummary);
// 获取二维码订单数量数据
List<QrcodeOrderDataVO> qrcodeOrderCount = dashboardMapper.getQrcodeOrderCount(params);
@@ -244,6 +278,7 @@ public class DashboardServiceImpl implements IDashboardService {
chartStats.setOrderTrend(new ArrayList<>());
chartStats.setOrderDistribution(new ArrayList<>());
chartStats.setIncomeBreakdown(new ArrayList<>());
chartStats.setDailySummary(new ArrayList<>());
chartStats.setQrcodeOrderCount(new ArrayList<>());
}
@@ -266,93 +301,6 @@ public class DashboardServiceImpl implements IDashboardService {
return options;
}
/**
* 解析租户ID集合(包含子租户)
* @deprecated 已迁移到TenantResolverService
*/
@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);
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);
}
/**
* 解析当前请求上下文的租户集合(包含子租户)
@@ -369,7 +317,7 @@ public class DashboardServiceImpl implements IDashboardService {
private List<Long> resolveTourismIdsByTenants(List<String> tenantIds) {
try {
if (tenantIds == null || tenantIds.isEmpty()) return java.util.Collections.emptyList();
// 先按景区表直接匹配租户ID
// 先按景区表直接匹配租户ID】
List<Long> byTourism = tourismMapper.findTourismIdsByTenantIds(tenantIds);
java.util.Set<Long> union = new java.util.LinkedHashSet<>();
if (byTourism != null) union.addAll(byTourism);
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.central.common.context.TenantContextHolder;
import com.central.common.feign.UserService;
import com.central.common.lock.DistributedLock;
import com.central.common.model.PageResult;
import com.central.common.model.SysUser;
@@ -11,7 +12,6 @@ import com.central.common.service.impl.SuperServiceImpl;
import com.central.order.enums.OrderStatus;
import com.central.order.event.OrderStatusChangeEvent;
import com.central.order.feign.FileCenterService;
import com.central.order.feign.UserCenterService;
import com.central.order.mapper.OrderFileMapper;
import com.central.order.mapper.OrderMapper;
import com.central.order.mapper.TourismFileMapper;
@@ -74,7 +74,7 @@ public class OrderServiceImpl extends SuperServiceImpl<OrderMapper, Order> imple
private ApplicationEventPublisher eventPublisher;
@Autowired
private UserCenterService userCenterService;
private UserService userCenterService;
@Autowired
private FileCenterService fileCenterService;
@@ -2,8 +2,8 @@ package com.central.order.service.impl;
import com.central.common.context.LoginUserContextHolder;
import com.central.common.context.TenantQueryOverrideContext;
import com.central.common.feign.UserService;
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;
@@ -21,7 +21,7 @@ import java.util.List;
@RequiredArgsConstructor
public class TenantResolverService {
private final UserCenterService userCenterService;
private final UserService userCenterService;
/**
* 解析租户ID集合(包含子租户)
@@ -58,23 +58,11 @@ public class TenantResolverService {
// 通过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");
}
List<String> tenantIds = userCenterService.getSonTenantIds(anchor);
return tenantIds;
} catch (Exception e) {
log.error("获取租户层级失败: {}", e.getMessage(), e);
}
// 降级:只返回当前租户
log.warn("获取子孙租户失败,降级为仅当前租户: {}", anchor);
return Collections.singletonList(anchor);
@@ -1,11 +1,11 @@
package com.central.order.service.impl;
import com.central.common.exception.BusinessException;
import com.central.common.feign.UserService;
import com.central.common.model.FileInfo;
import com.central.common.model.SysUser;
import com.central.order.constants.EditTaskStatus;
import com.central.order.feign.FileCenterService;
import com.central.order.feign.UserCenterService;
import com.central.order.model.EditTask;
import com.central.order.model.Order;
import com.central.order.model.OrderFile;
@@ -40,7 +40,7 @@ public class VideoEditServiceImpl implements IVideoEditService {
@Autowired
private IOrderService orderService;
@Autowired
private UserCenterService userCenterService;
private UserService userCenterService;
@Autowired
private ITourismService tourismService;
@Autowired
@@ -8,6 +8,7 @@
FROM orders o
LEFT JOIN tourisms t ON o.tourism_id = t.id
<where>
<!-- 业务筛选条件 -->
<if test="p.orderNo != null and p.orderNo != ''">
AND o.order_no LIKE CONCAT('%', #{p.orderNo}, '%')
</if>
@@ -20,15 +21,33 @@
<if test="p.deviceSn != null and p.deviceSn != ''">
AND o.device_sn LIKE CONCAT('%', #{p.deviceSn}, '%')
</if>
<!-- 兼容 dashboard 透传的 qrcodeId 与现有 codeId 参数名 -->
<if test="p.codeId != null and p.codeId != ''">
AND o.code_id LIKE CONCAT('%', #{p.codeId}, '%')
</if>
<if test="p.qrcodeId != null and p.qrcodeId != ''">
AND o.code_id LIKE CONCAT('%', #{p.qrcodeId}, '%')
</if>
<if test="p.userId != null and p.userId != ''">
AND o.user_id LIKE CONCAT('%', #{p.userId}, '%')
</if>
<if test="p.mobile != null and p.mobile != ''">
AND o.mobile LIKE CONCAT('%', #{p.mobile}, '%')
</if>
<!-- 时间范围(与仪表盘统计口径一致) -->
<if test="p.startTime != null and p.startTime != ''">
AND o.create_time &gt;= #{p.startTime}
</if>
<if test="p.endTime != null and p.endTime != ''">
AND o.create_time &lt;= #{p.endTime}
</if>
<!-- 景区集合过滤(由租户+子孙→景区ID集合的去重结果提供) -->
<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>
</where>
ORDER BY o.create_time DESC
</select>
@@ -1,9 +1,6 @@
package com.central.user.controller;
import com.central.common.model.CodeEnum;
import com.central.common.model.Result;
import com.central.common.model.Tenant;
import com.central.common.model.TenantInfo;
import com.central.common.model.*;
import com.central.common.service.ITenantHierarchyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
@@ -171,6 +168,18 @@ public class TenantHierarchyController {
}
}
/**
* 获取自己以及子孙租户
*
* @param tenantId 租户id
*/
@GetMapping(value = "/son/tenantId", params = "tenantId")
@Operation(summary = "获取自己以及子孙租户")
public List<String> getSonTenantIds(String tenantId) {
return tenantHierarchyService.getAllSubTenantIds(tenantId);
}
private String normalizePathForSave(String rawPath, String parentId, String tenantId) {
try {
String path = stripPathSafe(rawPath);