$userId, 'status' => YesNoEnum::YES, ])->order('create_time asc')->column('symbol'); return array_values(array_unique(array_map('strtoupper', $symbols))); } public static function addFavorite(int $userId, string $symbol): bool { $symbol = strtoupper(trim($symbol)); if (!CryptoMarketService::isSupportedSymbol($symbol)) { self::setError('暂不支持该币种'); return false; } try { $where = ['user_id' => $userId, 'symbol' => $symbol]; $favorite = CryptoFavorite::where($where)->findOrEmpty(); if ($favorite->isEmpty()) { try { CryptoFavorite::create($where + ['status' => YesNoEnum::YES]); } catch (\Throwable $e) { CryptoFavorite::update(['status' => YesNoEnum::YES], $where); } } else { $favorite->status = YesNoEnum::YES; $favorite->save(); } return true; } catch (\Throwable $e) { self::setError('收藏失败,请稍后重试'); return false; } } public static function cancelFavorite(int $userId, string $symbol): bool { try { CryptoFavorite::update( ['status' => YesNoEnum::NO], ['user_id' => $userId, 'symbol' => strtoupper(trim($symbol))] ); return true; } catch (\Throwable $e) { self::setError('取消收藏失败,请稍后重试'); return false; } } }