|
|
@@ -9,6 +9,7 @@ use App\Services\SportClientService;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
use App\Models\Config;
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
+use Throwable; // 使用 Throwable 可以捕获所有 PHP 7+ 的异常和错误
|
|
|
|
|
|
class SportOdds extends Command
|
|
|
{
|
|
|
@@ -69,53 +70,59 @@ class SportOdds extends Command
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
- $this->is_live = $this->argument('is_live');
|
|
|
- if ($this->is_live == 1) {
|
|
|
- // $this->info('直播赔率开始执行任务...');
|
|
|
- $this->sportOddsData($this->is_live);
|
|
|
- } else {
|
|
|
- // $this->info('开赛前赔率开始执行任务...');
|
|
|
- $this->sportOddsData($this->is_live);
|
|
|
- }
|
|
|
-
|
|
|
- // $this->info('结束执行赔率任务');
|
|
|
+ $this->is_live = $this->argument('is_live');
|
|
|
+ $this->sportOddsData($this->is_live);
|
|
|
}
|
|
|
|
|
|
public function sportOddsData($is_live)
|
|
|
{
|
|
|
- try {
|
|
|
- $limit = 3000;
|
|
|
- //直播赔率(获取正在直播的所有赛事的赔率)
|
|
|
- if ($is_live == 1) {
|
|
|
- $data = SportClientService::oddsLive([]);
|
|
|
- file_put_contents("oddsLive.json",json_encode($data));
|
|
|
+ if ($is_live == 1) {
|
|
|
+ // 直播赔率通常是一次获取全量,但也需要对整体更新逻辑做保护
|
|
|
+ $data = SportClientService::oddsLive([]);
|
|
|
+ if ($data) {
|
|
|
+ // file_put_contents("oddsLive.json",json_encode($data));
|
|
|
$this->updateOddsLive($data);
|
|
|
- } else {
|
|
|
- //普通球赛是开赛前购买,更新数据
|
|
|
- $where['state'] = 0; //比赛状态:0未开始1进行中2已完场3延期4取消
|
|
|
- $list = SportModel::where($where)->where('odds',null)->limit($limit)->get()->toArray();
|
|
|
- foreach($list as $item) {
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $limit = 3000;
|
|
|
+ //普通球赛是开赛前购买,更新数据
|
|
|
+ $where['state'] = 0; //比赛状态:0未开始1进行中2已完场3延期4取消
|
|
|
+ $list = SportModel::where($where)->where('odds',null)->limit($limit)->get()->toArray();
|
|
|
+
|
|
|
+ foreach ($list as $item) {
|
|
|
+ // --- 关键优化:在循环内部嵌套 try-catch ---
|
|
|
+ try {
|
|
|
$data = SportClientService::odds([
|
|
|
- 'fixture' => $item['data_id'],
|
|
|
+ 'fixture' => $item->data_id,
|
|
|
]);
|
|
|
- $this->updateOdds($item['data_id'],$data);
|
|
|
+ $this->updateOdds($item->data_id, $data);
|
|
|
+ } catch (Throwable $e) {
|
|
|
+ // 记录具体哪条赛事出错了,但不抛出,让循环继续
|
|
|
+ Log::error("赛前赔率更新失败 [Fixture: {$item->data_id}]: " . $e->getMessage());
|
|
|
+ continue;
|
|
|
}
|
|
|
}
|
|
|
- } catch (\Exception $e) {
|
|
|
- Log::error($e->getMessage());
|
|
|
}
|
|
|
+
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
public function updateOddsLive($data)
|
|
|
{
|
|
|
+ if (empty($data['response'])) return;
|
|
|
+
|
|
|
//体育赛事结束前几(分钟)锁盘,90分钟结束
|
|
|
$sport_locked = Config::where('field', 'sport_locked')->first()->val ?? 1;
|
|
|
- if(!empty($data['response'])) {
|
|
|
- $data = $data['response'];
|
|
|
- foreach($data as $item) {
|
|
|
+ $responses = $data['response'];
|
|
|
+
|
|
|
+ foreach ($responses as $item) {
|
|
|
+ // --- 关键优化:直播赔率逐条更新也要包裹,防止单条数据格式问题挂掉全盘 ---
|
|
|
+ try {
|
|
|
$data_id = $item['fixture']['id'];
|
|
|
$sport_info = SportModel::where('data_id',$data_id)->select('odds','odd_ids_locked')->first();
|
|
|
+
|
|
|
+ if (!$sport_info) continue;
|
|
|
+
|
|
|
$old_odd_ids_locked = $sport_info['odd_ids_locked'];
|
|
|
$odds_data = $this->doOdds($item['odds'], $sport_info['odds']);
|
|
|
$odds = $odds_data['odds'];
|
|
|
@@ -174,22 +181,21 @@ class SportOdds extends Command
|
|
|
$update_data['refund_status'] = 1;
|
|
|
}
|
|
|
|
|
|
- SportModel::where('data_id',$data_id)->update($update_data);
|
|
|
- // echo $data_id.": 更新成功\r\n";
|
|
|
+ SportModel::where('data_id', $data_id)->update($update_data);
|
|
|
+
|
|
|
+ } catch (Throwable $e) {
|
|
|
+ Log::error("直播赔率单条更新失败 [ID: {$data_id}]: " . $e->getMessage());
|
|
|
+ continue;
|
|
|
}
|
|
|
- }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public function updateOdds($data_id,$data)
|
|
|
+ public function updateOdds($data_id, $data)
|
|
|
{
|
|
|
- // echo $data_id;
|
|
|
if (!empty($data['response'][0]['bookmakers'][0]['bets'])) {
|
|
|
$odds = $data['response'][0]['bookmakers'][0]['bets'];
|
|
|
- // echo "更新成功\r\n";
|
|
|
SportModel::where('data_id',$data_id)->update(['odds' => json_encode($odds), 'is_send' => 0]);
|
|
|
- } else {
|
|
|
- // echo "更新失败\r\n";
|
|
|
- }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//去掉无效的赔率
|