mirror of
https://github.com/henry4682/finance_app.git
synced 2026-07-16 00:10:00 +00:00
41 lines
1.4 KiB
PHP
41 lines
1.4 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use App\Jobs\UpdateAssetPriceJob;
|
||
use App\Models\InvestmentHolding;
|
||
use App\Models\Security;
|
||
use Illuminate\Console\Command;
|
||
|
||
class RefreshAllAssetPrices extends Command
|
||
{
|
||
// 執行此指令的名稱:php artisan asset:refresh-prices
|
||
protected $signature = 'asset:refresh-prices';
|
||
protected $description = '將全系統所有股票與基金的價格更新任務推入 Queue 佇列';
|
||
|
||
public function handle()
|
||
{
|
||
// 🟢 1. 從持倉表中撈出所有使用者「目前有在使用」且「不重複」的股票與基金代號
|
||
// 這樣能完美確保 2330 只會被爬一次,不會對證交所造成重複負擔
|
||
$uniqueHoldings = Security::select('code', 'type', 'name')
|
||
->groupBy('code', 'type', 'name') // 去除重複
|
||
->get();
|
||
|
||
$this->info("全系統共發現 " . $uniqueHoldings->count() . " 檔不重複的現役標的,開始指派更新任務...");
|
||
|
||
$delaySeconds = 0;
|
||
|
||
foreach ($uniqueHoldings as $holding) {
|
||
UpdateAssetPriceJob::dispatch(
|
||
$holding->code,
|
||
$holding->type,
|
||
$holding->name
|
||
)->delay(now()->addSeconds($delaySeconds));
|
||
|
||
$delaySeconds += 2;
|
||
}
|
||
|
||
$this->info("所有任務皆已成功排入背景 Queue,預計花費 {$delaySeconds} 秒消化完畢。");
|
||
}
|
||
}
|