StZqResult.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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_v1($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. * 获取自动赛事结果
  298. * 只更新已结束+未手动修改的赛事结果
  299. * $match_id 本地赛事id 为0时 则用于后台手动刷新赛果
  300. */
  301. public static function ZQresult_v2($model,$match_id = 0){
  302. //如果有赛事id 则只更新当前赛事结果
  303. if($match_id > 0){
  304. $match_ids_str = $match_id;
  305. }else{
  306. //获取两天内的结束赛事
  307. $matchData = $model['model_match']
  308. ->join('st_zq_result','st_zq_result.match_id','=','st_zq_competition.id')
  309. ->select('st_zq_competition.id','is_correct')
  310. ->where([['st_zq_competition.match_date','>',date('Y-m-d', strtotime("-2 day"))],['st_zq_competition.status','=',2],['st_zq_result.is_correct','=',-1]])
  311. ->get()
  312. ->toArray();
  313. //没有数据,无需操作
  314. if(empty($matchData)) return Response::success();
  315. //获取需更新结果的赛事ID
  316. $match_ids = [];
  317. foreach($matchData as $k =>$v){
  318. //未手动修改比分
  319. if($v['is_correct'] == 0){
  320. $match_ids[] = $v['id'];
  321. }
  322. }
  323. if(empty($match_ids)) return Response::success();
  324. $match_ids_str = implode(",", $match_ids);
  325. }
  326. //上半场
  327. $sql_h = "select a.match_id,a.home_team,a.guest_team,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,
  328. (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
  329. where a.match_id = b.match_id and a.id = b.id ";
  330. //全场
  331. $sql_f = "select a.match_id,a.home_team,a.guest_team,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,
  332. (select match_id,max(id) id from st_zq_result_record where match_id IN ($match_ids_str) group by match_id)b
  333. where a.match_id = b.match_id and a.id = b.id ";
  334. //上半场最终结果
  335. $match_result_h = DB::select($sql_h);
  336. //全场最终结果
  337. $match_result_f = DB::select($sql_f);
  338. //拼装赛事结果数据
  339. $match_result_record = [];
  340. if(!empty($match_result_h) and !empty($match_result_f)){
  341. foreach($match_result_h as $k=>$v){
  342. foreach($match_result_f as $kk=>$vv){
  343. if($v->match_id == $vv->match_id){
  344. $match_result_record[$k] = [
  345. "match_id"=>$v->match_id,
  346. "home_team"=>$v->home_team,
  347. "guest_team"=>$v->guest_team,
  348. "home_score"=> $vv->home_score,
  349. "guest_score"=> $vv->guest_score,
  350. "a_time"=> $vv->a_time,
  351. "match_process"=> $vv->match_process,
  352. "all_goal"=> $vv->all_goal,
  353. "first_score"=> $vv->first_score,
  354. "last_score"=> $vv->last_score,
  355. "match_score"=> $vv->match_score,
  356. "match_winer"=> $vv->match_winer,
  357. "u_home_score"=> $v->home_score,
  358. "u_guest_score"=> $v->guest_score,
  359. "home_rate"=> $vv->home_rate,
  360. "guest_rate"=> $vv->guest_rate,
  361. ];
  362. }
  363. }
  364. }
  365. }
  366. if(!empty($match_result_record)){
  367. foreach($match_result_record as $k=>$v){
  368. //获取获胜球队
  369. if(($v['home_score']) > ($v['guest_score'])){
  370. $match_winer = $v['home_team'];
  371. }else{
  372. $match_winer = $v['guest_team'];
  373. }
  374. $set_match_r = [
  375. "match_id"=> $v['match_id'],
  376. "update_time"=>date('Y-m-d H:i:s'),
  377. "status"=>2,//已结束
  378. "home_rate"=> $v['home_rate']?:0, //主队让球
  379. "guest_rate"=> $v['guest_rate']?:0, //客队让球
  380. "home_score"=> $v['home_score']?:0, //主队进球数
  381. "guest_score"=> $v['guest_score']?:0, //客队进球数
  382. "all_goal"=> $v['all_goal']?:0, //总进球数
  383. // "first_score"=> $vv['first_score']?:'', //最先进球球队
  384. // "last_score"=> $vv['last_score']?:'', //最后进球球队
  385. "match_score"=> $v['match_score']?:0, //赛事比分
  386. "match_winer"=> $match_winer?:'',//获胜球队
  387. "match_process"=> $v['match_process']?:'',//比赛进程
  388. "u_home_score"=> $v['u_home_score']?:0,//上半场主队进球数
  389. "u_guest_score"=> $v['u_guest_score']?:0,//上半场客队进球数
  390. "is_correct"=> -1,//自动比分
  391. ];
  392. $ret = $model['model_result']::where(['match_id' => $v['match_id'],'is_correct'=>-1])
  393. ->update($set_match_r);
  394. // if($ret < 1) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  395. }
  396. }
  397. return 1;
  398. }
  399. //计算滚球 赛事进行时间
  400. public static function secTime($sec=0){
  401. $min = floor($sec/60);
  402. $res = $min.':'.($sec-$min*60);
  403. return $res;
  404. }
  405. /**
  406. * 写入预植赛果
  407. */
  408. public static function set_result($opt = []){
  409. if(!empty($opt)){
  410. //赛事数据
  411. $data = $opt['data'];
  412. //获取开始时间
  413. $start_time = ($data['match_date'].' '.$data['match_time']);
  414. $match_result = [
  415. "match_id"=> $opt['match_id'],
  416. "lg_id"=>$opt['lg_id'],
  417. "status"=>$data['status'],
  418. "tag"=> 0,
  419. 'match_time'=>0,//比赛进行时间
  420. "ctime"=>date('Y-m-d H:i:s'),
  421. "update_time"=>date('Y-m-d H:i:s'),
  422. "is_correct" => -1,
  423. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  424. ];
  425. //根据球类赋值
  426. if($opt['game_code'] == 'zq'){//足球
  427. $match_result['home_team'] = $data['home_team'];
  428. $match_result['guest_team'] = $data['guest_team'];
  429. }
  430. if($opt['game_code'] == 'lq'){//篮球
  431. $match_result['home_team'] = $data['home_team'];
  432. $match_result['guest_team'] = $data['guest_team'];
  433. }
  434. if($opt['game_code'] == 'wq'){//网球
  435. $match_result['home_player_name'] = $data['home_team'];
  436. $match_result['guest_player_name'] = $data['guest_team'];
  437. }
  438. if($opt['game_code'] == 'bq'){//棒球
  439. $match_result['home_team'] = $data['home_team'];
  440. $match_result['guest_team'] = $data['guest_team'];
  441. }
  442. //获取model
  443. $models = $opt['models'];
  444. //执行写入
  445. $ret_id = $models['model_result']::insertGetId($match_result);
  446. //如果失败,返回异常
  447. if ($ret_id < 1) throw new \Exception(Response::generate($opt['gameName'] . '赛事-match_id:' . $opt['match_id'] . ';', Response::SET_MATCH_ERR));
  448. }
  449. }
  450. }