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. }
  168. if(!empty($set_match_r)){
  169. $ret = $model['model_result']::insert($set_match_r);
  170. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  171. }
  172. return 1;
  173. }
  174. /*
  175. * 写赛事结果
  176. */
  177. public static function ZQresult($model){
  178. //获取当天开始并且已结束的所有赛事
  179. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  180. ->where([['match_date','>',date('Y-m-d', strtotime("-2 day"))],['status','=',2]])
  181. ->get()
  182. ->toArray();
  183. //获取赛事结果表 一天内
  184. $matchData_r = $model['model_result']::select('match_id')
  185. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-2 day"))]])
  186. ->get()
  187. ->toArray();
  188. if(!empty($matchData_r)){
  189. //如果结果表有数据,则获取结果表没有的赛事
  190. foreach ($matchData as $k => $v) {
  191. foreach ($matchData_r as $kk => $vv) {
  192. if ($v['id'] == $vv['match_id']) {
  193. unset($matchData[$k]);
  194. }
  195. }
  196. }
  197. }
  198. //没有数据,无需操作
  199. if(empty($matchData)) return Response::success();
  200. //获取赛事id 用于获取赛事对应结果比分
  201. $match_ids = [];
  202. foreach($matchData as $k =>$v){
  203. //只获取赛事已结束的
  204. if($v['status'] == 2){
  205. $match_ids[] = $v['id'];
  206. }
  207. }
  208. $match_ids_str = implode(",", $match_ids);
  209. //上半场
  210. $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,
  211. (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
  212. where a.match_id = b.match_id and a.id = b.id ";
  213. //全场
  214. $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,
  215. (select match_id,max(id) id from st_zq_result_record where match_id IN ($match_ids_str) group by match_id)b
  216. where a.match_id = b.match_id and a.id = b.id ";
  217. //上半场最终结果
  218. $match_result_h = DB::select($sql_h);
  219. //全场最终结果
  220. $match_result_f = DB::select($sql_f);
  221. //拼装赛事结果数据
  222. $match_result_record = [];
  223. if(!empty($match_result_h) and !empty($match_result_f)){
  224. foreach($match_result_h as $k=>$v){
  225. foreach($match_result_f as $kk=>$vv){
  226. if($v->match_id == $vv->match_id){
  227. $match_result_record[$k] = [
  228. "match_id"=>$v->match_id,
  229. "home_score"=> $vv->home_score,
  230. "guest_score"=> $vv->guest_score,
  231. "a_time"=> $vv->a_time,
  232. "match_process"=> $vv->match_process,
  233. "all_goal"=> $vv->all_goal,
  234. "first_score"=> $vv->first_score,
  235. "last_score"=> $vv->last_score,
  236. "match_score"=> $vv->match_score,
  237. "match_winer"=> $vv->match_winer,
  238. "u_home_score"=> $v->home_score,
  239. "u_guest_score"=> $v->guest_score,
  240. "home_rate"=> $vv->home_rate,
  241. "guest_rate"=> $vv->guest_rate,
  242. ];
  243. }
  244. }
  245. }
  246. }
  247. //组装赛果数据
  248. if(!empty($match_result_record)){
  249. foreach ($matchData as $k=>$v){
  250. $start_time = ($v['match_date'].' '.$v['match_time']);
  251. $time = time()-strtotime($v['match_time']);
  252. $match_time = self::secTime($time);
  253. foreach($match_result_record as $kk =>$vv){
  254. if($v['id'] == $vv['match_id']){
  255. //获取获胜球队
  256. if(($vv['home_score']) > ($vv['guest_score'])){
  257. $match_winer = $v['home_team'];
  258. }else{
  259. $match_winer = $v['guest_team'];
  260. }
  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"=> $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. }