diff --git a/.env.example b/.env.example index 1d5b959..0f6a077 100644 --- a/.env.example +++ b/.env.example @@ -97,3 +97,5 @@ MQTT_AUTO_RECONNECT_ENABLED=false EMQX_API_URL=http://localhost:18083/api/v5 EMQX_API_KEY= EMQX_API_SECRET_KEY= + +USER_GROUP_BIRTHDAY=1 diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 2560cc7..27894e3 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -10,7 +10,9 @@ use App\Jobs\HostCostJob; use App\Jobs\Module\FetchModuleJob; use App\Jobs\Module\PushWorkOrderJob; +use App\Jobs\RollbackUserTempGroupJob; use App\Jobs\SendModuleEarningsJob; +use App\Jobs\SetBirthdayGroupJob; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; @@ -23,7 +25,7 @@ class Kernel extends ConsoleKernel * * @return void */ - protected function schedule(Schedule $schedule) + protected function schedule(Schedule $schedule): void { // 清理过期的 Token $schedule->command('sanctum:prune-expired --hours=24')->daily(); @@ -54,6 +56,11 @@ protected function schedule(Schedule $schedule) // 发送模块收益 $schedule->job(new SendModuleEarningsJob())->dailyAt('20:00'); + // 回滚临时用户组 + $schedule->job(new RollbackUserTempGroupJob())->everyMinute()->onOneServer(); + + // 设置生日用户组 + $schedule->job(new SetBirthdayGroupJob())->dailyAt('00:00'); } /** diff --git a/app/Http/Controllers/Api/IndexController.php b/app/Http/Controllers/Api/IndexController.php index e364735..31d4a76 100644 --- a/app/Http/Controllers/Api/IndexController.php +++ b/app/Http/Controllers/Api/IndexController.php @@ -19,9 +19,7 @@ public function index(): JsonResponse public function birthdays(): JsonResponse { // 获取今天过生日的用户,每页显示 20 个,使用 carbon - $users = User::select(['id', 'name', 'birthday_at', 'email', 'created_at'])->whereMonth('birthday_at', now()->month) - ->whereDay('birthday_at', now()->day) - ->simplePaginate(20); + $users = User::birthday()->simplePaginate(20); return $this->success($users); } diff --git a/app/Jobs/RollbackUserTempGroupJob.php b/app/Jobs/RollbackUserTempGroupJob.php new file mode 100644 index 0000000..8075a6e --- /dev/null +++ b/app/Jobs/RollbackUserTempGroupJob.php @@ -0,0 +1,49 @@ + $temp_group) { + if (now()->gt($temp_group['expired_at'])) { + $user = User::find($user_id); + $user->user_group_id = $temp_group['user_group_id']; + $user->save(); + unset($temp_groups[$user_id]); + } + } + + Cache::forever('users_temp_groups', $temp_groups); + } +} diff --git a/app/Jobs/SetBirthdayGroupJob.php b/app/Jobs/SetBirthdayGroupJob.php new file mode 100644 index 0000000..c16fe7f --- /dev/null +++ b/app/Jobs/SetBirthdayGroupJob.php @@ -0,0 +1,48 @@ +chunk(100, function ($users) use ($birthday_group) { + foreach ($users as $user) { + $birthday_group->setTempGroup($user, $birthday_group, now()->addDay()); + } + }); + + + } +} diff --git a/app/Models/User.php b/app/Models/User.php index cb6cd55..0c62b4b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -3,8 +3,6 @@ namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; -use App\Exceptions\CommonException; -use App\Exceptions\User\BalanceNotEnoughException; use Database\Factories\UserFactory; use Eloquent; use GeneaLabs\LaravelModelCaching\CachedBuilder; @@ -158,61 +156,9 @@ public function user_group(): BelongsTo return $this->belongsTo(UserGroup::class); } - /** - * @throws CommonException - * @throws BalanceNotEnoughException - */ - // public function toDrops($amount = 1) - // { - // - // $cache_key = 'user_drops_' . $this->id; - // - // if ($amount === 0 || $amount === null) { - // return $this; - // } - // - // $rate = config('drops.rate'); - // - // - // $transactions = new Transaction(); - // - // $drops = $transactions->getDrops($this->id); - // - // $total = 0; - // - // if ($drops < 0) { - // $amount += abs($drops) / $rate; - // } - // - // $total += $amount * $rate; - // - // - // // amount 保留两位小数 - // $amount = round($amount, 2); - // - // $lock = Cache::lock("lock_" . $cache_key, 5); - // try { - // $lock->block(5); - // - // $this->balance -= $amount; - // $this->save(); - // - // $transactions->increaseDrops($this->id, $total); - // - // // $transactions - // - // $transactions->addPayoutBalance($this->id, $amount, '自动转换为 Drops'); - // - // // if user balance <= 0 - // if ($this->balance < $amount) { - // throw new BalanceNotEnoughException('余额不足'); - // } - // } catch (LockTimeoutException) { - // throw new CommonException('暂时无法处理此请求,请稍后再试。'); - // } finally { - // optional($lock)->release(); - // } - // - // return $this; - // } + public function scopeBirthday() + { + return $this->select(['id', 'name', 'birthday_at', 'email', 'created_at'])->whereMonth('birthday_at', now()->month) + ->whereDay('birthday_at', now()->day); + } } diff --git a/app/Models/UserGroup.php b/app/Models/UserGroup.php index 6e91c8b..dbda1d9 100644 --- a/app/Models/UserGroup.php +++ b/app/Models/UserGroup.php @@ -2,14 +2,14 @@ namespace App\Models; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Support\Facades\Cache; class UserGroup extends Model { - use HasFactory; - public $fillable = [ 'name', 'color', @@ -27,4 +27,38 @@ public function users(): HasMany return $this->hasMany(User::class); } + + /** + * 设置临时用户组 + * + * @param User $user + * @param UserGroup $group + * @param Carbon $expired_at + * + * @return User + */ + public function setTempGroup(User $user, self $group, Carbon $expired_at): User + { + $temp_groups = Cache::get('users_temp_groups', []); + + // 检测是否存在,存在则更新(更新过期时间,但是不更新 user_group_id) + if (isset($temp_groups[$user->id])) { + $temp_groups[$user->id]['expired_at'] = $expired_at; + } else { + $temp_groups[$user->id] = [ + 'user_group_id' => $user->user_group_id, + 'expired_at' => $expired_at, + ]; + } + + // 保存到缓存 + Cache::forever('users_temp_groups', $temp_groups); + + // 设置新的用户组 + $user->user_group_id = $group->id; + $user->save(); + + return $user; + } + } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index fcbea00..5261f00 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -4,6 +4,7 @@ use App\Models\PersonalAccessToken; use Illuminate\Pagination\Paginator; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Http; use Illuminate\Support\ServiceProvider; use Laravel\Sanctum\Sanctum; @@ -25,7 +26,7 @@ public function register() * * @return void */ - public function boot() + public function boot(): void { // @@ -43,5 +44,11 @@ public function boot() 'version' => 2, ])->baseUrl($url); }); + + + // Carbon setTestNow + // Carbon::setTestNow(now()->addDays(1)); + + } } diff --git a/config/settings.php b/config/settings.php index 6682415..7cc78e6 100644 --- a/config/settings.php +++ b/config/settings.php @@ -7,5 +7,8 @@ 'billing' => env('WECOM_ROBOT_HOOK_BILLING', ''), 'cluster_ready' => env('WECOM_ROBOT_HOOK_CLUSTER_READY', ''), ], - ] + ], + 'user_groups' => [ + 'birthday_group_id' => env('USER_GROUP_BIRTHDAY', 1), + ], ];