ALTER TABLE infra_file ADD hash VARCHAR(64);
COMMENT ON COLUMN infra_file.hash IS '文件哈希值（SHA-256）';
CREATE INDEX idx_infra_file_hash ON infra_file(hash);

-- 2. 附件信息表新增 AES 加密时存储的随机 IV 字段
ALTER TABLE infra_file ADD aes_iv VARCHAR(128);

COMMENT ON COLUMN infra_file.aes_iv IS 'AES加密时的随机IV（Base64编码）';

-- 3 业务附件统一管理
DROP TABLE IF EXISTS infra_bsn_file;
CREATE TABLE infra_bsn_file (
                                id bigint NOT NULL PRIMARY KEY,
                                bsn_id bigint  NOT NULL,
                                bsn_cd varchar(100) DEFAULT '' NULL,
                                file_id bigint  NOT NULL,
                                file_name varchar(500) DEFAULT '' NULL,
                                src varchar(100) DEFAULT '' NULL,
                                creator varchar(64) DEFAULT '' NULL,
                                create_time datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
                                updater varchar(64) DEFAULT '' NULL,
                                update_time datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
                                deleted smallint DEFAULT 0 NOT NULL,
                                tenant_id bigint DEFAULT 0 NOT NULL
);
COMMENT ON TABLE infra_bsn_file IS '业务附件关联表';
COMMENT ON COLUMN infra_bsn_file.id IS '主键ID';
COMMENT ON COLUMN infra_bsn_file.bsn_id IS '业务Id';
COMMENT ON COLUMN infra_bsn_file.bsn_cd IS '业务编码';
COMMENT ON COLUMN infra_bsn_file.file_id IS '附件fileId';
COMMENT ON COLUMN infra_bsn_file.file_name IS '附件名称';
COMMENT ON COLUMN infra_bsn_file.src IS '附件来源';
COMMENT ON COLUMN infra_bsn_file.creator IS '创建者';
COMMENT ON COLUMN infra_bsn_file.create_time IS '创建时间';
COMMENT ON COLUMN infra_bsn_file.updater IS '更新者';
COMMENT ON COLUMN infra_bsn_file.update_time IS '最后更新时间';
COMMENT ON COLUMN infra_bsn_file.deleted IS '是否删除';
COMMENT ON COLUMN infra_bsn_file.tenant_id IS '租户编号';

-- 业务菜单 sql
-- 菜单 SQL
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status, component_name
)
VALUES (
           '1948328245618204673', '业务附件关联管理', '', 2, 0, 1243,
           'business-file', '', 'infra/businessfile/index', 0, 'BusinessFile'
       );

-- 按钮 SQL
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status
)
VALUES (
           '1951092724040097793', '业务附件关联查询', 'infra:business-file:query', 3, 1, 1948328245618204673,
           '', '', '', 0
       );
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status
)
VALUES (
           '1951092724040097794', '业务附件关联创建', 'infra:business-file:create', 3, 2, 1948328245618204673,
           '', '', '', 0
       );
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status
)
VALUES (
           '1951092724040097795', '业务附件关联更新', 'infra:business-file:update', 3, 3, 1948328245618204673,
           '', '', '', 0
       );
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status
)
VALUES (
           '1951092724040097796', '业务附件关联删除', 'infra:business-file:delete', 3, 4, 1948328245618204673,
           '', '', '', 0
       );
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status
)
VALUES (
           '1951092724040097797', '业务附件关联导出', 'infra:business-file:export', 3, 5, 1948328245618204673,
           '', '', '', 0
       );

-- 新增命名规范字典菜单
-- 菜单 SQL
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status, component_name
)
VALUES (
           '1947909810016006146', '数据命名与简写标准管理', '', 2, 0, 2,
           'standard-name', '', 'infra/standardname/index', 0, 'StandardName'
       );

-- 按钮父菜单ID
-- 暂时只支持 MySQL。如果你是 Oracle、PostgreSQL、SQLServer 的话，需要手动修改 @parentId 的部分的代码
-- SELECT @parentId := LAST_INSERT_ID();


