StLqResult.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 StLqResult
  9. * @package App\Http\Model
  10. * 篮球 赛事 结果
  11. */
  12. class StLqResult extends Model
  13. {
  14. protected $table = 'st_lq_result';
  15. public $timestamps = false;
  16. /*
  17. * 写赛事结果
  18. * 弃用
  19. */
  20. public static function LQresult___($model){
  21. dd(123);
  22. //获取赛事表7天内所有赛事
  23. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  24. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-1 day"))]])
  25. ->get()
  26. ->toarray();
  27. //没有数据,无需操作
  28. if(empty($matchData)) return Response::success();
  29. //获取赛事结果表 15天内
  30. $matchData_r = $model['model_result']::select('match_id')
  31. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-1 day"))]])
  32. ->get()
  33. ->toarray();
  34. //结果表无数据,直接插入
  35. if(empty($matchData_r)){
  36. foreach ($matchData as $k=>$v){
  37. $process = 0;
  38. if((int)$v['status'] == 2){
  39. $process = 4;
  40. }
  41. if((int)$v['status'] == 1){
  42. $process = 1;
  43. }
  44. $start_time = ($v['match_date'].' '.$v['match_time']);
  45. $set_match_r[] = [
  46. "match_id"=> $v['id'],
  47. "home_team"=>$v['home_team'],
  48. "guest_team"=>$v['guest_team'],
  49. "lg_id"=>$v['lg_id'],
  50. "status"=>$v['status'],
  51. "tag"=> $v['tag'],
  52. "match_process"=>$process,
  53. 'match_time'=>12,
  54. "ctime"=>date('Y-m-d H:i:s'),
  55. "update_time"=>date('Y-m-d H:i:s'),
  56. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time))
  57. ];
  58. }
  59. $ret = $model['model_result']::insert($set_match_r);
  60. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  61. }else{
  62. //如果结果表有数据,则获取结果表没有的赛事
  63. foreach ($matchData as $k=>$v){
  64. foreach ($matchData_r as $kk=>$vv){
  65. if($v['id'] == $vv['match_id']){
  66. unset($matchData[$k]);
  67. }
  68. }
  69. }
  70. //如果还有未写入赛事
  71. if(!empty($matchData)){
  72. //写入结果表不存在赛事
  73. foreach ($matchData as $k=>$v){
  74. $process = 0;
  75. if((int)$v['status'] == 2){
  76. $process = 4;
  77. }
  78. if((int)$v['status'] == 1){
  79. $process = 1;
  80. }
  81. $start_time = ($v['match_date'].' '.$v['match_time']);
  82. $set_match_r[] = [
  83. "match_id"=> $v['id'],
  84. "home_team"=>$v['home_team'],
  85. "guest_team"=>$v['guest_team'],
  86. "lg_id"=>$v['lg_id'],
  87. "status"=>$v['status'],
  88. "tag"=> $v['tag'],
  89. "match_process"=>$process,
  90. 'match_time'=>12,
  91. "ctime"=>date('Y-m-d H:i:s'),
  92. "update_time"=>date('Y-m-d H:i:s'),
  93. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time))
  94. ];
  95. }
  96. $ret = $model['model_result']::insert($set_match_r);
  97. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));//Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
  98. }
  99. }
  100. }
  101. /*
  102. * 写赛事结果
  103. */
  104. public static function LQresult_v1($model){
  105. //获取当天开始并且已结束的所有赛事
  106. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  107. ->where([['match_date','>',date('Y-m-d',strtotime("-2 day"))],['status','=',2]])
  108. ->get()
  109. ->toArray();
  110. //获取赛事结果表 一天内
  111. $matchData_r = $model['model_result']::select('match_id')
  112. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-2 day"))]])
  113. ->get()
  114. ->toArray();
  115. if(!empty($matchData_r)){
  116. //如果结果表有数据,则获取结果表没有的赛事
  117. foreach ($matchData as $k => $v) {
  118. foreach ($matchData_r as $kk => $vv) {
  119. if ($v['id'] == $vv['match_id']) {
  120. unset($matchData[$k]);
  121. }
  122. }
  123. }
  124. }
  125. //没有数据,无需操作
  126. if(empty($matchData)) return Response::success();
  127. //获取赛事id 用于获取赛事对应结果比分
  128. $match_ids = [];
  129. foreach($matchData as $k =>$v){
  130. //只获取赛事已结束的
  131. if($v['status'] == 2){
  132. $match_ids[] = $v['id'];
  133. }
  134. }
  135. $match_ids_str = implode(",", $match_ids);
  136. //第一节
  137. $sql_1 = "select a.match_id,a.home_score,a.guest_score,a.match_time as a_time,a.home_rate,a.guest_rate,a.match_process,a.first_score,a.last_score,a.match_score,a.match_winer from st_lq_result_record a,
  138. (select match_id,max(id) id from st_lq_result_record where match_process = '第一节' and match_id IN ($match_ids_str) group by match_id)b
  139. where a.match_id = b.match_id and a.id = b.id ";
  140. //第二节
  141. $sql_2 = "select a.match_id,a.home_score,a.guest_score,a.match_time as a_time,a.home_rate,a.guest_rate,a.match_process,a.first_score,a.last_score,a.match_score,a.match_winer from st_lq_result_record a,
  142. (select match_id,max(id) id from st_lq_result_record where match_process = '第二节' and match_id IN ($match_ids_str) group by match_id)b
  143. where a.match_id = b.match_id and a.id = b.id ";
  144. //第三节
  145. $sql_3 = "select a.match_id,a.home_score,a.guest_score,a.match_time as a_time,a.home_rate,a.guest_rate,a.match_process,a.first_score,a.last_score,a.match_score,a.match_winer from st_lq_result_record a,
  146. (select match_id,max(id) id from st_lq_result_record where match_process = '第三节' and match_id IN ($match_ids_str) group by match_id)b
  147. where a.match_id = b.match_id and a.id = b.id ";
  148. //第四节
  149. $sql_4 = "select a.match_id,a.home_score,a.guest_score,a.match_time as a_time,a.home_rate,a.guest_rate,a.match_process,a.first_score,a.last_score,a.match_score,a.match_winer from st_lq_result_record a,
  150. (select match_id,max(id) id from st_lq_result_record where match_process = '第四节' and match_id IN ($match_ids_str) group by match_id)b
  151. where a.match_id = b.match_id and a.id = b.id ";
  152. //第一节最终结果
  153. $match_result_1 = DB::select($sql_1);
  154. //第二节最终结果
  155. $match_result_2 = DB::select($sql_2);
  156. //第三节最终结果
  157. $match_result_3 = DB::select($sql_3);
  158. //第四节最终结果
  159. $match_result_4 = DB::select($sql_4);
  160. //拼装赛事结果数据
  161. $set_match_r = [];
  162. if(!empty($match_result_1) and !empty($match_result_2) and !empty($match_result_3) and !empty($match_result_4)){
  163. foreach($matchData as $k =>$v ){
  164. //获取开赛时间
  165. $start_time = ($v['match_date'].' '.$v['match_time']);
  166. $time = time()-strtotime($v['match_time']);
  167. $match_time = self::secTime($time);
  168. //获取赛事每节的赛果
  169. $result_1 = commonFunction::filter_by_value($match_result_1,'match_id',$v['id']);
  170. $result_2 = commonFunction::filter_by_value($match_result_2,'match_id',$v['id']);
  171. $result_3 = commonFunction::filter_by_value($match_result_3,'match_id',$v['id']);
  172. $result_4 = commonFunction::filter_by_value($match_result_4,'match_id',$v['id']);
  173. if(!empty($result_1) and !empty($result_2) and !empty($result_3) and !empty($result_4)){
  174. //拼接赛事主队比分 {"1":0,"2":0,"3":0,"4":0}
  175. $home_score = [
  176. '1'=>$result_1['home_score'],
  177. '2'=>$result_2['home_score'] - $result_1['home_score'],
  178. '3'=>$result_3['home_score'] - $result_2['home_score'],
  179. '4'=>$result_4['home_score'] - $result_3['home_score'],
  180. ];
  181. //拼接赛事客队比分
  182. $guest_score = [
  183. '1'=>$result_1['guest_score'],
  184. '2'=>$result_2['guest_score'] - $result_1['guest_score'],
  185. '3'=>$result_3['guest_score'] - $result_2['guest_score'],
  186. '4'=>$result_4['guest_score'] - $result_3['guest_score'],
  187. ];
  188. //获取获胜球队
  189. if(($result_4['home_score']) > ($result_4['guest_score'])){
  190. $match_winer = $v['home_team'];
  191. }else{
  192. $match_winer = $v['guest_team'];
  193. }
  194. //获取总进球数
  195. $all_goal = ($result_4['home_score'])+($result_4['guest_score']);
  196. //赛事待写入赛果数据
  197. $set_match_r[] = [
  198. "match_id"=> $v['id'],
  199. "home_team"=>$v['home_team'],
  200. "guest_team"=>$v['guest_team'],
  201. "lg_id"=>$v['lg_id'],
  202. "status"=>$v['status'],
  203. "tag"=> $v['tag'],
  204. 'match_time'=>$result_4['a_time']?:0,//比赛进行时间
  205. "ctime"=>date('Y-m-d H:i:s'),
  206. "update_time"=>date('Y-m-d H:i:s'),
  207. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  208. "home_rate"=> $result_4['home_rate']?:0, //主队让球
  209. "guest_rate"=> $result_4['guest_rate']?:0, //客队让球
  210. "home_score"=> json_encode($home_score)?:'', //主队进球数
  211. "guest_score"=> json_encode($guest_score)?:'', //客队进球数
  212. "all_goal"=> $all_goal?:0, //总进球数
  213. // "first_score"=> $result_4['first_score']?:'', //最先进球球队
  214. // "last_score"=> $result_4['last_score']?:'', //最后进球球队
  215. "match_score"=> $result_4['match_score']?:0, //赛事比分
  216. "match_winer"=> $match_winer?:'',//获胜球队
  217. "match_process"=> $result_4['match_process']?:'',//比赛进程
  218. "u_home_score"=> $result_2['home_score']?:0,//上半场主队进球数
  219. "u_guest_score"=> $result_2['guest_score']?:0,//上半场客队进球数
  220. ];
  221. }
  222. }
  223. }
  224. //写入赛果
  225. if(!empty($set_match_r)){
  226. $ret = $model['model_result']::insert($set_match_r);
  227. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  228. }
  229. return 1;
  230. }
  231. /**
  232. * 获取自动赛事结果
  233. * 只更新已结束+未手动修改的赛事结果
  234. * $match_id 本地赛事id 为0时 则用于后台手动刷新赛果
  235. */
  236. public static function LQresult_v2($model,$match_id=0){
  237. //如果有赛事id 则只更新当前赛事结果
  238. if($match_id > 0){
  239. $match_ids_str = $match_id;
  240. }else{
  241. //获取两天内的结束赛事
  242. $matchData = $model['model_match']
  243. ->join('st_lq_result','st_lq_result.match_id','=','st_lq_competition.id')
  244. ->select('st_lq_competition.id','is_correct')
  245. ->where([['st_lq_competition.match_date','>',date('Y-m-d', strtotime("-2 day"))],['st_lq_competition.status','=',2],['st_lq_result.is_correct','=',-1]])
  246. ->get()
  247. ->toArray();
  248. //没有数据,无需操作
  249. if(empty($matchData)) return Response::success();
  250. //获取需更新结果的赛事ID
  251. $match_ids = [];
  252. foreach($matchData as $k =>$v){
  253. //未手动修改比分
  254. if($v['is_correct'] == 0){
  255. $match_ids[] = $v['id'];
  256. }
  257. }
  258. if(empty($match_ids)) return Response::success();
  259. $match_ids_str = implode(",", $match_ids);
  260. }
  261. //拼接sql
  262. $sql_result = "select a.match_id,a.home_team,a.guest_team,a.home_score,a.guest_score,a.match_time as a_time,a.home_rate,a.guest_rate,a.match_process,a.first_score,a.last_score,a.match_score,a.match_winer,a.result_mark from st_lq_result_record a,
  263. (select match_id,max(id) id from st_lq_result_record where match_id IN ($match_ids_str) group by match_id)b
  264. where a.match_id = b.match_id and a.id = b.id ";
  265. //最终结果
  266. $match_result = DB::select($sql_result);
  267. if(!empty($match_result)){
  268. foreach($match_result as $k=>$v){
  269. //获取结果json
  270. $mark_result = json_decode($v->result_mark,true);
  271. //如果为空,设默认值
  272. if($mark_result == []){
  273. $mark_result_json = '{"schedule":"ot","r_time":"00:00","sc_1th_H":0,"sc_1th_C":0,"sc_2th_H":0,"sc_2th_C":0,"sc_3th_H":0,"sc_3th_C":0,"sc_4th_H":0,"sc_4th_C":0,"overtime_H":0,"overtime_C":0,"half_H":0,"half_C":0,"full_H":0,"full_C":0,"whole_H":0,"whole_C":0}';
  274. $mark_result = json_decode($mark_result_json,true);
  275. }
  276. //获取总进球数
  277. $all_goal = $mark_result['whole_H']+$mark_result['whole_C'];
  278. //获取比分
  279. $match_score = $mark_result['whole_H'].':'.$mark_result['whole_C'];
  280. //获取获胜球队
  281. if(($mark_result['whole_H']) > ($mark_result['whole_C'])){
  282. $match_winer = $v->home_team;
  283. }else{
  284. $match_winer = $v->guest_team;
  285. }
  286. //拼接赛事主队比分 {"1":0,"2":0,"3":0,"4":0}
  287. $home_score = [
  288. '1'=>$mark_result['sc_1th_H'],
  289. '2'=>$mark_result['sc_2th_H'],
  290. '3'=>$mark_result['sc_3th_H'],
  291. '4'=>$mark_result['sc_4th_H'],
  292. ];
  293. //拼接赛事客队比分
  294. $guest_score = [
  295. '1'=>$mark_result['sc_1th_C'],
  296. '2'=>$mark_result['sc_2th_C'],
  297. '3'=>$mark_result['sc_3th_C'],
  298. '4'=>$mark_result['sc_4th_C'],
  299. ];
  300. //拼接赛事主客队比分
  301. $inning = [
  302. 1=>[
  303. "home"=>$mark_result['sc_1th_H'],
  304. "guest"=>$mark_result['sc_1th_C'],
  305. ],
  306. 2=>[
  307. "home"=>$mark_result['sc_2th_H'],
  308. "guest"=>$mark_result['sc_2th_C'],
  309. ],
  310. 3=>[
  311. "home"=>$mark_result['sc_3th_H'],
  312. "guest"=>$mark_result['sc_3th_C'],
  313. ],
  314. 4=>[
  315. "home"=>$mark_result['sc_4th_H'],
  316. "guest"=>$mark_result['sc_4th_C'],
  317. ],
  318. 5=>[
  319. "home"=>$mark_result['sc_1th_H']+$mark_result['sc_2th_H'],
  320. "guest"=>$mark_result['sc_1th_C']+$mark_result['sc_2th_C'],
  321. ],
  322. 6=>[
  323. "home"=>$mark_result['sc_3th_H']+$mark_result['sc_4th_H'],
  324. "guest"=>$mark_result['sc_3th_C']+$mark_result['sc_4th_C'],
  325. ],
  326. 'all'=>[
  327. "home"=>$mark_result['sc_1th_H']+$mark_result['sc_2th_H']+$mark_result['sc_3th_H']+$mark_result['sc_4th_H'],
  328. "guest"=>$mark_result['sc_1th_C']+$mark_result['sc_2th_C']+$mark_result['sc_3th_C']+$mark_result['sc_4th_C'],
  329. ],
  330. ];
  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_rate"=> $v->home_rate?:0, //主队让球
  337. "guest_rate"=> $v->guest_rate?:0, //客队让球
  338. "home_score"=> json_encode($home_score)?:'', //主队进球数
  339. "guest_score"=> json_encode($guest_score)?:'', //客队进球数
  340. "inning"=> json_encode($inning)?:'', //主客队进球数
  341. "all_goal"=> $all_goal?:0, //总进球数
  342. // "first_score"=> $result_4['first_score']?:'', //最先进球球队
  343. // "last_score"=> $result_4['last_score']?:'', //最后进球球队
  344. "match_score"=> $match_score?:0, //赛事比分
  345. "match_winer"=> $match_winer?:'',//获胜球队
  346. "match_process"=> $mark_result['schedule']?:'',//比赛进程
  347. "u_home_score"=> $mark_result['half_H']?:0,//上半场主队进球数
  348. "u_guest_score"=> $mark_result['half_C']?:0,//上半场客队进球数
  349. "is_correct"=> -1,//自动比分
  350. ];
  351. $ret = $model['model_result']::where(['match_id' => $v->match_id,'is_correct'=>-1])
  352. ->update($set_match_r);
  353. // if($ret < 1) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  354. }
  355. }
  356. return 1;
  357. }
  358. //计算滚球 赛事进行时间
  359. public static function secTime($sec=0){
  360. $min = floor($sec/60);
  361. $res = $min.':'.($sec-$min*60);
  362. return $res;
  363. }
  364. }