-- 大富翁游戏相关表增加 dept_id，关联 sa_system_dept（渠道表）

ALTER TABLE `dice_ante_config`
    ADD COLUMN `dept_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '所属渠道ID' AFTER `id`,
    ADD INDEX `idx_dept_id` (`dept_id`);

ALTER TABLE `dice_config`
    ADD COLUMN `dept_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '所属渠道ID' AFTER `id`,
    ADD INDEX `idx_dept_id` (`dept_id`);

ALTER TABLE `dice_game`
    ADD COLUMN `dept_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '所属渠道ID' AFTER `id`,
    ADD INDEX `idx_dept_id` (`dept_id`);

ALTER TABLE `dice_lottery_config`
    ADD COLUMN `dept_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '所属渠道ID' AFTER `id`,
    ADD INDEX `idx_dept_id` (`dept_id`);

ALTER TABLE `dice_lottery_poll_record`
    ADD COLUMN `dept_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '所属渠道ID' AFTER `id`,
    ADD INDEX `idx_dept_id` (`dept_id`);

ALTER TABLE `dice_lottery_pool_config`
    ADD COLUMN `dept_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '所属渠道ID' AFTER `id`,
    ADD INDEX `idx_dept_id` (`dept_id`);

ALTER TABLE `dice_play_record`
    ADD COLUMN `dept_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '所属渠道ID' AFTER `id`,
    ADD INDEX `idx_dept_id` (`dept_id`);

ALTER TABLE `dice_play_record_test`
    ADD COLUMN `dept_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '所属渠道ID' AFTER `id`,
    ADD INDEX `idx_dept_id` (`dept_id`);

ALTER TABLE `dice_player`
    ADD COLUMN `dept_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '所属渠道ID' AFTER `id`,
    ADD INDEX `idx_dept_id` (`dept_id`);

-- 同一渠道内用户名唯一（根本约束，新库初始化时执行；已有库请用 dice_player_dept_username_unique.sql）
ALTER TABLE `dice_player`
    ADD UNIQUE INDEX `uk_dice_player_dept_username` (`dept_id`, `username`);

ALTER TABLE `dice_player_ticket_record`
    ADD COLUMN `dept_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '所属渠道ID' AFTER `id`,
    ADD INDEX `idx_dept_id` (`dept_id`);

ALTER TABLE `dice_player_wallet_record`
    ADD COLUMN `dept_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '所属渠道ID' AFTER `id`,
    ADD INDEX `idx_dept_id` (`dept_id`);

ALTER TABLE `dice_reward`
    ADD COLUMN `dept_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '所属渠道ID' AFTER `id`,
    ADD INDEX `idx_dept_id` (`dept_id`);

ALTER TABLE `dice_reward_config`
    ADD COLUMN `dept_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '所属渠道ID' AFTER `id`,
    ADD INDEX `idx_dept_id` (`dept_id`);

ALTER TABLE `dice_reward_config_record`
    ADD COLUMN `dept_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '所属渠道ID' AFTER `id`,
    ADD INDEX `idx_dept_id` (`dept_id`);

-- 从管理员归属回填玩家 dept_id
UPDATE `dice_player` p
INNER JOIN `sa_system_user` u ON p.admin_id = u.id
SET p.dept_id = u.dept_id
WHERE p.dept_id IS NULL AND u.dept_id IS NOT NULL AND u.dept_id > 0;

UPDATE `dice_play_record` r
INNER JOIN `dice_player` p ON r.player_id = p.id
SET r.dept_id = p.dept_id
WHERE r.dept_id IS NULL AND p.dept_id IS NOT NULL;

UPDATE `dice_play_record_test` r
INNER JOIN `dice_player` p ON r.player_id = p.id
SET r.dept_id = p.dept_id
WHERE r.dept_id IS NULL AND p.dept_id IS NOT NULL;

UPDATE `dice_player_ticket_record` r
INNER JOIN `dice_player` p ON r.player_id = p.id
SET r.dept_id = p.dept_id
WHERE r.dept_id IS NULL AND p.dept_id IS NOT NULL;

UPDATE `dice_player_wallet_record` r
INNER JOIN `dice_player` p ON r.player_id = p.id
SET r.dept_id = p.dept_id
WHERE r.dept_id IS NULL AND p.dept_id IS NOT NULL;

UPDATE `dice_reward_config_record` r
INNER JOIN `sa_system_user` u ON r.admin_id = u.id
SET r.dept_id = u.dept_id
WHERE r.dept_id IS NULL AND u.dept_id IS NOT NULL AND u.dept_id > 0;