-- 按钮 SQL
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status
)
VALUES (
           '1953301310553645058', '数据命名与简写标准查询', 'infra:standard-name:query', 3, 1, 1947909810016006146,
           '', '', '', 0
       );
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status
)
VALUES (
           '1953301310553645059', '数据命名与简写标准创建', 'infra:standard-name:create', 3, 2, 1947909810016006146,
           '', '', '', 0
       );
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status
)
VALUES (
           '1953301310553645060', '数据命名与简写标准更新', 'infra:standard-name:update', 3, 3, 1947909810016006146,
           '', '', '', 0
       );
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status
)
VALUES (
           '1953301310553645061', '数据命名与简写标准删除', 'infra:standard-name:delete', 3, 4, 1947909810016006146,
           '', '', '', 0
       );
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status
)
VALUES (
           '1953301310553645062', '数据命名与简写标准导出', 'infra:standard-name:export', 3, 5, 1947909810016006146,
           '', '', '', 0
       );

-- 新增序列获取菜单
-- 菜单 SQL
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status, component_name
)
VALUES (
           '1953701540574969857', '系统序列号管理', '', 2, 0, ${table.parentMenuId},
           'sequence', '', 'system/sequence/index', 0, 'Sequence'
       );

-- 按钮父菜单ID
-- 暂时只支持 MySQL。如果你是 Oracle、PostgreSQL、SQLServer 的话，需要手动修改 @parentId 的部分的代码
-- SELECT @parentId := LAST_INSERT_ID();


-- 按钮 SQL
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status
)
VALUES (
           '1953702581324398594', '系统序列号查询', 'system:sequence:query', 3, 1, 1953701540574969857,
           '', '', '', 0
       );
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status
)
VALUES (
           '1953702581324398595', '系统序列号创建', 'system:sequence:create', 3, 2, 1953701540574969857,
           '', '', '', 0
       );
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status
)
VALUES (
           '1953702581324398596', '系统序列号更新', 'system:sequence:update', 3, 3, 1953701540574969857,
           '', '', '', 0
       );
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status
)
VALUES (
           '1953702581324398597', '系统序列号删除', 'system:sequence:delete', 3, 4, 1953701540574969857,
           '', '', '', 0
       );
INSERT INTO system_menu(
    id, name, permission, type, sort, parent_id,
    path, icon, component, status
)
VALUES (
           '1953702581324398598', '系统序列号导出', 'system:sequence:export', 3, 5, 1953701540574969857,
           '', '', '', 0
       );

-- 系统序列号相关字典数据初始化脚本（达梦8版）

-- ========================================
-- 1. 系统序列号循环类型字典
-- ========================================

-- 插入字典类型
INSERT INTO system_dict_type (id, name, type, status, remark, creator, create_time, updater, update_time, deleted, deleted_time)
VALUES (500, '系统序列号循环类型', 'system_sequence_cycle_type', 0, '系统序列号循环类型枚举', 'admin', SYSDATE, 'admin', SYSDATE, 0, NULL);

-- 插入字典数据
INSERT INTO system_dict_data (id, sort, label, value, dict_type, status, color_type, css_class, remark, creator, create_time, updater, update_time, deleted)
VALUES
    (5001, 1, '年循环', 'Y', 'system_sequence_cycle_type', 0, 'primary', '', '年循环。示例: 2025', 'admin', SYSDATE, 'admin', SYSDATE, 0),
    (5002, 2, '年-月循环', 'Y-M', 'system_sequence_cycle_type', 0, 'success', '', '年-月循环。示例: 2025-08', 'admin', SYSDATE, 'admin', SYSDATE, 0),
    (5003, 3, '年月紧凑', 'YM', 'system_sequence_cycle_type', 0, 'info', '', '年月紧凑。示例: 202508', 'admin', SYSDATE, 'admin', SYSDATE, 0),
    (5004, 4, '两位年+月', 'yM', 'system_sequence_cycle_type', 0, 'warning', '', '两位年+月。示例: 2508', 'admin', SYSDATE, 'admin', SYSDATE, 0),
    (5005, 5, '年-月-日循环', 'Y-M-D', 'system_sequence_cycle_type', 0, 'primary', '', '年-月-日循环。示例: 2025-08-08', 'admin', SYSDATE, 'admin', SYSDATE, 0),
    (5006, 6, '年月日紧凑', 'YMD', 'system_sequence_cycle_type', 0, 'success', '', '年月日紧凑。示例: 20250808', 'admin', SYSDATE, 'admin', SYSDATE, 0),
    (5007, 7, '两位年+月日', 'yMD', 'system_sequence_cycle_type', 0, 'info', '', '两位年+月日。示例: 250808', 'admin', SYSDATE, 'admin', SYSDATE, 0),
    (5008, 8, '自定义循环值', 'CUST', 'system_sequence_cycle_type', 0, 'warning', '', '自定义循环值；若未传 circulationValue，则默认用 seqId', 'admin', SYSDATE, 'admin', SYSDATE, 0),
    (5009, 9, '仅前缀', 'PFX', 'system_sequence_cycle_type', 0, 'danger', '', '仅前缀，不需要时间循环值（不设置则不抛错）', 'admin', SYSDATE, 'admin', SYSDATE, 0);

