StZqResult.php 24 KB

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