StWqResult.php 20 KB

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