改进 用户生日时自动设置用户组
This commit is contained in:
parent
fae674b8f2
commit
c2bbe7ab0f
@ -97,3 +97,5 @@ MQTT_AUTO_RECONNECT_ENABLED=false
|
|||||||
EMQX_API_URL=http://localhost:18083/api/v5
|
EMQX_API_URL=http://localhost:18083/api/v5
|
||||||
EMQX_API_KEY=
|
EMQX_API_KEY=
|
||||||
EMQX_API_SECRET_KEY=
|
EMQX_API_SECRET_KEY=
|
||||||
|
|
||||||
|
USER_GROUP_BIRTHDAY=1
|
||||||
|
@ -10,7 +10,9 @@
|
|||||||
use App\Jobs\HostCostJob;
|
use App\Jobs\HostCostJob;
|
||||||
use App\Jobs\Module\FetchModuleJob;
|
use App\Jobs\Module\FetchModuleJob;
|
||||||
use App\Jobs\Module\PushWorkOrderJob;
|
use App\Jobs\Module\PushWorkOrderJob;
|
||||||
|
use App\Jobs\RollbackUserTempGroupJob;
|
||||||
use App\Jobs\SendModuleEarningsJob;
|
use App\Jobs\SendModuleEarningsJob;
|
||||||
|
use App\Jobs\SetBirthdayGroupJob;
|
||||||
use Illuminate\Console\Scheduling\Schedule;
|
use Illuminate\Console\Scheduling\Schedule;
|
||||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||||
|
|
||||||
@ -23,7 +25,7 @@ class Kernel extends ConsoleKernel
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function schedule(Schedule $schedule)
|
protected function schedule(Schedule $schedule): void
|
||||||
{
|
{
|
||||||
// 清理过期的 Token
|
// 清理过期的 Token
|
||||||
$schedule->command('sanctum:prune-expired --hours=24')->daily();
|
$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 SendModuleEarningsJob())->dailyAt('20:00');
|
||||||
|
|
||||||
|
// 回滚临时用户组
|
||||||
|
$schedule->job(new RollbackUserTempGroupJob())->everyMinute()->onOneServer();
|
||||||
|
|
||||||
|
// 设置生日用户组
|
||||||
|
$schedule->job(new SetBirthdayGroupJob())->dailyAt('00:00');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,9 +19,7 @@ public function index(): JsonResponse
|
|||||||
public function birthdays(): JsonResponse
|
public function birthdays(): JsonResponse
|
||||||
{
|
{
|
||||||
// 获取今天过生日的用户,每页显示 20 个,使用 carbon
|
// 获取今天过生日的用户,每页显示 20 个,使用 carbon
|
||||||
$users = User::select(['id', 'name', 'birthday_at', 'email', 'created_at'])->whereMonth('birthday_at', now()->month)
|
$users = User::birthday()->simplePaginate(20);
|
||||||
->whereDay('birthday_at', now()->day)
|
|
||||||
->simplePaginate(20);
|
|
||||||
|
|
||||||
return $this->success($users);
|
return $this->success($users);
|
||||||
}
|
}
|
||||||
|
49
app/Jobs/RollbackUserTempGroupJob.php
Normal file
49
app/Jobs/RollbackUserTempGroupJob.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
|
class RollbackUserTempGroupJob implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
//
|
||||||
|
|
||||||
|
$temp_groups = Cache::get('users_temp_groups', []);
|
||||||
|
|
||||||
|
foreach ($temp_groups as $user_id => $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);
|
||||||
|
}
|
||||||
|
}
|
48
app/Jobs/SetBirthdayGroupJob.php
Normal file
48
app/Jobs/SetBirthdayGroupJob.php
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Models\UserGroup;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
|
||||||
|
class SetBirthdayGroupJob implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
$birthday_group = UserGroup::find(config('settings.user_groups.birthday_group_id'));
|
||||||
|
|
||||||
|
if (!$birthday_group) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
User::birthday()->chunk(100, function ($users) use ($birthday_group) {
|
||||||
|
foreach ($users as $user) {
|
||||||
|
$birthday_group->setTempGroup($user, $birthday_group, now()->addDay());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -3,8 +3,6 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||||
use App\Exceptions\CommonException;
|
|
||||||
use App\Exceptions\User\BalanceNotEnoughException;
|
|
||||||
use Database\Factories\UserFactory;
|
use Database\Factories\UserFactory;
|
||||||
use Eloquent;
|
use Eloquent;
|
||||||
use GeneaLabs\LaravelModelCaching\CachedBuilder;
|
use GeneaLabs\LaravelModelCaching\CachedBuilder;
|
||||||
@ -158,61 +156,9 @@ public function user_group(): BelongsTo
|
|||||||
return $this->belongsTo(UserGroup::class);
|
return $this->belongsTo(UserGroup::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function scopeBirthday()
|
||||||
* @throws CommonException
|
{
|
||||||
* @throws BalanceNotEnoughException
|
return $this->select(['id', 'name', 'birthday_at', 'email', 'created_at'])->whereMonth('birthday_at', now()->month)
|
||||||
*/
|
->whereDay('birthday_at', now()->day);
|
||||||
// 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;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,14 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
class UserGroup extends Model
|
class UserGroup extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
|
||||||
|
|
||||||
public $fillable = [
|
public $fillable = [
|
||||||
'name',
|
'name',
|
||||||
'color',
|
'color',
|
||||||
@ -27,4 +27,38 @@ public function users(): HasMany
|
|||||||
return $this->hasMany(User::class);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
use App\Models\PersonalAccessToken;
|
use App\Models\PersonalAccessToken;
|
||||||
use Illuminate\Pagination\Paginator;
|
use Illuminate\Pagination\Paginator;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Laravel\Sanctum\Sanctum;
|
use Laravel\Sanctum\Sanctum;
|
||||||
@ -25,7 +26,7 @@ public function register()
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function boot()
|
public function boot(): void
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
|
|
||||||
@ -43,5 +44,11 @@ public function boot()
|
|||||||
'version' => 2,
|
'version' => 2,
|
||||||
])->baseUrl($url);
|
])->baseUrl($url);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// Carbon setTestNow
|
||||||
|
// Carbon::setTestNow(now()->addDays(1));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,5 +7,8 @@
|
|||||||
'billing' => env('WECOM_ROBOT_HOOK_BILLING', ''),
|
'billing' => env('WECOM_ROBOT_HOOK_BILLING', ''),
|
||||||
'cluster_ready' => env('WECOM_ROBOT_HOOK_CLUSTER_READY', ''),
|
'cluster_ready' => env('WECOM_ROBOT_HOOK_CLUSTER_READY', ''),
|
||||||
],
|
],
|
||||||
]
|
],
|
||||||
|
'user_groups' => [
|
||||||
|
'birthday_group_id' => env('USER_GROUP_BIRTHDAY', 1),
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
Loading…
Reference in New Issue
Block a user