改进 计算

This commit is contained in:
iVampireSP.com 2022-10-09 15:01:21 +08:00
parent 6d75d872ce
commit 34fcad9f33
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
3 changed files with 48 additions and 7 deletions

View File

@ -100,7 +100,7 @@ public function calcModule(Module $module)
$endOfMonth = now()->endOfMonth();
$this_month_balance_and_drops = Cache::remember($module->id . '_this_month_balance_and_drops', 3600, function () use ($module, $beginOfMonth, $endOfMonth) {
$this_month = Transaction::where('module_id', $module->id)->whereBetween('created_at', [$beginOfMonth, $endOfMonth]);
$this_month = Transaction::where('module_id', $module->id)->where('type', 'payout')->whereBetween('created_at', [$beginOfMonth, $endOfMonth]);
// this month transactions
return [
@ -111,7 +111,7 @@ public function calcModule(Module $module)
$last_month_balance_and_drops = Cache::remember($module->id . '_last_month_balance_and_drops', 3600, function () use ($module, $beginOfMonth, $endOfMonth) {
// last month transactions
$last_moth = Transaction::where('module_id', $module->id)->whereBetween('created_at', [$beginOfMonth, $endOfMonth]);
$last_moth = Transaction::where('module_id', $module->id)->where('type', 'payout')->whereBetween('created_at', [$beginOfMonth, $endOfMonth]);
return [
'balance' => $last_moth->sum('outcome'),

View File

@ -171,7 +171,7 @@ public function costBalance($amount = 1)
Cache::put($month_cache_key, $hosts_drops, 604800);
$left = $transaction->reduceAmount($this->user_id, $amount);
$left = $transaction->reduceHostAmount($this->user_id, $this->id, $this->module_id, $amount);
broadcast(new UserEvent($this->user_id, 'balances.amount.reduced', $this->user));

View File

@ -142,7 +142,7 @@ public function addPayoutDrops($user_id, $amount, $description, $host_id, $modul
'income' => 0,
'income_drops' => 0,
'outcome' => 0,
'outcome_drops' => $amount,
'outcome_drops' => (float) $amount,
'host_id' => $host_id,
'module_id' => $module_id,
];
@ -167,7 +167,7 @@ public function addIncomeDrops($user_id, $amount, $description)
'payment' => 'balance',
'description' => $description,
'income' => 0,
'income_drops' => $amount,
'income_drops' => (float) $amount,
'outcome' => 0,
'outcome_drops' => 0,
];
@ -181,7 +181,7 @@ public function addIncomeBalance($user_id, $payment, $amount, $description)
'type' => 'income',
'payment' => $payment,
'description' => $description,
'income' => $amount,
'income' => (float) $amount,
'income_drops' => 0,
'outcome' => 0,
'outcome_drops' => 0,
@ -198,13 +198,30 @@ public function addPayoutBalance($user_id, $amount, $description)
'description' => $description,
'income' => 0,
'income_drops' => 0,
'outcome' => $amount,
'outcome' => (float) $amount,
'outcome_drops' => 0
];
return $this->addLog($user_id, $data);
}
public function addHostPayoutBalance($user_id, $host_id, $module_id, $amount, $description)
{
$data = [
'type' => 'payout',
'payment' => 'balance',
'description' => $description,
'income' => 0,
'income_drops' => 0,
'outcome' => (float) $amount,
'outcome_drops' => 0,
'host_id' => $host_id,
'module_id' => $module_id,
];
return $this->addLog($user_id, $data);
}
public function reduceAmount($user_id, $amount = 0, $description = '扣除费用请求。')
@ -230,6 +247,30 @@ public function reduceAmount($user_id, $amount = 0, $description = '扣除费用
return false;
}
public function reduceHostAmount($user_id, $host_id, $module_id, $amount = 0, $description = '扣除费用请求。')
{
$lock = Cache::lock("user_balance_lock_" . $user_id, 10);
try {
$lock->block(5);
$user = User::findOrFail($user_id);
$user->balance -= $amount;
$user->save();
$this->addHostPayoutBalance($user_id, $host_id, $module_id, $amount, $description);
return $user->balance;
} finally {
optional($lock)->release();
}
return false;
}
public function addAmount($user_id, $payment = 'console', $amount = 0, $description = null)
{
$lock = Cache::lock("user_balance_lock_" . $user_id, 10);