项目初始化
This commit is contained in:
39
app/admin/model/UserScoreLog.php
Normal file
39
app/admin/model/UserScoreLog.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use support\think\Model;
|
||||
use think\model\relation\BelongsTo;
|
||||
|
||||
class UserScoreLog extends Model
|
||||
{
|
||||
protected string $table = 'user_score_log';
|
||||
protected string $pk = 'id';
|
||||
protected bool $autoWriteTimestamp = true;
|
||||
protected bool $updateTime = false;
|
||||
|
||||
public static function onBeforeInsert($model): void
|
||||
{
|
||||
$user = User::where('id', $model->user_id)->lock(true)->find();
|
||||
if (!$user) {
|
||||
throw new \Exception(__("The user can't find it"));
|
||||
}
|
||||
if (!$model->memo) {
|
||||
throw new \Exception(__("Change note cannot be blank"));
|
||||
}
|
||||
$model->before = $user->score;
|
||||
$user->score += $model->score;
|
||||
$user->save();
|
||||
$model->after = $user->score;
|
||||
}
|
||||
|
||||
public static function onBeforeDelete(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user