diff --git a/app/Http/Controllers/Remote/ModuleController.php b/app/Http/Controllers/Remote/ModuleController.php index 8d1023c..d35ac5e 100644 --- a/app/Http/Controllers/Remote/ModuleController.php +++ b/app/Http/Controllers/Remote/ModuleController.php @@ -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'), diff --git a/app/Models/Host.php b/app/Models/Host.php index ae148e5..2642e63 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -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)); diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 82cb876..af66427 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -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);