Files
bl/help/随机id生成
xinian f434d88f29
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
编辑文件 随机id生成
2026-03-01 01:26:49 +08:00

60 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 清理旧的
ALTER TABLE base_sys_user
ALTER COLUMN id SET DEFAULT nextval('base_sys_user_id_seq');
DROP FUNCTION IF EXISTS shuffle_9digit() CASCADE;
-- 9位乱序首尾互换 + 中间全部打乱
-- 纯数字换位100% 不重复速度极快
CREATE OR REPLACE FUNCTION shuffle_9digit()
RETURNS bigint
LANGUAGE plpgsql
VOLATILE
AS $$
DECLARE
seq bigint;
d0 int; -- 原最后一位
d1 int;
d2 int;
d3 int;
d4 int;
d5 int;
d6 int;
d7 int; -- 原第一位
BEGIN
seq := nextval('base_sys_user_id_seq'); -- 8位自增
-- 拆分 8 d7 d6 d5 d4 d3 d2 d1 d0
d7 := (seq / 10000000) % 10;
d6 := (seq / 1000000) % 10;
d5 := (seq / 100000) % 10;
d4 := (seq / 10000) % 10;
d3 := (seq / 1000) % 10;
d2 := (seq / 100) % 10;
d1 := (seq / 10) % 10;
d0 := seq % 10;
-- 构造成 9 规则
-- 1. 首尾互换原最后一位放第1位原第1位放最后
-- 2. 中间全部打乱位置
RETURN
d0 * 100000000 + -- 原最后一位 第1位
d3 * 10000000 +
d1 * 1000000 +
d5 * 100000 +
d2 * 10000 +
d6 * 1000 +
d4 * 100 +
d7 * 10 +
d0; -- 原第一位 第9位
END;
$$;
-- 启用
ALTER TABLE base_sys_user
ALTER COLUMN id SET DEFAULT shuffle_9digit();
ALTER TABLE base_sys_user
ALTER COLUMN id
SET DEFAULT nextval('base_sys_user_id_seq');