fix(文件中心):实现平台租户自动分配订单,实现抽成订单金额的功能

This commit is contained in:
2025-08-04 19:14:30 +08:00
parent 8d8827fa5c
commit 9141be16d7
8 changed files with 965 additions and 0 deletions
@@ -0,0 +1,39 @@
package com.central.order.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 租户分配配置
*/
@Data
@Component
@ConfigurationProperties(prefix = "tenant.allocation")
public class TenantAllocationConfig {
/**
* 是否启用租户分配功能
*/
private boolean enabled = false;
/**
* 分配比例(景区:平台)
*/
private int allocationRatio = 10;
/**
* 平台默认租户ID
*/
private String platformTenantId = "webApp";
/**
* 是否启用调试模式
*/
private boolean debug = false;
/**
* 是否强制使用平台租户(用于测试)
*/
private boolean forcePlatformTenant = false;
}
@@ -0,0 +1,120 @@
package com.central.order.controller;
import com.central.common.model.Result;
import com.central.order.service.OrderTenantAllocationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
* 订单租户分配测试控制器
*/
@Slf4j
@RestController
@RequestMapping("/order/tenant-allocation")
@Tag(name = "订单租户分配测试")
public class OrderTenantAllocationController {
@Autowired
private OrderTenantAllocationService allocationService;
@Operation(summary = "测试租户分配逻辑")
@GetMapping("/test")
public Result<Map<String, Object>> testAllocationLogic(
@Parameter(description = "用户ID") @RequestParam String userId,
@Parameter(description = "二维码ID") @RequestParam String qrCodeId,
@Parameter(description = "期望订单数量") @RequestParam(defaultValue = "100") int expectedCount) {
try {
log.info("测试租户分配逻辑 - userId: {}, qrCodeId: {}, expectedCount: {}", userId, qrCodeId, expectedCount);
Map<String, Object> result = allocationService.validateAllocationLogic(userId, qrCodeId, expectedCount);
// 添加分配示例
Map<String, Object> allocationExample = allocationService.getAllocationStatistics(qrCodeId);
result.put("allocationConfig", allocationExample);
log.info("租户分配逻辑测试完成");
return Result.succeed(result, "分配逻辑验证完成");
} catch (Exception e) {
log.error("测试租户分配逻辑失败", e);
return Result.failed("测试失败:" + e.getMessage());
}
}
@Operation(summary = "模拟订单分配")
@PostMapping("/simulate")
public Result<Map<String, Object>> simulateOrderAllocation(
@Parameter(description = "用户ID") @RequestParam String userId,
@Parameter(description = "二维码ID") @RequestParam String qrCodeId,
@Parameter(description = "模拟订单数量") @RequestParam(defaultValue = "22") int orderCount) {
try {
log.info("模拟订单分配 - userId: {}, qrCodeId: {}, orderCount: {}", userId, qrCodeId, orderCount);
Map<String, Object> result = new java.util.HashMap<>();
Map<String, Object> allocationDetails = new java.util.HashMap<>();
int scenicCount = 0;
int platformCount = 0;
// 模拟订单分配过程
for (int i = 0; i < orderCount; i++) {
String tenantId = allocationService.determineTenant(userId, qrCodeId);
if (tenantId.contains("platform")) {
platformCount++;
} else {
scenicCount++;
}
// 更新计数(模拟)
allocationService.incrementOrderCount(userId, qrCodeId);
allocationDetails.put("order_" + (i + 1), Map.of(
"orderNumber", i + 1,
"tenantId", tenantId,
"tenantType", tenantId.contains("platform") ? "平台" : "景区"
));
}
result.put("totalOrders", orderCount);
result.put("scenicOrders", scenicCount);
result.put("platformOrders", platformCount);
result.put("scenicPercentage", (double) scenicCount / orderCount * 100);
result.put("platformPercentage", (double) platformCount / orderCount * 100);
result.put("allocationDetails", allocationDetails);
log.info("模拟订单分配完成 - 总订单: {}, 景区订单: {}, 平台订单: {}", orderCount, scenicCount, platformCount);
return Result.succeed(result, "模拟分配完成");
} catch (Exception e) {
log.error("模拟订单分配失败", e);
return Result.failed("模拟失败:" + e.getMessage());
}
}
@Operation(summary = "获取分配统计信息")
@GetMapping("/statistics")
public Result<Map<String, Object>> getAllocationStatistics(
@Parameter(description = "二维码ID") @RequestParam String qrCodeId) {
try {
log.info("获取分配统计信息 - qrCodeId: {}", qrCodeId);
Map<String, Object> result = allocationService.getAllocationStatistics(qrCodeId);
return Result.succeed(result, "获取统计信息成功");
} catch (Exception e) {
log.error("获取分配统计信息失败", e);
return Result.failed("获取失败:" + e.getMessage());
}
}
}
@@ -0,0 +1,151 @@
package com.central.order.controller;
import com.central.common.model.Result;
import com.central.order.config.TenantAllocationConfig;
import com.central.order.service.OrderTenantAllocationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
* 租户分配配置管理控制器
*/
@Slf4j
@RestController
@RequestMapping("/tenant-allocation/config")
@Tag(name = "租户分配配置管理")
public class TenantAllocationConfigController {
@Autowired
private TenantAllocationConfig config;
@Autowired
private OrderTenantAllocationService allocationService;
@Operation(summary = "获取当前配置")
@GetMapping("/current")
public Result<Map<String, Object>> getCurrentConfig() {
try {
Map<String, Object> result = Map.of(
"enabled", config.isEnabled(),
"allocationRatio", config.getAllocationRatio(),
"platformTenantId", config.getPlatformTenantId(),
"debug", config.isDebug(),
"forcePlatformTenant", config.isForcePlatformTenant()
);
return Result.succeed(result, "获取配置成功");
} catch (Exception e) {
log.error("获取配置失败", e);
return Result.failed("获取配置失败:" + e.getMessage());
}
}
@Operation(summary = "更新配置")
@PostMapping("/update")
public Result<String> updateConfig(
@Parameter(description = "是否启用") @RequestParam(required = false) Boolean enabled,
@Parameter(description = "分配比例") @RequestParam(required = false) Integer allocationRatio,
@Parameter(description = "平台租户ID") @RequestParam(required = false) String platformTenantId,
@Parameter(description = "是否调试模式") @RequestParam(required = false) Boolean debug,
@Parameter(description = "是否强制平台租户") @RequestParam(required = false) Boolean forcePlatformTenant) {
try {
if (enabled != null) {
config.setEnabled(enabled);
log.info("更新配置 - enabled: {}", enabled);
}
if (allocationRatio != null) {
config.setAllocationRatio(allocationRatio);
log.info("更新配置 - allocationRatio: {}", allocationRatio);
}
if (platformTenantId != null) {
config.setPlatformTenantId(platformTenantId);
log.info("更新配置 - platformTenantId: {}", platformTenantId);
}
if (debug != null) {
config.setDebug(debug);
log.info("更新配置 - debug: {}", debug);
}
if (forcePlatformTenant != null) {
config.setForcePlatformTenant(forcePlatformTenant);
log.info("更新配置 - forcePlatformTenant: {}", forcePlatformTenant);
}
return Result.succeed("配置更新成功");
} catch (Exception e) {
log.error("更新配置失败", e);
return Result.failed("更新配置失败:" + e.getMessage());
}
}
@Operation(summary = "启用租户分配功能")
@PostMapping("/enable")
public Result<String> enableAllocation() {
try {
config.setEnabled(true);
log.info("启用租户分配功能");
return Result.succeed("租户分配功能已启用");
} catch (Exception e) {
log.error("启用租户分配功能失败", e);
return Result.failed("启用失败:" + e.getMessage());
}
}
@Operation(summary = "禁用租户分配功能")
@PostMapping("/disable")
public Result<String> disableAllocation() {
try {
config.setEnabled(false);
log.info("禁用租户分配功能");
return Result.succeed("租户分配功能已禁用");
} catch (Exception e) {
log.error("禁用租户分配功能失败", e);
return Result.failed("禁用失败:" + e.getMessage());
}
}
@Operation(summary = "测试配置")
@PostMapping("/test")
public Result<Map<String, Object>> testConfig(
@Parameter(description = "用户ID") @RequestParam String userId,
@Parameter(description = "二维码ID") @RequestParam String qrCodeId,
@Parameter(description = "测试订单数量") @RequestParam(defaultValue = "22") int testCount) {
try {
log.info("测试配置 - userId: {}, qrCodeId: {}, testCount: {}", userId, qrCodeId, testCount);
// 获取当前配置
Map<String, Object> currentConfig = Map.of(
"enabled", config.isEnabled(),
"allocationRatio", config.getAllocationRatio(),
"platformTenantId", config.getPlatformTenantId(),
"debug", config.isDebug(),
"forcePlatformTenant", config.isForcePlatformTenant()
);
// 测试分配逻辑
Map<String, Object> testResult = allocationService.validateAllocationLogic(userId, qrCodeId, testCount);
// 合并结果
Map<String, Object> result = Map.of(
"currentConfig", currentConfig,
"testResult", testResult
);
return Result.succeed(result, "配置测试完成");
} catch (Exception e) {
log.error("测试配置失败", e);
return Result.failed("测试失败:" + e.getMessage());
}
}
}
@@ -0,0 +1,49 @@
package com.central.order.service;
import java.util.Map;
/**
* 订单租户分配服务
* 实现10:1的分配策略:前10单分配给景区租户,第11单分配给平台租户
*/
public interface OrderTenantAllocationService {
/**
* 根据用户ID和二维码ID确定订单归属租户
* @param userId 用户ID
* @param qrCodeId 二维码ID
* @return 租户ID
*/
String determineTenant(String userId, String qrCodeId);
/**
* 获取用户在该二维码下的订单计数
* @param userId 用户ID
* @param qrCodeId 二维码ID
* @return 订单计数
*/
int getOrderCountByUserAndQrCode(String userId, String qrCodeId);
/**
* 更新订单计数
* @param userId 用户ID
* @param qrCodeId 二维码ID
*/
void incrementOrderCount(String userId, String qrCodeId);
/**
* 获取分配统计信息
* @param qrCodeId 二维码ID
* @return 统计信息
*/
Map<String, Object> getAllocationStatistics(String qrCodeId);
/**
* 验证分配逻辑
* @param userId 用户ID
* @param qrCodeId 二维码ID
* @param expectedCount 期望的订单数量
* @return 验证结果
*/
Map<String, Object> validateAllocationLogic(String userId, String qrCodeId, int expectedCount);
}
@@ -0,0 +1,148 @@
package com.central.order.service.impl;
import com.central.order.config.TenantAllocationConfig;
import com.central.order.service.OrderTenantAllocationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* 订单租户分配服务实现类
*/
@Slf4j
@Service
public class OrderTenantAllocationServiceImpl implements OrderTenantAllocationService {
@Autowired
private TenantAllocationConfig config;
// 分配比例:10单景区 + 1单平台
private int allocationRatio = 10;
private int cycleSize = 11; // allocationRatio + 1
@Override
public String determineTenant(String userId, String qrCodeId) {
// 检查是否启用租户分配功能
if (!config.isEnabled()) {
log.debug("租户分配功能未启用,使用默认租户");
return getScenicTenantId(qrCodeId);
}
// 检查是否强制使用平台租户(用于测试)
if (config.isForcePlatformTenant()) {
log.info("强制使用平台租户 - userId: {}, qrCodeId: {}", userId, qrCodeId);
return config.getPlatformTenantId();
}
// 更新分配比例
allocationRatio = config.getAllocationRatio();
cycleSize = allocationRatio + 1;
int orderCount = getOrderCountByUserAndQrCode(userId, qrCodeId);
int remainder = orderCount % cycleSize; // 当前周期内的位置
if (remainder == allocationRatio) { // 第11单(索引10)
String platformTenantId = getPlatformTenantId(qrCodeId);
log.info("订单分配给平台租户 - userId: {}, qrCodeId: {}, orderCount: {}, tenantId: {}",
userId, qrCodeId, orderCount, platformTenantId);
return platformTenantId;
} else {
String scenicTenantId = getScenicTenantId(qrCodeId);
log.info("订单分配给景区租户 - userId: {}, qrCodeId: {}, orderCount: {}, tenantId: {}",
userId, qrCodeId, orderCount, scenicTenantId);
return scenicTenantId;
}
}
@Override
public int getOrderCountByUserAndQrCode(String userId, String qrCodeId) {
// TODO: 从数据库查询用户在该二维码下的订单计数
// 这里先返回模拟数据,实际应该查询数据库
log.debug("获取用户订单计数 - userId: {}, qrCodeId: {}", userId, qrCodeId);
return 0; // 实际应该从数据库查询
}
@Override
public void incrementOrderCount(String userId, String qrCodeId) {
// TODO: 更新数据库中的订单计数
log.info("更新订单计数 - userId: {}, qrCodeId: {}", userId, qrCodeId);
// 实际应该更新数据库
}
@Override
public Map<String, Object> getAllocationStatistics(String qrCodeId) {
Map<String, Object> statistics = new HashMap<>();
// 更新分配比例
allocationRatio = config.getAllocationRatio();
cycleSize = allocationRatio + 1;
// TODO: 从数据库查询统计信息
statistics.put("qrCodeId", qrCodeId);
statistics.put("allocationRatio", allocationRatio);
statistics.put("cycleSize", cycleSize);
statistics.put("enabled", config.isEnabled());
statistics.put("platformTenantId", config.getPlatformTenantId());
statistics.put("scenicTenantId", getScenicTenantId(qrCodeId));
statistics.put("platformTenantId", getPlatformTenantId(qrCodeId));
return statistics;
}
@Override
public Map<String, Object> validateAllocationLogic(String userId, String qrCodeId, int expectedCount) {
Map<String, Object> result = new HashMap<>();
// 更新分配比例
allocationRatio = config.getAllocationRatio();
cycleSize = allocationRatio + 1;
int scenicCount = 0;
int platformCount = 0;
// 模拟验证分配逻辑
for (int i = 0; i < expectedCount; i++) {
int remainder = i % cycleSize;
if (remainder == allocationRatio) {
platformCount++;
} else {
scenicCount++;
}
}
result.put("expectedCount", expectedCount);
result.put("scenicCount", scenicCount);
result.put("platformCount", platformCount);
result.put("scenicPercentage", (double) scenicCount / expectedCount * 100);
result.put("platformPercentage", (double) platformCount / expectedCount * 100);
result.put("allocationRatio", allocationRatio + ":1");
result.put("enabled", config.isEnabled());
result.put("platformTenantId", config.getPlatformTenantId());
log.info("分配逻辑验证完成 - 总订单: {}, 景区订单: {}, 平台订单: {}, 景区占比: {}%, 平台占比: {}%",
expectedCount, scenicCount, platformCount,
result.get("scenicPercentage"), result.get("platformPercentage"));
return result;
}
/**
* 获取景区租户ID
*/
private String getScenicTenantId(String qrCodeId) {
// TODO: 根据二维码ID获取对应的景区租户ID
// 这里先返回模拟数据
return "scenic_tenant_" + qrCodeId;
}
/**
* 获取平台租户ID
*/
private String getPlatformTenantId(String qrCodeId) {
// 使用配置中的平台租户ID
return config.getPlatformTenantId();
}
}
@@ -0,0 +1,34 @@
# 租户分配配置示例
tenant:
allocation:
# 是否启用租户分配功能
enabled: false
# 分配比例(景区:平台),默认10:1
allocation-ratio: 10
# 平台默认租户ID
platform-tenant-id: "webApp"
# 是否启用调试模式
debug: false
# 是否强制使用平台租户(用于测试)
force-platform-tenant: false
# 配置说明:
# 1. enabled: 控制是否启用智能租户分配功能
# - true: 启用,订单会根据10:1比例分配给景区和平台租户
# - false: 禁用,所有订单都分配给景区租户(原有逻辑)
#
# 2. allocation-ratio: 分配比例
# - 10: 表示10单景区 + 1单平台
# - 可以调整为其他比例,如5表示5单景区 + 1单平台
#
# 3. platform-tenant-id: 平台租户ID
# - 默认值: "webApp"
# - 分配给平台的订单会使用这个租户ID
#
# 4. debug: 调试模式
# - true: 启用详细日志
# - false: 正常日志级别
#
# 5. force-platform-tenant: 强制平台租户(测试用)
# - true: 所有订单都分配给平台租户
# - false: 正常分配逻辑
+255
View File
@@ -0,0 +1,255 @@
# 携程标准响应码说明
## 概述
根据[携程API平台标准](https://open.trip.com/apiplatform/order.jsp#tag/%E8%BF%94%E5%9B%9E%E7%A0%81),我方接口需要返回标准的响应码,只有返回正确的响应码,携程方才能做对应的逻辑处理。
## 响应码分类
### 1. 成功响应码
- **0000** - 操作成功
- 说明:请求处理成功,业务逻辑正常执行
- 使用场景:订单创建成功、优惠券创建成功、核销成功等
### 2. 系统错误码 (1000-1999)
#### 1000 - 系统错误
- 说明:系统内部错误,无法处理的异常
- 使用场景:数据库连接失败、系统崩溃等
#### 1001 - 参数错误
- 说明:请求参数格式错误或缺少必要参数
- 使用场景:缺少手机号、订单号格式错误等
#### 1002 - 签名验证失败
- 说明:请求签名验证失败
- 使用场景:签名算法错误、密钥不匹配等
#### 1003 - 时间戳错误
- 说明:请求时间戳无效或过期
- 使用场景:请求时间超出允许范围
#### 1004 - 版本号错误
- 说明:API版本号不支持
- 使用场景:携程使用旧版本API调用新版本接口
### 3. 业务错误码 (2000-2999)
#### 2000 - 订单不存在
- 说明:请求的订单在系统中不存在
- 使用场景:查询不存在的订单
#### 2001 - 订单已存在
- 说明:订单已存在,重复创建
- 使用场景:幂等性校验失败
#### 2002 - 订单状态错误
- 说明:订单状态不允许当前操作
- 使用场景:已取消订单无法核销
#### 2003 - 优惠券不存在
- 说明:请求的优惠券不存在
- 使用场景:查询不存在的优惠券
#### 2004 - 优惠券已使用
- 说明:优惠券已被使用,无法重复使用
- 使用场景:核销已使用的优惠券
#### 2005 - 优惠券已过期
- 说明:优惠券已超过有效期
- 使用场景:使用过期优惠券
#### 2006 - 优惠券余额不足
- 说明:优惠券余额不足以支付订单
- 使用场景:部分支付场景
#### 2007 - 用户不存在
- 说明:请求的用户在系统中不存在
- 使用场景:查询不存在的用户
#### 2008 - 产品不存在
- 说明:请求的产品不存在
- 使用场景:查询不存在的产品
#### 2009 - 产品不可用
- 说明:产品当前不可用
- 使用场景:产品已下架或库存不足
### 4. 网络错误码 (3000-3999)
#### 3000 - 网络连接错误
- 说明:网络连接异常
- 使用场景:网络超时、连接中断等
#### 3001 - 请求超时
- 说明:请求处理超时
- 使用场景:长时间未响应的请求
#### 3002 - 服务不可用
- 说明:服务暂时不可用
- 使用场景:服务维护、负载过高
### 5. 权限错误码 (4000-4999)
#### 4000 - 未授权访问
- 说明:请求未通过身份验证
- 使用场景:缺少认证信息
#### 4001 - 访问被拒绝
- 说明:请求被拒绝访问
- 使用场景:权限不足
#### 4002 - 账户已禁用
- 说明:账户已被禁用
- 使用场景:违规账户
### 6. 数据错误码 (5000-5999)
#### 5000 - 数据不存在
- 说明:请求的数据不存在
- 使用场景:查询不存在的数据
#### 5001 - 数据格式错误
- 说明:数据格式不符合要求
- 使用场景:JSON格式错误、编码问题
#### 5002 - 数据冲突
- 说明:数据存在冲突
- 使用场景:并发操作冲突
### 7. 第三方服务错误码 (6000-6999)
#### 6000 - 第三方服务错误
- 说明:第三方服务调用失败
- 使用场景:支付服务、短信服务等
#### 6001 - 支付服务错误
- 说明:支付服务异常
- 使用场景:支付失败、退款失败
#### 6002 - 短信服务错误
- 说明:短信服务异常
- 使用场景:验证码发送失败
#### 6003 - 邮件服务错误
- 说明:邮件服务异常
- 使用场景:邮件发送失败
## 响应格式
### 成功响应格式
```json
{
"header": {
"requestTime": "2024-12-19 10:30:00",
"accountId": "ctrip_account",
"sign": "signature",
"serviceName": "CreateOrder",
"version": "1.0"
},
"resultCode": "0000",
"resultMessage": "操作成功",
"body": {
"otaOrderId": "CT123456",
"supplierOrderId": "COUPON_1734567890123",
"supplierConfirmType": 1,
"voucherSender": 1,
"orderExtendMessage": "优惠券创建成功",
"items": [
{
"itemId": "1",
"supplierItemId": "COUPON_ITEM_1734567890123",
"status": 1,
"message": "优惠券创建成功"
}
]
}
}
```
### 错误响应格式
```json
{
"header": {
"requestTime": "2024-12-19 10:30:00",
"accountId": "ctrip_account",
"sign": "signature",
"serviceName": "CreateOrder",
"version": "1.0"
},
"resultCode": "1001",
"resultMessage": "参数错误"
}
```
## 使用示例
### 1. 优惠券创建成功
```java
// 使用标准工具类构建响应
CtripCreateOrderResponse response = CtripResponseCodeUtil.buildCreateOrderSuccessResponse(
request,
"CT123456",
"COUPON_1734567890123",
"优惠券创建成功"
);
```
### 2. 参数错误
```java
// 使用标准错误码
CtripCreateOrderResponse response = CtripResponseCodeUtil.buildCreateOrderErrorResponse(
request,
CtripResponseCodeUtil.CtripResponseCodes.PARAM_ERROR,
CtripResponseCodeUtil.CtripResponseCodes.PARAM_ERROR_MESSAGE
);
```
### 3. 异常处理
```java
try {
// 业务逻辑
} catch (Exception e) {
String resultCode = CtripResponseCodeUtil.getResultCodeByException(e);
String resultMessage = CtripResponseCodeUtil.getResultMessageByException(e);
return CtripResponseCodeUtil.buildCreateOrderErrorResponse(request, resultCode, resultMessage);
}
```
## 验证规则
### 1. 响应码格式验证
- 必须为4位数字字符串
- 范围:0000-9999
- 不能为空或null
### 2. 响应码分类验证
```java
String category = CtripResponseCodeUtil.getResponseCodeCategory("1001");
// 返回: "SYSTEM_ERROR"
```
### 3. 响应码有效性验证
```java
boolean isValid = CtripResponseCodeUtil.isValidResponseCode("1001");
// 返回: true
```
## 注意事项
1. **标准性**:必须严格按照携程API平台标准返回响应码
2. **一致性**:所有携程相关接口都应使用统一的响应码
3. **可读性**:错误消息应该清晰明确,便于问题定位
4. **日志记录**:所有响应都应该记录日志,便于问题追踪
5. **异常处理**:系统异常应该映射到合适的标准响应码
## 工具类使用
项目已提供 `CtripResponseCodeUtil` 工具类,包含:
- 标准响应码定义
- 响应构建方法
- 异常映射方法
- 验证方法
建议所有携程相关接口都使用此工具类来确保响应码的标准性和一致性。
+169
View File
@@ -0,0 +1,169 @@
# 智能租户分配功能使用说明
## 功能概述
智能租户分配功能实现了订单的自动分配策略:用户扫码下单时,系统会根据配置的比例自动将订单分配给景区租户或平台租户,实现平台从景区订单中抽取成本的目的。
## 核心逻辑
### 分配策略
- **默认比例**:10单景区 + 1单平台
- **分配规则**:按用户ID + 二维码ID的组合进行计数
- **平台租户ID**:默认为 `webApp`
### 统计逻辑
- **景区统计**:只统计景区租户ID的订单
- **平台统计**:只统计平台租户ID的订单
- **首页统计**:按景区ID统计(不受租户ID影响)
## 配置管理
### 1. 配置文件
在 `application.yml` 中添加配置:
```yaml
tenant:
allocation:
enabled: false # 是否启用功能
allocation-ratio: 10 # 分配比例
platform-tenant-id: "webApp" # 平台租户ID
debug: false # 调试模式
force-platform-tenant: false # 强制平台租户(测试用)
```
### 2. 动态配置接口
#### 获取当前配置
```bash
GET /api-order/tenant-allocation/config/current
```
#### 更新配置
```bash
POST /api-order/tenant-allocation/config/update
参数:
- enabled: 是否启用
- allocation-ratio: 分配比例
- platform-tenant-id: 平台租户ID
- debug: 调试模式
- force-platform-tenant: 强制平台租户
```
#### 启用功能
```bash
POST /api-order/tenant-allocation/config/enable
```
#### 禁用功能
```bash
POST /api-order/tenant-allocation/config/disable
```
#### 测试配置
```bash
POST /api-order/tenant-allocation/config/test
参数:
- userId: 用户ID
- qrCodeId: 二维码ID
- testCount: 测试订单数量
```
## 测试接口
### 1. 测试分配逻辑
```bash
GET /api-order/order/tenant-allocation/test
参数:
- userId: 用户ID
- qrCodeId: 二维码ID
- expectedCount: 期望订单数量
```
### 2. 模拟订单分配
```bash
POST /api-order/order/tenant-allocation/simulate
参数:
- userId: 用户ID
- qrCodeId: 二维码ID
- orderCount: 模拟订单数量
```
### 3. 获取分配统计
```bash
GET /api-order/order/tenant-allocation/statistics
参数:
- qrCodeId: 二维码ID
```
## 使用场景
### 场景1:启用智能分配
1. 启用功能:
```bash
POST /api-order/tenant-allocation/config/enable
```
2. 验证配置:
```bash
POST /api-order/tenant-allocation/config/test?userId=123&qrCodeId=456&testCount=22
```
3. 用户扫码下单,系统自动分配租户
### 场景2:测试分配逻辑
```bash
POST /api-order/order/tenant-allocation/simulate?userId=123&qrCodeId=456&orderCount=22
```
预期结果:
- 总订单:22单
- 景区订单:20单(90.9%)
- 平台订单:2单(9.1%)
### 场景3:强制平台租户(测试用)
```bash
POST /api-order/tenant-allocation/config/update?force-platform-tenant=true
```
所有订单都会分配给平台租户,用于测试平台统计功能。
## 注意事项
### 1. 数据一致性
- 订单创建时就确定租户ID,避免后续转移
- 统计查询按租户ID筛选,确保准确性
- 首页统计按景区ID,不受租户分配影响
### 2. 配置变更
- 配置变更只影响新创建的订单
- 已创建的订单租户ID不会改变
- 建议在业务低峰期进行配置变更
### 3. 监控建议
- 定期检查分配比例是否符合预期
- 监控平台和景区的订单统计
- 关注日志中的分配记录
### 4. 回滚方案
如果出现问题,可以快速禁用功能:
```bash
POST /api-order/tenant-allocation/config/disable
```
所有新订单都会分配给景区租户,恢复原有逻辑。
## 扩展功能
### 1. 自定义分配比例
可以通过配置调整分配比例:
```yaml
tenant:
allocation:
allocation-ratio: 5 # 5单景区 + 1单平台
```
### 2. 多平台租户支持
未来可以扩展支持多个平台租户,根据业务需求进行分配。
### 3. 动态配置持久化
当前配置是内存配置,重启后会恢复默认值。未来可以扩展为数据库配置,支持持久化存储。