70 lines
1.6 KiB
PHP
70 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace app\admin\model;
|
|
|
|
use support\think\Model;
|
|
use think\model\relation\BelongsTo;
|
|
|
|
class UserMoneyLog extends Model
|
|
{
|
|
protected string $table = 'user_money_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->money;
|
|
$user->money += $model->money;
|
|
$user->save();
|
|
$model->after = $user->money;
|
|
}
|
|
|
|
public static function onBeforeDelete(): bool
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public function getMoneyAttr($value): string
|
|
{
|
|
return bcdiv((string) $value, '100', 2);
|
|
}
|
|
|
|
public function setMoneyAttr($value): string
|
|
{
|
|
return bcmul((string) $value, '100', 2);
|
|
}
|
|
|
|
public function getBeforeAttr($value): string
|
|
{
|
|
return bcdiv((string) $value, '100', 2);
|
|
}
|
|
|
|
public function setBeforeAttr($value): string
|
|
{
|
|
return bcmul((string) $value, '100', 2);
|
|
}
|
|
|
|
public function getAfterAttr($value): string
|
|
{
|
|
return bcdiv((string) $value, '100', 2);
|
|
}
|
|
|
|
public function setAfterAttr($value): string
|
|
{
|
|
return bcmul((string) $value, '100', 2);
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
}
|