StBqResult.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. */
  239. public static function BQresult_v2($model){
  240. //获取两天内的结束赛事
  241. $matchData = $model['model_match']
  242. ->join('st_bq_result','st_bq_result.match_id','=','st_bq_competition.id')
  243. ->select('st_bq_competition.id','is_correct')
  244. ->where([['st_bq_competition.match_date','>',date('Y-m-d', strtotime("-2 day"))],['st_bq_competition.status','=',2]])
  245. ->get()
  246. ->toArray();
  247. //没有数据,无需操作
  248. if(empty($matchData)) return Response::success();
  249. //获取需更新结果的赛事ID
  250. $match_ids = [];
  251. foreach($matchData as $k =>$v){
  252. //未手动修改比分
  253. if($v['is_correct'] == 0){
  254. $match_ids[] = $v['id'];
  255. }
  256. }
  257. if(empty($match_ids)) return Response::success();
  258. $match_ids_str = implode(",", $match_ids);
  259. // 组装sql
  260. $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,
  261. (select match_id,max(id) id from st_bq_result_record where match_id IN ($match_ids_str) group by match_id)b
  262. where a.match_id = b.match_id and a.id = b.id ";
  263. //赛事最终结果
  264. $match_result = DB::select($sql_result);
  265. if(!empty($match_result)){
  266. foreach($match_result as $k=>$v){
  267. //获取赛事每节的赛果
  268. $resultData = commonFunction::filter_by_value($match_result,'match_id',$v->match_id);
  269. if(!empty($resultData)){
  270. //获取赛果json
  271. $result_mark = json_decode($resultData['result_mark'],true);
  272. //获取进程
  273. $match_process = $resultData['match_process'];
  274. //获胜球员
  275. $match_winer = '';
  276. if($result_mark['game_num_H'] > $result_mark['game_num_C']){
  277. $match_winer = $v->home_team;
  278. }else{
  279. $match_winer = $v->guest_team;
  280. }
  281. //组装输赢结果json
  282. $inning = [
  283. 1=>[
  284. "home"=>$result_mark['sc_1th_H'],
  285. "guest"=>$result_mark['sc_1th_C'],
  286. ],
  287. 2=>[
  288. "home"=>$result_mark['sc_2th_H'],
  289. "guest"=>$result_mark['sc_2th_C'],
  290. ],
  291. 3=>[
  292. "home"=>$result_mark['sc_3th_H'],
  293. "guest"=>$result_mark['sc_3th_C'],
  294. ],
  295. 4=>[
  296. "home"=>$result_mark['sc_4th_H'],
  297. "guest"=>$result_mark['sc_4th_C'],
  298. ],
  299. 5=>[
  300. "home"=>$result_mark['sc_5th_H'],
  301. "guest"=>$result_mark['sc_5th_C'],
  302. ],
  303. 6=>[
  304. "home"=>$result_mark['sc_6th_H'],
  305. "guest"=>$result_mark['sc_6th_C'],
  306. ],
  307. 7=>[
  308. "home"=>$result_mark['sc_7th_H'],
  309. "guest"=>$result_mark['sc_7th_C'],
  310. ],
  311. 8=>[
  312. "home"=>$result_mark['sc_8th_H'],
  313. "guest"=>$result_mark['sc_8th_C'],
  314. ],
  315. 9=>[
  316. "home"=>$result_mark['sc_9th_H'],
  317. "guest"=>$result_mark['sc_9th_C'],
  318. ],
  319. "other"=>[//加时
  320. "home"=>$result_mark['OT_H'],
  321. "guest"=>$result_mark['OT_C'],
  322. ]
  323. ];
  324. //上半场主队进球
  325. $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'];
  326. //上半场客队进球
  327. $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'];
  328. //赛事比分
  329. $match_score = $result_mark['game_num_H'].':'.$result_mark['game_num_C'];
  330. //总进球数
  331. $all_goal = $result_mark['game_num_H'] + $result_mark['game_num_C'];
  332. //赛事待写入赛果数据
  333. $set_match_r = [
  334. "match_id"=> $v->match_id,
  335. "status"=>2,
  336. "update_time"=>date('Y-m-d H:i:s'),
  337. "home_rate"=> $resultData['home_rate']?:0, //主队让球
  338. "guest_rate"=> $resultData['guest_rate']?:0, //客队让球
  339. "home_score"=> $result_mark['game_num_H']?:0, //主队进球
  340. "guest_score"=> $result_mark['game_num_C']?:0, //客队进球
  341. "all_goal"=> $all_goal?:0, //总局数
  342. // "first_score"=> $resultData['first_score']?:'', //最先得分
  343. // "last_score"=> $resultData['last_score']?:'', //最后得分
  344. "match_score"=> $match_score?:'',//赛事比分
  345. "match_winer"=> $match_winer?:'',//获胜队员
  346. "match_process"=> $match_process?:'',//比赛进程
  347. "u_home_score"=> $u_home_score,//上半场主队进球
  348. "u_guest_score"=> $u_guest_score ,//上半场客队进球
  349. "match_score_t"=> json_encode($inning)?:'',//每局输赢结果
  350. "is_correct"=> -1,//自动比分
  351. ];
  352. $ret = $model['model_result']::where(['match_id' => $v->match_id])
  353. ->update($set_match_r);
  354. if($ret < 1) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  355. }
  356. }
  357. }
  358. return 1;
  359. }
  360. //计算滚球 赛事进行时间
  361. public static function secTime($sec=0){
  362. $min = floor($sec/60);
  363. $res = $min.':'.($sec-$min*60);
  364. return $res;
  365. }
  366. }