<?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 plugin\saiadmin\app\cache\UserInfoCache;
use plugin\saiadmin\app\logic\system\SystemUserLogic;
use support\think\Db;

$uid = 123;
$user = Db::table('sa_system_user')->where('id', $uid)->find();
echo "DB user: " . json_encode($user, JSON_UNESCAPED_UNICODE) . "\n";

$logic = new SystemUserLogic();
$info = $logic->getUser($uid);
echo "getUser deptList: " . json_encode($info['deptList'] ?? null, JSON_UNESCAPED_UNICODE) . "\n";
echo "getUser dept_id: " . ($info['dept_id'] ?? 'null') . "\n";

$cached = UserInfoCache::getUserInfo($uid);
echo "cache deptList: " . json_encode($cached['deptList'] ?? null, JSON_UNESCAPED_UNICODE) . "\n";
echo "getDeptId: " . var_export(AdminScopeHelper::getDeptId($cached), true) . "\n";
echo "resolveConfigDeptId(null): " . AdminScopeHelper::resolveConfigDeptId($cached, null) . "\n";
echo "resolveConfigDeptId(0): " . AdminScopeHelper::resolveConfigDeptId($cached, 0) . "\n";

$deptId = 1123;
$all = Db::table('dice_reward_config')->where('dept_id', $deptId)->count();
$bigwin = Db::table('dice_reward_config')->where('dept_id', $deptId)->where('tier', 'BIGWIN')->count();
$reward = Db::table('dice_reward')->where('dept_id', $deptId)->count();
echo "dept {$deptId}: reward_config={$all}, BIGWIN={$bigwin}, dice_reward={$reward}\n";

$sample = Db::table('dice_reward_config')->where('dept_id', $deptId)->limit(3)->select();
echo "sample reward_config: " . json_encode($sample, JSON_UNESCAPED_UNICODE) . "\n";
