-- DM8 外部系统推送配置初始化脚本
-- 用于配置不同公司/部门/业务类型下的外部系统推送开关
-- 创建日期：2026-01-20

-- 重复执行时请先备份数据
-- DROP TABLE IF EXISTS system_external_push_config;

-- 创建表
CREATE TABLE system_external_push_config (
    id                BIGINT            NOT NULL PRIMARY KEY,
    company_id        BIGINT            DEFAULT NULL NULL,
    dept_id           BIGINT            DEFAULT NULL NULL,
    business_type     VARCHAR(32)       DEFAULT NULL NULL,
    external_system   VARCHAR(64)       DEFAULT NULL NULL,
    enable_push       BIT               DEFAULT '1' NOT NULL,
    remark            VARCHAR(512)      DEFAULT NULL NULL,
    tenant_id         BIGINT            DEFAULT 0 NOT 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           TINYINT           DEFAULT 0 NOT NULL
);

-- 表和字段注释
COMMENT ON TABLE system_external_push_config IS '外部系统推送配置';
COMMENT ON COLUMN system_external_push_config.id IS '主键编号';
COMMENT ON COLUMN system_external_push_config.company_id IS '公司编号（关联 system_dept 表的 is_company=1 的记录，为空表示不限制公司）';
COMMENT ON COLUMN system_external_push_config.dept_id IS '部门编号（关联 system_dept 表的 is_company=0 的记录，为空表示公司级配置）';
COMMENT ON COLUMN system_external_push_config.business_type IS '业务类型（PURCHASE/SALE/PRODUCTION，为空表示所有业务类型）';
COMMENT ON COLUMN system_external_push_config.external_system IS '外部系统标识（ERP/IWORK/等，为空表示所有外部系统）';
COMMENT ON COLUMN system_external_push_config.enable_push IS '是否启用推送（1启用 0停用）';
COMMENT ON COLUMN system_external_push_config.remark IS '备注';
COMMENT ON COLUMN system_external_push_config.tenant_id IS '租户编号';
COMMENT ON COLUMN system_external_push_config.creator IS '创建者';
COMMENT ON COLUMN system_external_push_config.create_time IS '创建时间';
COMMENT ON COLUMN system_external_push_config.updater IS '更新者';
COMMENT ON COLUMN system_external_push_config.update_time IS '更新时间';
COMMENT ON COLUMN system_external_push_config.deleted IS '删除标记（0未删除 1已删除）';

/*-- 唯一索引：租户+公司+部门+业务类型+外部系统的组合唯一
-- 注意：因为 dept_id 和 external_system 可以为 NULL，使用 COALESCE 处理
CREATE UNIQUE INDEX uk_external_push_config_unique
    ON system_external_push_config (tenant_id, company_id, COALESCE(dept_id, 0), business_type, COALESCE(external_system, ''));

-- 辅助索引：按公司查询
CREATE INDEX idx_external_push_config_company
    ON system_external_push_config (tenant_id, company_id);

-- 辅助索引：按业务类型查询
CREATE INDEX idx_external_push_config_biz_type
    ON system_external_push_config (tenant_id, business_type);



-- 初始化菜单权限数据
-- 主菜单
INSERT INTO system_menu (
    id, name, permission, type, sort, parent_id,
    path, icon, component, component_name, status,
    visible, keep_alive, always_show, creator, create_time,
    updater, update_time, deleted
)
SELECT
    20060, '外部系统推送配置', '', 2, 60, 1,
    'external-push-config', 'setting', 'system/push/config/index', 'SystemExternalPushConfig', 0,
    '1', '1', '1', 'admin', SYSDATE,
    'admin', SYSDATE, 0
FROM dual
WHERE NOT EXISTS (
    SELECT 1 FROM system_menu WHERE id = 20060
);

-- 查询权限
INSERT INTO system_menu (
    id, name, permission, type, sort, parent_id,
    path, icon, component, status, visible, keep_alive,
    creator, create_time, updater, update_time, deleted
)
SELECT
    2006001, '推送配置查询', 'system:external-push-config:query', 3, 1, 20060,
    '', '', '', 0, '1', '1',
    'admin', SYSDATE, 'admin', SYSDATE, 0
FROM dual
WHERE NOT EXISTS (
    SELECT 1 FROM system_menu WHERE id = 2006001
);

-- 创建权限
INSERT INTO system_menu (
    id, name, permission, type, sort, parent_id,
    path, icon, component, status, visible, keep_alive,
    creator, create_time, updater, update_time, deleted
)
SELECT
    2006002, '推送配置创建', 'system:external-push-config:create', 3, 2, 20060,
    '', '', '', 0, '1', '1',
    'admin', SYSDATE, 'admin', SYSDATE, 0
FROM dual
WHERE NOT EXISTS (
    SELECT 1 FROM system_menu WHERE id = 2006002
);

-- 修改权限
INSERT INTO system_menu (
    id, name, permission, type, sort, parent_id,
    path, icon, component, status, visible, keep_alive,
    creator, create_time, updater, update_time, deleted
)
SELECT
    2006003, '推送配置修改', 'system:external-push-config:update', 3, 3, 20060,
    '', '', '', 0, '1', '1',
    'admin', SYSDATE, 'admin', SYSDATE, 0
FROM dual
WHERE NOT EXISTS (
    SELECT 1 FROM system_menu WHERE id = 2006003
);

-- 删除权限
INSERT INTO system_menu (
    id, name, permission, type, sort, parent_id,
    path, icon, component, status, visible, keep_alive,
    creator, create_time, updater, update_time, deleted
)
SELECT
    2006004, '推送配置删除', 'system:external-push-config:delete', 3, 4, 20060,
    '', '', '', 0, '1', '1',
    'admin', SYSDATE, 'admin', SYSDATE, 0
FROM dual
WHERE NOT EXISTS (
    SELECT 1 FROM system_menu WHERE id = 2006004
);*/
