StWqResult.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 StWqResult
  9. * @package App\Http\Model
  10. * 网球 赛事 结果
  11. */
  12. class StWqResult extends Model
  13. {
  14. protected $table = 'st_wq_result';
  15. public $timestamps = false;
  16. public static function WQresult_v1($model){
  17. //获取赛事表7天内所有赛事
  18. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  19. ->where([['match_date','>',date('Y-m-d',strtotime("-2 day"))],['status','=',2]])
  20. ->get()
  21. ->toArray();
  22. //获取赛事结果表 一天内
  23. $matchData_r = $model['model_result']::select('match_id')
  24. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-2 day"))]])
  25. ->get()
  26. ->toArray();
  27. if(!empty($matchData_r)){
  28. //如果结果表有数据,则获取结果表没有的赛事
  29. foreach ($matchData as $k => $v) {
  30. foreach ($matchData_r as $kk => $vv) {
  31. if ($v['id'] == $vv['match_id']) {
  32. unset($matchData[$k]);
  33. }
  34. }
  35. }
  36. }
  37. //没有数据,无需操作
  38. if(empty($matchData)) return Response::success();
  39. //获取赛事id 用于获取赛事对应结果比分
  40. $match_ids = [];
  41. foreach($matchData as $k =>$v){
  42. //只获取赛事已结束的
  43. if($v['status'] == 2){
  44. $match_ids[] = $v['id'];
  45. }
  46. }
  47. $match_ids_str = implode(",", $match_ids);
  48. //组装sql
  49. //获取赛事最终赛果
  50. $sql_result = "select a.match_id,a.home_player_score,a.guest_player_score,a.match_time as a_time,a.home_player_let_plate,a.guest_player_let_plate,a.home_player_let_inning,a.guest_player_let_inning,a.all_inning,a.match_process,a.first_score_player,a.last_score_player,a.first_inning_score,a.second_inning_score,a.third_inning_score,a.match_winer_player,a.result_mark from st_wq_result_record a,
  51. (select match_id,max(id) id from st_wq_result_record where match_id IN ($match_ids_str) group by match_id)b
  52. where a.match_id = b.match_id and a.id = b.id ";
  53. //赛事最终结果
  54. $match_result = DB::select($sql_result);
  55. //拼装赛事结果数据
  56. $set_match_r = [];
  57. $ss = [];
  58. if(!empty($match_result)){
  59. foreach($matchData as $k =>$v ){
  60. //获取开赛时间
  61. $start_time = ($v['match_date'].' '.$v['match_time']);
  62. $time = time()-strtotime($v['match_time']);
  63. $match_time = self::secTime($time);
  64. //获取赛事每节的赛果
  65. $resultData = commonFunction::filter_by_value($match_result,'match_id',$v['id']);
  66. //如果有获取到结果,并且进程不为空,则写入赛事结果数据
  67. if(!empty($resultData) and $resultData['match_process'] != ''){
  68. //获取赛果json
  69. $result_mark = json_decode($resultData['result_mark'],true);
  70. $sss[] = $result_mark;
  71. //获取进程
  72. $match_process = '';
  73. if($result_mark['schedule']){
  74. $match_process = $result_mark['schedule'];
  75. }
  76. //获胜球员
  77. $match_winer = '';
  78. if($result_mark['game_num_H'] > $result_mark['game_num_C']){
  79. $match_winer = $v['home_team'];
  80. }else{
  81. $match_winer = $v['guest_team'];
  82. }
  83. //组装输赢结果json
  84. $inning = [
  85. 1=>[
  86. "home"=>$result_mark['sc_1th_H'],
  87. "guest"=>$result_mark['sc_1th_C'],
  88. ],
  89. 2=>[
  90. "home"=>$result_mark['sc_2th_H'],
  91. "guest"=>$result_mark['sc_2th_C'],
  92. ],
  93. 3=>[
  94. "home"=>$result_mark['sc_3th_H'],
  95. "guest"=>$result_mark['sc_3th_C'],
  96. ],
  97. 4=>[
  98. "home"=>$result_mark['sc_4th_H'],
  99. "guest"=>$result_mark['sc_4th_C'],
  100. ],
  101. 5=>[
  102. "home"=>$result_mark['sc_5th_H'],
  103. "guest"=>$result_mark['sc_5th_C'],
  104. ],
  105. 'all'=>[
  106. "home"=>$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'],
  107. "guest"=>$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'],
  108. ],
  109. ];
  110. //赛事比分
  111. $match_score = $result_mark['game_num_H'].':'.$result_mark['game_num_C'];
  112. //赛事待写入赛果数据
  113. /*
  114. $set_match_r[] = [
  115. "match_id"=> $v['id'],
  116. "home_player_name"=>$v['home_team'],
  117. "guest_player_name"=>$v['guest_team'],
  118. "lg_id"=>$v['lg_id'],
  119. "status"=>$v['status'],
  120. "tag"=> $v['tag'],
  121. 'match_time'=>$resultData['a_time']?:0,
  122. "ctime"=>date('Y-m-d H:i:s'),
  123. "update_time"=>date('Y-m-d H:i:s'),
  124. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  125. "home_player_let_plate"=> $resultData['home_player_let_plate']?:0, //主队让盘
  126. "guest_player_let_plate"=> $resultData['guest_player_let_plate']?:0, //客队让盘
  127. "home_player_let_inning"=> $resultData['home_player_let_inning']?:0, //主队让局
  128. "guest_player_let_inning"=> $resultData['guest_player_let_inning']?:0, //客队让局
  129. "home_player_score"=> $result_mark['game_num_H']?:'', //主队得分
  130. "guest_player_score"=> $result_mark['game_num_C']?:'', //客队得分
  131. "all_inning"=> $resultData['all_inning']?:0, //总局数
  132. // "first_score_player"=> $resultData['first_score']?:'', //最先得分
  133. // "last_score_player"=> $resultData['last_score']?:'', //最后得分
  134. "first_inning_score"=> $resultData['first_inning_score']?:0, //第一局比分
  135. "second_inning_score"=> $resultData['second_inning_score']?:0,//第二局比分
  136. "third_inning_score"=> $resultData['third_inning_score']?:0,//第三局比分
  137. "match_score"=> $match_score?:'',//赛事比分
  138. "match_winer_player"=> $match_winer?:'',//获胜队员
  139. "match_process"=> $match_process?:'',//比赛进程
  140. "inning"=> json_encode($inning)?:'',//每局输赢结果
  141. ];
  142. */
  143. $set_match_r[] = [
  144. "match_id"=> $v['id'],
  145. "home_player_name"=>$v['home_team'],
  146. "guest_player_name"=>$v['guest_team'],
  147. "lg_id"=>$v['lg_id'],
  148. "status"=>$v['status'],
  149. "tag"=> $v['tag'],
  150. 'match_time'=>$resultData['a_time']?:0,
  151. "ctime"=>date('Y-m-d H:i:s'),
  152. "update_time"=>date('Y-m-d H:i:s'),
  153. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  154. "home_player_let_plate"=> $resultData['home_player_let_plate']?:0, //主队让盘
  155. "guest_player_let_plate"=> $resultData['guest_player_let_plate']?:0, //客队让盘
  156. "home_player_let_inning"=> $resultData['home_player_let_inning']?:0, //主队让局
  157. "guest_player_let_inning"=> $resultData['guest_player_let_inning']?:0, //客队让局
  158. "home_player_score"=> $result_mark['game_num_H']?:0, //主队得分
  159. "guest_player_score"=> $result_mark['game_num_C']?:0, //客队得分
  160. "all_inning"=> $resultData['all_inning']?:0, //总局数
  161. // "first_score_player"=> $resultData['first_score']?:'', //最先得分
  162. // "last_score_player"=> $resultData['last_score']?:'', //最后得分
  163. "first_inning_score"=> $resultData['first_inning_score']?:'', //第一局比分
  164. "second_inning_score"=> $resultData['second_inning_score']?:'',//第二局比分
  165. "third_inning_score"=> $resultData['third_inning_score']?:'',//第三局比分
  166. "match_score"=> $match_score?:'',//赛事比分
  167. "match_winer_player"=> $match_winer?:'',//获胜队员
  168. "match_process"=> $match_process?:'',//比赛进程
  169. "inning"=> json_encode($inning)?:'',//每局输赢结果
  170. ];
  171. }
  172. }
  173. }
  174. //写入赛果1
  175. if(!empty($set_match_r)){
  176. $ret = $model['model_result']::insert($set_match_r);
  177. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  178. }
  179. return 1;
  180. }
  181. /**
  182. * 获取自动赛事结果
  183. * 只更新已结束+未手动修改的赛事结果
  184. * $match_id 本地赛事id 为0时 则用于后台手动刷新赛果
  185. */
  186. public static function WQresult_v2($model,$match_id=0){
  187. //如果有赛事id 则只更新当前赛事结果
  188. if($match_id > 0){
  189. $match_ids_str = $match_id;
  190. $match_ids = [$match_id];
  191. }else{
  192. //获取两天内的结束赛事
  193. $matchData = $model['model_match']
  194. ->join('st_wq_result','st_wq_result.match_id','=','st_wq_competition.id')
  195. ->select('st_wq_competition.id','is_correct')
  196. ->where([['st_wq_competition.match_date','>',date('Y-m-d', strtotime("-2 day"))],['st_wq_competition.status','=',2],['st_wq_result.is_correct','=',-1]])
  197. ->get()
  198. ->toArray();
  199. //没有数据,无需操作
  200. if(empty($matchData)) return Response::success();
  201. //获取需更新结果的赛事ID
  202. $match_ids = [];
  203. foreach($matchData as $k =>$v){
  204. //未手动修改比分
  205. if($v['is_correct'] == 0){
  206. $match_ids[] = $v['id'];
  207. }
  208. }
  209. if(empty($match_ids)) return Response::success();
  210. $match_ids_str = implode(",", $match_ids);
  211. }
  212. //如果有赛事id 获取结果中的手动数据
  213. if(!empty($match_ids)){
  214. $manual_result_data = self::select('match_id','manual_result')
  215. ->whereIn('match_id',$match_ids)
  216. ->get()
  217. ->toArray();
  218. }
  219. //组装sql
  220. //获取赛事最终赛果
  221. $sql_result = "select a.match_id,a.home_player_name,a.guest_player_name,a.home_player_score,a.guest_player_score,a.match_time as a_time,a.home_player_let_plate,a.guest_player_let_plate,a.home_player_let_inning,a.guest_player_let_inning,a.all_inning,a.match_process,a.first_score_player,a.last_score_player,a.first_inning_score,a.second_inning_score,a.third_inning_score,a.match_winer_player,a.result_mark from st_wq_result_record a,
  222. (select match_id,max(id) id from st_wq_result_record where match_id IN ($match_ids_str) group by match_id)b
  223. where a.match_id = b.match_id and a.id = b.id ";
  224. //赛事最终结果
  225. $match_result = DB::select($sql_result);
  226. if(!empty($match_result)){
  227. foreach($match_result as $k=>$v){
  228. //获取赛事每节的赛果
  229. $resultData = commonFunction::filter_by_value($match_result,'match_id',$v->match_id);
  230. //如果有获取到结果,并且进程不为空,则写入赛事结果数据
  231. if(!empty($resultData) and $resultData['match_process'] != ''){
  232. //获取赛果json
  233. $result_mark = json_decode($resultData['result_mark'],true);
  234. //获取进程
  235. $match_process = '';
  236. if($result_mark['schedule']){
  237. $match_process = $result_mark['schedule'];
  238. }
  239. //获胜球员
  240. $match_winer = '';
  241. if($result_mark['game_num_H'] > $result_mark['game_num_C']){
  242. $match_winer = $resultData['home_player_name'];
  243. }else{
  244. $match_winer = $resultData['guest_player_name'];
  245. }
  246. //组装输赢结果json
  247. $inning = [
  248. 1=>[
  249. "home"=>$result_mark['sc_1th_H'],
  250. "guest"=>$result_mark['sc_1th_C'],
  251. ],
  252. 2=>[
  253. "home"=>$result_mark['sc_2th_H'],
  254. "guest"=>$result_mark['sc_2th_C'],
  255. ],
  256. 3=>[
  257. "home"=>$result_mark['sc_3th_H'],
  258. "guest"=>$result_mark['sc_3th_C'],
  259. ],
  260. 4=>[
  261. "home"=>$result_mark['sc_4th_H'],
  262. "guest"=>$result_mark['sc_4th_C'],
  263. ],
  264. 5=>[
  265. "home"=>$result_mark['sc_5th_H'],
  266. "guest"=>$result_mark['sc_5th_C'],
  267. ],
  268. 'all'=>[
  269. "home"=>$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'],
  270. "guest"=>$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'],
  271. ],
  272. ];
  273. //赛事比分
  274. $match_score = $result_mark['game_num_H'].':'.$result_mark['game_num_C'];
  275. //===追加获取手动结果数据===
  276. $manual_result_json = commonFunction::getManual_result($manual_result_data,$inning,$v->match_id);
  277. //赛事待写入赛果数据
  278. $set_match_r = [
  279. "match_id"=> $v->match_id,
  280. "status"=>2,
  281. "update_time"=>date('Y-m-d H:i:s'),
  282. "home_player_let_plate"=> $resultData['home_player_let_plate']?:0, //主队让盘
  283. "guest_player_let_plate"=> $resultData['guest_player_let_plate']?:0, //客队让盘
  284. "home_player_let_inning"=> $resultData['home_player_let_inning']?:0, //主队让局
  285. "guest_player_let_inning"=> $resultData['guest_player_let_inning']?:0, //客队让局
  286. "home_player_score"=> $result_mark['game_num_H']?:0, //主队得分
  287. "guest_player_score"=> $result_mark['game_num_C']?:0, //客队得分
  288. "all_inning"=> $resultData['all_inning']?:0, //总局数
  289. // "first_score_player"=> $resultData['first_score']?:'', //最先得分
  290. // "last_score_player"=> $resultData['last_score']?:'', //最后得分
  291. "first_inning_score"=> $resultData['first_inning_score']?:'', //第一局比分
  292. "second_inning_score"=> $resultData['second_inning_score']?:'',//第二局比分
  293. "third_inning_score"=> $resultData['third_inning_score']?:'',//第三局比分
  294. "match_score"=> $match_score?:'',//赛事比分
  295. "match_winer_player"=> $match_winer?:'',//获胜队员
  296. "match_process"=> $match_process?:'',//比赛进程
  297. "inning"=> json_encode($inning)?:'',//每局输赢结果
  298. "is_correct"=> -1,//自动比分
  299. "manual_result"=> $manual_result_json,//手动结果
  300. ];
  301. $ret = $model['model_result']::where(['match_id' => $v->match_id,'is_correct'=>-1])
  302. ->update($set_match_r);
  303. // if($ret < 1) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  304. }
  305. }
  306. }
  307. return 1;
  308. }
  309. //计算滚球 赛事进行时间
  310. public static function secTime($sec=0){
  311. $min = floor($sec/60);
  312. $res = $min.':'.($sec-$min*60);
  313. return $res;
  314. }
  315. }