StWqResult.php 21 KB

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