StZqResult.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. /**
  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. */
  19. public static function ZQresult__($model){
  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. //获取赛事结果表 所有当月
  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. //获取赛事id
  34. $match_ids = [];
  35. foreach($matchData as $k =>$v){
  36. //只获取赛事已结束的
  37. if($v['status'] == 2){
  38. $match_ids[] = $v['id'];
  39. }
  40. }
  41. $match_ids_str = implode(",", $match_ids);
  42. //上半场
  43. $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,
  44. (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
  45. where a.match_id = b.match_id and a.id = b.id ";
  46. //全场
  47. $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,
  48. (select match_id,max(id) id from st_zq_result_record where match_id IN ($match_ids_str) group by match_id)b
  49. where a.match_id = b.match_id and a.id = b.id ";
  50. //上半场最终结果
  51. $match_result_h = DB::select($sql_h);
  52. //全场最终结果
  53. $match_result_f = DB::select($sql_f);
  54. //拼装赛事结果数据
  55. $match_result_record = [];
  56. if(!empty($match_result_h) and !empty($match_result_f)){
  57. foreach($match_result_h as $k=>$v){
  58. foreach($match_result_f as $kk=>$vv){
  59. if($v->match_id == $vv->match_id){
  60. $match_result_record[$k] = [
  61. "match_id"=>$v->match_id,
  62. "home_score"=> $vv->home_score,
  63. "guest_score"=> $vv->guest_score,
  64. "a_time"=> $vv->a_time,
  65. "match_process"=> $vv->match_process,
  66. "all_goal"=> $vv->all_goal,
  67. "first_score"=> $vv->first_score,
  68. "last_score"=> $vv->last_score,
  69. "match_score"=> $vv->match_score,
  70. "match_winer"=> $vv->match_winer,
  71. "u_home_score"=> $v->home_score,
  72. "u_guest_score"=> $v->guest_score,
  73. "home_rate"=> $vv->home_rate,
  74. "guest_rate"=> $vv->guest_rate,
  75. ];
  76. }
  77. }
  78. }
  79. }
  80. //结果表无数据,直接插入
  81. if(empty($matchData_r)){
  82. foreach ($matchData as $k=>$v){
  83. $start_time = ($v['match_date'].' '.$v['match_time']);
  84. $time = time()-strtotime($v['match_time']);
  85. $match_time = self::secTime($time);
  86. foreach($match_result_record as $kk =>$vv){
  87. if($v['id'] == $vv['match_id']){
  88. $set_match_r[] = [
  89. "match_id"=> $v['id'],
  90. "home_team"=>$v['home_team'],
  91. "guest_team"=>$v['guest_team'],
  92. "lg_id"=>$v['lg_id'],
  93. "status"=>$v['status'],
  94. "tag"=> $v['tag'],
  95. 'match_time'=>$vv['a_time']?:0,//比赛进行时间
  96. "ctime"=>date('Y-m-d H:i:s'),
  97. "update_time"=>date('Y-m-d H:i:s'),
  98. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  99. "home_rate"=> $vv['home_rate']?:0, //主队让球
  100. "guest_rate"=> $vv['guest_rate']?:0, //客队让球
  101. "home_score"=> $vv['home_score']?:0, //主队进球数
  102. "guest_score"=> $vv['guest_score']?:0, //客队进球数
  103. "all_goal"=> $vv['all_goal']?:0, //总进球数
  104. "first_score"=> $vv['first_score']?:'', //最先进球球队
  105. "last_score"=> $vv['last_score']?:'', //最后进球球队
  106. "match_score"=> $vv['match_score']?:0, //赛事比分
  107. "match_winer"=> $vv['match_winer']?:'',//获胜球队
  108. "match_process"=> $vv['match_process']?:'',//比赛进程
  109. "u_home_score"=> $vv['u_home_score']?:0,//上半场主队进球数
  110. "u_guest_score"=> $vv['u_guest_score']?:0,//上半场客队进球数
  111. ];
  112. }
  113. }
  114. }
  115. /*
  116. if(!empty($set_match_r)){
  117. $ret = $model['model_result']::insert($set_match_r);
  118. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  119. }
  120. */
  121. }else {
  122. //如果结果表有数据,则获取结果表没有的赛事
  123. foreach ($matchData as $k => $v) {
  124. foreach ($matchData_r as $kk => $vv) {
  125. if ($v['id'] == $vv['match_id']) {
  126. unset($matchData[$k]);
  127. }
  128. }
  129. }
  130. //如果还有未写入赛事
  131. if (!empty($matchData)) {
  132. //写入结果表不存在赛事
  133. foreach ($matchData as $k => $v) {
  134. $start_time = ($v['match_date'].' '.$v['match_time']);
  135. $time = time()-strtotime($v['match_time']);
  136. // $match_time = self::secTime($time);
  137. foreach($match_result_record as $kk =>$vv){
  138. if($v['id'] == $vv['match_id']){
  139. $set_match_r[] = [
  140. "match_id"=> $v['id'],
  141. "home_team"=>$v['home_team'],
  142. "guest_team"=>$v['guest_team'],
  143. "lg_id"=>$v['lg_id'],
  144. "status"=>$v['status'],
  145. "tag"=> $v['tag'],
  146. 'match_time'=>$vv['a_time']?:0,//比赛进行时间
  147. "ctime"=>date('Y-m-d H:i:s'),
  148. "update_time"=>date('Y-m-d H:i:s'),
  149. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  150. "home_rate"=> $vv['home_rate']?:0, //主队让球
  151. "guest_rate"=> $vv['guest_rate']?:0, //客队让球
  152. "home_score"=> $vv['home_score']?:0, //主队进球数
  153. "guest_score"=> $vv['guest_score']?:0, //客队进球数
  154. "all_goal"=> $vv['all_goal']?:0, //总进球数
  155. "first_score"=> $vv['first_score']?:'', //最先进球球队
  156. "last_score"=> $vv['last_score']?:'', //最后进球球队
  157. "match_score"=> $vv['match_score']?:0, //赛事比分
  158. "match_winer"=> $vv['match_winer']?:'',//获胜球队
  159. "match_process"=> $vv['match_process']?:'',//比赛进程
  160. "u_home_score"=> $vv['u_home_score']?:0,//上半场主队进球数
  161. "u_guest_score"=> $vv['u_guest_score']?:0,//上半场客队进球数
  162. ];
  163. }
  164. }
  165. }
  166. /*
  167. if(!empty($set_match_r)){
  168. $ret = $model['model_result']::insert($set_match_r);
  169. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  170. }
  171. */
  172. }
  173. }
  174. if(!empty($set_match_r)){
  175. $ret = $model['model_result']::insert($set_match_r);
  176. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  177. }
  178. return 1;
  179. }
  180. /*
  181. * 写赛事结果
  182. */
  183. public static function ZQresult($model){
  184. //获取当天开始并且已结束的所有赛事
  185. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  186. ->where([['match_date','=',date('Y-m-d', time())],['status','=',2]])
  187. ->get()
  188. ->toArray();
  189. //获取赛事结果表 一天内
  190. $matchData_r = $model['model_result']::select('match_id')
  191. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-1 day"))]])
  192. ->get()
  193. ->toarray();
  194. if(!empty($matchData_r)){
  195. //如果结果表有数据,则获取结果表没有的赛事
  196. foreach ($matchData as $k => $v) {
  197. foreach ($matchData_r as $kk => $vv) {
  198. if ($v['id'] == $vv['match_id']) {
  199. unset($matchData[$k]);
  200. }
  201. }
  202. }
  203. }
  204. //没有数据,无需操作
  205. if(empty($matchData)) return Response::success();
  206. //获取赛事id 用于获取赛事对应结果比分
  207. $match_ids = [];
  208. foreach($matchData as $k =>$v){
  209. //只获取赛事已结束的
  210. if($v['status'] == 2){
  211. $match_ids[] = $v['id'];
  212. }
  213. }
  214. $match_ids_str = implode(",", $match_ids);
  215. //上半场
  216. $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,
  217. (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
  218. where a.match_id = b.match_id and a.id = b.id ";
  219. //全场
  220. $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,
  221. (select match_id,max(id) id from st_zq_result_record where match_id IN ($match_ids_str) group by match_id)b
  222. where a.match_id = b.match_id and a.id = b.id ";
  223. //上半场最终结果
  224. $match_result_h = DB::select($sql_h);
  225. //全场最终结果
  226. $match_result_f = DB::select($sql_f);
  227. //拼装赛事结果数据
  228. $match_result_record = [];
  229. if(!empty($match_result_h) and !empty($match_result_f)){
  230. foreach($match_result_h as $k=>$v){
  231. foreach($match_result_f as $kk=>$vv){
  232. if($v->match_id == $vv->match_id){
  233. $match_result_record[$k] = [
  234. "match_id"=>$v->match_id,
  235. "home_score"=> $vv->home_score,
  236. "guest_score"=> $vv->guest_score,
  237. "a_time"=> $vv->a_time,
  238. "match_process"=> $vv->match_process,
  239. "all_goal"=> $vv->all_goal,
  240. "first_score"=> $vv->first_score,
  241. "last_score"=> $vv->last_score,
  242. "match_score"=> $vv->match_score,
  243. "match_winer"=> $vv->match_winer,
  244. "u_home_score"=> $v->home_score,
  245. "u_guest_score"=> $v->guest_score,
  246. "home_rate"=> $vv->home_rate,
  247. "guest_rate"=> $vv->guest_rate,
  248. ];
  249. }
  250. }
  251. }
  252. }
  253. //组装赛果数据
  254. if(!empty($match_result_record)){
  255. foreach ($matchData as $k=>$v){
  256. $start_time = ($v['match_date'].' '.$v['match_time']);
  257. $time = time()-strtotime($v['match_time']);
  258. $match_time = self::secTime($time);
  259. foreach($match_result_record as $kk =>$vv){
  260. if($v['id'] == $vv['match_id']){
  261. $set_match_r[] = [
  262. "match_id"=> $v['id'],
  263. "home_team"=>$v['home_team'],
  264. "guest_team"=>$v['guest_team'],
  265. "lg_id"=>$v['lg_id'],
  266. "status"=>$v['status'],
  267. "tag"=> $v['tag'],
  268. 'match_time'=>$vv['a_time']?:0,//比赛进行时间
  269. "ctime"=>date('Y-m-d H:i:s'),
  270. "update_time"=>date('Y-m-d H:i:s'),
  271. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  272. "home_rate"=> $vv['home_rate']?:0, //主队让球
  273. "guest_rate"=> $vv['guest_rate']?:0, //客队让球
  274. "home_score"=> $vv['home_score']?:0, //主队进球数
  275. "guest_score"=> $vv['guest_score']?:0, //客队进球数
  276. "all_goal"=> $vv['all_goal']?:0, //总进球数
  277. "first_score"=> $vv['first_score']?:'', //最先进球球队
  278. "last_score"=> $vv['last_score']?:'', //最后进球球队
  279. "match_score"=> $vv['match_score']?:0, //赛事比分
  280. "match_winer"=> $vv['match_winer']?:'',//获胜球队
  281. "match_process"=> $vv['match_process']?:'',//比赛进程
  282. "u_home_score"=> $vv['u_home_score']?:0,//上半场主队进球数
  283. "u_guest_score"=> $vv['u_guest_score']?:0,//上半场客队进球数
  284. ];
  285. }
  286. }
  287. }
  288. }
  289. //写入赛果
  290. if(!empty($set_match_r)){
  291. $ret = $model['model_result']::insert($set_match_r);
  292. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  293. }
  294. return 1;
  295. }
  296. //计算滚球 赛事进行时间
  297. public static function secTime($sec=0){
  298. $min = floor($sec/60);
  299. $res = $min.':'.($sec-$min*60);
  300. return $res;
  301. }
  302. }