StLqResult.php 10 KB

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