StZqResult.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Http\Model;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Http\Response\Response;
  5. /**
  6. * Class Account
  7. * @package App\Sports\Model
  8. * 足球结果
  9. */
  10. class StZqResult extends Model
  11. {
  12. protected $table = 'st_zq_result';
  13. public $timestamps = false;
  14. //写赛事结果
  15. public static function ZQresult($model){
  16. //获取赛事表15天内所有非 未开始赛事
  17. $matchData = $model['model_match']::select('id','match_id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  18. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-7 day"))]])
  19. ->get()
  20. ->toarray();
  21. //没有数据,无需操作
  22. if(empty($matchData)) return Response::success();
  23. //获取赛事结果表 所有当月
  24. $matchData_r = $model['model_result']::select('match_id')
  25. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-7 day"))]])
  26. ->get()
  27. ->toarray();
  28. //结果表无数据,直接插入
  29. if(empty($matchData_r)){
  30. foreach ($matchData as $k=>$v){
  31. $start_time = ($v['match_date'].' '.$v['match_time']);
  32. $time = time()-strtotime($v['match_time']);
  33. $match_time = self::secTime($time);
  34. $set_match_r[] = [
  35. "match_id"=> $v['match_id'],
  36. "home_team"=>$v['home_team'],
  37. "guest_team"=>$v['guest_team'],
  38. "lg_id"=>$v['lg_id'],
  39. "status"=>$v['status'],
  40. "tag"=> $v['tag'],
  41. 'match_time'=>$match_time,
  42. "ctime"=>date('Y-m-d H:i:s'),
  43. "update_time"=>date('Y-m-d H:i:s'),
  44. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time))
  45. ];
  46. }
  47. $ret = $model['model_result']::insert($set_match_r);
  48. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  49. }else {
  50. //如果结果表有数据,则获取结果表没有的赛事
  51. foreach ($matchData as $k => $v) {
  52. foreach ($matchData_r as $kk => $vv) {
  53. if ($v['match_id'] == $vv['match_id']) {
  54. unset($matchData[$k]);
  55. }
  56. }
  57. }
  58. //如果还有未写入赛事
  59. if (!empty($matchData)) {
  60. //写入结果表不存在赛事
  61. foreach ($matchData as $k => $v) {
  62. $start_time = ($v['match_date'] . ' ' . $v['match_time']);
  63. $time = time() - strtotime($v['match_time']);
  64. $match_time = self::secTime($time);
  65. $set_match_r[] = [
  66. "match_id" => $v['match_id'],
  67. "home_team" => $v['home_team'],
  68. "guest_team" => $v['guest_team'],
  69. "lg_id" => $v['lg_id'],
  70. "status" => $v['status'],
  71. "tag" => $v['tag'],
  72. 'match_time' => $match_time,
  73. "ctime" => date('Y-m-d H:i:s'),
  74. "update_time" => date('Y-m-d H:i:s'),
  75. "start_time" => date('Y-m-d H:i:s', strtotime($start_time))
  76. ];
  77. }
  78. $ret = $model['model_result']::insert($set_match_r);
  79. if ($ret != true) throw new \Exception(Response::generate('', Response::ADD_MATCH_R_ERROR));//Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
  80. }
  81. }
  82. return Response::success();
  83. }
  84. //计算滚球 赛事进行时间
  85. public static function secTime($sec=0){
  86. $min = floor($sec/60);
  87. $res = $min.':'.($sec-$min*60);
  88. return $res;
  89. }
  90. }