<?php
declare(strict_types=1);
define('BASE_PATH', dirname(__DIR__));
require_once BASE_PATH . '/vendor/autoload.php';
\Dotenv\Dotenv::createUnsafeMutable(BASE_PATH)->load();
\Webman\Config::load(BASE_PATH . '/config', ['route', 'plugin']);
\Webman\ThinkOrm\ThinkOrm::start(null);

use plugin\saiadmin\app\model\system\SystemDept;
use support\think\Db;

$deptIds = SystemDept::column('id');
echo "渠道数: " . count($deptIds) . " [" . implode(',', $deptIds) . "]\n\n";

$tables = [
    'dice_config',
    'dice_ante_config',
    'dice_lottery_pool_config',
    'dice_reward_config',
    'dice_reward',
    'dice_game',
];

echo "=== 配置按 dept_id 统计 ===\n";
foreach ($tables as $table) {
    $rows = Db::query("SELECT dept_id, COUNT(*) AS cnt FROM `{$table}` GROUP BY dept_id ORDER BY dept_id");
    echo "{$table}:\n";
    foreach ($rows as $r) {
        $dept = $r['dept_id'] === null ? 'NULL' : (string) $r['dept_id'];
        echo "  dept_id={$dept}: {$r['cnt']}\n";
    }
}

echo "\n=== 业务数据未回填 dept_id ===\n";
$biz = ['dice_player', 'dice_play_record', 'dice_play_record_test'];
foreach ($biz as $table) {
    if (!Db::getFields($table)) {
        continue;
    }
    $nullCnt = Db::table($table)->where(function ($q) {
        $q->whereNull('dept_id')->whereOr('dept_id', 0);
    })->count();
    $total = Db::table($table)->count();
    echo "{$table}: 未回填 {$nullCnt} / 总计 {$total}\n";
}
