liujie
2026-06-09 c96fbe6b0aeb413a17c429133ee6d81d138144b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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("===================================================");
    }
}