Files
sbnews/server/app/common/model/ai/AiUnlock.php
T

71 lines
1.9 KiB
PHP

<?php
namespace app\common\model\ai;
use app\common\model\BaseModel;
use app\common\model\user\User;
use app\common\service\ai\AiQuotaService;
class AiUnlock extends BaseModel
{
/**
* 检查用户是否已解锁某场比赛
*/
public static function isUnlocked(int $userId, int $matchId): bool
{
return self::where('user_id', $userId)->where('match_id', $matchId)->count() > 0;
}
/**
* 解锁比赛(扣次数 + 写记录)
* @return array ['ok' => bool, 'msg' => string]
*/
public static function unlock(int $userId, int $matchId): array
{
// 已解锁则直接返回
if (self::isUnlocked($userId, $matchId)) {
return ['ok' => true, 'msg' => '已解锁'];
}
// 检查今日剩余次数
$user = User::findOrEmpty($userId);
if ($user->isEmpty()) {
return ['ok' => false, 'msg' => '用户不存在'];
}
// VIP用户不限次数
if (AiQuotaService::isVip($user)) {
self::create([
'user_id' => $userId,
'match_id' => $matchId,
]);
return ['ok' => true, 'msg' => 'VIP解锁'];
}
$freeCount = AiQuotaService::resetDailyIfNeeded($user);
if ($freeCount <= 0) {
return ['ok' => false, 'msg' => '今日免费次数已用完,升级VIP可无限使用'];
}
// 扣次数 + 写解锁记录
$user->ai_free_count = $freeCount - 1;
$user->save();
self::create([
'user_id' => $userId,
'match_id' => $matchId,
]);
return ['ok' => true, 'msg' => '解锁成功', 'remain' => $user->ai_free_count];
}
/**
* 获取用户今日剩余次数
*/
public static function getRemainCount(int $userId): int
{
return AiQuotaService::getRemainCount($userId);
}
}