From 24647a7af76d04e7974f72c76ae7b6136f6a7471 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Thu, 15 Sep 2022 13:38:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/Host.php | 4 +-- app/Models/Transaction.php | 31 ++++++++++--------- ...09_14_215159_create_transactions_table.php | 4 +++ 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/app/Models/Host.php b/app/Models/Host.php index bc3ba29..7f286b0 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -137,9 +137,7 @@ public function cost($price = null) Cache::put($month_cache_key, $hosts_drops, 604800); - $description = '主机服务 ' . $this->name . ' 扣费。'; - - $transaction->reduceDrops($this->user_id, $this->price, $description); + $transaction->reduceDrops($this->user_id, $this->id, $this->module_id, 1, $this->price); return true; } diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 5d2b950..781bd7c 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -55,6 +55,8 @@ class Transaction extends Model 'gift_drops', 'user_id', + 'host_id', + 'module_id', ]; @@ -83,12 +85,6 @@ public function getDrops($user_id = null) return $drops; } - - public function reduceCurrentUserDrops($amount = 0) - { - return $this->reduceDrops(auth()->id(), $amount); - } - public function increaseCurrentUserDrops($amount = 0) { return $this->increaseDrops(auth()->id(), $amount); @@ -110,30 +106,32 @@ public function increaseDrops($user_id, $amount = 0) } - public function reduceDrops($user_id, $amount = 0, $description = null) + public function reduceDrops($user_id, $host_id, $module_id, $auto = 1, $amount = 0) { + $cache_key = 'user_drops_' . $user_id; $decimal = config('drops.decimal'); - $month = now()->month; Cache::increment('user_' . $user_id . '_month_' . $month . '_drops', $amount); $amount = $amount * $decimal; - $drops = Cache::decrement($cache_key, $amount); + Cache::decrement($cache_key, $amount); - // (new App\Models\Transaction)->create(['name' => 1]); + if ($auto) { + $description = '平台按时间自动扣费。'; + } else { + $description = '集成模块发起的扣费。'; + } - $this->addPayoutDrops($user_id, $amount / $decimal, $description); - - return $drops; + $this->addPayoutDrops($user_id, $amount / $decimal, $description, $host_id, $module_id); } - public function addPayoutDrops($user_id, $amount, $description) + public function addPayoutDrops($user_id, $amount, $description, $host_id, $module_id) { $data = [ 'type' => 'payout', @@ -143,11 +141,14 @@ public function addPayoutDrops($user_id, $amount, $description) 'income_drops' => 0, 'outcome' => 0, 'outcome_drops' => $amount, + 'host_id' => $host_id, + 'module_id' => $module_id, ]; return $this->addLog($user_id, $data); } + public function addIncomeDrops($user_id, $amount, $description) { $data = [ @@ -187,7 +188,7 @@ public function addPayoutBalance($user_id, $amount, $description) 'income' => 0, 'income_drops' => 0, 'outcome' => $amount, - 'outcome_drops' => 0, + 'outcome_drops' => 0 ]; return $this->addLog($user_id, $data); diff --git a/database/migrations/2022_09_14_215159_create_transactions_table.php b/database/migrations/2022_09_14_215159_create_transactions_table.php index f4618dd..598865f 100644 --- a/database/migrations/2022_09_14_215159_create_transactions_table.php +++ b/database/migrations/2022_09_14_215159_create_transactions_table.php @@ -20,6 +20,10 @@ public function up() 'type' )->index(); $collection->unsignedBigInteger('payment')->index(); + $collection->unsignedBigInteger( + 'module_id' + )->index()->nullable(); + $collection->unsignedBigInteger('host_id')->index()->nullable(); // a year $year = 365 * 24 * 60 * 60;