Files
webman-buildadmin/app/common/service/GameChannelUserCount.php

48 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace app\common\service;
use support\think\Db;
/**
* 按 game_user 表统计各渠道用户数,回写 game_channel.user_count
*/
class GameChannelUserCount
{
/**
* 统计 game_user.game_channel_id = 该渠道 id 的行数,更新 game_channel.user_count
*/
public static function syncFromGameUser(int|string|null $channelId): void
{
if ($channelId === null || $channelId === '') {
return;
}
if (is_numeric($channelId) && (float) $channelId < 1) {
return;
}
$count = Db::name('game_user')->where('game_channel_id', $channelId)->count();
Db::name('game_channel')->where('id', $channelId)->update(['user_count' => $count]);
}
/**
* @param list<int|string|null> $channelIds
*/
public static function syncChannels(array $channelIds): void
{
$seen = [];
foreach ($channelIds as $cid) {
if ($cid === null || $cid === '') {
continue;
}
$k = (string) $cid;
if (isset($seen[$k])) {
continue;
}
$seen[$k] = true;
self::syncFromGameUser($cid);
}
}
}