|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Bavix\Wallet\Models\Wallet; |
|
6
|
|
|
use Bavix\Wallet\Services\DbService; |
|
7
|
|
|
use Illuminate\Console\Command; |
|
8
|
|
|
use Illuminate\Database\PostgresConnection; |
|
9
|
|
|
use Illuminate\Database\Query\JoinClause; |
|
10
|
|
|
use Illuminate\Database\SQLiteConnection; |
|
11
|
|
|
use function config; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class RefreshBalance |
|
15
|
|
|
* @package Bavix\Wallet\Commands |
|
16
|
|
|
* @codeCoverageIgnore |
|
17
|
|
|
*/ |
|
18
|
|
|
class RefreshBalance extends Command |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The name and signature of the console command. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $signature = 'wallet:refresh'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The console command description. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $description = 'Recalculates all wallets'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return void |
|
37
|
|
|
* @throws |
|
38
|
|
|
*/ |
|
39
|
|
|
public function handle(): void |
|
40
|
|
|
{ |
|
41
|
|
|
app(DbService::class)->transaction(function () { |
|
42
|
|
|
$connection = app(DbService::class)->connection(); |
|
43
|
|
|
if ($connection instanceof SQLiteConnection || $connection instanceof PostgresConnection) { |
|
44
|
|
|
$wallet = config('wallet.wallet.table', 'wallets'); |
|
45
|
|
|
$connection->table($wallet)->update(['balance' => 0]); |
|
46
|
|
|
$this->singleUpdate(); |
|
47
|
|
|
} else { |
|
48
|
|
|
$this->multiUpdate(); |
|
49
|
|
|
} |
|
50
|
|
|
}); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* SQLite |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function singleUpdate(): void |
|
59
|
|
|
{ |
|
60
|
|
|
Wallet::query()->each(static function (Wallet $wallet) { |
|
61
|
|
|
$wallet->refreshBalance(); |
|
62
|
|
|
}); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* MySQL/PgSQL |
|
67
|
|
|
* |
|
68
|
|
|
* @return void |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function multiUpdate(): void |
|
71
|
|
|
{ |
|
72
|
|
|
$wallet = config('wallet.wallet.table', 'wallets'); |
|
73
|
|
|
$trans = config('wallet.transaction.table', 'transactions'); |
|
74
|
|
|
$availableBalance = app(DbService::class) |
|
75
|
|
|
->connection() |
|
76
|
|
|
->table($trans) |
|
77
|
|
|
->select('wallet_id', app(DbService::class)->raw('sum(amount) balance')) |
|
78
|
|
|
->where('confirmed', true) |
|
79
|
|
|
->groupBy('wallet_id'); |
|
80
|
|
|
|
|
81
|
|
|
$joinClause = static function (JoinClause $join) use ($wallet) { |
|
82
|
|
|
$join->on("$wallet.id", '=', 'b.wallet_id'); |
|
83
|
|
|
}; |
|
84
|
|
|
|
|
85
|
|
|
app(DbService::class) |
|
86
|
|
|
->connection() |
|
87
|
|
|
->table($wallet) |
|
88
|
|
|
->joinSub($availableBalance, 'b', $joinClause, null, null, 'left') |
|
89
|
|
|
->update(["$wallet.balance" => app(DbService::class)->raw('ifnull(b.balance, 0)')]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|