StBqResult.php 18 KB

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