Lae/app/Models/Transaction.php

87 lines
1.6 KiB
PHP
Raw Normal View History

2022-09-14 14:19:28 +00:00
<?php
namespace App\Models;
2022-11-18 09:16:30 +00:00
use Carbon\Carbon;
2022-11-06 11:28:22 +00:00
use Jenssegers\Mongodb\Eloquent\Model;
2022-09-14 14:19:28 +00:00
class Transaction extends Model
{
const UPDATED_AT = null;
2023-01-30 16:14:07 +00:00
2022-09-14 14:19:28 +00:00
protected $connection = 'mongodb';
2022-09-15 04:13:37 +00:00
// 停用 updated_at
protected $collection = 'transactions';
2023-01-30 16:14:07 +00:00
2022-09-14 14:19:28 +00:00
protected $dates = [
'created_at',
'updated_at',
2022-09-15 04:13:37 +00:00
'paid_at',
2022-09-14 14:19:28 +00:00
];
2022-11-30 12:26:35 +00:00
protected $casts = [
'balance' => 'decimal:2',
];
2022-09-14 14:19:28 +00:00
protected $fillable = [
2023-01-16 20:36:43 +00:00
// 类型
2022-09-15 04:13:37 +00:00
'type',
// 交易渠道
'payment',
// 描述
'description',
2023-01-16 20:36:43 +00:00
// 交易金额,负数则是扣除
'amount',
2022-09-15 04:13:37 +00:00
2023-01-16 20:36:43 +00:00
// 剩余余额
'user_remain',
'module_remain',
2022-09-15 04:13:37 +00:00
// 赠送金额
'gift',
'user_id',
2022-09-15 05:38:50 +00:00
'host_id',
'module_id',
2022-09-14 14:19:28 +00:00
];
2022-09-15 04:13:37 +00:00
2023-01-16 20:36:43 +00:00
protected static function boot()
{
2023-01-16 20:36:43 +00:00
parent::boot();
2023-01-16 20:36:43 +00:00
static::creating(function (self $transaction) {
$user = null;
$module = null;
2022-12-27 16:25:22 +00:00
2023-01-16 20:36:43 +00:00
if ($transaction->user_id) {
$user = (new User)->find($transaction->user_id);
2022-10-29 03:44:52 +00:00
}
2023-01-16 20:36:43 +00:00
if ($transaction->module_id) {
$module = (new Module)->find($transaction->module_id);
2022-10-07 05:15:08 +00:00
}
2023-01-16 20:36:43 +00:00
if ($user) {
$transaction->user_remain = $user->balance;
2022-11-18 08:15:30 +00:00
}
2023-01-16 20:36:43 +00:00
if ($module) {
$transaction->module_remain = $module->balance;
2022-11-16 13:02:20 +00:00
}
2023-01-16 20:36:43 +00:00
$transaction->expired_at = Carbon::now()->addSeconds(7)->toString();
});
}
2023-01-16 20:42:59 +00:00
// on create
public function scopeThisUser($query)
{
return $query->where('user_id', auth()->id());
}
2022-09-14 14:19:28 +00:00
}