Files
gofly-platform-services/zlt-doc/多租户订单统计功能测试指南.md
2025-10-01 05:05:23 +08:00

218 lines
7.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 多租户订单统计功能测试指南
## 功能概述
实现了基于多租户层级的订单统计功能,能够根据当前租户统计自己以及所有子孙租户的订单数据,支持日、月、年三种时间维度。
## 实现架构
### 统计逻辑流程
1. **租户层级解析**:根据当前租户ID获取所有子孙租户ID列表
2. **景区关联查询**:根据租户ID列表查询所有关联的景区
3. **订单数据统计**:根据景区ID列表统计相关订单数据
### 核心文件改动
#### 后端改动
1. **DashboardController.java** (F:\project\go-fly-platform-services\zlt-business\order-center\src\main\java\com\central\order\controller\DashboardController.java:36-56)
- 新增参数:`tenantId`(租户ID)、`includeChildren`(是否包含子租户)、`statType`(统计类型:day/month/year)
2. **DashboardServiceImpl.java** (F:\project\go-fly-platform-services\zlt-business\order-center\src\main\java\com\central\order\service\impl\DashboardServiceImpl.java:36-95)
- 实现租户层级解析逻辑
- 支持本地服务和远程Feign调用两种方式获取子孙租户
- 根据statType自动计算时间范围
3. **TenantHierarchyController.java** (F:\project\go-fly-platform-services\zlt-business\user-center\src\main\java\com\central\user\controller\TenantHierarchyController.java:159-172)
- 提供获取子孙租户ID的REST接口
#### 前端支持
1. **Welcome.tsx** (F:\project\go-fly-platform-services\zlt-web\react-web-next\src\pages\Welcome.tsx)
- 已集成租户选择器和时间维度切换组件
- 支持按租户和时间维度查询统计数据
## 测试步骤
### 1. 准备测试数据
```sql
-- 创建测试租户层级(如果尚未创建)
-- 父租户
INSERT INTO sys_tenant (tenant_id, tenant_name, parent_tenant_id, tenant_level, tenant_path, status)
VALUES ('tenant-001', '总公司', null, 1, 'tenant-001', 1);
-- 子租户
INSERT INTO sys_tenant (tenant_id, tenant_name, parent_tenant_id, tenant_level, tenant_path, status)
VALUES ('tenant-002', '分公司A', 'tenant-001', 2, 'tenant-001/tenant-002', 1);
INSERT INTO sys_tenant (tenant_id, tenant_name, parent_tenant_id, tenant_level, tenant_path, status)
VALUES ('tenant-003', '分公司B', 'tenant-001', 2, 'tenant-001/tenant-003', 1);
-- 孙租户
INSERT INTO sys_tenant (tenant_id, tenant_name, parent_tenant_id, tenant_level, tenant_path, status)
VALUES ('tenant-004', '办事处A1', 'tenant-002', 3, 'tenant-001/tenant-002/tenant-004', 1);
-- 创建测试景区
INSERT INTO tourisms (id, name, tenant_id, is_del) VALUES
(1, '景区1-总公司', 'tenant-001', 0),
(2, '景区2-分公司A', 'tenant-002', 0),
(3, '景区3-分公司B', 'tenant-003', 0),
(4, '景区4-办事处A1', 'tenant-004', 0);
-- 创建测试订单
INSERT INTO orders (tourism_id, order_status, pay_type, create_time, amount) VALUES
-- 总公司订单
(1, 3, 1, '2025-01-01 10:00:00', 10000),
(1, 3, 1, '2025-01-02 10:00:00', 20000),
-- 分公司A订单
(2, 3, 1, '2025-01-01 11:00:00', 15000),
(2, 8, 1, '2025-01-02 11:00:00', 5000), -- 退款订单
-- 分公司B订单
(3, 3, 1, '2025-01-01 12:00:00', 30000),
-- 办事处A1订单
(4, 3, 1, '2025-01-01 13:00:00', 8000);
```
### 2. API测试
#### 测试场景1:查询总公司(含所有子孙)的订单统计
```bash
curl -X GET "http://localhost:9900/api-order/dashboard/statistics?tenantId=tenant-001&includeChildren=true&statType=day" \
-H "Authorization: Bearer {your_token}"
```
预期结果:
- 应返回所有4个景区的订单总和
- totalOrders: 6
- totalAmount: 88000 (分)
#### 测试场景2:查询分公司A(含子孙)的订单统计
```bash
curl -X GET "http://localhost:9900/api-order/dashboard/statistics?tenantId=tenant-002&includeChildren=true&statType=day" \
-H "Authorization: Bearer {your_token}"
```
预期结果:
- 应返回景区2和景区4的订单
- totalOrders: 3
- totalAmount: 28000 (分)
#### 测试场景3:查询分公司B(不含子孙)的订单统计
```bash
curl -X GET "http://localhost:9900/api-order/dashboard/statistics?tenantId=tenant-003&includeChildren=false&statType=day" \
-H "Authorization: Bearer {your_token}"
```
预期结果:
- 仅返回景区3的订单
- totalOrders: 1
- totalAmount: 30000 (分)
#### 测试场景4:按月统计
```bash
curl -X GET "http://localhost:9900/api-order/dashboard/statistics?tenantId=tenant-001&includeChildren=true&statType=month" \
-H "Authorization: Bearer {your_token}"
```
预期结果:
- 返回最近12个月的统计数据
- 订单趋势按月分组
#### 测试场景5:按年统计
```bash
curl -X GET "http://localhost:9900/api-order/dashboard/statistics?tenantId=tenant-001&includeChildren=true&statType=year" \
-H "Authorization: Bearer {your_token}"
```
预期结果:
- 返回最近5年的统计数据
- 订单趋势按年分组
### 3. 前端界面测试
1. 登录系统,进入首页
2. 在右上角选择不同的租户
3. 切换"包含子租户"开关,观察数据变化
4. 切换统计粒度(日/月/年),观察图表变化
5. 验证数据准确性:
- 订单总数是否正确
- 金额计算是否准确
- 趋势图是否按时间维度正确显示
### 4. 数据准确性验证SQL
```sql
-- 验证租户001及其子孙的订单总数
SELECT COUNT(*) as total_orders, SUM(amount) as total_amount
FROM orders o
JOIN tourisms t ON o.tourism_id = t.id
WHERE t.tenant_id IN (
WITH RECURSIVE tenant_tree AS (
SELECT tenant_id FROM sys_tenant WHERE tenant_id = 'tenant-001'
UNION ALL
SELECT st.tenant_id
FROM sys_tenant st
JOIN tenant_tree tt ON st.parent_tenant_id = tt.tenant_id
)
SELECT tenant_id FROM tenant_tree
)
AND o.order_status IN (1, 3, 4, 5, 7, 8, 9)
AND (o.pay_type = 1 OR o.pay_type IS NULL);
-- 按日分组统计
SELECT DATE_FORMAT(o.create_time, '%Y-%m-%d') as date,
COUNT(*) as order_count,
SUM(o.amount) as amount
FROM orders o
JOIN tourisms t ON o.tourism_id = t.id
WHERE t.tenant_id IN ('tenant-001', 'tenant-002', 'tenant-003', 'tenant-004')
AND o.order_status IN (1, 3, 4, 5, 7, 8, 9)
GROUP BY DATE_FORMAT(o.create_time, '%Y-%m-%d')
ORDER BY date;
```
## 注意事项
1. **性能优化**
- 对于大租户树,建议启用应用层缓存租户ID列表
- 确保 `tourisms.tenant_id` 和 `orders.tourism_id` 有索引
2. **权限控制**
- 管理端用户(BACKEND)默认可切换租户
- C端用户(APP)仅能查看自身租户数据
3. **故障降级**
- 当远程获取租户层级失败时,自动降级为仅查询当前租户
4. **日志监控**
- 关注日志中的 "统计范围" 信息,确认租户和景区范围正确
- 如出现 "远程获取租户层级失败" 警告,检查user-center服务状态
## 常见问题
### Q1: 统计数据为空?
- 检查当前租户是否有关联的景区
- 检查景区是否有订单数据
- 查看后端日志确认租户ID和景区ID列表
### Q2: 子租户数据未包含?
- 确认 `includeChildren` 参数为 true
- 检查 sys_tenant 表中的 parent_tenant_id 关系是否正确
- 验证 TenantHierarchyService 是否正确返回子孙租户
### Q3: 时间维度切换无效?
- 确认 `statType` 参数值为 day/month/year 之一
- 检查 DashboardMapper 中的 SQL 是否正确处理时间分组
## 性能测试建议
1. 模拟大量租户层级(100+ 租户,3-5层深度)
2. 每个租户关联 5-10 个景区
3. 每个景区生成 1000+ 订单
4. 测试不同时间维度的查询响应时间
5. 监控数据库查询性能,必要时添加索引
## 后续优化建议
1. 添加租户层级缓存机制,减少递归查询
2. 实现订单数据预聚合,提升统计查询性能
3. 支持更多统计维度(按景区、按设备、按二维码等)
4. 添加数据导出功能(Excel/CSV)