StWqResult.php 20 KB

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