StZqResult.php 20 KB

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