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

42 lines
890 B
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 next_8digit_shuffle_id() CASCADE;
-- 8位自增 洗牌 仍然8位 绝对不重复
CREATE OR REPLACE FUNCTION next_8digit_shuffle_id()
RETURNS bigint
LANGUAGE plpgsql
VOLATILE
AS $$
DECLARE
seq bigint;
a int;
b int;
BEGIN
seq := nextval('base_sys_user_id_seq');
-- 8 位数字拆开洗牌
a := (seq / 10000)::int; -- 前4位
b := (seq % 10000)::int; -- 后4位
-- 前后互换 + 简单扰乱保证
-- 1. 还是8位
-- 2. seq 不同 结果一定不同
return ((b * 9871) % 10000) * 10000
+ ((a * 1237) % 10000);
END;
$$;
-- 启用
ALTER TABLE base_sys_user
ALTER COLUMN id SET DEFAULT next_8digit_shuffle_id();
ALTER TABLE base_sys_user
ALTER COLUMN id
SET DEFAULT nextval('base_sys_user_id_seq');