45 lines
1.1 KiB
PHP
45 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace app\common\service\ai;
|
|
|
|
use app\common\model\user\User;
|
|
|
|
class AiQuotaService
|
|
{
|
|
public const DAILY_FREE_COUNT = 3;
|
|
public const VIP_REMAIN_COUNT = 999;
|
|
|
|
public static function isVip(User $user): bool
|
|
{
|
|
return (int) $user->getData('is_vip') === 1 && (int) $user->getData('vip_expire_time') > time();
|
|
}
|
|
|
|
public static function resetDailyIfNeeded(User $user): int
|
|
{
|
|
$today = date('Y-m-d');
|
|
if ($user->getData('ai_free_date') !== $today) {
|
|
$user->save([
|
|
'ai_free_count' => self::DAILY_FREE_COUNT,
|
|
'ai_free_date' => $today,
|
|
]);
|
|
return self::DAILY_FREE_COUNT;
|
|
}
|
|
|
|
return max(0, (int) $user->getData('ai_free_count'));
|
|
}
|
|
|
|
public static function getRemainCount(int $userId): int
|
|
{
|
|
$user = User::findOrEmpty($userId);
|
|
if ($user->isEmpty()) {
|
|
return 0;
|
|
}
|
|
|
|
if (self::isVip($user)) {
|
|
return self::VIP_REMAIN_COUNT;
|
|
}
|
|
|
|
return self::resetDailyIfNeeded($user);
|
|
}
|
|
}
|