50 lines
1.5 KiB
PHP
50 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace app\common\model\ai;
|
|
|
|
use app\common\model\BaseModel;
|
|
use app\common\service\ai\AiQuotaService;
|
|
|
|
class AiUnlock extends BaseModel
|
|
{
|
|
/**
|
|
* 检查用户是否已解锁某场比赛
|
|
*/
|
|
public static function isUnlocked(int $userId, int $matchId): bool
|
|
{
|
|
$analysisKey = AiQuotaService::makeKey(AiQuotaService::TYPE_MATCH, $matchId);
|
|
return AiQuotaService::hasUnlocked($userId, $analysisKey)
|
|
|| 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
|
|
{
|
|
$analysisKey = AiQuotaService::makeKey(AiQuotaService::TYPE_MATCH, $matchId);
|
|
$result = AiQuotaService::unlock($userId, AiQuotaService::TYPE_MATCH, $analysisKey, $matchId);
|
|
if (!empty($result['ok']) && self::where('user_id', $userId)->where('match_id', $matchId)->count() <= 0) {
|
|
self::create([
|
|
'user_id' => $userId,
|
|
'match_id' => $matchId,
|
|
]);
|
|
}
|
|
|
|
return [
|
|
'ok' => (bool) ($result['ok'] ?? false),
|
|
'msg' => (string) ($result['msg'] ?? ''),
|
|
'remain' => $result['remain'] ?? AiQuotaService::getRemainCount($userId),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 获取用户今日剩余次数
|
|
*/
|
|
public static function getRemainCount(int $userId): int
|
|
{
|
|
return AiQuotaService::getRemainCount($userId);
|
|
}
|
|
}
|