StLqResult.php 3.9 KB

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