package com.xiaopeng.order.utils;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
/**
|
* 分表工具类
|
* 根据 ShardingSphere 配置计算实际的分表名称
|
*
|
* @author auto-generated
|
* @date 2026-03-05
|
*/
|
@Slf4j
|
public class ShardingTableUtil {
|
|
/**
|
* 根据用户ID计算订单表的实际表名
|
* 分片规则: xp_order_${app_user_id % 20}
|
*
|
* @param appUserId 用户ID
|
* @return 实际表名,如: xp_order_5
|
*/
|
public static String getOrderTableName(Long appUserId) {
|
if (appUserId == null) {
|
log.warn("appUserId is null, returning default table name");
|
return "xp_order_0";
|
}
|
int tableIndex = (int) (appUserId % 20);
|
String tableName = "xp_order_" + tableIndex;
|
log.debug("appUserId: {}, calculated table name: {}", appUserId, tableName);
|
return tableName;
|
}
|
|
/**
|
* 根据订单ID计算订单取消表的实际表名
|
* 分片规则: xp_order_cancel_${order_id % 20}
|
*
|
* @param orderId 订单ID
|
* @return 实际表名,如: xp_order_cancel_5
|
*/
|
public static String getOrderCancelTableName(Long orderId) {
|
if (orderId == null) {
|
log.warn("orderId is null, returning default table name");
|
return "xp_order_cancel_0";
|
}
|
int tableIndex = (int) (orderId % 20);
|
String tableName = "xp_order_cancel_" + tableIndex;
|
log.debug("orderId: {}, calculated table name: {}", orderId, tableName);
|
return tableName;
|
}
|
|
/**
|
* 根据订单ID计算订单详情表的实际表名
|
* 分片规则: xp_order_detail_${order_id % 20}
|
*
|
* @param orderId 订单ID
|
* @return 实际表名,如: xp_order_detail_5
|
*/
|
public static String getOrderDetailTableName(Long orderId) {
|
if (orderId == null) {
|
log.warn("orderId is null, returning default table name");
|
return "xp_order_detail_0";
|
}
|
int tableIndex = (int) (orderId % 20);
|
String tableName = "xp_order_detail_" + tableIndex;
|
log.debug("orderId: {}, calculated table name: {}", orderId, tableName);
|
return tableName;
|
}
|
|
/**
|
* 根据订单ID计算订单评价表的实际表名
|
* 分片规则: xp_order_evaluate_${order_id % 20}
|
*
|
* @param orderId 订单ID
|
* @return 实际表名,如: xp_order_evaluate_5
|
*/
|
public static String getOrderEvaluateTableName(Long orderId) {
|
if (orderId == null) {
|
log.warn("orderId is null, returning default table name");
|
return "xp_order_evaluate_0";
|
}
|
int tableIndex = (int) (orderId % 20);
|
String tableName = "xp_order_evaluate_" + tableIndex;
|
log.debug("orderId: {}, calculated table name: {}", orderId, tableName);
|
return tableName;
|
}
|
|
/**
|
* 根据订单ID计算订单支付表的实际表名
|
* 分片规则: xp_order_payment_${order_id % 20}
|
*
|
* @param orderId 订单ID
|
* @return 实际表名,如: xp_order_payment_5
|
*/
|
public static String getOrderPaymentTableName(Long orderId) {
|
if (orderId == null) {
|
log.warn("orderId is null, returning default table name");
|
return "xp_order_payment_0";
|
}
|
int tableIndex = (int) (orderId % 20);
|
String tableName = "xp_order_payment_" + tableIndex;
|
log.debug("orderId: {}, calculated table name: {}", orderId, tableName);
|
return tableName;
|
}
|
|
/**
|
* 根据任务ID计算订单重新分配表的实际表名
|
* 分片规则: xp_order_reassign_${task_id % 10}
|
*
|
* @param taskId 任务ID
|
* @return 实际表名,如: xp_order_reassign_5
|
*/
|
public static String getOrderReassignTableName(Long taskId) {
|
if (taskId == null) {
|
log.warn("taskId is null, returning default table name");
|
return "xp_order_reassign_0";
|
}
|
int tableIndex = (int) (taskId % 10);
|
String tableName = "xp_order_reassign_" + tableIndex;
|
log.debug("taskId: {}, calculated table name: {}", taskId, tableName);
|
return tableName;
|
}
|
|
/**
|
* 获取所有订单表名列表(用于全表扫描场景)
|
*
|
* @return 所有订单表名数组,共20个表
|
*/
|
public static String[] getAllOrderTableNames() {
|
String[] tableNames = new String[20];
|
for (int i = 0; i < 20; i++) {
|
tableNames[i] = "xp_order_" + i;
|
}
|
return tableNames;
|
}
|
|
/**
|
* 获取所有订单取消表名列表(用于全表扫描场景)
|
*
|
* @return 所有订单取消表名数组,共20个表
|
*/
|
public static String[] getAllOrderCancelTableNames() {
|
String[] tableNames = new String[20];
|
for (int i = 0; i < 20; i++) {
|
tableNames[i] = "xp_order_cancel_" + i;
|
}
|
return tableNames;
|
}
|
|
/**
|
* 获取所有订单详情表名列表(用于全表扫描场景)
|
*
|
* @return 所有订单详情表名数组,共20个表
|
*/
|
public static String[] getAllOrderDetailTableNames() {
|
String[] tableNames = new String[20];
|
for (int i = 0; i < 20; i++) {
|
tableNames[i] = "xp_order_detail_" + i;
|
}
|
return tableNames;
|
}
|
|
/**
|
* 获取所有订单评价表名列表(用于全表扫描场景)
|
*
|
* @return 所有订单评价表名数组,共20个表
|
*/
|
public static String[] getAllOrderEvaluateTableNames() {
|
String[] tableNames = new String[20];
|
for (int i = 0; i < 20; i++) {
|
tableNames[i] = "xp_order_evaluate_" + i;
|
}
|
return tableNames;
|
}
|
|
/**
|
* 获取所有订单支付表名列表(用于全表扫描场景)
|
*
|
* @return 所有订单支付表名数组,共20个表
|
*/
|
public static String[] getAllOrderPaymentTableNames() {
|
String[] tableNames = new String[20];
|
for (int i = 0; i < 20; i++) {
|
tableNames[i] = "xp_order_payment_" + i;
|
}
|
return tableNames;
|
}
|
|
/**
|
* 获取所有订单重新分配表名列表(用于全表扫描场景)
|
*
|
* @return 所有订单重新分配表名数组,共10个表
|
*/
|
public static String[] getAllOrderReassignTableNames() {
|
String[] tableNames = new String[10];
|
for (int i = 0; i < 10; i++) {
|
tableNames[i] = "xp_order_reassign_" + i;
|
}
|
return tableNames;
|
}
|
|
/**
|
* 计算分片索引(通用方法)
|
*
|
* @param shardingValue 分片键值
|
* @param shardingCount 分片数量
|
* @return 分片索引
|
*/
|
public static int calculateShardingIndex(Long shardingValue, int shardingCount) {
|
if (shardingValue == null) {
|
log.warn("shardingValue is null, returning 0");
|
return 0;
|
}
|
return (int) (shardingValue % shardingCount);
|
}
|
|
/**
|
* 获取指定表的分片表名(通用方法)
|
*
|
* @param baseTableName 基础表名(如: xp_order)
|
* @param shardingValue 分片键值
|
* @param shardingCount 分片数量
|
* @return 实际表名
|
*/
|
public static String getShardingTableName(String baseTableName, Long shardingValue, int shardingCount) {
|
int index = calculateShardingIndex(shardingValue, shardingCount);
|
String tableName = baseTableName + "_" + index;
|
log.debug("baseTableName: {}, shardingValue: {}, shardingCount: {}, calculated table name: {}",
|
baseTableName, shardingValue, shardingCount, tableName);
|
return tableName;
|
}
|
|
/**
|
* 打印所有分表配置信息(用于调试)
|
*/
|
public static void printShardingInfo() {
|
log.info("========== Sharding Table Configuration ==========");
|
log.info("xp_order: 20 tables, sharding by app_user_id % 20");
|
log.info("xp_order_cancel: 20 tables, sharding by order_id % 20");
|
log.info("xp_order_detail: 20 tables, sharding by order_id % 20");
|
log.info("xp_order_evaluate: 20 tables, sharding by order_id % 20");
|
log.info("xp_order_payment: 20 tables, sharding by order_id % 20");
|
log.info("xp_order_reassign: 10 tables, sharding by task_id % 10");
|
log.info("===================================================");
|
}
|
}
|