-- PostgreSQL init script converted from llm_20260208.sql
-- 建议先连接到目标数据库后执行该脚本
CREATE EXTENSION IF NOT EXISTS pgcrypto;

-- ----------------------------
-- 1、部门表
-- ----------------------------
drop table if exists d_sys_dept cascade;
create table d_sys_dept (
                          dept_id          varchar(36)     not null default (gen_random_uuid()::text),
                          parent_id        varchar(36)     default '0',
                          ancestors         varchar(2000)     default '',
                          dept_name         varchar(30)     default '',
                          order_num         int          default 0,
                          leader            varchar(20)     default null,
                          phone             varchar(11)     default null,
                          email             varchar(50)     default null,
                          status            char(1)         default '0',
                          del_flag          char(1)         default '0',
                          create_by         varchar(64)     default '',
                          create_time 	    timestamp,
                          update_by         varchar(64)     default '',
                          update_time       timestamp,
                          primary key (dept_id)
);

-- ----------------------------
-- 初始化-部门表数据
-- ----------------------------
insert into d_sys_dept values('717f5780-62a9-44b6-892a-228bd694c91c', '0', '0', '南方电网', '0', 'lm', '15888888888', 'lm@qq.com', '0', '0', 'admin', CURRENT_TIMESTAMP, '', null);
insert into d_sys_dept values('9f322bdf-21eb-43c5-b255-4522d620e8ae', '717f5780-62a9-44b6-892a-228bd694c91c', '0,717f5780-62a9-44b6-892a-228bd694c91c', '深圳总公司', '1', 'lm', '15888888888', 'lm@qq.com', '0', '0', 'admin', CURRENT_TIMESTAMP, '', null);
insert into d_sys_dept values('0b06323e-8748-40d5-bedf-8bc4b7cea91e', '717f5780-62a9-44b6-892a-228bd694c91c', '0,717f5780-62a9-44b6-892a-228bd694c91c', '长沙分公司', '2', 'lm', '15888888888', 'lm@qq.com', '0', '0', 'admin', CURRENT_TIMESTAMP, '', null);
insert into d_sys_dept values('a4209f68-23e3-45e4-88f4-2e2ceabf0851', '9f322bdf-21eb-43c5-b255-4522d620e8ae', '0,717f5780-62a9-44b6-892a-228bd694c91c,9f322bdf-21eb-43c5-b255-4522d620e8ae', '研发部门', '1', 'lm', '15888888888', 'lm@qq.com', '0', '0', 'admin', CURRENT_TIMESTAMP, '', null);
insert into d_sys_dept values('c3047488-aed6-4363-b9c1-08de2619b486', '9f322bdf-21eb-43c5-b255-4522d620e8ae', '0,717f5780-62a9-44b6-892a-228bd694c91c,9f322bdf-21eb-43c5-b255-4522d620e8ae', '市场部门', '2', 'lm', '15888888888', 'lm@qq.com', '0', '0', 'admin', CURRENT_TIMESTAMP, '', null);
insert into d_sys_dept values('6c442480-97e8-42b7-a509-08b458e44dcd', '9f322bdf-21eb-43c5-b255-4522d620e8ae', '0,717f5780-62a9-44b6-892a-228bd694c91c,9f322bdf-21eb-43c5-b255-4522d620e8ae', '测试部门', '3', 'lm', '15888888888', 'lm@qq.com', '0', '0', 'admin', CURRENT_TIMESTAMP, '', null);
insert into d_sys_dept values('96091b84-9b5e-4e3d-984c-ad6637602a85', '9f322bdf-21eb-43c5-b255-4522d620e8ae', '0,717f5780-62a9-44b6-892a-228bd694c91c,9f322bdf-21eb-43c5-b255-4522d620e8ae', '财务部门', '4', 'lm', '15888888888', 'lm@qq.com', '0', '0', 'admin', CURRENT_TIMESTAMP, '', null);
insert into d_sys_dept values('53ea3f7c-4822-4adc-b68c-fac8e4e09e89', '9f322bdf-21eb-43c5-b255-4522d620e8ae', '0,717f5780-62a9-44b6-892a-228bd694c91c,9f322bdf-21eb-43c5-b255-4522d620e8ae', '运维部门', '5', 'lm', '15888888888', 'lm@qq.com', '0', '0', 'admin', CURRENT_TIMESTAMP, '', null);
insert into d_sys_dept values('ea04f34e-3fde-4a0d-b6b3-1d8590629649', '0b06323e-8748-40d5-bedf-8bc4b7cea91e', '0,717f5780-62a9-44b6-892a-228bd694c91c,0b06323e-8748-40d5-bedf-8bc4b7cea91e', '市场部门', '1', 'lm', '15888888888', 'lm@qq.com', '0', '0', 'admin', CURRENT_TIMESTAMP, '', null);
insert into d_sys_dept values('3228f814-2abe-4ebb-9a6e-acdff4098ca3', '0b06323e-8748-40d5-bedf-8bc4b7cea91e', '0,717f5780-62a9-44b6-892a-228bd694c91c,0b06323e-8748-40d5-bedf-8bc4b7cea91e', '财务部门', '2', 'lm', '15888888888', 'lm@qq.com', '0', '0', 'admin', CURRENT_TIMESTAMP, '', null);


-- ----------------------------
-- 2、用户信息表
-- ----------------------------
drop table if exists d_sys_user cascade;
create table d_sys_user (
                          user_id          varchar(36)     not null default (gen_random_uuid()::text),
                          dept_id          varchar(36)     default null,
                          user_name         varchar(30)     not null,
                          nick_name         varchar(30)     not null,
                          user_type         varchar(2)      default '00',
                          email             varchar(50)     default '',
                          phonenumber       varchar(11)     default '',
                          sex               char(1)         default '0',
                          avatar            varchar(100)    default '',
                          password          varchar(100)    default '',
                          api_key           varchar(128)    default '',
                          api_secret        varchar(256)    default '',
                          status            char(1)         default '0',
                          del_flag          char(1)         default '0',
                          login_ip          varchar(128)    default '',
                          login_date        timestamp,
                          pwd_update_date   timestamp,
                          create_by         varchar(64)     default '',
                          create_time       timestamp,
                          update_by         varchar(64)     default '',
                          update_time       timestamp,
                          remark            varchar(500)    default null,
                          primary key (user_id)
);
CREATE UNIQUE INDEX uk_d_sys_user_api_key ON d_sys_user (api_key);

