4.2 KiB
4.2 KiB
CREATE TABLE `auth_info` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '认证信息ID',
`phone` varchar(50) NOT NULL COMMENT '手机号',
`password` varchar(255) NOT NULL COMMENT '加密后的密码',
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted_at` datetime DEFAULT NULL COMMENT '删除时间(软删除)',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_phone_deleted` (`phone`, `deleted_at`),
KEY `idx_phone` (`phone`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='统一认证信息表';
CREATE TABLE `auth_info_agent` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '关联ID',
`auth_info_id` bigint(20) NOT NULL COMMENT '认证信息ID',
`agent_id` bigint(20) NOT NULL COMMENT '客服ID',
`ent_id` bigint(20) NOT NULL COMMENT '企业ID(冗余字段,便于查询)',
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_auth_agent` (`auth_info_id`, `agent_id`),
KEY `idx_agent` (`agent_id`),
KEY `idx_auth_info` (`auth_info_id`),
KEY `idx_ent` (`ent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='认证信息与客服关联表';
CREATE TABLE `user_login_status` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`agent_id` bigint(20) NOT NULL COMMENT '客服ID',
`platform` varchar(50) NOT NULL COMMENT '登录平台: web, ios, android, mac, windows等',
`token` varchar(255) NOT NULL COMMENT '登录token',
`user_agent` varchar(500) DEFAULT NULL COMMENT '用户代理字符串',
`ip` varchar(50) DEFAULT NULL COMMENT 'IP地址',
`isp` varchar(100) DEFAULT NULL COMMENT '运营商',
`country` varchar(100) DEFAULT NULL COMMENT '国家',
`province` varchar(100) DEFAULT NULL COMMENT '省份',
`city` varchar(100) DEFAULT NULL COMMENT '城市',
`status` varchar(20) NOT NULL COMMENT '状态: online(在线), invisible(隐身), off_duty(离线)',
`login_at` datetime NOT NULL COMMENT '登录时间',
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_token` (`token`),
KEY `idx_ent_agent`(`ent_id`,`agent_id`),
KEY `idx_agent_platform` (`agent_id`, `platform`),
KEY `idx_status` (`status`),
KEY `idx_login_at` (`login_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户登录状态表';
INSERT INTO auth_info (phone, password, created_at, updated_at)
SELECT DISTINCT
phone,
COALESCE(password, '') as password,
NOW(6),
NOW(6)
FROM agent
WHERE deleted_at IS NULL
AND phone IS NOT NULL
AND phone != ''
AND NOT EXISTS (
SELECT 1 FROM auth_info ai WHERE ai.phone = agent.phone AND ai.deleted_at IS NULL
)
GROUP BY phone;
-- 步骤2: 创建 auth_info 与 agent 的关联关系
INSERT INTO auth_info_agent (auth_info_id, agent_id, created_at)
SELECT
ai.id as auth_info_id,
a.id as agent_id,
NOW(6)
FROM agent a
INNER JOIN auth_info ai ON a.phone = ai.phone AND ai.deleted_at IS NULL
WHERE a.deleted_at IS NULL
AND a.phone IS NOT NULL
AND a.phone != ''
AND NOT EXISTS (
SELECT 1 FROM auth_info_agent aia
WHERE aia.auth_info_id = ai.id AND aia.agent_id = a.id
);
-- 步骤3(可选): 验证迁移结果
-- 检查是否所有 agent 都有对应的 auth_info_agent 记录
SELECT
'未关联的 agent 数量' as check_item,
COUNT(*) as count
FROM agent a
WHERE a.deleted_at IS NULL
AND a.phone IS NOT NULL
AND a.phone != ''
AND NOT EXISTS (
SELECT 1 FROM auth_info_agent aia WHERE aia.agent_id = a.id
);
-- 检查 auth_info 记录数
SELECT
'auth_info 记录数' as check_item,
COUNT(*) as count
FROM auth_info
WHERE deleted_at IS NULL;
-- 检查 auth_info_agent 关联记录数
SELECT
'auth_info_agent 关联记录数' as check_item,
COUNT(*) as count
FROM auth_info_agent;
-- 检查有多个 agent 的手机号
SELECT
ai.phone,
COUNT(aia.agent_id) as agent_count,
GROUP_CONCAT(a.realname SEPARATOR ', ') as agent_names
FROM auth_info ai
INNER JOIN auth_info_agent aia ON ai.id = aia.auth_info_id
INNER JOIN agent a ON aia.agent_id = a.id
WHERE ai.deleted_at IS NULL AND a.deleted_at IS NULL
GROUP BY ai.phone
HAVING COUNT(aia.agent_id) > 1;