124 lines
4.7 KiB
PHP
124 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Customer;
|
|
use App\Services\PargarInboundWalletService;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Rooyesh\Wallet\interfaces\WalletInterface;
|
|
use Rooyesh\Wallet\models\Transaction;
|
|
|
|
class PargarWalletFreezeCommand extends Command
|
|
{
|
|
protected $signature = 'pargar:wallet-freeze
|
|
{action=on : on|off|status}
|
|
{--customer= : شناسه مشتری برای گزارش تراکنشهای type=22}
|
|
{--set-cash= : تنظیم مستقیم cash کیف اصلی}
|
|
{--set-gold= : تنظیم مستقیم gold کیف اصلی}';
|
|
|
|
protected $description = 'یخزدن اضطراری اعمال کیف از پرگار + اصلاح دستی موجودی';
|
|
|
|
public function handle(): int
|
|
{
|
|
$action = strtolower((string) $this->argument('action'));
|
|
|
|
if ($action === 'on') {
|
|
PargarInboundWalletService::freezeApplies(true);
|
|
$this->warn('اعمال کیف از پرگار یخزده شد (KILL SWITCH ON)');
|
|
} elseif ($action === 'off') {
|
|
PargarInboundWalletService::freezeApplies(false);
|
|
$this->info('اعمال کیف از پرگار آزاد شد (KILL SWITCH OFF)');
|
|
} elseif ($action === 'status') {
|
|
$frozen = PargarInboundWalletService::isApplyFrozen();
|
|
$this->line($frozen ? 'وضعیت: یخزده (ON)' : 'وضعیت: آزاد (OFF)');
|
|
} else {
|
|
$this->error('action باید on|off|status باشد');
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$customerId = (int) ($this->option('customer') ?: 0);
|
|
if ($customerId > 0) {
|
|
$this->reportCustomer($customerId);
|
|
$this->maybeSetBalances($customerId);
|
|
}
|
|
|
|
$this->newLine();
|
|
$this->warn('اگر هنوز موجودی بالا میرود: php-fpm / octane / queue را حتماً ریاستارت کنید.');
|
|
$this->line('درخواستهای قدیمیِ قبل از دیپلوی با کد قبلی هنوز ممکن است در حال اجرا باشند.');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
private function reportCustomer(int $customerId): void
|
|
{
|
|
$customer = Customer::find($customerId);
|
|
if (! $customer) {
|
|
$this->error("مشتری {$customerId} یافت نشد");
|
|
|
|
return;
|
|
}
|
|
|
|
$wallet = (new WalletInterface($customerId, (int) ($customer->type ?? 2)))->wallet;
|
|
$this->info("مشتری {$customerId} — {$customer->name}");
|
|
$this->line(' cash='.((float) ($wallet->cash ?? 0)));
|
|
$this->line(' gold='.((float) ($wallet->gold ?? 0)));
|
|
|
|
$recent = Transaction::query()
|
|
->where('customer_id', $customerId)
|
|
->where('type', PargarInboundWalletService::TX_TYPE_PARGAR)
|
|
->orderByDesc('id')
|
|
->limit(15)
|
|
->get(['id', 'cash', 'gold', 'title', 'note', 'created_at']);
|
|
|
|
$this->line('آخرین تراکنشهای type=22 (پرگار):');
|
|
foreach ($recent as $row) {
|
|
$this->line(sprintf(
|
|
' #%d cash=%s gold=%s at=%s title=%s',
|
|
$row->id,
|
|
$row->cash,
|
|
$row->gold,
|
|
$row->created_at,
|
|
$row->title
|
|
));
|
|
}
|
|
|
|
$countLastMinute = Transaction::query()
|
|
->where('customer_id', $customerId)
|
|
->where('type', PargarInboundWalletService::TX_TYPE_PARGAR)
|
|
->where('created_at', '>=', now()->subMinute())
|
|
->count();
|
|
$this->line("تراکنش type=22 در ۱ دقیقهٔ اخیر: {$countLastMinute}");
|
|
}
|
|
|
|
private function maybeSetBalances(int $customerId): void
|
|
{
|
|
$setCash = $this->option('set-cash');
|
|
$setGold = $this->option('set-gold');
|
|
if ($setCash === null && $setGold === null) {
|
|
return;
|
|
}
|
|
|
|
if (! $this->confirm("موجودی مشتری {$customerId} مستقیم در DB عوض شود؟", false)) {
|
|
return;
|
|
}
|
|
|
|
$customer = Customer::findOrFail($customerId);
|
|
$wallet = (new WalletInterface($customerId, (int) ($customer->type ?? 2)))->wallet;
|
|
if (! $wallet) {
|
|
$this->error('کیف یافت نشد');
|
|
|
|
return;
|
|
}
|
|
|
|
DB::table($wallet->getTable())->where('id', $wallet->id)->update(array_filter([
|
|
'cash' => $setCash !== null ? (float) $setCash : null,
|
|
'gold' => $setGold !== null ? (float) $setGold : null,
|
|
'updated_at' => now(),
|
|
], fn ($v) => $v !== null));
|
|
|
|
$this->info('موجودی بهروز شد. صفحه را رفرش کنید.');
|
|
}
|
|
}
|