Files
finance_app/app/Console/Commands/RefreshAllAssetPrices.php
henry yo 85972f950c feat: 前端分離+功能api化
1. 新增資產管理CRUD、googleOAuth登入
2. 更改原記帳controller
2026-06-25 14:19:07 +08:00

41 lines
1.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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} 秒消化完畢。");
}
}