<?php
/**
 * 渠道配置初始化：默认模板 + 为已有渠道补齐配置 + 业务数据 dept_id 回填
 * 用法：在 server 目录执行 php db/run_channel_config_init.php
 */
require_once __DIR__ . '/../vendor/autoload.php';

$bootstrap = __DIR__ . '/../support/bootstrap.php';
if (is_file($bootstrap)) {
    require_once $bootstrap;
}

use app\dice\service\DiceChannelConfigService;
use support\think\Db;

$service = new DiceChannelConfigService();

echo "1. 将 dept_id=0 的配置归为默认模板（dept_id 置空）...\n";
echo "   若需将全部现有配置作为模板，请确认后手动执行 UPDATE ... SET dept_id=NULL\n";
foreach (
    [
        'dice_config',
        'dice_ante_config',
        'dice_lottery_pool_config',
        'dice_reward_config',
        'dice_reward',
        'dice_game',
    ] as $table
) {
    try {
        Db::table($table)->where('dept_id', 0)->update(['dept_id' => null]);
        echo "  {$table}: ok\n";
    } catch (\Throwable $e) {
        echo "  {$table}: 跳过 ({$e->getMessage()})\n";
    }
}

echo "2. 为所有渠道补齐默认配置...\n";
$summary = $service->syncAllChannelsFromDefault();
print_r($summary);

echo "3. 回填玩家及记录 dept_id...\n";
$stats = $service->backfillDataDeptId();
print_r($stats);

echo "完成。\n";
