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: 正常分配逻辑