StLqResult.php 15 KB

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