<?php
declare(strict_types=1);
define('BASE_PATH', dirname(__DIR__));
require 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 app\dice\helper\AdminScopeHelper;
use app\dice\logic\reward_config\DiceRewardConfigLogic;
use plugin\saiadmin\app\cache\UserInfoCache;
use support\think\Db;

$adminInfo = UserInfoCache::getUserInfo(123);
$logic = new DiceRewardConfigLogic();
$query = $logic->search([]);
AdminScopeHelper::applyConfigScope($query, $adminInfo, 0);
$_GET['limit'] = 200;
$_REQUEST['limit'] = 200;
$result = $logic->getList($query);
echo "limit=200 data count: " . count($result['data'] ?? []) . " total=" . ($result['total'] ?? 0) . "\n";
$bw = 0;
foreach ($result['data'] ?? [] as $row) {
    if (($row['tier'] ?? '') === 'BIGWIN') {
        $bw++;
        echo "BIGWIN id={$row['id']} grid={$row['grid_number']}\n";
    }
}

$query3 = $logic->search([]);
AdminScopeHelper::applyConfigScope($query3, $adminInfo, 0);
unset($_GET['limit'], $_REQUEST['limit']);
$result3 = $logic->getList($query3);
echo "default limit data count: " . count($result3['data'] ?? []) . "\n";
$bw3 = 0;
foreach ($result3['data'] ?? [] as $row) {
    if (($row['tier'] ?? '') === 'BIGWIN') {
        $bw3++;
    }
}
echo "default limit BIGWIN: {$bw3}\n";

$_GET['saiType'] = 'all';
$_REQUEST['saiType'] = 'all';
$resultAll = $logic->getList($query);
echo "saiType=all count: " . (is_array($resultAll) ? count($resultAll) : 0) . "\n";
if (is_array($resultAll)) {
    $bwAll = 0;
    foreach ($resultAll as $row) {
        if (($row['tier'] ?? '') === 'BIGWIN') {
            $bwAll++;
        }
    }
    echo "saiType=all BIGWIN: {$bwAll}\n";
}

echo "\nAll rows by id for dept 1123:\n";
$allRows = \support\think\Db::table('dice_reward_config')->where('dept_id', 1123)->order('id', 'asc')->select();
foreach ($allRows as $r) {
    echo "id={$r['id']} tier={$r['tier']} grid={$r['grid_number']}\n";
}
