-- ============================================================
-- RBAC 表结构补丁（幂等，可重复执行）
-- 文件：database/sql/rbac_migrate_ddl.sql
--
-- 用途：给 nl_admin / nl_role 补齐迁移与日常接口所需列
--   - nl_admin.department_id          → 解除 admin/my-info 报 Unknown column
--   - nl_admin.legacy_password*       → 老无盐 sha1 惰性升级
--   - nl_role.status / color          → 角色禁用与标签色
--
-- 用法（任选其一）：
--   1. Navicat：选中库 cc_new_stash → 新建查询 → 整份粘贴 → 运行
--      （本文件不用 DELIMITER / 存储过程，Navicat 可直接跑）
--   2. mysql 客户端：
--      mysql -h... -u... -p cc_new_stash < database/sql/rbac_migrate_ddl.sql
--
-- 说明：
--   - 用 information_schema + PREPARE 实现「列/索引已存在则跳过」
--   - 不依赖 Laravel DB_PREFIX
--   - uk_phone 唯一索引在文末【可选】段，有重复手机号时会失败
-- ============================================================

-- ------------------------------------------------------------
-- nl_admin.department_id
-- ------------------------------------------------------------
SET @db := DATABASE();
SET @exists := (
    SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'nl_admin' AND COLUMN_NAME = 'department_id'
);
SET @sql := IF(@exists = 0,
    'ALTER TABLE `nl_admin` ADD COLUMN `department_id` int NOT NULL DEFAULT 0 COMMENT ''所属部门ID，对应 cc_department.id'' AFTER `role_id`',
    'SELECT ''skip nl_admin.department_id'' AS msg'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ------------------------------------------------------------
-- nl_admin.legacy_password
-- ------------------------------------------------------------
SET @exists := (
    SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'nl_admin' AND COLUMN_NAME = 'legacy_password'
);
SET @sql := IF(@exists = 0,
    'ALTER TABLE `nl_admin` ADD COLUMN `legacy_password` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '''' COMMENT ''老系统无盐sha1密码，登录成功后清空'' AFTER `password`',
    'SELECT ''skip nl_admin.legacy_password'' AS msg'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ------------------------------------------------------------
-- nl_admin.legacy_password_expire_at
-- ------------------------------------------------------------
SET @exists := (
    SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'nl_admin' AND COLUMN_NAME = 'legacy_password_expire_at'
);
SET @sql := IF(@exists = 0,
    'ALTER TABLE `nl_admin` ADD COLUMN `legacy_password_expire_at` int NOT NULL DEFAULT 0 COMMENT ''遗留密码失效时间，过期强制走重置'' AFTER `legacy_password`',
    'SELECT ''skip nl_admin.legacy_password_expire_at'' AS msg'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ------------------------------------------------------------
-- nl_admin 索引 idx_role / idx_department
-- ------------------------------------------------------------
SET @exists := (
    SELECT COUNT(*) FROM information_schema.STATISTICS
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'nl_admin' AND INDEX_NAME = 'idx_role'
);
SET @sql := IF(@exists = 0,
    'ALTER TABLE `nl_admin` ADD INDEX `idx_role`(`role_id` ASC, `deleted_at` ASC) USING BTREE',
    'SELECT ''skip idx_role'' AS msg'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @exists := (
    SELECT COUNT(*) FROM information_schema.STATISTICS
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'nl_admin' AND INDEX_NAME = 'idx_department'
);
SET @sql := IF(@exists = 0,
    'ALTER TABLE `nl_admin` ADD INDEX `idx_department`(`department_id` ASC, `deleted_at` ASC) USING BTREE',
    'SELECT ''skip idx_department'' AS msg'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ------------------------------------------------------------
-- nl_role.status / color  （你当前 audit 缺的就是这两列）
-- ------------------------------------------------------------
SET @exists := (
    SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'nl_role' AND COLUMN_NAME = 'status'
);
SET @sql := IF(@exists = 0,
    'ALTER TABLE `nl_role` ADD COLUMN `status` tinyint(1) NOT NULL DEFAULT 0 COMMENT ''状态 0正常 1禁用'' AFTER `desc`',
    'SELECT ''skip nl_role.status'' AS msg'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @exists := (
    SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'nl_role' AND COLUMN_NAME = 'color'
);
SET @sql := IF(@exists = 0,
    'ALTER TABLE `nl_role` ADD COLUMN `color` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '''' COMMENT ''角色标签颜色'' AFTER `status`',
    'SELECT ''skip nl_role.color'' AS msg'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ------------------------------------------------------------
-- 修正种子：超管 status 必须是 0（0正常 1禁用）
-- ------------------------------------------------------------
UPDATE `nl_admin` SET `status` = 0 WHERE `id` = 1 AND `status` = 1;

-- ============================================================
-- 【可选】uk_phone —— 先跑 lgp:rbac-audit 确认手机号无重复后再执行
-- ============================================================
-- SET @exists := (
--     SELECT COUNT(*) FROM information_schema.STATISTICS
--     WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'nl_admin' AND INDEX_NAME = 'uk_phone'
-- );
-- SET @sql := IF(@exists = 0,
--     'ALTER TABLE `nl_admin` ADD UNIQUE INDEX `uk_phone`(`phone` ASC) USING BTREE',
--     'SELECT ''skip uk_phone'' AS msg'
-- );
-- PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
