Files
jk8_admin/app/admin/model/UserScore.php

52 lines
1.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\admin\model;
use think\model;
use think\model\relation\BelongsTo;
use Throwable;
/**
* UserScoreLog 模型
* 1. 创建积分日志自动完成会员积分的添加
* 2. 创建积分日志时,请开启事务
*/
class UserScore extends model
{
protected $updateTime = false;
/**
* 更新前
* @throws Throwable
*/
public static function onBeforeUpdate($model): void
{
$before = $model->getOrigin('score');
$after = $model->score;
// 1. 只有当分数确实发生变化时才记录日志
if (bccomp($before, $after, 2) !== 0) {
// 计算分值变动after - before
$change = bcsub($after, $before);
if ($change >= 0) {
$type = 1;
} else {
$type = 2;
}
$userScoreLog = new UserScoreLog();
$userScoreLog->save([
'user_id' => $model->user_id,
'game_type' => $model->game_type,
'before' => $before,
'after' => $after,
'score' => $change,
'memo' => $model->memo ?? '',
'type' => $type,
'created_by'=> $model->created_by ?? null,
]);
}
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
}