StZqResult.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. use Illuminate\Database\Capsule\Manager as DB;
  7. /**
  8. * Class StZqResult
  9. * @package App\Http\Model
  10. * 足球 赛事 结果
  11. */
  12. class StZqResult extends Model
  13. {
  14. protected $table = 'st_zq_result';
  15. public $timestamps = false;
  16. /*
  17. * 写赛事结果
  18. * 弃用
  19. */
  20. public static function ZQresult__($model){
  21. //获取赛事表7天内所有赛事
  22. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  23. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-1 day"))]])
  24. ->get()
  25. ->toarray();
  26. //没有数据,无需操作
  27. if(empty($matchData)) return Response::success();
  28. //获取赛事结果表 所有当月
  29. $matchData_r = $model['model_result']::select('match_id')
  30. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-1 day"))]])
  31. ->get()
  32. ->toarray();
  33. //获取赛事对应结果比分
  34. //获取赛事id
  35. $match_ids = [];
  36. foreach($matchData as $k =>$v){
  37. //只获取赛事已结束的
  38. if($v['status'] == 2){
  39. $match_ids[] = $v['id'];
  40. }
  41. }
  42. $match_ids_str = implode(",", $match_ids);
  43. //上半场
  44. $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,
  45. (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
  46. where a.match_id = b.match_id and a.id = b.id ";
  47. //全场
  48. $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,
  49. (select match_id,max(id) id from st_zq_result_record where match_id IN ($match_ids_str) group by match_id)b
  50. where a.match_id = b.match_id and a.id = b.id ";
  51. //上半场最终结果
  52. $match_result_h = DB::select($sql_h);
  53. //全场最终结果
  54. $match_result_f = DB::select($sql_f);
  55. //拼装赛事结果数据
  56. $match_result_record = [];
  57. if(!empty($match_result_h) and !empty($match_result_f)){
  58. foreach($match_result_h as $k=>$v){
  59. foreach($match_result_f as $kk=>$vv){
  60. if($v->match_id == $vv->match_id){
  61. $match_result_record[$k] = [
  62. "match_id"=>$v->match_id,
  63. "home_score"=> $vv->home_score,
  64. "guest_score"=> $vv->guest_score,
  65. "a_time"=> $vv->a_time,
  66. "match_process"=> $vv->match_process,
  67. "all_goal"=> $vv->all_goal,
  68. "first_score"=> $vv->first_score,
  69. "last_score"=> $vv->last_score,
  70. "match_score"=> $vv->match_score,
  71. "match_winer"=> $vv->match_winer,
  72. "u_home_score"=> $v->home_score,
  73. "u_guest_score"=> $v->guest_score,
  74. "home_rate"=> $vv->home_rate,
  75. "guest_rate"=> $vv->guest_rate,
  76. ];
  77. }
  78. }
  79. }
  80. }
  81. //结果表无数据,直接插入
  82. if(empty($matchData_r)){
  83. foreach ($matchData as $k=>$v){
  84. $start_time = ($v['match_date'].' '.$v['match_time']);
  85. $time = time()-strtotime($v['match_time']);
  86. $match_time = self::secTime($time);
  87. foreach($match_result_record as $kk =>$vv){
  88. if($v['id'] == $vv['match_id']){
  89. $set_match_r[] = [
  90. "match_id"=> $v['id'],
  91. "home_team"=>$v['home_team'],
  92. "guest_team"=>$v['guest_team'],
  93. "lg_id"=>$v['lg_id'],
  94. "status"=>$v['status'],
  95. "tag"=> $v['tag'],
  96. 'match_time'=>$vv['a_time']?:0,//比赛进行时间
  97. "ctime"=>date('Y-m-d H:i:s'),
  98. "update_time"=>date('Y-m-d H:i:s'),
  99. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  100. "home_rate"=> $vv['home_rate']?:0, //主队让球
  101. "guest_rate"=> $vv['guest_rate']?:0, //客队让球
  102. "home_score"=> $vv['home_score']?:0, //主队进球数
  103. "guest_score"=> $vv['guest_score']?:0, //客队进球数
  104. "all_goal"=> $vv['all_goal']?:0, //总进球数
  105. "first_score"=> $vv['first_score']?:'', //最先进球球队
  106. "last_score"=> $vv['last_score']?:'', //最后进球球队
  107. "match_score"=> $vv['match_score']?:0, //赛事比分
  108. "match_winer"=> $vv['match_winer']?:'',//获胜球队
  109. "match_process"=> $vv['match_process']?:'',//比赛进程
  110. "u_home_score"=> $vv['u_home_score']?:0,//上半场主队进球数
  111. "u_guest_score"=> $vv['u_guest_score']?:0,//上半场客队进球数
  112. ];
  113. }
  114. }
  115. }
  116. /*
  117. if(!empty($set_match_r)){
  118. $ret = $model['model_result']::insert($set_match_r);
  119. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  120. }
  121. */
  122. }else {
  123. //如果结果表有数据,则获取结果表没有的赛事
  124. foreach ($matchData as $k => $v) {
  125. foreach ($matchData_r as $kk => $vv) {
  126. if ($v['id'] == $vv['match_id']) {
  127. unset($matchData[$k]);
  128. }
  129. }
  130. }
  131. //如果还有未写入赛事
  132. if (!empty($matchData)) {
  133. //写入结果表不存在赛事
  134. foreach ($matchData as $k => $v) {
  135. $start_time = ($v['match_date'].' '.$v['match_time']);
  136. $time = time()-strtotime($v['match_time']);
  137. // $match_time = self::secTime($time);
  138. foreach($match_result_record as $kk =>$vv){
  139. if($v['id'] == $vv['match_id']){
  140. $set_match_r[] = [
  141. "match_id"=> $v['id'],
  142. "home_team"=>$v['home_team'],
  143. "guest_team"=>$v['guest_team'],
  144. "lg_id"=>$v['lg_id'],
  145. "status"=>$v['status'],
  146. "tag"=> $v['tag'],
  147. 'match_time'=>$vv['a_time']?:0,//比赛进行时间
  148. "ctime"=>date('Y-m-d H:i:s'),
  149. "update_time"=>date('Y-m-d H:i:s'),
  150. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  151. "home_rate"=> $vv['home_rate']?:0, //主队让球
  152. "guest_rate"=> $vv['guest_rate']?:0, //客队让球
  153. "home_score"=> $vv['home_score']?:0, //主队进球数
  154. "guest_score"=> $vv['guest_score']?:0, //客队进球数
  155. "all_goal"=> $vv['all_goal']?:0, //总进球数
  156. "first_score"=> $vv['first_score']?:'', //最先进球球队
  157. "last_score"=> $vv['last_score']?:'', //最后进球球队
  158. "match_score"=> $vv['match_score']?:0, //赛事比分
  159. "match_winer"=> $vv['match_winer']?:'',//获胜球队
  160. "match_process"=> $vv['match_process']?:'',//比赛进程
  161. "u_home_score"=> $vv['u_home_score']?:0,//上半场主队进球数
  162. "u_guest_score"=> $vv['u_guest_score']?:0,//上半场客队进球数
  163. ];
  164. }
  165. }
  166. }
  167. }
  168. }
  169. if(!empty($set_match_r)){
  170. $ret = $model['model_result']::insert($set_match_r);
  171. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  172. }
  173. return 1;
  174. }
  175. /*
  176. * 写赛事结果
  177. */
  178. public static function ZQresult_v1($model){
  179. //获取当天开始并且已结束的所有赛事
  180. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  181. ->where([['match_date','>',date('Y-m-d', strtotime("-2 day"))],['status','=',2]])
  182. ->get()
  183. ->toArray();
  184. //获取赛事结果表 一天内
  185. $matchData_r = $model['model_result']::select('match_id')
  186. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-2 day"))]])
  187. ->get()
  188. ->toArray();
  189. if(!empty($matchData_r)){
  190. //如果结果表有数据,则获取结果表没有的赛事
  191. foreach ($matchData as $k => $v) {
  192. foreach ($matchData_r as $kk => $vv) {
  193. if ($v['id'] == $vv['match_id']) {
  194. unset($matchData[$k]);
  195. }
  196. }
  197. }
  198. }
  199. //没有数据,无需操作
  200. if(empty($matchData)) return Response::success();
  201. //获取赛事id 用于获取赛事对应结果比分
  202. $match_ids = [];
  203. foreach($matchData as $k =>$v){
  204. //只获取赛事已结束的
  205. if($v['status'] == 2){
  206. $match_ids[] = $v['id'];
  207. }
  208. }
  209. $match_ids_str = implode(",", $match_ids);
  210. //上半场
  211. $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,
  212. (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
  213. where a.match_id = b.match_id and a.id = b.id ";
  214. //全场
  215. $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,
  216. (select match_id,max(id) id from st_zq_result_record where match_id IN ($match_ids_str) group by match_id)b
  217. where a.match_id = b.match_id and a.id = b.id ";
  218. //上半场最终结果
  219. $match_result_h = DB::select($sql_h);
  220. //全场最终结果
  221. $match_result_f = DB::select($sql_f);
  222. //拼装赛事结果数据
  223. $match_result_record = [];
  224. if(!empty($match_result_h) and !empty($match_result_f)){
  225. foreach($match_result_h as $k=>$v){
  226. foreach($match_result_f as $kk=>$vv){
  227. if($v->match_id == $vv->match_id){
  228. $match_result_record[$k] = [
  229. "match_id"=>$v->match_id,
  230. "home_score"=> $vv->home_score,
  231. "guest_score"=> $vv->guest_score,
  232. "a_time"=> $vv->a_time,
  233. "match_process"=> $vv->match_process,
  234. "all_goal"=> $vv->all_goal,
  235. "first_score"=> $vv->first_score,
  236. "last_score"=> $vv->last_score,
  237. "match_score"=> $vv->match_score,
  238. "match_winer"=> $vv->match_winer,
  239. "u_home_score"=> $v->home_score,
  240. "u_guest_score"=> $v->guest_score,
  241. "home_rate"=> $vv->home_rate,
  242. "guest_rate"=> $vv->guest_rate,
  243. ];
  244. }
  245. }
  246. }
  247. }
  248. //组装赛果数据
  249. if(!empty($match_result_record)){
  250. foreach ($matchData as $k=>$v){
  251. $start_time = ($v['match_date'].' '.$v['match_time']);
  252. $time = time()-strtotime($v['match_time']);
  253. $match_time = self::secTime($time);
  254. foreach($match_result_record as $kk =>$vv){
  255. if($v['id'] == $vv['match_id']){
  256. //获取获胜球队
  257. if(($vv['home_score']) > ($vv['guest_score'])){
  258. $match_winer = $v['home_team'];
  259. }else{
  260. $match_winer = $v['guest_team'];
  261. }
  262. $set_match_r[] = [
  263. "match_id"=> $v['id'],
  264. "home_team"=>$v['home_team'],
  265. "guest_team"=>$v['guest_team'],
  266. "lg_id"=>$v['lg_id'],
  267. "status"=>$v['status'],
  268. "tag"=> $v['tag'],
  269. 'match_time'=>$vv['a_time']?:0,//比赛进行时间
  270. "ctime"=>date('Y-m-d H:i:s'),
  271. "update_time"=>date('Y-m-d H:i:s'),
  272. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  273. "home_rate"=> $vv['home_rate']?:0, //主队让球
  274. "guest_rate"=> $vv['guest_rate']?:0, //客队让球
  275. "home_score"=> $vv['home_score']?:0, //主队进球数
  276. "guest_score"=> $vv['guest_score']?:0, //客队进球数
  277. "all_goal"=> $vv['all_goal']?:0, //总进球数
  278. // "first_score"=> $vv['first_score']?:'', //最先进球球队
  279. // "last_score"=> $vv['last_score']?:'', //最后进球球队
  280. "match_score"=> $vv['match_score']?:0, //赛事比分
  281. "match_winer"=> $match_winer?:'',//获胜球队
  282. "match_process"=> $vv['match_process']?:'',//比赛进程
  283. "u_home_score"=> $vv['u_home_score']?:0,//上半场主队进球数
  284. "u_guest_score"=> $vv['u_guest_score']?:0,//上半场客队进球数
  285. ];
  286. }
  287. }
  288. }
  289. }
  290. //写入赛果
  291. if(!empty($set_match_r)){
  292. $ret = $model['model_result']::insert($set_match_r);
  293. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  294. }
  295. return 1;
  296. }
  297. /**
  298. * 获取自动赛事结果
  299. * 只更新已结束+未手动修改的赛事结果
  300. * $match_id 本地赛事id 为0时 则用于后台手动刷新赛果
  301. */
  302. public static function ZQresult_v2($model,$match_id = 0){
  303. //如果有赛事id 则只更新当前赛事结果
  304. if($match_id > 0){
  305. $match_ids_str = $match_id;
  306. }else{
  307. //获取两天内的结束赛事
  308. $matchData = $model['model_match']
  309. ->join('st_zq_result','st_zq_result.match_id','=','st_zq_competition.id')
  310. ->select('st_zq_competition.id','is_correct')
  311. ->where([['st_zq_competition.match_date','>',date('Y-m-d', strtotime("-2 day"))],['st_zq_competition.status','=',2],['st_zq_result.is_correct','=',-1]])
  312. ->get()
  313. ->toArray();
  314. //没有数据,无需操作
  315. if(empty($matchData)) return Response::success();
  316. //获取需更新结果的赛事ID
  317. $match_ids = [];
  318. foreach($matchData as $k =>$v){
  319. //未手动修改比分
  320. if($v['is_correct'] == 0){
  321. $match_ids[] = $v['id'];
  322. }
  323. }
  324. if(empty($match_ids)) return Response::success();
  325. $match_ids_str = implode(",", $match_ids);
  326. }
  327. //上半场
  328. $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,
  329. (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
  330. where a.match_id = b.match_id and a.id = b.id ";
  331. //全场
  332. $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,
  333. (select match_id,max(id) id from st_zq_result_record where match_id IN ($match_ids_str) group by match_id)b
  334. where a.match_id = b.match_id and a.id = b.id ";
  335. //上半场最终结果
  336. $match_result_h = DB::select($sql_h);
  337. //全场最终结果
  338. $match_result_f = DB::select($sql_f);
  339. //拼装赛事结果数据
  340. $match_result_record = [];
  341. if(!empty($match_result_h) and !empty($match_result_f)){
  342. foreach($match_result_h as $k=>$v){
  343. foreach($match_result_f as $kk=>$vv){
  344. if($v->match_id == $vv->match_id){
  345. $match_result_record[$k] = [
  346. "match_id"=>$v->match_id,
  347. "home_team"=>$v->home_team,
  348. "guest_team"=>$v->guest_team,
  349. "home_score"=> $vv->home_score,
  350. "guest_score"=> $vv->guest_score,
  351. "a_time"=> $vv->a_time,
  352. "match_process"=> $vv->match_process,
  353. "all_goal"=> $vv->all_goal,
  354. "first_score"=> $vv->first_score,
  355. "last_score"=> $vv->last_score,
  356. "match_score"=> $vv->match_score,
  357. "match_winer"=> $vv->match_winer,
  358. "u_home_score"=> $v->home_score,
  359. "u_guest_score"=> $v->guest_score,
  360. "home_rate"=> $vv->home_rate,
  361. "guest_rate"=> $vv->guest_rate,
  362. ];
  363. }
  364. }
  365. }
  366. }
  367. if(!empty($match_result_record)){
  368. foreach($match_result_record as $k=>$v){
  369. //获取获胜球队
  370. if(($v['home_score']) > ($v['guest_score'])){
  371. $match_winer = $v['home_team'];
  372. }else{
  373. $match_winer = $v['guest_team'];
  374. }
  375. $set_match_r = [
  376. "match_id"=> $v['match_id'],
  377. "update_time"=>date('Y-m-d H:i:s'),
  378. "status"=>2,//已结束
  379. "home_rate"=> $v['home_rate']?:0, //主队让球
  380. "guest_rate"=> $v['guest_rate']?:0, //客队让球
  381. "home_score"=> $v['home_score']?:0, //主队进球数
  382. "guest_score"=> $v['guest_score']?:0, //客队进球数
  383. "all_goal"=> $v['all_goal']?:0, //总进球数
  384. // "first_score"=> $vv['first_score']?:'', //最先进球球队
  385. // "last_score"=> $vv['last_score']?:'', //最后进球球队
  386. "match_score"=> $v['match_score']?:0, //赛事比分
  387. "match_winer"=> $match_winer?:'',//获胜球队
  388. "match_process"=> $v['match_process']?:'',//比赛进程
  389. "u_home_score"=> $v['u_home_score']?:0,//上半场主队进球数
  390. "u_guest_score"=> $v['u_guest_score']?:0,//上半场客队进球数
  391. "is_correct"=> -1,//自动比分
  392. ];
  393. $ret = $model['model_result']::where(['match_id' => $v['match_id'],'is_correct'=>-1])
  394. ->update($set_match_r);
  395. // if($ret < 1) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  396. }
  397. }
  398. return 1;
  399. }
  400. //计算滚球 赛事进行时间
  401. public static function secTime($sec=0){
  402. $min = floor($sec/60);
  403. $res = $min.':'.($sec-$min*60);
  404. return $res;
  405. }
  406. /**
  407. * 写入预植赛果
  408. */
  409. public static function set_result($opt = []){
  410. if(!empty($opt)){
  411. //赛事数据
  412. $data = $opt['data'];
  413. //获取开始时间
  414. $start_time = ($data['match_date'].' '.$data['match_time']);
  415. $match_result = [
  416. "match_id"=> $opt['match_id'],
  417. "lg_id"=>$opt['lg_id'],
  418. "status"=>$data['status'],
  419. "tag"=> 0,
  420. 'match_time'=>0,//比赛进行时间
  421. "ctime"=>date('Y-m-d H:i:s'),
  422. "update_time"=>date('Y-m-d H:i:s'),
  423. "is_correct" => -1,
  424. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  425. ];
  426. //根据球类赋值
  427. if($opt['game_code'] == 'zq'){//足球
  428. $match_result['home_team'] = $data['home_team'];
  429. $match_result['guest_team'] = $data['guest_team'];
  430. }
  431. if($opt['game_code'] == 'lq'){//篮球
  432. $match_result['home_team'] = $data['home_team'];
  433. $match_result['guest_team'] = $data['guest_team'];
  434. }
  435. if($opt['game_code'] == 'wq'){//网球
  436. $match_result['home_player_name'] = $data['home_team'];
  437. $match_result['guest_player_name'] = $data['guest_team'];
  438. }
  439. if($opt['game_code'] == 'bq'){//棒球
  440. $match_result['home_team'] = $data['home_team'];
  441. $match_result['guest_team'] = $data['guest_team'];
  442. }
  443. //获取model
  444. $models = $opt['models'];
  445. //执行写入
  446. $ret_id = $models['model_result']::insertGetId($match_result);
  447. //如果失败,返回异常
  448. if ($ret_id < 1) throw new \Exception(Response::generate($opt['gameName'] . '赛事-match_id:' . $opt['match_id'] . ';', Response::SET_MATCH_ERR));
  449. }
  450. }
  451. }