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("==================================================="); } }