-- ========================================
-- 2. 系统序列号分段类型字典
-- ========================================

-- 插入字典类型
INSERT INTO system_dict_type (id, name, type, status, remark, creator, create_time, updater, update_time, deleted, deleted_time)
VALUES (501, '系统序列号分段类型', 'system_sequence_detail_type', 0, '系统序列号分段类型枚举', 'admin', SYSDATE, 'admin', SYSDATE, 0, NULL);

-- 插入字典数据
INSERT INTO system_dict_data (id, sort, label, value, dict_type, status, color_type, css_class, remark, creator, create_time, updater, update_time, deleted)
VALUES
    (5011, 1, '默认字符分段', 'STR', 'system_sequence_detail_type', 0, 'primary', '', '固定字符串分段', 'admin', SYSDATE, 'admin', SYSDATE, 0),
    (5012, 2, '给定字符分段', 'INPUT', 'system_sequence_detail_type', 0, 'success', '', '根据输入参数动态生成的字符分段', 'admin', SYSDATE, 'admin', SYSDATE, 0),
    (5013, 3, '日期分段', 'DATE', 'system_sequence_detail_type', 0, 'info', '', '基于日期格式的分段', 'admin', SYSDATE, 'admin', SYSDATE, 0),
    (5014, 4, '流水号分段', 'SEQ', 'system_sequence_detail_type', 0, 'warning', '', '自增流水号分段', 'admin', SYSDATE, 'admin', SYSDATE, 0);

-- ========================================
-- 3. 系统序列号分段规则类型字典
-- ========================================

-- 插入字典类型
INSERT INTO system_dict_type (id, name, type, status, remark, creator, create_time, updater, update_time, deleted, deleted_time)
VALUES (502, '系统序列号分段规则类型', 'system_sequence_detail_rule_type', 0, '系统序列号分段规则类型枚举', 'admin', SYSDATE, 'admin', SYSDATE, 0, NULL);

-- 插入字典数据
INSERT INTO system_dict_data (id, sort, label, value, dict_type, status, color_type, css_class, remark, creator, create_time, updater, update_time, deleted)
VALUES
    (5021, 1, '固定值', 'FIXED', 'system_sequence_detail_rule_type', 0, 'primary', '', '固定字符串值', 'admin', SYSDATE, 'admin', SYSDATE, 0),
    (5022, 2, '日期格式', 'DATE', 'system_sequence_detail_rule_type', 0, 'success', '', '日期格式规则', 'admin', SYSDATE, 'admin', SYSDATE, 0),
    (5023, 3, '数字格式', 'NUMBER', 'system_sequence_detail_rule_type', 0, 'info', '', '数字格式规则', 'admin', SYSDATE, 'admin', SYSDATE, 0),
    (5024, 4, '自定义格式', 'CUSTOM', 'system_sequence_detail_rule_type', 0, 'warning', '', '自定义格式规则', 'admin', SYSDATE, 'admin', SYSDATE, 0);                                                                                                                                                                               (5024, 4, '自定义格式', 'CUSTOM', 'system_sequence_detail_rule_type', 0, 'warning', '', '自定义格式规则', 'admin', NOW(), 'admin', NOW(), b'0');
