StBqResult.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. namespace App\Http\Model;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Http\Response\Response;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Lib\Biz\Sport\Common as commonFunction;
  7. /**
  8. * Class StBqResult
  9. * @package App\Http\Model
  10. * 棒球 赛事 结果
  11. */
  12. class StBqResult extends Model
  13. {
  14. protected $table = 'st_bq_result';
  15. public $timestamps = false;
  16. /*
  17. * 写赛事结果
  18. * 弃用
  19. */
  20. public static function BQresult__($model){
  21. //获取赛事表7天内所有赛事
  22. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  23. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-7 day"))]])
  24. ->get()
  25. ->toarray();
  26. //没有数据,无需操作
  27. if(empty($matchData)) return Response::success();
  28. //获取赛事结果表 所有当月
  29. $matchData_r = $model['model_result']::select('match_id')
  30. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-7 day"))]])
  31. ->get()
  32. ->toarray();
  33. //结果表无数据,直接插入
  34. if(empty($matchData_r)){
  35. foreach ($matchData as $k=>$v){
  36. $start_time = ($v['match_date'].' '.$v['match_time']);
  37. $time = time()-strtotime($v['match_time']);
  38. $match_time = self::secTime($time);
  39. $set_match_r[] = [
  40. "match_id"=> $v['id'],
  41. "home_team"=>$v['home_team'],
  42. "guest_team"=>$v['guest_team'],
  43. "lg_id"=>$v['lg_id'],
  44. "status"=>$v['status'],
  45. "tag"=> $v['tag'],
  46. 'match_time'=>$match_time,
  47. "ctime"=>date('Y-m-d H:i:s'),
  48. "update_time"=>date('Y-m-d H:i:s'),
  49. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time))
  50. ];
  51. }
  52. $ret = $model['model_result']::insert($set_match_r);
  53. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  54. }else {
  55. //如果结果表有数据,则获取结果表没有的赛事
  56. foreach ($matchData as $k => $v) {
  57. foreach ($matchData_r as $kk => $vv) {
  58. if ($v['id'] == $vv['match_id']) {
  59. unset($matchData[$k]);
  60. }
  61. }
  62. }
  63. //如果还有未写入赛事
  64. if (!empty($matchData)) {
  65. //写入结果表不存在赛事
  66. foreach ($matchData as $k => $v) {
  67. $start_time = ($v['match_date'] . ' ' . $v['match_time']);
  68. $time = time() - strtotime($v['match_time']);
  69. $match_time = self::secTime($time);
  70. $set_match_r[] = [
  71. "match_id" => $v['id'],
  72. "home_team" => $v['home_team'],
  73. "guest_team" => $v['guest_team'],
  74. "lg_id" => $v['lg_id'],
  75. "status" => $v['status'],
  76. "tag" => $v['tag'],
  77. 'match_time' => $match_time,
  78. "ctime" => date('Y-m-d H:i:s'),
  79. "update_time" => date('Y-m-d H:i:s'),
  80. "start_time" => date('Y-m-d H:i:s', strtotime($start_time))
  81. ];
  82. }
  83. $ret = $model['model_result']::insert($set_match_r);
  84. if ($ret != true) throw new \Exception(Response::generate('', Response::ADD_MATCH_R_ERROR));//Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
  85. }
  86. }
  87. }
  88. public static function BQresult_v1($model){
  89. //获取赛事表7天内所有赛事
  90. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  91. ->where([['match_date','>',date('Y-m-d',strtotime("-2 day"))],['status','=',2]])
  92. ->get()
  93. ->toArray();
  94. //获取赛事结果表 一天内
  95. $matchData_r = $model['model_result']::select('match_id')
  96. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-2 day"))]])
  97. ->get()
  98. ->toArray();
  99. if(!empty($matchData_r)){
  100. //如果结果表有数据,则获取结果表没有的赛事
  101. foreach ($matchData as $k => $v) {
  102. foreach ($matchData_r as $kk => $vv) {
  103. if ($v['id'] == $vv['match_id']) {
  104. unset($matchData[$k]);
  105. }
  106. }
  107. }
  108. }
  109. //没有数据,无需操作
  110. if(empty($matchData)) return Response::success();
  111. //获取赛事id 用于获取赛事对应结果比分
  112. $match_ids = [];
  113. foreach($matchData as $k =>$v){
  114. //只获取赛事已结束的
  115. if($v['status'] == 2){
  116. $match_ids[] = $v['id'];
  117. }
  118. }
  119. $match_ids_str = implode(",", $match_ids);
  120. // 组装sql
  121. $sql_result = "select a.match_id,a.home_score,a.guest_score,a.match_time as a_time,a.match_process,a.all_inning,a.first_score,a.last_score,a.match_score,a.match_winer,a.home_rate,a.guest_rate,a.result_mark from st_bq_result_record a,
  122. (select match_id,max(id) id from st_bq_result_record where match_id IN ($match_ids_str) group by match_id)b
  123. where a.match_id = b.match_id and a.id = b.id ";
  124. //赛事最终结果
  125. $match_result = DB::select($sql_result);
  126. //拼装赛事结果数据
  127. $set_match_r = [];
  128. if(!empty($match_result)){
  129. foreach($matchData as $k =>$v ){
  130. //获取开赛时间
  131. $start_time = ($v['match_date'].' '.$v['match_time']);
  132. $time = time()-strtotime($v['match_time']);
  133. $match_time = self::secTime($time);
  134. //获取赛事每节的赛果
  135. $resultData = commonFunction::filter_by_value($match_result,'match_id',$v['id']);
  136. if(!empty($resultData)){
  137. //获取赛果json
  138. $result_mark = json_decode($resultData['result_mark'],true);
  139. //获取进程
  140. $match_process = $resultData['match_process'];
  141. //获胜球员
  142. $match_winer = '';
  143. if($result_mark['game_num_H'] > $result_mark['game_num_C']){
  144. $match_winer = $v['home_team'];
  145. }else{
  146. $match_winer = $v['guest_team'];
  147. }
  148. //组装输赢结果json
  149. $inning = [
  150. 1=>[
  151. "home"=>$result_mark['sc_1th_H'],
  152. "guest"=>$result_mark['sc_1th_C'],
  153. ],
  154. 2=>[
  155. "home"=>$result_mark['sc_2th_H'],
  156. "guest"=>$result_mark['sc_2th_C'],
  157. ],
  158. 3=>[
  159. "home"=>$result_mark['sc_3th_H'],
  160. "guest"=>$result_mark['sc_3th_C'],
  161. ],
  162. 4=>[
  163. "home"=>$result_mark['sc_4th_H'],
  164. "guest"=>$result_mark['sc_4th_C'],
  165. ],
  166. 5=>[
  167. "home"=>$result_mark['sc_5th_H'],
  168. "guest"=>$result_mark['sc_5th_C'],
  169. ],
  170. 6=>[
  171. "home"=>$result_mark['sc_6th_H'],
  172. "guest"=>$result_mark['sc_6th_C'],
  173. ],
  174. 7=>[
  175. "home"=>$result_mark['sc_7th_H'],
  176. "guest"=>$result_mark['sc_7th_C'],
  177. ],
  178. 8=>[
  179. "home"=>$result_mark['sc_8th_H'],
  180. "guest"=>$result_mark['sc_8th_C'],
  181. ],
  182. 9=>[
  183. "home"=>$result_mark['sc_9th_H'],
  184. "guest"=>$result_mark['sc_9th_C'],
  185. ],
  186. "other"=>[//加时
  187. "home"=>$result_mark['OT_H'],
  188. "guest"=>$result_mark['OT_C'],
  189. ]
  190. ];
  191. //上半场主队进球
  192. $u_home_score = $result_mark['sc_1th_H']+$result_mark['sc_2th_H']+$result_mark['sc_3th_H']+$result_mark['sc_4th_H']+$result_mark['sc_5th_H'];
  193. //上半场客队进球
  194. $u_guest_score = $result_mark['sc_1th_C']+$result_mark['sc_2th_C']+$result_mark['sc_3th_C']+$result_mark['sc_4th_C']+$result_mark['sc_5th_C'];
  195. //赛事比分
  196. $match_score = $result_mark['game_num_H'].':'.$result_mark['game_num_C'];
  197. //总进球数
  198. $all_goal = $result_mark['game_num_H'] + $result_mark['game_num_C'];
  199. //赛事待写入赛果数据
  200. $set_match_r[] = [
  201. "match_id"=> $v['id'],
  202. "home_team"=>$v['home_team'],
  203. "guest_team"=>$v['guest_team'],
  204. "lg_id"=>$v['lg_id'],
  205. "status"=>$v['status'],
  206. "tag"=> $v['tag'],
  207. 'match_time'=>$resultData['a_time']?:0,
  208. "ctime"=>date('Y-m-d H:i:s'),
  209. "update_time"=>date('Y-m-d H:i:s'),
  210. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  211. "home_rate"=> $resultData['home_rate']?:0, //主队让球
  212. "guest_rate"=> $resultData['guest_rate']?:0, //客队让球
  213. "home_score"=> $result_mark['game_num_H']?:0, //主队进球
  214. "guest_score"=> $result_mark['game_num_C']?:0, //客队进球
  215. "all_goal"=> $all_goal?:0, //总局数
  216. // "first_score"=> $resultData['first_score']?:'', //最先得分
  217. // "last_score"=> $resultData['last_score']?:'', //最后得分
  218. "match_score"=> $match_score?:'',//赛事比分
  219. "match_winer"=> $match_winer?:'',//获胜队员
  220. "match_process"=> $match_process?:'',//比赛进程
  221. "u_home_score"=> $u_home_score,//上半场主队进球
  222. "u_guest_score"=> $u_guest_score ,//上半场客队进球
  223. "match_score_t"=> json_encode($inning)?:'',//每局输赢结果
  224. ];
  225. }
  226. }
  227. }
  228. //写入赛果
  229. if(!empty($set_match_r)){
  230. $ret = $model['model_result']::insert($set_match_r);
  231. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  232. }
  233. return 1;
  234. }
  235. /**
  236. * 获取自动赛事结果
  237. * 只更新已结束+未手动修改的赛事结果
  238. * $match_id 本地赛事id 为0时 则用于后台手动刷新赛果
  239. */
  240. public static function BQresult_v2($model,$match_id=0){
  241. //如果有赛事id 则只更新当前赛事结果
  242. if($match_id > 0){
  243. $match_ids_str = $match_id;
  244. $match_ids = [$match_id];
  245. }else{
  246. //获取两天内的结束赛事
  247. $matchData = $model['model_match']
  248. ->join('st_bq_result','st_bq_result.match_id','=','st_bq_competition.id')
  249. ->select('st_bq_competition.id','is_correct')
  250. ->where([['st_bq_competition.match_date','>',date('Y-m-d', strtotime("-2 day"))],['st_bq_competition.status','=',2],['st_bq_result.is_correct','=',-1]])
  251. ->get()
  252. ->toArray();
  253. //没有数据,无需操作
  254. if(empty($matchData)) return Response::success();
  255. //获取需更新结果的赛事ID
  256. $match_ids = [];
  257. foreach($matchData as $k =>$v){
  258. //未手动修改比分
  259. if($v['is_correct'] == 0){
  260. $match_ids[] = $v['id'];
  261. }
  262. }
  263. if(empty($match_ids)) return Response::success();
  264. $match_ids_str = implode(",", $match_ids);
  265. }
  266. //如果有赛事id 获取结果中的手动数据
  267. if(!empty($match_ids)){
  268. $manual_result_data = self::select('match_id','manual_result')
  269. ->whereIn('match_id',$match_ids)
  270. ->get()
  271. ->toArray();
  272. }
  273. // 组装sql
  274. $sql_result = "select a.match_id,a.home_team,a.guest_team,a.home_score,a.guest_score,a.match_time as a_time,a.match_process,a.all_inning,a.first_score,a.last_score,a.match_score,a.match_winer,a.home_rate,a.guest_rate,a.result_mark from st_bq_result_record a,
  275. (select match_id,max(id) id from st_bq_result_record where match_id IN ($match_ids_str) group by match_id)b
  276. where a.match_id = b.match_id and a.id = b.id ";
  277. //赛事最终结果
  278. $match_result = DB::select($sql_result);
  279. if(!empty($match_result)){
  280. foreach($match_result as $k=>$v){
  281. //获取赛事每节的赛果
  282. $resultData = commonFunction::filter_by_value($match_result,'match_id',$v->match_id);
  283. if(!empty($resultData)){
  284. //获取赛果json
  285. $result_mark = json_decode($resultData['result_mark'],true);
  286. //获取进程
  287. $match_process = $resultData['match_process'];
  288. //获胜球员
  289. $match_winer = '';
  290. if($result_mark['game_num_H'] > $result_mark['game_num_C']){
  291. $match_winer = $v->home_team;
  292. }else{
  293. $match_winer = $v->guest_team;
  294. }
  295. //组装输赢结果json
  296. $inning = [
  297. 1=>[
  298. "home"=>$result_mark['sc_1th_H'],
  299. "guest"=>$result_mark['sc_1th_C'],
  300. ],
  301. 2=>[
  302. "home"=>$result_mark['sc_2th_H'],
  303. "guest"=>$result_mark['sc_2th_C'],
  304. ],
  305. 3=>[
  306. "home"=>$result_mark['sc_3th_H'],
  307. "guest"=>$result_mark['sc_3th_C'],
  308. ],
  309. 4=>[
  310. "home"=>$result_mark['sc_4th_H'],
  311. "guest"=>$result_mark['sc_4th_C'],
  312. ],
  313. 5=>[
  314. "home"=>$result_mark['sc_5th_H'],
  315. "guest"=>$result_mark['sc_5th_C'],
  316. ],
  317. 6=>[
  318. "home"=>$result_mark['sc_6th_H'],
  319. "guest"=>$result_mark['sc_6th_C'],
  320. ],
  321. 7=>[
  322. "home"=>$result_mark['sc_7th_H'],
  323. "guest"=>$result_mark['sc_7th_C'],
  324. ],
  325. 8=>[
  326. "home"=>$result_mark['sc_8th_H'],
  327. "guest"=>$result_mark['sc_8th_C'],
  328. ],
  329. 9=>[
  330. "home"=>$result_mark['sc_9th_H'],
  331. "guest"=>$result_mark['sc_9th_C'],
  332. ],
  333. "other"=>[//加时
  334. "home"=>$result_mark['OT_H'],
  335. "guest"=>$result_mark['OT_C'],
  336. ]
  337. ];
  338. //上半场主队进球
  339. $u_home_score = $result_mark['sc_1th_H']+$result_mark['sc_2th_H']+$result_mark['sc_3th_H']+$result_mark['sc_4th_H']+$result_mark['sc_5th_H'];
  340. //上半场客队进球
  341. $u_guest_score = $result_mark['sc_1th_C']+$result_mark['sc_2th_C']+$result_mark['sc_3th_C']+$result_mark['sc_4th_C']+$result_mark['sc_5th_C'];
  342. //赛事比分
  343. $match_score = $result_mark['game_num_H'].':'.$result_mark['game_num_C'];
  344. //总进球数
  345. $all_goal = $result_mark['game_num_H'] + $result_mark['game_num_C'];
  346. //===追加获取手动结果数据===
  347. $manual_result_json = commonFunction::getManual_result($manual_result_data,$inning,$v->match_id);
  348. //赛事待写入赛果数据
  349. $set_match_r = [
  350. "match_id"=> $v->match_id,
  351. "status"=>2,
  352. "update_time"=>date('Y-m-d H:i:s'),
  353. "home_rate"=> $resultData['home_rate']?:0, //主队让球
  354. "guest_rate"=> $resultData['guest_rate']?:0, //客队让球
  355. "home_score"=> $result_mark['game_num_H']?:0, //主队进球
  356. "guest_score"=> $result_mark['game_num_C']?:0, //客队进球
  357. "all_goal"=> $all_goal?:0, //总局数
  358. // "first_score"=> $resultData['first_score']?:'', //最先得分
  359. // "last_score"=> $resultData['last_score']?:'', //最后得分
  360. "match_score"=> $match_score?:'',//赛事比分
  361. "match_winer"=> $match_winer?:'',//获胜队员
  362. "match_process"=> $match_process?:'',//比赛进程
  363. "u_home_score"=> $u_home_score,//上半场主队进球
  364. "u_guest_score"=> $u_guest_score ,//上半场客队进球
  365. "match_score_t"=> json_encode($inning)?:'',//每局输赢结果
  366. "is_correct"=> -1,//自动比分
  367. "manual_result"=> $manual_result_json,//手动结果
  368. ];
  369. $ret = $model['model_result']::where(['match_id' => $v->match_id,'is_correct'=>-1])
  370. ->update($set_match_r);
  371. // if($ret < 1) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  372. }
  373. }
  374. }
  375. return 1;
  376. }
  377. //计算滚球 赛事进行时间
  378. public static function secTime($sec=0){
  379. $min = floor($sec/60);
  380. $res = $min.':'.($sec-$min*60);
  381. return $res;
  382. }
  383. }