Sport.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Sport as SportModel;
  5. use App\Services\SportClientService;
  6. use Carbon\Carbon;
  7. use Illuminate\Support\Facades\DB;
  8. use App\Models\SportEvent;
  9. class Sport extends Command
  10. {
  11. /**
  12. * 命令名称和签名
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'sport';
  17. protected $short_status = [
  18. 'TBD' => 0,
  19. 'NS' => 0,
  20. '1H' => 1,
  21. 'HT' => 1,
  22. '2H' => 1,
  23. 'ET' => 1,
  24. 'BT' => 1,
  25. 'P' => 1,
  26. 'SUSP' => 1,
  27. 'INT' => 1,
  28. 'FT' => 2,
  29. 'AET' => 2,
  30. 'PEN' => 2,
  31. 'PST' => 3,
  32. 'CANC' => 4,
  33. 'ABD' => 4,
  34. 'AWD' => 4,
  35. 'WO' => 4,
  36. 'LIVE' => 1,
  37. ];
  38. /**
  39. * 命令描述
  40. *
  41. * @var string
  42. */
  43. protected $description = '当天会去更新明天的赛事(23:59:00执行一次)';
  44. /**
  45. * 执行命令
  46. *
  47. * @return int
  48. */
  49. public function handle()
  50. {
  51. $this->info('开始执行统计比赛数据任务...');
  52. // $data = SportClientService::fixtures(['id' => '1506018']);
  53. // file_put_contents('fixtures-1506018.json', json_encode($data));
  54. // die;
  55. $this->fixtures();
  56. $this->info('结束执行统计比赛数据任务');
  57. }
  58. public function initOdds(){
  59. $page = 1;
  60. $limit = 10;
  61. while (true) {
  62. $list = SportModel::where('odds','<>', null)->forPage($page, $limit)->get()->toArray();
  63. if (empty($list)) {
  64. break;
  65. }
  66. echo $page.PHP_EOL;
  67. foreach($list as $item) {
  68. $odds = json_decode($item['odds'], true);
  69. foreach($odds as $odd) {
  70. $odd_id = $odd['id'];
  71. $odd_name = $odd['name'];
  72. $info = DB::table('sport_odds')->where('odd_id',$odd_id)->where('odd_name_en',$odd_name)->first();
  73. if ($info && (!$info->odd_name || $odd_name != $info->odd_name_en)) {
  74. DB::table('sport_odds')->where('id', $info->id)->update([
  75. 'odd_name_en' => $odd_name,
  76. 'odd_name' => $this->getZhName($odd_name),
  77. ]);
  78. echo '更新数据:'.$odd_id.'-'.$odd_name.PHP_EOL;
  79. } elseif (!$info) {
  80. DB::table('sport_odds')->insert([
  81. 'odd_id' => $odd_id,
  82. 'odd_name_en' => $odd_name,
  83. 'odd_name' => $this->getZhName($odd_name),
  84. 'created_at' => date('Y-m-d H:i:s'),
  85. 'updated_at' => date('Y-m-d H:i:s'),
  86. ]);
  87. echo '插入数据:'.$odd_id.'-'.$odd_name.PHP_EOL;
  88. }
  89. }
  90. }
  91. $page++;
  92. }
  93. }
  94. public function getZhName ($name) {
  95. $betting_terms = [
  96. "Match Winner" => "全场胜负",
  97. "Home/Away" => "主胜/客胜",
  98. "Second Half Winner" => "下半场胜负",
  99. "Asian Handicap" => "亚洲让球盘",
  100. "Goals Over/Under" => "全场大小球",
  101. "Goals Over/Under First Half" => "上半场大小球",
  102. "Goals Over/Under - Second Half" => "下半场大小球",
  103. "HT/FT Double" => "半场+全场双猜",
  104. "Both Teams Score" => "双方均进球",
  105. "Handicap Result" => "让球结果",
  106. "Exact Score" => "精确比分",
  107. "Correct Score - First Half" => "上半场精确比分",
  108. "Correct Score - Second Half" => "下半场精确比分",
  109. "Double Chance" => "双选胜平负",
  110. "First Half Winner" => "上半场胜负",
  111. "Team To Score First" => "首支进球球队",
  112. "Team To Score Last" => "最后进球球队",
  113. "Win Both Halves" => "上下半场均获胜",
  114. "Total - Home" => "主队总进球数",
  115. "Total - Away" => "客队总进球数",
  116. "Both Teams Score - First Half" => "上半场双方均进球",
  117. "Both Teams To Score - Second Half" => "下半场双方均进球",
  118. "Odd/Even" => "总进球数奇偶",
  119. "Odd/Even - First Half" => "上半场进球奇偶",
  120. "Home Team Exact Goals Number" => "主队精确进球数",
  121. "Away Team Exact Goals Number" => "客队精确进球数",
  122. "Results/Both Teams Score" => "赛果+双方进球",
  123. "Odd/Even - Second Half" => "下半场进球奇偶",
  124. "Clean Sheet - Home" => "主队零封",
  125. "Clean Sheet - Away" => "客队零封",
  126. "Win to Nil - Home" => "主队零封获胜",
  127. "Win to Nil - Away" => "客队零封获胜",
  128. "Highest Scoring Half" => "进球更多的半场",
  129. "Handicap Result - First Half" => "上半场让球结果",
  130. "Asian Handicap First Half" => "上半场亚洲让球盘",
  131. "Double Chance - First Half" => "上半场双选胜平负",
  132. "Win To Nil" => "零封获胜",
  133. "Home Odd/Even" => "主队进球奇偶",
  134. "Away Odd/Even" => "客队进球奇偶",
  135. "To Win Either Half" => "赢得任意半场",
  136. "Result/Total Goals" => "赛果+总进球数",
  137. "First 10 min Winner" => "前10分钟胜负",
  138. "Corners Over Under" => "角球大小",
  139. "Home Team Total Goals(1st Half)" => "主队上半场总进球",
  140. "Away Team Total Goals(1st Half)" => "客队上半场总进球",
  141. "Home Team Total Goals(2nd Half)" => "主队下半场总进球",
  142. "Away Team Total Goals(2nd Half)" => "客队下半场总进球",
  143. "Draw No Bet (1st Half)" => "上半场让球平注",
  144. "European Handicap (2nd Half)" => "下半场欧洲让球",
  145. "Draw No Bet (2nd Half)" => "下半场让球平注",
  146. "Total Goals/Both Teams To Score" => "总进球+双方进球",
  147. "Home Corners Over/Under" => "主队角球大小",
  148. "Away Corners Over/Under" => "客队角球大小",
  149. "Total Corners (3 way)" => "总角球三路",
  150. "1x2 - 60 minutes" => "60分钟胜平负",
  151. "1x2 - 30 minutes" => "30分钟胜平负",
  152. "First Team to Score (3 way) 1st Half" => "上半场首支进球球队(三路)",
  153. "Total Corners (1st Half)" => "上半场总角球",
  154. "Corners. Odd/Even" => "角球奇偶",
  155. "RTG_H1" => "上半场进球数(简写)",
  156. "Cards Over/Under" => "黄牌大小",
  157. "To Qualify" => "晋级球队",
  158. "Goal Line" => "进球线",
  159. "Goal Line (1st Half)" => "上半场进球线",
  160. "Home team will score in both halves" => "主队上下半场均进球",
  161. "Away team will score in both halves" => "客队上下半场均进球",
  162. "Last Corner" => "最后角球",
  163. "How many goals will Away Team score?" => "客队进球数",
  164. "Asian Corners" => "亚洲让角球",
  165. "Match Corners" => "全场角球",
  166. "Final Score" => "最终比分",
  167. "Match Goals" => "全场进球",
  168. "Home Team Score a Goal (2nd Half)" => "主队下半场进球",
  169. "Result / Both Teams To Score" => "赛果/双方进球",
  170. "To Win 2nd Half" => "赢下半场",
  171. "Over/Under Line" => "大小球盘口",
  172. "3-Way Handicap" => "三路让球",
  173. "Away Team Goals" => "客队进球",
  174. "Both Teams To Score (2nd Half)" => "下半场双方进球",
  175. "Which team will score the 5th corner? (2 Way)" => "第5个角球归属(二路)",
  176. "Race to the 9th corner?" => "先得9个角球",
  177. "Race to the 7th corner?" => "先得7个角球",
  178. "Draw No Bet" => "让球平注",
  179. "Home Team Goals" => "主队进球",
  180. "Total Corners" => "总角球",
  181. "Fulltime Result" => "全场赛果",
  182. "Race to the 5th corner?" => "先得5个角球",
  183. "Last Team to Score (3 way)" => "最后进球球队(三路)",
  184. "Which team will score the 2nd goal?" => "第二球归属球队",
  185. "Home Team Clean Sheet" => "主队零封",
  186. "How many goals will Home Team score?" => "主队进球数",
  187. "Goals Odd/Even" => "进球奇偶",
  188. "Both Teams to Score" => "双方均进球",
  189. "Away Team Score a Goal (2nd Half)" => "客队下半场进球",
  190. "Which team will score the 4th goal?" => "第四球归属球队",
  191. "Which team will score the 7th corner? (2 Way)" => "第7个角球归属(二路)",
  192. ];
  193. if (isset($betting_terms[$name])) {
  194. return $betting_terms[$name];
  195. }
  196. return '';
  197. }
  198. /**
  199. * 获取指定日期的所有赛事
  200. *
  201. * @return array
  202. */
  203. public function fixtures()
  204. {
  205. $date = Carbon::tomorrow()->toDateString();
  206. // $date = '2026-03-07';
  207. $data = SportClientService::fixtures(['date' => $date]);
  208. // $data = SportClientService::fixtures(['live' => 'all']);
  209. $data = $data['response'];
  210. $tableData = [];
  211. $status = $this->short_status;
  212. foreach ($data as $item) {
  213. $home_statistics = !empty($item['statistics']) ? $item['statistics'][0]['statistics'] : '';
  214. $away_statistics = !empty($item['statistics']) ? $item['statistics'][1]['statistics'] : '';
  215. $sport_data = [
  216. 'data_id' => $item['fixture']['id'],
  217. 'home_team_id' => $item['teams']['home']['id'],
  218. 'home_team_en' => $item['teams']['home']['name'],
  219. 'home_team' => lang($item['teams']['home']['name']),
  220. 'home_team_logo' => $item['teams']['home']['logo'],
  221. 'guest_team_id' => $item['teams']['away']['id'],
  222. 'guest_team_en' => $item['teams']['away']['name'],
  223. 'guest_team' => lang($item['teams']['away']['name']),
  224. 'guest_team_logo' => $item['teams']['away']['logo'],
  225. 'half_score' => "{$item['score']['halftime']['home']}-{$item['score']['halftime']['away']}",
  226. 'rbt' => $item['fixture']['timestamp'],
  227. 'score' => isset($item['score']['fulltime']) ? "{$item['score']['fulltime']['home']}-{$item['score']['fulltime']['away']}":'-',
  228. 'league' => lang($item['league']['name']),
  229. 'league_en' => $item['league']['name'],
  230. 'state' => $status[$item['fixture']['status']['short']],//比赛状态:0未开始1进行中2已完场3延期4取消
  231. 'game_time' => $item['fixture']['timestamp'],
  232. 'status' => 1,
  233. 'updated_at' => now(),
  234. 'home_statistics' => $home_statistics,
  235. 'away_statistics' => $away_statistics,
  236. ];
  237. $sport_data['score'] = $sport_data['score'] == '-' ? '' : $sport_data['score'];
  238. $sport_data['half_score'] = $sport_data['half_score'] == '-' ? '' : $sport_data['half_score'];
  239. if (!SportModel::where('data_id', $item['fixture']['id'])->exists()) {
  240. $sport_data['created_at'] = now();
  241. $tableData[] = $sport_data;
  242. } else {
  243. SportModel::where('data_id', $item['fixture']['id'])->update($sport_data);
  244. }
  245. //比赛结束,插入比赛事件
  246. if ($sport_data['state'] == 2 && !empty($item['events'])) {
  247. foreach($item['events'] as $event) {
  248. SportEvent::create([
  249. 'data_id' => $item['fixture']['id'],
  250. 'type' => $event['type'],
  251. 'time_elapsed' => $event['time']['elapsed'],
  252. 'time' => json_encode($event['time']),
  253. 'detail' => $event['detail'],
  254. 'player' => $event['player'] ? json_encode($event['player']) : $event['player'],
  255. 'team_id' => $event['team']['id'],
  256. 'comments' => $event['comments'],
  257. 'assist' => $event['assist'] ? json_encode($event['assist']) : $event['assist'],
  258. ]);
  259. }
  260. }
  261. }
  262. if ($tableData) {
  263. SportModel::insert($tableData);
  264. }
  265. return $tableData;
  266. }
  267. }