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
package com.xiaopeng.itinerary.utils;
 
import lombok.extern.slf4j.Slf4j;
 
/**
 * 分表工具类
 * 根据 ShardingSphere 配置计算实际的分表名称
 *
 * @author auto-generated
 */
@Slf4j
public class ShardingTableUtil {
 
    /**
     * 根据用户ID计算操作日志表的实际表名
     * 分片规则: sys_oper_log_${userId % 10}
     *
     * @param userId 用户ID
     * @return 实际表名,如: sys_oper_log_5
     */
    public static String getSysOperLogTableName(Long userId) {
        if (userId == null) {
            log.warn("userId is null, returning default table name");
            return "sys_oper_log_0";
        }
        int tableIndex = (int) (userId % 10);
        String tableName = "sys_oper_log_" + tableIndex;
        log.debug("userId: {}, calculated table name: {}", userId, tableName);
        return tableName;
    }
 
    /**
     * 根据用户ID计算协议用户表的实际表名
     * 分片规则: xp_agreement_app_user_${app_user_id % 10}
     *
     * @param appUserId 用户ID
     * @return 实际表名,如: xp_agreement_app_user_5
     */
    public static String getAgreementAppUserTableName(Long appUserId) {
        if (appUserId == null) {
            log.warn("appUserId is null, returning default table name");
            return "xp_agreement_app_user_0";
        }
        int tableIndex = (int) (appUserId % 10);
        String tableName = "xp_agreement_app_user_" + tableIndex;
        log.debug("appUserId: {}, calculated table name: {}", appUserId, tableName);
        return tableName;
    }
 
    /**
     * 根据电子围栏区域ID计算点位表的实际表名
     * 分片规则: xp_point_${electronic_area_id % 10}
     *
     * @param electronicAreaId 电子围栏区域ID
     * @return 实际表名,如: xp_point_5
     */
    public static String getPointTableName(Long electronicAreaId) {
        if (electronicAreaId == null) {
            log.warn("electronicAreaId is null, returning default table name");
            return "xp_point_0";
        }
        int tableIndex = (int) (electronicAreaId % 10);
        String tableName = "xp_point_" + tableIndex;
        log.debug("electronicAreaId: {}, calculated table name: {}", electronicAreaId, tableName);
        return tableName;
    }
 
    /**
     * 获取所有操作日志表名列表(用于全表扫描场景)
     *
     * @return 所有操作日志表名数组,共10个表
     */
    public static String[] getAllSysOperLogTableNames() {
        String[] tableNames = new String[10];
        for (int i = 0; i < 10; i++) {
            tableNames[i] = "sys_oper_log_" + i;
        }
        return tableNames;
    }
 
    /**
     * 获取所有协议用户表名列表(用于全表扫描场景)
     *
     * @return 所有协议用户表名数组,共10个表
     */
    public static String[] getAllAgreementAppUserTableNames() {
        String[] tableNames = new String[10];
        for (int i = 0; i < 10; i++) {
            tableNames[i] = "xp_agreement_app_user_" + i;
        }
        return tableNames;
    }
 
    /**
     * 获取所有点位表名列表(用于全表扫描场景)
     *
     * @return 所有点位表名数组,共10个表
     */
    public static String[] getAllPointTableNames() {
        String[] tableNames = new String[10];
        for (int i = 0; i < 10; i++) {
            tableNames[i] = "xp_point_" + 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 基础表名(如: sys_oper_log)
     * @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("sys_oper_log: 10 tables, sharding by userId % 10");
        log.info("xp_agreement_app_user: 10 tables, sharding by app_user_id % 10");
        log.info("xp_point: 10 tables, sharding by electronic_area_id % 10");
        log.info("===================================================");
    }
}