Files
bl/modules/base/base.go
昔念 d71b1dd169 feat(base): 重置数据库序列并优化查询语句
- 在 base 模块初始化时添加重置所有序列的函数
- 修改多个模块中的查询语句,使用 Where(key, value) 替代 Where("key = ?", value)
- 优化部分代码结构,提高可读性和维护性
2025-07-11 03:36:42 +08:00

69 lines
1.8 KiB
Go

package base
import (
_ "blazing/modules/base/packed"
"context"
"blazing/cool"
_ "blazing/modules/base/controller"
_ "blazing/modules/base/funcs"
_ "blazing/modules/base/middleware"
"blazing/modules/base/model"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func init() {
var (
ctx = gctx.GetInitCtx()
)
g.Log().Debug(ctx, "module base init start ...")
cool.FillInitData(ctx, "base", &model.BaseSysMenu{})
//fmt.Println(err)
cool.FillInitData(ctx, "base", &model.BaseSysUser{})
cool.FillInitData(ctx, "base", &model.BaseSysUserRole{})
cool.FillInitData(ctx, "base", &model.BaseSysRole{})
cool.FillInitData(ctx, "base", &model.BaseSysRoleMenu{})
cool.FillInitData(ctx, "base", &model.BaseSysDepartment{})
cool.FillInitData(ctx, "base", &model.BaseSysRoleDepartment{})
cool.FillInitData(ctx, "base", &model.BaseSysParam{})
cool.FillInitData(ctx, "base", &model.BaseSysConf{})
g.DB("default").Exec(context.Background(), `CREATE OR REPLACE FUNCTION reset_all_sequences()
RETURNS void AS $$
DECLARE
seq_record record;
BEGIN
FOR seq_record IN
SELECT
'SELECT SETVAL(' ||
quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) ||
', COALESCE(MAX(' ||quote_ident(C.attname)|| '), 1) ) FROM ' ||
quote_ident(PGT.schemaname)|| '.'||quote_ident(T.relname)|| ';' AS sql
FROM
pg_class AS S,
pg_depend AS D,
pg_class AS T,
pg_attribute AS C,
pg_tables AS PGT
WHERE
S.relkind = 'S'
AND S.oid = D.objid
AND D.refobjid = T.oid
AND D.refobjid = C.attrelid
AND D.refobjsubid = C.attnum
AND T.relname = PGT.tablename
LOOP
EXECUTE seq_record.sql;
END LOOP;
END;
$$ LANGUAGE plpgsql;
-- 执行函数
SELECT reset_all_sequences();`, nil) //重置所有序列
g.Log().Debug(ctx, "module base init finished ...")
}