StZqResult.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /**
  7. * Class StZqResult
  8. * @package App\Http\Model
  9. * 足球 赛事 结果
  10. */
  11. class StZqResult extends Model
  12. {
  13. protected $table = 'st_zq_result';
  14. public $timestamps = false;
  15. /*
  16. * 写赛事结果
  17. */
  18. public static function ZQresult($model){
  19. //获取赛事表7天内所有赛事
  20. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  21. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-1 day"))]])
  22. ->get()
  23. ->toarray();
  24. //没有数据,无需操作
  25. if(empty($matchData)) return Response::success();
  26. //获取赛事结果表 所有当月
  27. $matchData_r = $model['model_result']::select('match_id')
  28. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-1 day"))]])
  29. ->get()
  30. ->toarray();
  31. //获取赛事对应结果比分
  32. //获取赛事id
  33. $match_ids = [];
  34. foreach($matchData as $k =>$v){
  35. //只获取赛事已结束的
  36. if($v['status'] == 2){
  37. $match_ids[] = $v['id'];
  38. }
  39. }
  40. $match_ids_str = implode(",", $match_ids);
  41. //上半场
  42. $sql_h = "select a.match_id,a.home_score,a.guest_score,a.match_time as a_time,a.match_process,a.all_goal,a.first_score,a.last_score,a.match_score,a.match_winer from st_zq_result_record a,
  43. (select match_id,max(id) id from st_zq_result_record where match_process = '上半场' and match_id IN ($match_ids_str) group by match_id)b
  44. where a.match_id = b.match_id and a.id = b.id ";
  45. //全场
  46. $sql_f = "select a.match_id,a.home_score,a.guest_score,a.match_time as a_time,a.match_process,a.all_goal,a.first_score,a.last_score,a.match_score,a.match_winer,a.home_rate,a.guest_rate from st_zq_result_record a,
  47. (select match_id,max(id) id from st_zq_result_record where match_id IN ($match_ids_str) group by match_id)b
  48. where a.match_id = b.match_id and a.id = b.id ";
  49. //上半场最终结果
  50. $match_result_h = DB::select($sql_h);
  51. //全场最终结果
  52. $match_result_f = DB::select($sql_f);
  53. //拼装赛事结果数据
  54. $match_result_record = [];
  55. if(!empty($match_result_h) and !empty($match_result_f)){
  56. foreach($match_result_h as $k=>$v){
  57. foreach($match_result_f as $kk=>$vv){
  58. if($v->match_id == $vv->match_id){
  59. $match_result_record[$k] = [
  60. "match_id"=>$v->match_id,
  61. "home_score"=> $vv->home_score,
  62. "guest_score"=> $vv->guest_score,
  63. "a_time"=> $vv->a_time,
  64. "match_process"=> $vv->match_process,
  65. "all_goal"=> $vv->all_goal,
  66. "first_score"=> $vv->first_score,
  67. "last_score"=> $vv->last_score,
  68. "match_score"=> $vv->match_score,
  69. "match_winer"=> $vv->match_winer,
  70. "u_home_score"=> $v->home_score,
  71. "u_guest_score"=> $v->guest_score,
  72. "home_rate"=> $vv->home_rate,
  73. "guest_rate"=> $vv->guest_rate,
  74. ];
  75. }
  76. }
  77. }
  78. }
  79. //结果表无数据,直接插入
  80. if(empty($matchData_r)){
  81. foreach ($matchData as $k=>$v){
  82. $start_time = ($v['match_date'].' '.$v['match_time']);
  83. $time = time()-strtotime($v['match_time']);
  84. $match_time = self::secTime($time);
  85. foreach($match_result_record as $kk =>$vv){
  86. if($v['id'] == $vv['match_id']){
  87. $set_match_r[] = [
  88. "match_id"=> $v['id'],
  89. "home_team"=>$v['home_team'],
  90. "guest_team"=>$v['guest_team'],
  91. "lg_id"=>$v['lg_id'],
  92. "status"=>$v['status'],
  93. "tag"=> $v['tag'],
  94. 'match_time'=>$vv['a_time']?:0,//比赛进行时间
  95. "ctime"=>date('Y-m-d H:i:s'),
  96. "update_time"=>date('Y-m-d H:i:s'),
  97. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  98. "home_rate"=> $vv['home_rate']?:0, //主队让球
  99. "guest_rate"=> $vv['guest_rate']?:0, //客队让球
  100. "home_score"=> $vv['home_score']?:0, //主队进球数
  101. "guest_score"=> $vv['guest_score']?:0, //客队进球数
  102. "all_goal"=> $vv['all_goal']?:0, //总进球数
  103. "first_score"=> $vv['first_score']?:'', //最先进球球队
  104. "last_score"=> $vv['last_score']?:'', //最后进球球队
  105. "match_score"=> $vv['match_score']?:0, //赛事比分
  106. "match_winer"=> $vv['match_winer']?:'',//获胜球队
  107. "match_process"=> $vv['match_process']?:'',//比赛进程
  108. "u_home_score"=> $vv['u_home_score']?:0,//上半场主队进球数
  109. "u_guest_score"=> $vv['u_guest_score']?:0,//上半场客队进球数
  110. ];
  111. }
  112. }
  113. }
  114. /*
  115. if(!empty($set_match_r)){
  116. $ret = $model['model_result']::insert($set_match_r);
  117. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  118. }
  119. */
  120. }else {
  121. //如果结果表有数据,则获取结果表没有的赛事
  122. foreach ($matchData as $k => $v) {
  123. foreach ($matchData_r as $kk => $vv) {
  124. if ($v['id'] == $vv['match_id']) {
  125. unset($matchData[$k]);
  126. }
  127. }
  128. }
  129. //如果还有未写入赛事
  130. if (!empty($matchData)) {
  131. //写入结果表不存在赛事
  132. foreach ($matchData as $k => $v) {
  133. $start_time = ($v['match_date'].' '.$v['match_time']);
  134. $time = time()-strtotime($v['match_time']);
  135. // $match_time = self::secTime($time);
  136. foreach($match_result_record as $kk =>$vv){
  137. if($v['id'] == $vv['match_id']){
  138. $set_match_r[] = [
  139. "match_id"=> $v['id'],
  140. "home_team"=>$v['home_team'],
  141. "guest_team"=>$v['guest_team'],
  142. "lg_id"=>$v['lg_id'],
  143. "status"=>$v['status'],
  144. "tag"=> $v['tag'],
  145. 'match_time'=>$vv['a_time']?:0,//比赛进行时间
  146. "ctime"=>date('Y-m-d H:i:s'),
  147. "update_time"=>date('Y-m-d H:i:s'),
  148. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  149. "home_rate"=> $vv['home_rate']?:0, //主队让球
  150. "guest_rate"=> $vv['guest_rate']?:0, //客队让球
  151. "home_score"=> $vv['home_score']?:0, //主队进球数
  152. "guest_score"=> $vv['guest_score']?:0, //客队进球数
  153. "all_goal"=> $vv['all_goal']?:0, //总进球数
  154. "first_score"=> $vv['first_score']?:'', //最先进球球队
  155. "last_score"=> $vv['last_score']?:'', //最后进球球队
  156. "match_score"=> $vv['match_score']?:0, //赛事比分
  157. "match_winer"=> $vv['match_winer']?:'',//获胜球队
  158. "match_process"=> $vv['match_process']?:'',//比赛进程
  159. "u_home_score"=> $vv['u_home_score']?:0,//上半场主队进球数
  160. "u_guest_score"=> $vv['u_guest_score']?:0,//上半场客队进球数
  161. ];
  162. }
  163. }
  164. }
  165. /*
  166. if(!empty($set_match_r)){
  167. $ret = $model['model_result']::insert($set_match_r);
  168. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  169. }
  170. */
  171. }
  172. }
  173. if(!empty($set_match_r)){
  174. $ret = $model['model_result']::insert($set_match_r);
  175. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  176. }
  177. return 1;
  178. }
  179. //计算滚球 赛事进行时间
  180. public static function secTime($sec=0){
  181. $min = floor($sec/60);
  182. $res = $min.':'.($sec-$min*60);
  183. return $res;
  184. }
  185. }