-- ----------------------------
-- 初始化-用户信息表数据
-- ----------------------------
insert into d_sys_user values('838fef38-55e6-4938-b995-b0814081e93c', 'a4209f68-23e3-45e4-88f4-2e2ceabf0851', 'admin', 'lm', '00', 'lm@163.com', '15888888888', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', 'ak_admin_demo', 'sk_admin_demo', '0', '0', '127.0.0.1', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'admin', CURRENT_TIMESTAMP, '', null, '管理员');
insert into d_sys_user values('568c27fd-4dcd-4871-8f43-56fc2ccf1b60', '6c442480-97e8-42b7-a509-08b458e44dcd', 'lm', 'lm', '00', 'lm@qq.com', '15666666666', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', 'ak_lm_demo', 'sk_lm_demo', '0', '0', '127.0.0.1', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'admin', CURRENT_TIMESTAMP, '', null, '测试员');


-- ----------------------------
-- 3、岗位信息表
-- ----------------------------
drop table if exists d_sys_post cascade;
create table d_sys_post
(
    post_id          varchar(36)     not null default (gen_random_uuid()::text),
    post_code     varchar(64)     not null,
    post_name     varchar(50)     not null,
    post_sort     int          not null,
    status        char(1)         not null,
    create_by     varchar(64)     default '',
    create_time   timestamp,
    update_by     varchar(64)     default '',
    update_time   timestamp,
    remark        varchar(500)    default null,
    primary key (post_id)
);

-- ----------------------------
-- 初始化-岗位信息表数据
-- ----------------------------
insert into d_sys_post values('b98237e8-bc4a-470e-b7af-5c7f592a601b', 'ceo', '董事长', '1', '0', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_post values('c06754e4-2df1-4441-b693-099648713b5b', 'se', '项目经理', '2', '0', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_post values('698c86b0-a28c-4e7e-8dfd-8b2619ca2ac1', 'hr', '人力资源', '3', '0', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_post values('3fbe9843-9751-4e55-aa75-2ca57b25fe4a', 'user', '普通员工', '4', '0', 'admin', CURRENT_TIMESTAMP, '', null, '');


-- ----------------------------
-- 4、角色信息表
-- ----------------------------
drop table if exists d_sys_role cascade;
create table d_sys_role (
                          role_id          varchar(36)     not null default (gen_random_uuid()::text),
                          role_name            varchar(30)     not null,
                          role_key             varchar(100)    not null,
                          role_sort            int          not null,
                          data_scope           char(1)         default '1',
                          menu_check_strictly  smallint      default 1,
                          dept_check_strictly  smallint      default 1,
                          status               char(1)         not null,
                          del_flag             char(1)         default '0',
                          create_by            varchar(64)     default '',
                          create_time          timestamp,
                          update_by            varchar(64)     default '',
                          update_time          timestamp,
                          remark               varchar(500)    default null,
                          primary key (role_id)
);

-- ----------------------------
-- 初始化-角色信息表数据
-- ----------------------------
insert into d_sys_role values('b786abf3-181f-498e-b958-12adce55701d', '超级管理员', 'admin', '1', '1', '1', '1', '0', '0', 'admin', CURRENT_TIMESTAMP, '', null, '超级管理员');
insert into d_sys_role values('f4cd3e43-fa24-48a8-85b1-27c312263ef5', '普通角色', 'common', '2', '2', '1', '1', '0', '0', 'admin', CURRENT_TIMESTAMP, '', null, '普通角色');


-- ----------------------------
-- 5、菜单权限表
-- ----------------------------
drop table if exists d_sys_menu cascade;
create table d_sys_menu (
                          menu_id          varchar(36)     not null default (gen_random_uuid()::text),
                          menu_name         varchar(50)     not null,
                          parent_id        varchar(36)     default '0',
                          order_num         int          default 0,
                          path              varchar(200)    default '',
                          component         varchar(255)    default null,
                          query             varchar(255)    default null,
                          route_name        varchar(50)     default '',
                          is_frame          int          default 1,
                          is_cache          int          default 0,
                          menu_type         char(1)         default '',
                          '0',
                          '0',
                          perms             varchar(100)    default null,
                          icon              varchar(100)    default '#',
                          create_by         varchar(64)     default '',
                          create_time       timestamp,
                          update_by         varchar(64)     default '',
                          update_time       timestamp,
                          remark            varchar(500)    default '',
                          primary key (menu_id)
);

-- ----------------------------
-- 初始化-菜单信息表数据
-- ----------------------------
-- 一级菜单
insert into d_sys_menu values('7ad4765d-291f-4c9e-9480-4098cf51b501', '系统管理', '0', '1', 'system', null, '', '', '1', '0', 'M', '0', '0', '', 'system', 'admin', CURRENT_TIMESTAMP, '', null, '系统管理目录');
-- 二级菜单
insert into d_sys_menu values('49f1663f-84a7-4f13-83a5-4e92836826c7', '用户管理', '7ad4765d-291f-4c9e-9480-4098cf51b501', '1', 'user', 'system/user/index', '', '', '1', '0', 'C', '0', '0', 'system:user:list', 'user', 'admin', CURRENT_TIMESTAMP, '', null, '用户管理菜单');
insert into d_sys_menu values('6a613b29-6d27-4748-864e-e56379fdcddf', '角色管理', '7ad4765d-291f-4c9e-9480-4098cf51b501', '2', 'role', 'system/role/index', '', '', '1', '0', 'C', '0', '0', 'system:role:list', 'peoples', 'admin', CURRENT_TIMESTAMP, '', null, '角色管理菜单');
insert into d_sys_menu values('823d8a8f-dc2e-4cd8-9cbf-0cf307b9a83f', '菜单管理', '7ad4765d-291f-4c9e-9480-4098cf51b501', '3', 'menu', 'system/menu/index', '', '', '1', '0', 'C', '0', '0', 'system:menu:list', 'tree-table', 'admin', CURRENT_TIMESTAMP, '', null, '菜单管理菜单');
insert into d_sys_menu values('84b894e6-3a31-4dc3-b0eb-ab8d3796cdc1', '部门管理', '7ad4765d-291f-4c9e-9480-4098cf51b501', '4', 'dept', 'system/dept/index', '', '', '1', '0', 'C', '0', '0', 'system:dept:list', 'tree', 'admin', CURRENT_TIMESTAMP, '', null, '部门管理菜单');
insert into d_sys_menu values('2812d414-ac56-4952-a749-a29f08c8ccd6', '岗位管理', '7ad4765d-291f-4c9e-9480-4098cf51b501', '5', 'post', 'system/post/index', '', '', '1', '0', 'C', '0', '0', 'system:post:list', 'post', 'admin', CURRENT_TIMESTAMP, '', null, '岗位管理菜单');
insert into d_sys_menu values('ce84ea31-1e4c-4504-aaf0-bf2a7f67d7d7', '字典管理', '7ad4765d-291f-4c9e-9480-4098cf51b501', '6', 'dict', 'system/dict/index', '', '', '1', '0', 'C', '0', '0', 'system:dict:list', 'dict', 'admin', CURRENT_TIMESTAMP, '', null, '字典管理菜单');
insert into d_sys_menu values('97a5f1ed-d1b2-4375-a4d4-0c670bf697f4', '参数设置', '7ad4765d-291f-4c9e-9480-4098cf51b501', '7', 'config', 'system/config/index', '', '', '1', '0', 'C', '0', '0', 'system:config:list', 'edit', 'admin', CURRENT_TIMESTAMP, '', null, '参数设置菜单');
-- 用户管理按钮
insert into d_sys_menu values('4596a8e4-96a4-4aab-b503-474fc9ec6cdd', '用户查询', '49f1663f-84a7-4f13-83a5-4e92836826c7', '1', '', '', '', '', '1', '0', 'F', '0', '0', 'system:user:query', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('d95ff216-ebbd-4f72-90be-cf070fcef386', '用户新增', '49f1663f-84a7-4f13-83a5-4e92836826c7', '2', '', '', '', '', '1', '0', 'F', '0', '0', 'system:user:add', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('ba15e345-b495-4c1e-a1b2-7bd080d55c87', '用户修改', '49f1663f-84a7-4f13-83a5-4e92836826c7', '3', '', '', '', '', '1', '0', 'F', '0', '0', 'system:user:edit', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('fdff8177-e25f-4959-b4c9-7fce08921de9', '用户删除', '49f1663f-84a7-4f13-83a5-4e92836826c7', '4', '', '', '', '', '1', '0', 'F', '0', '0', 'system:user:remove', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('6c3799d3-e17e-4fa1-9a1c-28b8f93740f4', '用户导出', '49f1663f-84a7-4f13-83a5-4e92836826c7', '5', '', '', '', '', '1', '0', 'F', '0', '0', 'system:user:export', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('b82e4497-008d-4c6e-8fcb-132b17352e3b', '用户导入', '49f1663f-84a7-4f13-83a5-4e92836826c7', '6', '', '', '', '', '1', '0', 'F', '0', '0', 'system:user:import', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('ea06e068-e12a-44be-9c63-4904a4780e92', '重置密码', '49f1663f-84a7-4f13-83a5-4e92836826c7', '7', '', '', '', '', '1', '0', 'F', '0', '0', 'system:user:resetPwd', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
-- 角色管理按钮
insert into d_sys_menu values('332d6c13-b570-441d-ae96-54d7e0103c8e', '角色查询', '6a613b29-6d27-4748-864e-e56379fdcddf', '1', '', '', '', '', '1', '0', 'F', '0', '0', 'system:role:query', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('7511bd0d-4713-4438-937d-010a26412e10', '角色新增', '6a613b29-6d27-4748-864e-e56379fdcddf', '2', '', '', '', '', '1', '0', 'F', '0', '0', 'system:role:add', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('cac96384-fb28-4e6b-a54e-39d5e32dc75a', '角色修改', '6a613b29-6d27-4748-864e-e56379fdcddf', '3', '', '', '', '', '1', '0', 'F', '0', '0', 'system:role:edit', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('0e54cc31-c421-4b35-aaa6-6c1a062b0654', '角色删除', '6a613b29-6d27-4748-864e-e56379fdcddf', '4', '', '', '', '', '1', '0', 'F', '0', '0', 'system:role:remove', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('844a163d-9acb-4400-ab1d-58e70a8031cf', '角色导出', '6a613b29-6d27-4748-864e-e56379fdcddf', '5', '', '', '', '', '1', '0', 'F', '0', '0', 'system:role:export', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
-- 菜单管理按钮
insert into d_sys_menu values('888d5b9e-5e14-44e2-b756-db6f52f9904c', '菜单查询', '823d8a8f-dc2e-4cd8-9cbf-0cf307b9a83f', '1', '', '', '', '', '1', '0', 'F', '0', '0', 'system:menu:query', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('874d27ca-164b-4a90-9f80-79463a2fc6fd', '菜单新增', '823d8a8f-dc2e-4cd8-9cbf-0cf307b9a83f', '2', '', '', '', '', '1', '0', 'F', '0', '0', 'system:menu:add', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('5a874707-73fb-4381-ac13-a58bead4a73f', '菜单修改', '823d8a8f-dc2e-4cd8-9cbf-0cf307b9a83f', '3', '', '', '', '', '1', '0', 'F', '0', '0', 'system:menu:edit', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('ad7c5468-d1a2-4c0b-aa14-236eba1d6af1', '菜单删除', '823d8a8f-dc2e-4cd8-9cbf-0cf307b9a83f', '4', '', '', '', '', '1', '0', 'F', '0', '0', 'system:menu:remove', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
-- 部门管理按钮
insert into d_sys_menu values('f8f4fb37-fbe4-4c1a-85c0-1f02c0a01a2c', '部门查询', '84b894e6-3a31-4dc3-b0eb-ab8d3796cdc1', '1', '', '', '', '', '1', '0', 'F', '0', '0', 'system:dept:query', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('7112b424-2e04-487f-9846-a3675a5d8b65', '部门新增', '84b894e6-3a31-4dc3-b0eb-ab8d3796cdc1', '2', '', '', '', '', '1', '0', 'F', '0', '0', 'system:dept:add', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('6dc4643a-7c47-4697-8b8d-b91d5aa335e7', '部门修改', '84b894e6-3a31-4dc3-b0eb-ab8d3796cdc1', '3', '', '', '', '', '1', '0', 'F', '0', '0', 'system:dept:edit', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('7f630738-1f64-4e09-a770-fae5221d028d', '部门删除', '84b894e6-3a31-4dc3-b0eb-ab8d3796cdc1', '4', '', '', '', '', '1', '0', 'F', '0', '0', 'system:dept:remove', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
-- 岗位管理按钮
insert into d_sys_menu values('3cf27725-d451-4925-ab04-6b25162c8e0a', '岗位查询', '2812d414-ac56-4952-a749-a29f08c8ccd6', '1', '', '', '', '', '1', '0', 'F', '0', '0', 'system:post:query', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('76875eb4-4e09-4972-a055-d8420b373b64', '岗位新增', '2812d414-ac56-4952-a749-a29f08c8ccd6', '2', '', '', '', '', '1', '0', 'F', '0', '0', 'system:post:add', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('40a73825-4f18-4378-8694-9becaeecf67f', '岗位修改', '2812d414-ac56-4952-a749-a29f08c8ccd6', '3', '', '', '', '', '1', '0', 'F', '0', '0', 'system:post:edit', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('efd83f4c-3bdc-4bc0-8bdc-8b823cc9abea', '岗位删除', '2812d414-ac56-4952-a749-a29f08c8ccd6', '4', '', '', '', '', '1', '0', 'F', '0', '0', 'system:post:remove', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('67e04efc-1be3-42e6-a84a-a1276eb15aba', '岗位导出', '2812d414-ac56-4952-a749-a29f08c8ccd6', '5', '', '', '', '', '1', '0', 'F', '0', '0', 'system:post:export', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
-- 字典管理按钮
insert into d_sys_menu values('42b877a6-b571-460a-a157-0c5037cb550b', '字典查询', 'ce84ea31-1e4c-4504-aaf0-bf2a7f67d7d7', '1', '#', '', '', '', '1', '0', 'F', '0', '0', 'system:dict:query', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('e307c147-b528-4a83-b824-c3d3c8067f3c', '字典新增', 'ce84ea31-1e4c-4504-aaf0-bf2a7f67d7d7', '2', '#', '', '', '', '1', '0', 'F', '0', '0', 'system:dict:add', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('58975b90-46d2-4ff7-82ee-4395f65fc7f1', '字典修改', 'ce84ea31-1e4c-4504-aaf0-bf2a7f67d7d7', '3', '#', '', '', '', '1', '0', 'F', '0', '0', 'system:dict:edit', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('1bcbdd1f-cac4-4ed0-8aa8-b7316755b73a', '字典删除', 'ce84ea31-1e4c-4504-aaf0-bf2a7f67d7d7', '4', '#', '', '', '', '1', '0', 'F', '0', '0', 'system:dict:remove', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('16274f84-dfea-46a5-a05a-e72c38bcd46d', '字典导出', 'ce84ea31-1e4c-4504-aaf0-bf2a7f67d7d7', '5', '#', '', '', '', '1', '0', 'F', '0', '0', 'system:dict:export', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
-- 参数设置按钮
insert into d_sys_menu values('68280480-129e-4961-88d6-6ae780737a1f', '参数查询', '97a5f1ed-d1b2-4375-a4d4-0c670bf697f4', '1', '#', '', '', '', '1', '0', 'F', '0', '0', 'system:config:query', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('3bb4beef-865f-429d-ac4a-81605540fb15', '参数新增', '97a5f1ed-d1b2-4375-a4d4-0c670bf697f4', '2', '#', '', '', '', '1', '0', 'F', '0', '0', 'system:config:add', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('231a9731-de2a-4f86-aa8e-d31e997b0057', '参数修改', '97a5f1ed-d1b2-4375-a4d4-0c670bf697f4', '3', '#', '', '', '', '1', '0', 'F', '0', '0', 'system:config:edit', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('9617c646-121d-437d-bb5b-91a0247fe912', '参数删除', '97a5f1ed-d1b2-4375-a4d4-0c670bf697f4', '4', '#', '', '', '', '1', '0', 'F', '0', '0', 'system:config:remove', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_menu values('89b9918f-63e6-4670-90f7-3b6e7573a094', '参数导出', '97a5f1ed-d1b2-4375-a4d4-0c670bf697f4', '5', '#', '', '', '', '1', '0', 'F', '0', '0', 'system:config:export', '#', 'admin', CURRENT_TIMESTAMP, '', null, '');


-- ----------------------------
-- 6、用户和角色关联表  用户N-1角色
-- ----------------------------
drop table if exists d_sys_user_role cascade;
create table d_sys_user_role (
                               user_id          varchar(36)     not null,
                               role_id          varchar(36)     not null,
                               primary key(user_id, role_id)
);

-- ----------------------------
-- 初始化-用户和角色关联表数据
-- ----------------------------
insert into d_sys_user_role values('838fef38-55e6-4938-b995-b0814081e93c', 'b786abf3-181f-498e-b958-12adce55701d');
insert into d_sys_user_role values('568c27fd-4dcd-4871-8f43-56fc2ccf1b60', 'f4cd3e43-fa24-48a8-85b1-27c312263ef5');


-- ----------------------------
-- 7、角色和菜单关联表  角色1-N菜单
-- ----------------------------
drop table if exists d_sys_role_menu cascade;
create table d_sys_role_menu (
                               role_id          varchar(36)     not null,
                               menu_id          varchar(36)     not null,
                               primary key(role_id, menu_id)
);

-- ----------------------------
-- 8、角色和部门关联表  角色1-N部门
-- ----------------------------
drop table if exists d_sys_role_dept cascade;
create table d_sys_role_dept (
                               role_id          varchar(36)     not null,
                               dept_id          varchar(36)     not null,
                               primary key(role_id, dept_id)
);

-- ----------------------------
-- 初始化-角色和部门关联表数据
-- ----------------------------
insert into d_sys_role_dept values('f4cd3e43-fa24-48a8-85b1-27c312263ef5', '717f5780-62a9-44b6-892a-228bd694c91c');
insert into d_sys_role_dept values('f4cd3e43-fa24-48a8-85b1-27c312263ef5', '9f322bdf-21eb-43c5-b255-4522d620e8ae');
insert into d_sys_role_dept values('f4cd3e43-fa24-48a8-85b1-27c312263ef5', '6c442480-97e8-42b7-a509-08b458e44dcd');


-- ----------------------------
-- 9、用户与岗位关联表  用户1-N岗位
-- ----------------------------
drop table if exists d_sys_user_post cascade;
create table d_sys_user_post
(
    user_id          varchar(36)     not null,
    post_id          varchar(36)     not null,
    primary key (user_id, post_id)
);

-- ----------------------------
-- 初始化-用户与岗位关联表数据
-- ----------------------------
insert into d_sys_user_post values('838fef38-55e6-4938-b995-b0814081e93c', 'b98237e8-bc4a-470e-b7af-5c7f592a601b');
insert into d_sys_user_post values('568c27fd-4dcd-4871-8f43-56fc2ccf1b60', 'c06754e4-2df1-4441-b693-099648713b5b');


-- ----------------------------
-- 10、操作日志记录
-- ----------------------------
drop table if exists d_sys_oper_log cascade;
create table d_sys_oper_log (
                              oper_id          varchar(36)     not null default (gen_random_uuid()::text),
                              title             varchar(50)     default '',
                              business_type     int          default 0,
                              method            varchar(200)    default '',
                              request_method    varchar(10)     default '',
                              operator_type     int          default 0,
                              oper_name         varchar(50)     default '',
                              dept_name         varchar(50)     default '',
                              oper_url          varchar(255)    default '',
                              oper_ip           varchar(128)    default '',
                              oper_location     varchar(255)    default '',
                              oper_param        varchar(2000)   default '',
                              json_result       varchar(2000)   default '',
                              status            int          default 0,
                              error_msg         varchar(2000)   default '',
                              oper_time         timestamp,
                              cost_time         bigint      default 0,
                              primary key (oper_id)
);
CREATE INDEX idx_d_sys_oper_log_bt ON d_sys_oper_log (business_type);
CREATE INDEX idx_d_sys_oper_log_s ON d_sys_oper_log (status);
CREATE INDEX idx_d_sys_oper_log_ot ON d_sys_oper_log (oper_time);


-- ----------------------------
-- 11、字典类型表
-- ----------------------------
drop table if exists d_sys_dict_type cascade;
create table d_sys_dict_type
(
    dict_id          varchar(36)     not null default (gen_random_uuid()::text),
    dict_name        varchar(100)    default '',
    dict_type        varchar(100)    default '',
    status           char(1)         default '0',
    create_by        varchar(64)     default '',
    create_time      timestamp,
    update_by        varchar(64)     default '',
    update_time      timestamp,
    remark           varchar(500)    default null,
    primary key (dict_id),
    unique (dict_type)
);

insert into d_sys_dict_type values('a1420291-6bea-4d72-b8eb-3c2064062072', '用户性别', 'd_sys_user_sex', '0', 'admin', CURRENT_TIMESTAMP, '', null, '用户性别列表');
insert into d_sys_dict_type values('162864cd-f5eb-4d4f-b6e7-1e64b66984e0', '菜单状态', 'd_sys_show_hide', '0', 'admin', CURRENT_TIMESTAMP, '', null, '菜单状态列表');
insert into d_sys_dict_type values('a128688d-7eca-4878-8df4-f6eaefe9a0d7', '系统开关', 'd_sys_normal_disable', '0', 'admin', CURRENT_TIMESTAMP, '', null, '系统开关列表');
insert into d_sys_dict_type values('b6185ea5-040b-40a6-bec4-06765df1e748', '任务状态', 'd_sys_job_status', '0', 'admin', CURRENT_TIMESTAMP, '', null, '任务状态列表');
insert into d_sys_dict_type values('6eb43625-1a31-4bc5-b072-a579d047d746', '任务分组', 'd_sys_job_group', '0', 'admin', CURRENT_TIMESTAMP, '', null, '任务分组列表');
insert into d_sys_dict_type values('d30495f5-2800-44ee-bf00-1ab306f6c822', '系统是否', 'd_sys_yes_no', '0', 'admin', CURRENT_TIMESTAMP, '', null, '系统是否列表');
insert into d_sys_dict_type values('1d13f277-0eec-4c12-b887-60ea07bfb1c6', '通知类型', 'd_sys_notice_type', '0', 'admin', CURRENT_TIMESTAMP, '', null, '通知类型列表');
insert into d_sys_dict_type values('6edfb2a9-2ee0-4e1c-8226-a3d617825965', '通知状态', 'd_sys_notice_status', '0', 'admin', CURRENT_TIMESTAMP, '', null, '通知状态列表');
insert into d_sys_dict_type values('347d423b-7c20-4a0e-ba31-83db39883adb', '操作类型', 'd_sys_oper_type', '0', 'admin', CURRENT_TIMESTAMP, '', null, '操作类型列表');
insert into d_sys_dict_type values('7e0ddcc2-fa10-42b9-a794-eac5fccefd89', '系统状态', 'd_sys_common_status', '0', 'admin', CURRENT_TIMESTAMP, '', null, '登录状态列表');


-- ----------------------------
-- 12、字典数据表
-- ----------------------------
drop table if exists d_sys_dict_data cascade;
create table d_sys_dict_data
(
    dict_code        varchar(36)     not null default (gen_random_uuid()::text),
    dict_sort        int          default 0,
    dict_label       varchar(100)    default '',
    dict_value       varchar(100)    default '',
    dict_type        varchar(100)    default '',
    css_class        varchar(100)    default null,
    list_class       varchar(100)    default null,
    is_default       char(1)         default 'N',
    status           char(1)         default '0',
    create_by        varchar(64)     default '',
    create_time      timestamp,
    update_by        varchar(64)     default '',
    update_time      timestamp,
    remark           varchar(500)    default null,
    primary key (dict_code)
);

insert into d_sys_dict_data values('784ea33c-f058-410f-8221-5e5ef6a67a8b', '1', '男', '0', 'd_sys_user_sex', '', '', 'Y', '0', 'admin', CURRENT_TIMESTAMP, '', null, '性别男');
insert into d_sys_dict_data values('d458d974-f882-4d22-bb47-09dc2c33e208', '2', '女', '1', 'd_sys_user_sex', '', '', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '性别女');
insert into d_sys_dict_data values('c2573a9a-5d9d-4d60-b414-d3f1cd5bb935', '3', '未知', '2', 'd_sys_user_sex', '', '', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '性别未知');
insert into d_sys_dict_data values('467b5e1f-4de8-4ce8-848c-e9b1283b12ec', '1', '显示', '0', 'd_sys_show_hide', '', 'primary', 'Y', '0', 'admin', CURRENT_TIMESTAMP, '', null, '显示菜单');
insert into d_sys_dict_data values('d93d78a1-ac9d-495a-917e-d838a993a4d9', '2', '隐藏', '1', 'd_sys_show_hide', '', 'danger', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '隐藏菜单');
insert into d_sys_dict_data values('4b8e236c-0ea0-492b-b629-577ac8ffc371', '1', '正常', '0', 'd_sys_normal_disable', '', 'primary', 'Y', '0', 'admin', CURRENT_TIMESTAMP, '', null, '正常状态');
insert into d_sys_dict_data values('390a3e22-f61c-4531-a055-c524028f29a5', '2', '停用', '1', 'd_sys_normal_disable', '', 'danger', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '停用状态');
insert into d_sys_dict_data values('5de8dcbc-c3f6-496a-9f21-e6cb6f3dcdf4', '1', '正常', '0', 'd_sys_job_status', '', 'primary', 'Y', '0', 'admin', CURRENT_TIMESTAMP, '', null, '正常状态');
insert into d_sys_dict_data values('96427d18-7733-4d8d-a1ad-4af34e855a12', '2', '暂停', '1', 'd_sys_job_status', '', 'danger', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '停用状态');
insert into d_sys_dict_data values('4cb4bf17-a9dc-4e4b-9fbe-6fd4bdaccc50', '1', '默认', 'DEFAULT', 'd_sys_job_group', '', '', 'Y', '0', 'admin', CURRENT_TIMESTAMP, '', null, '默认分组');
insert into d_sys_dict_data values('6b973a28-cfa9-480b-aaba-b351fd23ac71', '2', '系统', 'SYSTEM', 'd_sys_job_group', '', '', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '系统分组');
insert into d_sys_dict_data values('4f7b8fba-00b1-4bb5-9e40-0c1f6291d673', '1', '是', 'Y', 'd_sys_yes_no', '', 'primary', 'Y', '0', 'admin', CURRENT_TIMESTAMP, '', null, '系统默认是');
insert into d_sys_dict_data values('f669e968-e3a0-438a-aa0f-a81bc4273905', '2', '否', 'N', 'd_sys_yes_no', '', 'danger', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '系统默认否');
insert into d_sys_dict_data values('faaf2358-917f-442a-a433-e4c0f34a8e5c', '1', '通知', '1', 'd_sys_notice_type', '', 'warning', 'Y', '0', 'admin', CURRENT_TIMESTAMP, '', null, '通知');
insert into d_sys_dict_data values('e2570fc0-bc7f-4965-ac55-49db760209c3', '2', '公告', '2', 'd_sys_notice_type', '', 'success', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '公告');
insert into d_sys_dict_data values('3a27f696-f043-454c-8ad7-4494bf6cfa4e', '1', '正常', '0', 'd_sys_notice_status', '', 'primary', 'Y', '0', 'admin', CURRENT_TIMESTAMP, '', null, '正常状态');
insert into d_sys_dict_data values('391a5794-1abe-4405-8389-b859884e0ce5', '2', '关闭', '1', 'd_sys_notice_status', '', 'danger', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '关闭状态');
insert into d_sys_dict_data values('9e5d4ec7-e985-4662-a4a8-0a2db9cca76a', '99', '其他', '0', 'd_sys_oper_type', '', 'info', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '其他操作');
insert into d_sys_dict_data values('7d762779-833c-46c7-b6ac-139de11ee6d7', '1', '新增', '1', 'd_sys_oper_type', '', 'info', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '新增操作');
insert into d_sys_dict_data values('45422278-38fd-4b5a-9285-774c6fd455cc', '2', '修改', '2', 'd_sys_oper_type', '', 'info', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '修改操作');
insert into d_sys_dict_data values('9b45ecbf-2816-466b-a56b-d2455d205724', '3', '删除', '3', 'd_sys_oper_type', '', 'danger', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '删除操作');
insert into d_sys_dict_data values('e21a1875-da88-436a-bd1a-c44bbda31944', '4', '授权', '4', 'd_sys_oper_type', '', 'primary', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '授权操作');
insert into d_sys_dict_data values('8818c41a-1afa-49cf-abc2-28f636be44d0', '5', '导出', '5', 'd_sys_oper_type', '', 'warning', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '导出操作');
insert into d_sys_dict_data values('ed500592-d6a6-4d5e-bb6a-4c64f7ad0703', '6', '导入', '6', 'd_sys_oper_type', '', 'warning', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '导入操作');
insert into d_sys_dict_data values('491adcc7-6cc4-4736-b6ed-d4049bed7021', '7', '强退', '7', 'd_sys_oper_type', '', 'danger', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '强退操作');
insert into d_sys_dict_data values('bde0241c-2a85-4b7b-9809-15c25ee7675a', '8', '生成代码', '8', 'd_sys_oper_type', '', 'warning', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '生成操作');
insert into d_sys_dict_data values('f8840672-e168-40bd-890e-5b621b15197d', '9', '清空数据', '9', 'd_sys_oper_type', '', 'danger', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '清空操作');
insert into d_sys_dict_data values('1344513e-f14e-4485-aaf2-7c8f2bbea0e0', '1', '成功', '0', 'd_sys_common_status', '', 'primary', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '正常状态');
insert into d_sys_dict_data values('31395dd0-f9b2-478b-a510-3a604c9aad46', '2', '失败', '1', 'd_sys_common_status', '', 'danger', 'N', '0', 'admin', CURRENT_TIMESTAMP, '', null, '停用状态');


-- ----------------------------
-- 13、参数配置表
-- ----------------------------
drop table if exists d_sys_config cascade;
create table d_sys_config (
                            config_id        varchar(36)     not null default (gen_random_uuid()::text),
                            config_name       varchar(100)    default '',
                            config_key        varchar(100)    default '',
                            config_value      varchar(500)    default '',
                            config_type       char(1)         default 'N',
                            create_by         varchar(64)     default '',
                            create_time       timestamp,
                            update_by         varchar(64)     default '',
                            update_time       timestamp,
                            remark            varchar(500)    default null,
                            primary key (config_id)
);

insert into d_sys_config values('f1ccc747-416a-4640-8f99-973d2083d00c', '主框架页-默认皮肤样式名称', 'sys.index.skinName', 'skin-blue', 'Y', 'admin', CURRENT_TIMESTAMP, '', null, '蓝色 skin-blue、绿色 skin-green、紫色 skin-purple、红色 skin-red、黄色 skin-yellow');
insert into d_sys_config values('ff4f06ed-ec5b-4830-9ddf-9e2ec394fbb7', '用户管理-账号初始密码', 'sys.user.initPassword', '123456', 'Y', 'admin', CURRENT_TIMESTAMP, '', null, '初始化密码 123456');
insert into d_sys_config values('d63402f2-fcd5-47fb-a2a5-b465a42bbeda', '主框架页-侧边栏主题', 'sys.index.sideTheme', 'theme-dark', 'Y', 'admin', CURRENT_TIMESTAMP, '', null, '深色主题theme-dark，浅色主题theme-light');
insert into d_sys_config values('14132083-e9a4-43b6-a5ee-77382e7dd4aa', '账号自助-是否开启用户注册功能', 'sys.account.registerUser', 'false', 'Y', 'admin', CURRENT_TIMESTAMP, '', null, '是否开启注册用户功能（true开启，false关闭）');
insert into d_sys_config values('ecbc6b0f-ef40-4639-b952-1888029ddc6c', '用户登录-黑名单列表', 'sys.login.blackIPList', '', 'Y', 'admin', CURRENT_TIMESTAMP, '', null, '设置登录IP黑名单限制，多个匹配项以;分隔，支持匹配（*通配、网段）');
insert into d_sys_config values('43d2d51c-5d8a-486c-8976-8a68d0c9cc6d', '用户管理-初始密码修改策略', 'sys.account.initPasswordModify', '1', 'Y', 'admin', CURRENT_TIMESTAMP, '', null, '0：初始密码修改策略关闭，没有任何提示，1：提醒用户，如果未修改初始密码，则在登录时就会提醒修改密码对话框');
insert into d_sys_config values('3636a38c-e4ca-43e7-bdb4-d1b0ba03e849', '用户管理-账号密码更新周期', 'sys.account.passwordValidateDays', '0', 'Y', 'admin', CURRENT_TIMESTAMP, '', null, '密码更新周期（填写数字，数据初始化值为0不限制，若修改必须为大于0小于365的正整数），如果超过这个周期登录系统时，则在登录时就会提醒修改密码对话框');


-- ----------------------------
-- 14、系统访问记录
-- ----------------------------
drop table if exists d_sys_logininfor cascade;
create table d_sys_logininfor (
                                info_id          varchar(36)     not null default (gen_random_uuid()::text),
                                user_name      varchar(50)    default '',
                                ipaddr         varchar(128)   default '',
                                status         char(1)        default '0',
                                msg            varchar(255)   default '',
                                access_time    timestamp,
                                primary key (info_id)
);
CREATE INDEX idx_d_sys_logininfor_s ON d_sys_logininfor (status);
CREATE INDEX idx_d_sys_logininfor_lt ON d_sys_logininfor (access_time);


-- ----------------------------
-- 15、定时任务调度表
-- ----------------------------
drop table if exists d_sys_job cascade;
create table d_sys_job (
                         job_id           varchar(36)     not null default (gen_random_uuid()::text),
                         job_name            varchar(64)   default '',
                         job_group           varchar(64)   default 'DEFAULT',
                         invoke_target       varchar(500)  not null,
                         cron_expression     varchar(255)  default '',
                         misfire_policy      varchar(20)   default '3',
                         concurrent          char(1)       default '1',
                         status              char(1)       default '0',
                         create_by           varchar(64)   default '',
                         create_time         timestamp,
                         update_by           varchar(64)   default '',
                         update_time         timestamp,
                         remark              varchar(500)  default '',
                         primary key (job_id, job_name, job_group)
);

insert into d_sys_job values('9768a4f3-d06d-4500-9090-324981897ff8', '系统默认（无参）', 'DEFAULT', 'ryTask.ryNoParams', '0/10 * * * * ?', '3', '1', '1', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_job values('120b5777-7bcb-4359-98a5-6bc0b2865090', '系统默认（有参）', 'DEFAULT', 'ryTask.ryParams(''ry'')', '0/15 * * * * ?', '3', '1', '1', 'admin', CURRENT_TIMESTAMP, '', null, '');
insert into d_sys_job values('77112782-6053-49fe-88b8-0b1701449642', '系统默认（多参）', 'DEFAULT', 'ryTask.ryMultipleParams(''ry'', true, 2000L, 316.50D, 100)', '0/20 * * * * ?', '3', '1', '1', 'admin', CURRENT_TIMESTAMP, '', null, '');


-- ----------------------------
-- 16、定时任务调度日志表
-- ----------------------------
drop table if exists d_sys_job_log cascade;
create table d_sys_job_log (
                             job_log_id       varchar(36)     not null default (gen_random_uuid()::text),
                             job_name            varchar(64)    not null,
                             job_group           varchar(64)    not null,
                             invoke_target       varchar(500)   not null,
                             job_message         varchar(500),
                             status              char(1)        default '0',
                             exception_info      varchar(2000)  default '',
                             create_time         timestamp,
                             primary key (job_log_id)
);


-- ----------------------------
-- 17、通知公告表
-- ----------------------------
drop table if exists d_sys_notice cascade;
create table d_sys_notice (
                            notice_id        varchar(36)     not null default (gen_random_uuid()::text),
                            notice_title      varchar(50)     not null,
                            notice_type       char(1)         not null,
                            notice_content    longblob        default null,
                            status            char(1)         default '0',
                            create_by         varchar(64)     default '',
                            create_time       timestamp,
                            update_by         varchar(64)     default '',
                            update_time       timestamp,
                            remark            varchar(255)    default null,
                            primary key (notice_id)
);








-- =========================
-- 访问控制
-- =========================

drop table if exists d_log_alert_hit cascade;
drop table if exists d_log_alert_event cascade;
drop table if exists d_log_attack_policy cascade;
drop table if exists d_attack_signature cascade;
drop table if exists d_attack_rule cascade;
drop table if exists d_attack_switch cascade;
drop table if exists d_content_mask_policy cascade;
drop table if exists d_content_dlp_rule cascade;
drop table if exists d_content_policy cascade;
drop table if exists d_acl_custom_condition cascade;
drop table if exists d_acl_custom_rule cascade;
drop table if exists d_acl_endpoint_rule cascade;
drop table if exists d_acl_ip_rule cascade;

CREATE TABLE d_acl_ip_rule (
                               id                  VARCHAR(64)  NOT NULL,
                               scope_code          VARCHAR(64)  NOT NULL,
                               list_type           VARCHAR(16)  NOT NULL,
                               ip_type             VARCHAR(16)  NOT NULL,
                               ip_value            VARCHAR(255) NOT NULL,
                               priority            INT          NOT NULL DEFAULT 100,
                               effective_text      VARCHAR(128) NOT NULL DEFAULT '长期',
                               status              VARCHAR(20)  NOT NULL DEFAULT 'ENABLED',
                               create_by           VARCHAR(64)  DEFAULT '',
                               create_time         timestamp,
                               update_by           VARCHAR(64)  DEFAULT '',
                               update_time         timestamp,
                               is_deleted          INT          NOT NULL DEFAULT 0,
                               PRIMARY KEY (id)
);
COMMENT ON TABLE d_acl_ip_rule IS '访问控制-IP黑白名单规则';
CREATE INDEX idx_acl_ip_scope_status ON d_acl_ip_rule (scope_code, status);
CREATE INDEX idx_acl_ip_priority ON d_acl_ip_rule (priority);

CREATE TABLE d_acl_endpoint_rule (
                                     id                  VARCHAR(64)   NOT NULL,
                                     scope_code          VARCHAR(64)   NOT NULL,
                                     match_type          VARCHAR(16)   NOT NULL,
                                     uri_pattern         VARCHAR(1024) NOT NULL,
                                     methods             VARCHAR(64)   NOT NULL DEFAULT 'ALL',
                                     block_mode          VARCHAR(16)   NOT NULL,
                                     status              VARCHAR(20)   NOT NULL DEFAULT 'ENABLED',
                                     create_by           VARCHAR(64)   DEFAULT '',
                                     create_time         timestamp,
                                     update_by           VARCHAR(64)   DEFAULT '',
                                     update_time         timestamp,
                                     is_deleted          INT           NOT NULL DEFAULT 0,
                                     PRIMARY KEY (id)
);
COMMENT ON TABLE d_acl_endpoint_rule IS '访问控制-接口封堵规则';
CREATE INDEX idx_acl_ep_scope_status ON d_acl_endpoint_rule (scope_code, status);
CREATE INDEX idx_acl_ep_match_type ON d_acl_endpoint_rule (match_type);

CREATE TABLE d_acl_custom_rule (
                                   id                  VARCHAR(64)   NOT NULL,
                                   scope_code          VARCHAR(64)   NOT NULL,
                                   rule_code           VARCHAR(100)  NOT NULL,
                                   rule_name           VARCHAR(255)  NOT NULL,
                                   priority            INT           NOT NULL DEFAULT 100,
                                   logic_op            VARCHAR(8)    NOT NULL DEFAULT 'AND',
                                   action              VARCHAR(16)   NOT NULL,
                                   status              VARCHAR(20)   NOT NULL DEFAULT 'ENABLED',
                                   create_by           VARCHAR(64)   DEFAULT '',
                                   create_time         timestamp,
                                   update_by           VARCHAR(64)   DEFAULT '',
                                   update_time         timestamp,
                                   is_deleted          INT           NOT NULL DEFAULT 0,
                                   PRIMARY KEY (id),
                                   CONSTRAINT uk_acl_custom_rule_code UNIQUE (rule_code)
);
COMMENT ON TABLE d_acl_custom_rule IS '访问控制-自定义组合规则';
CREATE INDEX idx_acl_custom_scope_status ON d_acl_custom_rule (scope_code, status);
CREATE INDEX idx_acl_custom_priority ON d_acl_custom_rule (priority);

CREATE TABLE d_acl_custom_condition (
                                        id                  BIGINT        NOT NULL GENERATED BY DEFAULT AS IDENTITY,
                                        rule_id             VARCHAR(64)   NOT NULL,
                                        seq_no              INT           NOT NULL DEFAULT 1,
                                        condition_expr      TEXT          NOT NULL,
                                        create_by           VARCHAR(64)   DEFAULT '',
                                        create_time         timestamp,
                                        update_by           VARCHAR(64)   DEFAULT '',
                                        update_time         timestamp,
                                        is_deleted          INT           NOT NULL DEFAULT 0,
                                        PRIMARY KEY (id),
                                        CONSTRAINT fk_acl_condition_rule FOREIGN KEY (rule_id) REFERENCES d_acl_custom_rule(id) ON DELETE CASCADE
);
COMMENT ON TABLE d_acl_custom_condition IS '访问控制-组合规则条件明细';
CREATE INDEX idx_acl_condition_rule_seq ON d_acl_custom_condition (rule_id, seq_no);

-- =========================
-- 内容管控
-- =========================

CREATE TABLE d_content_policy (
                                  id                  VARCHAR(64)  NOT NULL,
                                  scope_code          VARCHAR(64)  NOT NULL,
                                  policy_code         VARCHAR(100) NOT NULL,
                                  detect_mode         VARCHAR(20)  NOT NULL,
                                  risk_level          VARCHAR(20)  NOT NULL,
                                  action              VARCHAR(20)  NOT NULL,
                                  detect_scope_text   VARCHAR(64)  NOT NULL,
                                  status              VARCHAR(20)  NOT NULL DEFAULT 'ENABLED',
                                  create_by           VARCHAR(64)  DEFAULT '',
                                  create_time         timestamp,
                                  update_by           VARCHAR(64)  DEFAULT '',
                                  update_time         timestamp,
                                  is_deleted          INT          NOT NULL DEFAULT 0,
                                  PRIMARY KEY (id),
                                  CONSTRAINT uk_content_policy_code UNIQUE (policy_code)
);
COMMENT ON TABLE d_content_policy IS '内容管控-合规策略';
CREATE INDEX idx_content_policy_scope_status ON d_content_policy (scope_code, status);

CREATE TABLE d_content_dlp_rule (
                                    id                  VARCHAR(64)  NOT NULL,
                                    scope_code          VARCHAR(64)  NOT NULL,
                                    rule_code           VARCHAR(100) NOT NULL,
                                    location            VARCHAR(32)  NOT NULL,
                                    data_type           VARCHAR(64)  NOT NULL,
                                    detect_mode         VARCHAR(20)  NOT NULL,
                                    action              VARCHAR(20)  NOT NULL,
                                    status              VARCHAR(20)  NOT NULL DEFAULT 'ENABLED',
                                    create_by           VARCHAR(64)  DEFAULT '',
                                    create_time         timestamp,
                                    update_by           VARCHAR(64)  DEFAULT '',
                                    update_time         timestamp,
                                    is_deleted          INT          NOT NULL DEFAULT 0,
                                    PRIMARY KEY (id),
                                    CONSTRAINT uk_content_dlp_code UNIQUE (rule_code)
);
COMMENT ON TABLE d_content_dlp_rule IS '内容管控-DLP识别规则';
CREATE INDEX idx_content_dlp_scope_status ON d_content_dlp_rule (scope_code, status);

CREATE TABLE d_content_mask_policy (
                                       id                  VARCHAR(64)  NOT NULL,
                                       scope_code          VARCHAR(64)  NOT NULL,
                                       policy_code         VARCHAR(100) NOT NULL,
                                       apply_by            VARCHAR(16)  NOT NULL,
                                       location            VARCHAR(32)  NOT NULL,
                                       template_name       VARCHAR(128) NOT NULL,
                                       action              VARCHAR(20)  NOT NULL,
                                       status              VARCHAR(20)  NOT NULL DEFAULT 'ENABLED',
                                       create_by           VARCHAR(64)  DEFAULT '',
                                       create_time         timestamp,
                                       update_by           VARCHAR(64)  DEFAULT '',
                                       update_time         timestamp,
                                       is_deleted          INT          NOT NULL DEFAULT 0,
                                       PRIMARY KEY (id),
                                       CONSTRAINT uk_content_mask_policy_code UNIQUE (policy_code)
);
COMMENT ON TABLE d_content_mask_policy IS '内容管控-脱敏策略';
CREATE INDEX idx_content_mask_scope_status ON d_content_mask_policy (scope_code, status);

-- =========================
-- 攻击防护
-- =========================

CREATE TABLE d_attack_switch (
                                 id                  BIGINT        NOT NULL GENERATED BY DEFAULT AS IDENTITY,
                                 scope_code          VARCHAR(64)   NOT NULL,
                                 switch_key          VARCHAR(64)   NOT NULL,
                                 switch_name         VARCHAR(128)  NOT NULL,
                                 switch_desc         VARCHAR(255)  NULL,
                                 enabled             smallint    NOT NULL DEFAULT 1,
                                 sort_order          INT           NOT NULL DEFAULT 100,
                                 create_by           VARCHAR(64)   DEFAULT '',
                                 create_time         timestamp,
                                 update_by           VARCHAR(64)   DEFAULT '',
                                 update_time         timestamp,
                                 is_deleted          INT           NOT NULL DEFAULT 0,
                                 PRIMARY KEY (id),
                                 CONSTRAINT uk_attack_switch_scope_key UNIQUE (scope_code, switch_key)
);
COMMENT ON TABLE d_attack_switch IS '攻击防护-开关矩阵';
CREATE INDEX idx_attack_switch_scope_sort ON d_attack_switch (scope_code, sort_order);

CREATE TABLE d_attack_rule (
                               id                  VARCHAR(64)   NOT NULL,
                               scope_code          VARCHAR(64)   NOT NULL,
                               rule_code           VARCHAR(100)  NOT NULL,
                               category            VARCHAR(20)   NOT NULL,
                               sub_type            VARCHAR(64)   NOT NULL,
                               detect_mode         VARCHAR(20)   NOT NULL,
                               action              VARCHAR(20)   NOT NULL,
                               status              VARCHAR(20)   NOT NULL DEFAULT 'ENABLED',
                               create_by           VARCHAR(64)   DEFAULT '',
                               create_time         timestamp,
                               update_by           VARCHAR(64)   DEFAULT '',
                               update_time         timestamp,
                               is_deleted          INT           NOT NULL DEFAULT 0,
                               PRIMARY KEY (id),
                               CONSTRAINT uk_attack_rule_code UNIQUE (rule_code)
);
COMMENT ON TABLE d_attack_rule IS '攻击防护-注入/提示词规则';
CREATE INDEX idx_attack_rule_scope_category ON d_attack_rule (scope_code, category);
CREATE INDEX idx_attack_rule_status ON d_attack_rule (status);

CREATE TABLE d_attack_signature (
                                    id                  VARCHAR(64)   NOT NULL,
                                    scope_code          VARCHAR(64)   NOT NULL,
                                    sig_type            VARCHAR(20)   NOT NULL,
                                    sig_value           TEXT          NOT NULL,
                                    rule_code           VARCHAR(100)  NOT NULL,
                                    weight              INT           NOT NULL DEFAULT 1,
                                    status              VARCHAR(20)   NOT NULL DEFAULT 'ENABLED',
                                    create_by           VARCHAR(64)   DEFAULT '',
                                    create_time         timestamp,
                                    update_by           VARCHAR(64)   DEFAULT '',
                                    update_time         timestamp,
                                    is_deleted          INT           NOT NULL DEFAULT 0,
                                    PRIMARY KEY (id)
);
COMMENT ON TABLE d_attack_signature IS '攻击防护-特征库';
CREATE INDEX idx_attack_signature_scope_status ON d_attack_signature (scope_code, status);
CREATE INDEX idx_attack_signature_rule_code ON d_attack_signature (rule_code);

CREATE TABLE d_log_attack_policy (
                                     id                  VARCHAR(64)   NOT NULL,
                                     scope_code          VARCHAR(64)   NOT NULL,
                                     log_mode            VARCHAR(20)   NOT NULL,
                                     retain_days         INT           NOT NULL,
                                     sample_rate         VARCHAR(16)   NOT NULL,
                                     alert_level         VARCHAR(20)   NOT NULL,
                                     status              VARCHAR(20)   NOT NULL DEFAULT 'ENABLED',
                                     create_by           VARCHAR(64)   DEFAULT '',
                                     create_time         timestamp,
                                     update_by           VARCHAR(64)   DEFAULT '',
                                     update_time         timestamp,
                                     is_deleted          INT           NOT NULL DEFAULT 0,
                                     PRIMARY KEY (id)
);
COMMENT ON TABLE d_log_attack_policy IS '攻击防护-日志策略';
CREATE INDEX idx_log_attack_policy_scope_status ON d_log_attack_policy (scope_code, status);

-- =========================
-- 告警命中日志（新增）
-- =========================

CREATE TABLE d_log_alert_event (
                                   id                  BIGINT         NOT NULL GENERATED BY DEFAULT AS IDENTITY,
                                   event_no            VARCHAR(64)    NOT NULL,
                                   request_id          VARCHAR(64)    NOT NULL,
                                   trace_id            VARCHAR(64)    NULL,
                                   scope_code          VARCHAR(64)    NOT NULL,
                                   module_type         VARCHAR(20)    NOT NULL,
                                   event_type          VARCHAR(32)    NOT NULL,
                                   rule_type           VARCHAR(32)    NULL,
                                   rule_id             VARCHAR(64)    NULL,
                                   rule_code           VARCHAR(100)   NULL,
                                   switch_key          VARCHAR(64)    NULL,
                                   severity            VARCHAR(20)    NOT NULL,
                                   action_taken        VARCHAR(20)    NOT NULL,
                                   hit_message         VARCHAR(512)   NULL,
                                   hit_detail_json     JSON           NULL,
                                   request_path        VARCHAR(1024)  NULL,
                                   request_method      VARCHAR(16)    NULL,
                                   source_ip           VARCHAR(45)    NULL,
                                   caller_id           VARCHAR(128)   NULL,
                                   is_stream           smallint     NOT NULL DEFAULT 0,
                                   alert_status        VARCHAR(20)    NOT NULL DEFAULT 'NEW',
                                   ack_by              VARCHAR(64)    NULL,
                                   ack_at              timestamp       NULL,
                                   close_by            VARCHAR(64)    NULL,
                                   close_at            timestamp       NULL,
                                   occurred_at         timestamp       NOT NULL,
                                   partition_date      DATE           NOT NULL,
                                   create_by           VARCHAR(64)    DEFAULT '',
                                   create_time         timestamp,
                                   update_by           VARCHAR(64)    DEFAULT '',
                                   update_time         timestamp,
                                   is_deleted          INT            NOT NULL DEFAULT 0,
                                   PRIMARY KEY (id),
                                   CONSTRAINT uk_alert_event_no UNIQUE (event_no)
);
COMMENT ON TABLE d_log_alert_event IS '告警命中日志主表(记录请求触发的告警)';
CREATE INDEX idx_alert_request ON d_log_alert_event (request_id);
CREATE INDEX idx_alert_trace ON d_log_alert_event (trace_id);
CREATE INDEX idx_alert_scope_time ON d_log_alert_event (scope_code, occurred_at);
CREATE INDEX idx_alert_module_type ON d_log_alert_event (module_type, event_type);
CREATE INDEX idx_alert_status_severity ON d_log_alert_event (alert_status, severity);
CREATE INDEX idx_alert_partition_date ON d_log_alert_event (partition_date);

CREATE TABLE d_log_alert_hit (
                                 id                  BIGINT         NOT NULL GENERATED BY DEFAULT AS IDENTITY,
                                 event_id            BIGINT         NOT NULL,
                                 hit_order           INT            NOT NULL DEFAULT 1,
                                 hit_target          VARCHAR(32)    NULL,
                                 hit_field           VARCHAR(255)   NULL,
                                 hit_operator        VARCHAR(32)    NULL,
                                 expected_value      TEXT           NULL,
                                 actual_value_preview VARCHAR(512)  NULL,
                                 confidence          DECIMAL(5,2)   NULL,
                                 create_by           VARCHAR(64)    DEFAULT '',
                                 create_time         timestamp,
                                 update_by           VARCHAR(64)    DEFAULT '',
                                 update_time         timestamp,
                                 is_deleted          INT            NOT NULL DEFAULT 0,
                                 PRIMARY KEY (id),
                                 CONSTRAINT fk_log_alert_hit_event FOREIGN KEY (event_id) REFERENCES d_log_alert_event(id) ON DELETE CASCADE
);
COMMENT ON TABLE d_log_alert_hit IS '告警命中日志明细表(一条事件可包含多条命中明细)';
CREATE INDEX idx_alert_hit_event ON d_log_alert_hit (event_id, hit_order);
