CREATE TABLE IF NOT EXISTS project_user_binding (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    project_id BIGINT UNSIGNED NOT NULL,
    user_id BIGINT UNSIGNED NOT NULL,
    bind_role_code VARCHAR(64) NOT NULL COMMENT 'PROJECT_OWNER/PROJECT_EXECUTOR',
    is_deleted TINYINT(1) NOT NULL DEFAULT 0,
    created_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_project_user_role (tenant_id, project_id, user_id, bind_role_code),
    KEY idx_user_role (tenant_id, user_id, bind_role_code, is_deleted)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO permission (id, permission_code, permission_name, module)
VALUES (20001, 'project.bind.user', '绑定项目人员', 'project')
ON DUPLICATE KEY UPDATE permission_name = VALUES(permission_name), module = VALUES(module);

INSERT INTO role_permission (id, tenant_id, role_id, permission_id)
SELECT 20001, 1, r.id, 20001
FROM role r
WHERE r.tenant_id = 1 AND r.role_code = 'TENANT_ADMIN'
ON DUPLICATE KEY UPDATE permission_id = VALUES(permission_id);
