137 lines
7.4 KiB
PL/PgSQL
137 lines
7.4 KiB
PL/PgSQL
-- 统一认证(IDP) 存量账号 → Hub 身份:手工预写映射
|
||
-- 对应方案 附录 M。上线顺序:加列 → 预写映射 → dry-run 对账 → 首切 apply → 每日全量 → 免密灰度 → 增量
|
||
-- 适用 dev unis_crm_dev / prod unis_scc(PostgreSQL)。本脚本只碰 hub_* 列与临时表 crm_idp_premark,不碰业务数据。
|
||
--
|
||
-- 五条红线(方案 M1):
|
||
-- 1. 手工只允许写 5 个 hub_* 列;user_id / username / display_name / phone / status 一个都不许动。
|
||
-- 2. 一个 Hub user_id 只能绑一个本地行,反向同理;uk_sys_user_hub_user_id 报错是好事,别绕过。
|
||
-- 3. 对不上的人宁可留空,别为了清零硬绑;留空的人走首次免密登录自助绑定,零风险。
|
||
-- 4. admin / admin_crm 这类不在 Hub 花名册里的本地账号留空,由 excluded-accounts 兜住。
|
||
-- 5. 绑之前备份 sys_user,并把对照表另存一份(见第 ⑥ 步导出语句)。
|
||
|
||
-- 前置:hub_* 六列必须已存在。它们由后端 IdpSchemaInitializer 在应用启动时建(方案 §4.2 要求走启动器,不要只丢 SQL 等人手工执行)。
|
||
-- 若要在首次启动之前就手工预写映射,先执行下面 6 句 —— 与启动器里的语句完全一致,add column if not exists 幂等,重复执行无害。
|
||
alter table sys_user add column if not exists hub_user_id bigint;
|
||
alter table sys_user add column if not exists hub_account varchar(128);
|
||
alter table sys_user add column if not exists hub_display_name varchar(255);
|
||
alter table sys_user add column if not exists hub_mobile varchar(32);
|
||
alter table sys_user add column if not exists hub_org_id varchar(128);
|
||
alter table sys_user add column if not exists hub_synced_at timestamptz;
|
||
create unique index if not exists uk_sys_user_hub_user_id on sys_user (hub_user_id) where hub_user_id is not null;
|
||
|
||
-- ① 对照表:把 Hub 管理员给的清单灌进来(含 user_id / account / wecom_userid / 姓名 / phone)
|
||
create table if not exists crm_idp_premark (
|
||
hub_user_id bigint primary key,
|
||
hub_account text,
|
||
hub_wecom_userid text,
|
||
hub_name text,
|
||
hub_phone text,
|
||
local_user_id bigint,
|
||
match_by text,
|
||
note text,
|
||
updated_at timestamptz not null default now()
|
||
);
|
||
|
||
-- 注意 wecom_user_mapping 没有 is_deleted / status 列(见 ReportReminderSchemaInitializer.java:70),别按逻辑删除过滤。
|
||
-- ② 先让机器自己认一遍。Hub 全量目录不下发手机号(方案 §3),所以 wecom_userid 是唯一能批量对的桥。
|
||
update crm_idp_premark t
|
||
set local_user_id = c.user_id,
|
||
match_by = case when c.hits = 1 then 'wecom' else t.match_by end,
|
||
note = coalesce(t.note, '') || case when c.hits > 1 then ' | 企微号多命中,需人工裁定' else '' end,
|
||
updated_at = now()
|
||
from (select wecom_user_id,
|
||
count(distinct user_id) hits,
|
||
min(user_id) user_id
|
||
from wecom_user_mapping
|
||
where nullif(btrim(wecom_user_id), '') is not null
|
||
group by wecom_user_id) c
|
||
where t.local_user_id is null
|
||
and nullif(btrim(t.hub_wecom_userid), '') is not null
|
||
and t.hub_wecom_userid = c.wecom_user_id
|
||
and c.hits = 1;
|
||
|
||
-- 第二条一次性线索:Hub account 等于本地 username(account 会随企微改名,只做一次性补充,方案 §5.2 第 3 级)
|
||
update crm_idp_premark t
|
||
set local_user_id = u.user_id,
|
||
match_by = 'username',
|
||
note = coalesce(t.note, '') || ' | 按 account=username 一次性认领,请复核',
|
||
updated_at = now()
|
||
from sys_user u
|
||
where t.local_user_id is null
|
||
and nullif(btrim(t.hub_account), '') is not null
|
||
and u.username = t.hub_account
|
||
and u.is_deleted = 0
|
||
and u.hub_user_id is null
|
||
and u.username not in ('admin', 'admin_crm')
|
||
and coalesce(u.is_platform_admin, false) = false
|
||
and (select count(*) from sys_user u2
|
||
where u2.username = t.hub_account
|
||
and u2.is_deleted = 0
|
||
and u2.hub_user_id is null) = 1;
|
||
|
||
-- 剩下的行 local_user_id 仍为 null,人工填结论;填不满就留空(红线 3)
|
||
select hub_user_id, hub_account, hub_wecom_userid, hub_name, hub_phone, local_user_id, match_by
|
||
from crm_idp_premark where local_user_id is null order by hub_user_id;
|
||
|
||
-- ③ 预检:以下五类必须全部 0 行,才允许进第 ④ 步
|
||
select 'A 本地目标行被多个 hub 主键抢' k, local_user_id, count(*)
|
||
from crm_idp_premark where local_user_id is not null
|
||
group by local_user_id having count(*) > 1;
|
||
|
||
select 'B 目标行不存在或已删' k, t.hub_user_id, t.local_user_id
|
||
from crm_idp_premark t left join sys_user u on u.user_id = t.local_user_id
|
||
where t.local_user_id is not null and u.user_id is null;
|
||
|
||
select 'C 目标行已绑别的 hub 主键' k, t.hub_user_id, u.hub_user_id bound_to
|
||
from crm_idp_premark t join sys_user u on u.user_id = t.local_user_id
|
||
where u.hub_user_id is not null and u.hub_user_id <> t.hub_user_id;
|
||
|
||
select 'D 姓名或手机号对不上(人工确认是否同一人)' k,
|
||
t.hub_user_id, t.hub_name, u.display_name, t.hub_phone, u.phone
|
||
from crm_idp_premark t join sys_user u on u.user_id = t.local_user_id
|
||
where (nullif(btrim(t.hub_phone), '') is not null
|
||
and nullif(btrim(u.phone), '') is not null
|
||
and t.hub_phone <> u.phone)
|
||
or (nullif(btrim(t.hub_name), '') is not null
|
||
and nullif(btrim(u.display_name), '') is not null
|
||
and t.hub_name <> u.display_name);
|
||
|
||
-- 红线 4:排除名单里的账号一律不许绑
|
||
select 'E 排除名单被误绑' k, t.hub_user_id, u.username
|
||
from crm_idp_premark t join sys_user u on u.user_id = t.local_user_id
|
||
where u.username in ('admin', 'admin_crm') or coalesce(u.is_platform_admin, false);
|
||
|
||
-- ④ 事务内绑定:先看行数,数字对得上再 commit,对不上直接 rollback
|
||
-- begin;
|
||
-- update sys_user u set
|
||
-- hub_user_id = t.hub_user_id,
|
||
-- hub_account = t.hub_account,
|
||
-- hub_display_name = t.hub_name,
|
||
-- hub_mobile = nullif(btrim(t.hub_phone), ''),
|
||
-- hub_synced_at = now(),
|
||
-- updated_at = now()
|
||
-- from crm_idp_premark t
|
||
-- where t.local_user_id = u.user_id;
|
||
-- select count(*) bound_now from sys_user where hub_user_id is not null;
|
||
-- commit; -- 数字对得上再提交,否则 rollback
|
||
|
||
-- ⑤ 绑完立刻跑 dry-run(只算不写),核对三个数:已绑数 / Hub 里未对上本地的人数 / CRM 里仍未绑的活跃账号数
|
||
-- 此时 dry-run 的 created 应当只等于 Hub 里确实没有本地对应的新人,其余为 0
|
||
select count(*) filter (where hub_user_id is not null) bound,
|
||
count(*) filter (where hub_user_id is null and is_deleted = 0) still_unbound,
|
||
count(*) total
|
||
from sys_user;
|
||
|
||
-- ⑥ 留档(红线 5):把对照表导出另存
|
||
-- \copy (select t.hub_user_id, t.hub_account, t.hub_name, u.user_id, u.username, u.display_name, t.match_by, t.note
|
||
-- from crm_idp_premark t join sys_user u on u.user_id = t.local_user_id
|
||
-- order by t.hub_user_id) to 'idp_premark_audit.csv' csv header
|
||
|
||
-- 回滚(方案 M3):预写阶段解绑很干净,同步还没开始,不影响任何业务数据
|
||
-- update sys_user set hub_user_id = null, hub_account = null, hub_display_name = null,
|
||
-- hub_mobile = null, hub_synced_at = null
|
||
-- where user_id in (select local_user_id from crm_idp_premark where local_user_id is not null);
|
||
|
||
-- 手工映射只做一次;确认对齐后删掉对照表,避免被误当成同步表使用
|
||
-- drop table crm_idp_premark;
|