2022-09-14 14:19:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Jenssegers\Mongodb\Eloquent\Model;
|
2022-09-15 04:13:37 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2022-09-14 14:19:28 +00:00
|
|
|
|
|
|
|
class Transaction extends Model
|
|
|
|
{
|
|
|
|
// $t = (new App\Models\Transaction)->create(['name' => 1])
|
|
|
|
|
|
|
|
protected $connection = 'mongodb';
|
|
|
|
protected $collection = 'transactions';
|
|
|
|
|
2022-09-15 04:13:37 +00:00
|
|
|
// 停用 updated_at
|
|
|
|
const UPDATED_AT = null;
|
|
|
|
|
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
|
|
|
];
|
|
|
|
|
|
|
|
protected $fillable = [
|
2022-09-15 04:13:37 +00:00
|
|
|
// 交易类型
|
|
|
|
'type',
|
|
|
|
|
|
|
|
// 交易渠道
|
|
|
|
'payment',
|
|
|
|
|
|
|
|
// 描述
|
|
|
|
'description',
|
|
|
|
|
|
|
|
// 入账
|
|
|
|
'income',
|
|
|
|
|
|
|
|
// 入账 Drops
|
|
|
|
'income_drops',
|
|
|
|
|
|
|
|
// 出账
|
|
|
|
'outcome',
|
|
|
|
// 出账 Drops
|
|
|
|
'outcome_drops',
|
|
|
|
|
|
|
|
// 可用余额
|
|
|
|
'balance',
|
|
|
|
|
|
|
|
// 可用 Drops
|
|
|
|
'drops',
|
|
|
|
|
|
|
|
// 赠送金额
|
|
|
|
'gift',
|
|
|
|
|
|
|
|
// 赠送 Drops
|
|
|
|
'gift_drops',
|
|
|
|
|
|
|
|
'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
|
|
|
|
|
|
|
|
|
|
|
// scope this user
|
|
|
|
public function scopeThisUser($query)
|
|
|
|
{
|
|
|
|
return $query->where('user_id', auth()->id());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getDrops($user_id = null)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
if (!$user_id) {
|
|
|
|
$user_id = auth()->id();
|
|
|
|
}
|
|
|
|
|
|
|
|
$cache_key = 'user_drops_' . $user_id;
|
|
|
|
|
|
|
|
$decimal = config('drops.decimal');
|
|
|
|
|
|
|
|
$drops = Cache::get($cache_key);
|
|
|
|
|
2022-09-15 05:09:15 +00:00
|
|
|
$drops = $drops / $decimal;
|
2022-09-15 04:13:37 +00:00
|
|
|
|
|
|
|
return $drops;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function increaseCurrentUserDrops($amount = 0)
|
|
|
|
{
|
|
|
|
return $this->increaseDrops(auth()->id(), $amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function increaseDrops($user_id, $amount = 0)
|
|
|
|
{
|
|
|
|
$cache_key = 'user_drops_' . $user_id;
|
|
|
|
|
|
|
|
$decimal = config('drops.decimal');
|
|
|
|
|
|
|
|
|
2022-09-15 05:09:15 +00:00
|
|
|
$amount = $amount * $decimal;
|
2022-09-15 04:13:37 +00:00
|
|
|
|
|
|
|
$drops = Cache::increment($cache_key, $amount);
|
|
|
|
|
|
|
|
return $drops;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-15 05:38:50 +00:00
|
|
|
public function reduceDrops($user_id, $host_id, $module_id, $auto = 1, $amount = 0)
|
2022-09-15 04:13:37 +00:00
|
|
|
{
|
2022-09-15 05:38:50 +00:00
|
|
|
|
2022-09-15 04:13:37 +00:00
|
|
|
$cache_key = 'user_drops_' . $user_id;
|
|
|
|
|
|
|
|
$decimal = config('drops.decimal');
|
|
|
|
|
2022-09-15 05:38:50 +00:00
|
|
|
Cache::decrement($cache_key, $amount);
|
2022-09-15 04:13:37 +00:00
|
|
|
|
2022-09-15 05:38:50 +00:00
|
|
|
if ($auto) {
|
|
|
|
$description = '平台按时间自动扣费。';
|
|
|
|
} else {
|
|
|
|
$description = '集成模块发起的扣费。';
|
|
|
|
}
|
2022-09-15 04:13:37 +00:00
|
|
|
|
2022-09-15 05:38:50 +00:00
|
|
|
$this->addPayoutDrops($user_id, $amount / $decimal, $description, $host_id, $module_id);
|
2022-09-15 04:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-15 05:38:50 +00:00
|
|
|
public function addPayoutDrops($user_id, $amount, $description, $host_id, $module_id)
|
2022-09-15 04:13:37 +00:00
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'type' => 'payout',
|
|
|
|
'payment' => 'drops',
|
|
|
|
'description' => $description,
|
|
|
|
'income' => 0,
|
|
|
|
'income_drops' => 0,
|
|
|
|
'outcome' => 0,
|
|
|
|
'outcome_drops' => $amount,
|
2022-09-15 05:38:50 +00:00
|
|
|
'host_id' => $host_id,
|
|
|
|
'module_id' => $module_id,
|
2022-09-15 04:13:37 +00:00
|
|
|
];
|
|
|
|
|
2022-10-03 02:38:25 +00:00
|
|
|
$month = now()->month;
|
|
|
|
|
|
|
|
Cache::increment('user_' . $user_id . '_month_' . $month . '_drops', $amount);
|
|
|
|
|
2022-09-15 04:13:37 +00:00
|
|
|
return $this->addLog($user_id, $data);
|
|
|
|
}
|
|
|
|
|
2022-09-15 05:38:50 +00:00
|
|
|
|
2022-09-15 04:13:37 +00:00
|
|
|
public function addIncomeDrops($user_id, $amount, $description)
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'type' => 'income',
|
|
|
|
'payment' => 'balance',
|
|
|
|
'description' => $description,
|
|
|
|
'income' => 0,
|
|
|
|
'income_drops' => $amount,
|
|
|
|
'outcome' => 0,
|
|
|
|
'outcome_drops' => 0,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->addLog($user_id, $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addIncomeBalance($user_id, $payment, $amount, $description)
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'type' => 'income',
|
|
|
|
'payment' => $payment,
|
|
|
|
'description' => $description,
|
|
|
|
'income' => $amount,
|
|
|
|
'income_drops' => 0,
|
|
|
|
'outcome' => 0,
|
|
|
|
'outcome_drops' => 0,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->addLog($user_id, $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addPayoutBalance($user_id, $amount, $description)
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'type' => 'payout',
|
|
|
|
'payment' => 'balance',
|
|
|
|
'description' => $description,
|
|
|
|
'income' => 0,
|
|
|
|
'income_drops' => 0,
|
|
|
|
'outcome' => $amount,
|
2022-09-15 05:38:50 +00:00
|
|
|
'outcome_drops' => 0
|
2022-09-15 04:13:37 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
return $this->addLog($user_id, $data);
|
|
|
|
}
|
|
|
|
|
2022-10-02 13:26:59 +00:00
|
|
|
public function reduceAmount($user_id, $amount = 0, $description = '扣除费用请求。')
|
|
|
|
{
|
|
|
|
$user = User::findOrFail($user_id);
|
|
|
|
|
|
|
|
if ($user) {
|
|
|
|
$user->balance -= $amount;
|
|
|
|
|
|
|
|
$user->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'type' => 'payout',
|
|
|
|
'payment' => 'balance',
|
|
|
|
'description' => $description,
|
|
|
|
'income' => 0,
|
|
|
|
'income_drops' => 0,
|
|
|
|
'outcome' => $amount,
|
|
|
|
'outcome_drops' => 0
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->addLog($user_id, $data);
|
|
|
|
}
|
|
|
|
|
2022-09-15 04:13:37 +00:00
|
|
|
|
|
|
|
private function addLog($user_id, $data)
|
|
|
|
{
|
|
|
|
$user = User::find($user_id);
|
|
|
|
|
|
|
|
$current = [
|
|
|
|
'balance' => $user->balance,
|
|
|
|
'drops' => $this->getDrops($user_id),
|
2022-10-02 13:26:59 +00:00
|
|
|
'user_id' => intval($user_id),
|
2022-09-15 04:13:37 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// merge
|
|
|
|
$data = array_merge($data, $current);
|
|
|
|
|
|
|
|
// add expired at
|
|
|
|
$data['expired_at'] = now()->addSeconds(7);
|
|
|
|
|
|
|
|
|
|
|
|
return $this->create($data);
|
|
|
|
}
|
2022-09-14 14:19:28 +00:00
|
|
|
}
|