StWqResult.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. /*
  17. * 写赛事结果
  18. * 弃用
  19. */
  20. public static function WQresult__($model){
  21. //获取赛事表7天内所有赛事
  22. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  23. ->where([['match_date','>',date('Y-m-d',strtotime("-2 day"))],['status','=',2]])
  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_player_name"=>$v['home_team'],
  42. "guest_player_name"=>$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_player_name" => $v['home_team'],
  73. "guest_player_name" => $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 WQresult_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. //获取赛事最终赛果
  122. $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,
  123. (select match_id,max(id) id from st_wq_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. $ss = [];
  130. if(!empty($match_result)){
  131. foreach($matchData as $k =>$v ){
  132. //获取开赛时间
  133. $start_time = ($v['match_date'].' '.$v['match_time']);
  134. $time = time()-strtotime($v['match_time']);
  135. $match_time = self::secTime($time);
  136. //获取赛事每节的赛果
  137. $resultData = commonFunction::filter_by_value($match_result,'match_id',$v['id']);
  138. //如果有获取到结果,并且进程不为空,则写入赛事结果数据
  139. if(!empty($resultData) and $resultData['match_process'] != ''){
  140. //获取赛果json
  141. $result_mark = json_decode($resultData['result_mark'],true);
  142. $sss[] = $result_mark;
  143. //获取进程
  144. $match_process = '';
  145. if($result_mark['schedule']){
  146. $match_process = $result_mark['schedule'];
  147. }
  148. //获胜球员
  149. $match_winer = '';
  150. if($result_mark['game_num_H'] > $result_mark['game_num_C']){
  151. $match_winer = $v['home_team'];
  152. }else{
  153. $match_winer = $v['guest_team'];
  154. }
  155. //组装输赢结果json
  156. $inning = [
  157. 1=>[
  158. "home"=>$result_mark['sc_1th_H'],
  159. "guest"=>$result_mark['sc_1th_C'],
  160. ],
  161. 2=>[
  162. "home"=>$result_mark['sc_2th_H'],
  163. "guest"=>$result_mark['sc_2th_C'],
  164. ],
  165. 3=>[
  166. "home"=>$result_mark['sc_3th_H'],
  167. "guest"=>$result_mark['sc_3th_C'],
  168. ],
  169. 4=>[
  170. "home"=>$result_mark['sc_4th_H'],
  171. "guest"=>$result_mark['sc_4th_C'],
  172. ],
  173. 5=>[
  174. "home"=>$result_mark['sc_5th_H'],
  175. "guest"=>$result_mark['sc_5th_C'],
  176. ],
  177. 'all'=>[
  178. "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'],
  179. "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'],
  180. ],
  181. ];
  182. //赛事比分
  183. $match_score = $result_mark['game_num_H'].':'.$result_mark['game_num_C'];
  184. //赛事待写入赛果数据
  185. /*
  186. $set_match_r[] = [
  187. "match_id"=> $v['id'],
  188. "home_player_name"=>$v['home_team'],
  189. "guest_player_name"=>$v['guest_team'],
  190. "lg_id"=>$v['lg_id'],
  191. "status"=>$v['status'],
  192. "tag"=> $v['tag'],
  193. 'match_time'=>$resultData['a_time']?:0,
  194. "ctime"=>date('Y-m-d H:i:s'),
  195. "update_time"=>date('Y-m-d H:i:s'),
  196. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  197. "home_player_let_plate"=> $resultData['home_player_let_plate']?:0, //主队让盘
  198. "guest_player_let_plate"=> $resultData['guest_player_let_plate']?:0, //客队让盘
  199. "home_player_let_inning"=> $resultData['home_player_let_inning']?:0, //主队让局
  200. "guest_player_let_inning"=> $resultData['guest_player_let_inning']?:0, //客队让局
  201. "home_player_score"=> $result_mark['game_num_H']?:'', //主队得分
  202. "guest_player_score"=> $result_mark['game_num_C']?:'', //客队得分
  203. "all_inning"=> $resultData['all_inning']?:0, //总局数
  204. // "first_score_player"=> $resultData['first_score']?:'', //最先得分
  205. // "last_score_player"=> $resultData['last_score']?:'', //最后得分
  206. "first_inning_score"=> $resultData['first_inning_score']?:0, //第一局比分
  207. "second_inning_score"=> $resultData['second_inning_score']?:0,//第二局比分
  208. "third_inning_score"=> $resultData['third_inning_score']?:0,//第三局比分
  209. "match_score"=> $match_score?:'',//赛事比分
  210. "match_winer_player"=> $match_winer?:'',//获胜队员
  211. "match_process"=> $match_process?:'',//比赛进程
  212. "inning"=> json_encode($inning)?:'',//每局输赢结果
  213. ];
  214. */
  215. $set_match_r[] = [
  216. "match_id"=> $v['id'],
  217. "home_player_name"=>$v['home_team'],
  218. "guest_player_name"=>$v['guest_team'],
  219. "lg_id"=>$v['lg_id'],
  220. "status"=>$v['status'],
  221. "tag"=> $v['tag'],
  222. 'match_time'=>$resultData['a_time']?:0,
  223. "ctime"=>date('Y-m-d H:i:s'),
  224. "update_time"=>date('Y-m-d H:i:s'),
  225. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  226. "home_player_let_plate"=> $resultData['home_player_let_plate']?:0, //主队让盘
  227. "guest_player_let_plate"=> $resultData['guest_player_let_plate']?:0, //客队让盘
  228. "home_player_let_inning"=> $resultData['home_player_let_inning']?:0, //主队让局
  229. "guest_player_let_inning"=> $resultData['guest_player_let_inning']?:0, //客队让局
  230. "home_player_score"=> $result_mark['game_num_H']?:0, //主队得分
  231. "guest_player_score"=> $result_mark['game_num_C']?:0, //客队得分
  232. "all_inning"=> $resultData['all_inning']?:0, //总局数
  233. // "first_score_player"=> $resultData['first_score']?:'', //最先得分
  234. // "last_score_player"=> $resultData['last_score']?:'', //最后得分
  235. "first_inning_score"=> $resultData['first_inning_score']?:'', //第一局比分
  236. "second_inning_score"=> $resultData['second_inning_score']?:'',//第二局比分
  237. "third_inning_score"=> $resultData['third_inning_score']?:'',//第三局比分
  238. "match_score"=> $match_score?:'',//赛事比分
  239. "match_winer_player"=> $match_winer?:'',//获胜队员
  240. "match_process"=> $match_process?:'',//比赛进程
  241. "inning"=> json_encode($inning)?:'',//每局输赢结果
  242. ];
  243. }
  244. }
  245. }
  246. //写入赛果1
  247. if(!empty($set_match_r)){
  248. $ret = $model['model_result']::insert($set_match_r);
  249. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  250. }
  251. return 1;
  252. }
  253. /**
  254. * 获取自动赛事结果
  255. * 只更新已结束+未手动修改的赛事结果
  256. * $match_id 本地赛事id 为0时 则用于后台手动刷新赛果
  257. */
  258. public static function WQresult_v2($model,$match_id=0){
  259. //如果有赛事id 则只更新当前赛事结果
  260. if($match_id > 0){
  261. $match_ids_str = $match_id;
  262. }else{
  263. //获取两天内的结束赛事
  264. $matchData = $model['model_match']
  265. ->join('st_wq_result','st_wq_result.match_id','=','st_wq_competition.id')
  266. ->select('st_wq_competition.id','is_correct')
  267. ->where([['st_wq_competition.match_date','>',date('Y-m-d', strtotime("-2 day"))],['st_wq_competition.status','=',2],['st_wq_result.is_correct','=',-1]])
  268. ->get()
  269. ->toArray();
  270. //没有数据,无需操作
  271. if(empty($matchData)) return Response::success();
  272. //获取需更新结果的赛事ID
  273. $match_ids = [];
  274. foreach($matchData as $k =>$v){
  275. //未手动修改比分
  276. if($v['is_correct'] == 0){
  277. $match_ids[] = $v['id'];
  278. }
  279. }
  280. if(empty($match_ids)) return Response::success();
  281. $match_ids_str = implode(",", $match_ids);
  282. }
  283. //组装sql
  284. //获取赛事最终赛果
  285. $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,
  286. (select match_id,max(id) id from st_wq_result_record where match_id IN ($match_ids_str) group by match_id)b
  287. where a.match_id = b.match_id and a.id = b.id ";
  288. //赛事最终结果
  289. $match_result = DB::select($sql_result);
  290. if(!empty($match_result)){
  291. foreach($match_result as $k=>$v){
  292. //获取赛事每节的赛果
  293. $resultData = commonFunction::filter_by_value($match_result,'match_id',$v->match_id);
  294. //如果有获取到结果,并且进程不为空,则写入赛事结果数据
  295. if(!empty($resultData) and $resultData['match_process'] != ''){
  296. //获取赛果json
  297. $result_mark = json_decode($resultData['result_mark'],true);
  298. //获取进程
  299. $match_process = '';
  300. if($result_mark['schedule']){
  301. $match_process = $result_mark['schedule'];
  302. }
  303. //获胜球员
  304. $match_winer = '';
  305. if($result_mark['game_num_H'] > $result_mark['game_num_C']){
  306. $match_winer = $resultData['home_player_name'];
  307. }else{
  308. $match_winer = $resultData['guest_player_name'];
  309. }
  310. //组装输赢结果json
  311. $inning = [
  312. 1=>[
  313. "home"=>$result_mark['sc_1th_H'],
  314. "guest"=>$result_mark['sc_1th_C'],
  315. ],
  316. 2=>[
  317. "home"=>$result_mark['sc_2th_H'],
  318. "guest"=>$result_mark['sc_2th_C'],
  319. ],
  320. 3=>[
  321. "home"=>$result_mark['sc_3th_H'],
  322. "guest"=>$result_mark['sc_3th_C'],
  323. ],
  324. 4=>[
  325. "home"=>$result_mark['sc_4th_H'],
  326. "guest"=>$result_mark['sc_4th_C'],
  327. ],
  328. 5=>[
  329. "home"=>$result_mark['sc_5th_H'],
  330. "guest"=>$result_mark['sc_5th_C'],
  331. ],
  332. 'all'=>[
  333. "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'],
  334. "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'],
  335. ],
  336. ];
  337. //赛事比分
  338. $match_score = $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_player_let_plate"=> $resultData['home_player_let_plate']?:0, //主队让盘
  345. "guest_player_let_plate"=> $resultData['guest_player_let_plate']?:0, //客队让盘
  346. "home_player_let_inning"=> $resultData['home_player_let_inning']?:0, //主队让局
  347. "guest_player_let_inning"=> $resultData['guest_player_let_inning']?:0, //客队让局
  348. "home_player_score"=> $result_mark['game_num_H']?:0, //主队得分
  349. "guest_player_score"=> $result_mark['game_num_C']?:0, //客队得分
  350. "all_inning"=> $resultData['all_inning']?:0, //总局数
  351. // "first_score_player"=> $resultData['first_score']?:'', //最先得分
  352. // "last_score_player"=> $resultData['last_score']?:'', //最后得分
  353. "first_inning_score"=> $resultData['first_inning_score']?:'', //第一局比分
  354. "second_inning_score"=> $resultData['second_inning_score']?:'',//第二局比分
  355. "third_inning_score"=> $resultData['third_inning_score']?:'',//第三局比分
  356. "match_score"=> $match_score?:'',//赛事比分
  357. "match_winer_player"=> $match_winer?:'',//获胜队员
  358. "match_process"=> $match_process?:'',//比赛进程
  359. "inning"=> json_encode($inning)?:'',//每局输赢结果
  360. "is_correct"=> -1,//自动比分
  361. ];
  362. $ret = $model['model_result']::where(['match_id' => $v->match_id,'is_correct'=>-1])
  363. ->update($set_match_r);
  364. // if($ret < 1) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  365. }
  366. }
  367. }
  368. return 1;
  369. }
  370. //计算滚球 赛事进行时间
  371. public static function secTime($sec=0){
  372. $min = floor($sec/60);
  373. $res = $min.':'.($sec-$min*60);
  374. return $res;
  375. }
  376. }