StLqResult.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. use App\Lib\Biz\Sport\Common as commonFunction;
  8. /**
  9. * Class StLqResult
  10. * @package App\Http\Model
  11. * 篮球 赛事 结果
  12. */
  13. class StLqResult extends Model
  14. {
  15. protected $table = 'st_lq_result';
  16. public $timestamps = false;
  17. /*
  18. * 写赛事结果
  19. * 弃用
  20. */
  21. public static function LQresult___($model){
  22. dd(123);
  23. //获取赛事表7天内所有赛事
  24. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  25. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-1 day"))]])
  26. ->get()
  27. ->toarray();
  28. //没有数据,无需操作
  29. if(empty($matchData)) return Response::success();
  30. //获取赛事结果表 15天内
  31. $matchData_r = $model['model_result']::select('match_id')
  32. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-1 day"))]])
  33. ->get()
  34. ->toarray();
  35. //结果表无数据,直接插入
  36. if(empty($matchData_r)){
  37. foreach ($matchData as $k=>$v){
  38. $process = 0;
  39. if((int)$v['status'] == 2){
  40. $process = 4;
  41. }
  42. if((int)$v['status'] == 1){
  43. $process = 1;
  44. }
  45. $start_time = ($v['match_date'].' '.$v['match_time']);
  46. $set_match_r[] = [
  47. "match_id"=> $v['id'],
  48. "home_team"=>$v['home_team'],
  49. "guest_team"=>$v['guest_team'],
  50. "lg_id"=>$v['lg_id'],
  51. "status"=>$v['status'],
  52. "tag"=> $v['tag'],
  53. "match_process"=>$process,
  54. 'match_time'=>12,
  55. "ctime"=>date('Y-m-d H:i:s'),
  56. "update_time"=>date('Y-m-d H:i:s'),
  57. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time))
  58. ];
  59. }
  60. $ret = $model['model_result']::insert($set_match_r);
  61. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  62. }else{
  63. //如果结果表有数据,则获取结果表没有的赛事
  64. foreach ($matchData as $k=>$v){
  65. foreach ($matchData_r as $kk=>$vv){
  66. if($v['id'] == $vv['match_id']){
  67. unset($matchData[$k]);
  68. }
  69. }
  70. }
  71. //如果还有未写入赛事
  72. if(!empty($matchData)){
  73. //写入结果表不存在赛事
  74. foreach ($matchData as $k=>$v){
  75. $process = 0;
  76. if((int)$v['status'] == 2){
  77. $process = 4;
  78. }
  79. if((int)$v['status'] == 1){
  80. $process = 1;
  81. }
  82. $start_time = ($v['match_date'].' '.$v['match_time']);
  83. $set_match_r[] = [
  84. "match_id"=> $v['id'],
  85. "home_team"=>$v['home_team'],
  86. "guest_team"=>$v['guest_team'],
  87. "lg_id"=>$v['lg_id'],
  88. "status"=>$v['status'],
  89. "tag"=> $v['tag'],
  90. "match_process"=>$process,
  91. 'match_time'=>12,
  92. "ctime"=>date('Y-m-d H:i:s'),
  93. "update_time"=>date('Y-m-d H:i:s'),
  94. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time))
  95. ];
  96. }
  97. $ret = $model['model_result']::insert($set_match_r);
  98. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));//Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
  99. }
  100. }
  101. }
  102. /*
  103. * 写赛事结果
  104. */
  105. public static function LQresult_v1($model){
  106. //获取当天开始并且已结束的所有赛事
  107. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  108. ->where([['match_date','>',date('Y-m-d',strtotime("-2 day"))],['status','=',2]])
  109. ->get()
  110. ->toArray();
  111. //获取赛事结果表 一天内
  112. $matchData_r = $model['model_result']::select('match_id')
  113. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-2 day"))]])
  114. ->get()
  115. ->toArray();
  116. if(!empty($matchData_r)){
  117. //如果结果表有数据,则获取结果表没有的赛事
  118. foreach ($matchData as $k => $v) {
  119. foreach ($matchData_r as $kk => $vv) {
  120. if ($v['id'] == $vv['match_id']) {
  121. unset($matchData[$k]);
  122. }
  123. }
  124. }
  125. }
  126. //没有数据,无需操作
  127. if(empty($matchData)) return Response::success();
  128. //获取赛事id 用于获取赛事对应结果比分
  129. $match_ids = [];
  130. foreach($matchData as $k =>$v){
  131. //只获取赛事已结束的
  132. if($v['status'] == 2){
  133. $match_ids[] = $v['id'];
  134. }
  135. }
  136. $match_ids_str = implode(",", $match_ids);
  137. //第一节
  138. $sql_1 = "select a.match_id,a.home_score,a.guest_score,a.match_time as a_time,a.home_rate,a.guest_rate,a.match_process,a.first_score,a.last_score,a.match_score,a.match_winer from st_lq_result_record a,
  139. (select match_id,max(id) id from st_lq_result_record where match_process = '第一节' and match_id IN ($match_ids_str) group by match_id)b
  140. where a.match_id = b.match_id and a.id = b.id ";
  141. //第二节
  142. $sql_2 = "select a.match_id,a.home_score,a.guest_score,a.match_time as a_time,a.home_rate,a.guest_rate,a.match_process,a.first_score,a.last_score,a.match_score,a.match_winer from st_lq_result_record a,
  143. (select match_id,max(id) id from st_lq_result_record where match_process = '第二节' and match_id IN ($match_ids_str) group by match_id)b
  144. where a.match_id = b.match_id and a.id = b.id ";
  145. //第三节
  146. $sql_3 = "select a.match_id,a.home_score,a.guest_score,a.match_time as a_time,a.home_rate,a.guest_rate,a.match_process,a.first_score,a.last_score,a.match_score,a.match_winer from st_lq_result_record a,
  147. (select match_id,max(id) id from st_lq_result_record where match_process = '第三节' and match_id IN ($match_ids_str) group by match_id)b
  148. where a.match_id = b.match_id and a.id = b.id ";
  149. //第四节
  150. $sql_4 = "select a.match_id,a.home_score,a.guest_score,a.match_time as a_time,a.home_rate,a.guest_rate,a.match_process,a.first_score,a.last_score,a.match_score,a.match_winer from st_lq_result_record a,
  151. (select match_id,max(id) id from st_lq_result_record where match_process = '第四节' and match_id IN ($match_ids_str) group by match_id)b
  152. where a.match_id = b.match_id and a.id = b.id ";
  153. //第一节最终结果
  154. $match_result_1 = DB::select($sql_1);
  155. //第二节最终结果
  156. $match_result_2 = DB::select($sql_2);
  157. //第三节最终结果
  158. $match_result_3 = DB::select($sql_3);
  159. //第四节最终结果
  160. $match_result_4 = DB::select($sql_4);
  161. //拼装赛事结果数据
  162. $set_match_r = [];
  163. if(!empty($match_result_1) and !empty($match_result_2) and !empty($match_result_3) and !empty($match_result_4)){
  164. foreach($matchData as $k =>$v ){
  165. //获取开赛时间
  166. $start_time = ($v['match_date'].' '.$v['match_time']);
  167. $time = time()-strtotime($v['match_time']);
  168. $match_time = self::secTime($time);
  169. //获取赛事每节的赛果
  170. $result_1 = commonFunction::filter_by_value($match_result_1,'match_id',$v['id']);
  171. $result_2 = commonFunction::filter_by_value($match_result_2,'match_id',$v['id']);
  172. $result_3 = commonFunction::filter_by_value($match_result_3,'match_id',$v['id']);
  173. $result_4 = commonFunction::filter_by_value($match_result_4,'match_id',$v['id']);
  174. if(!empty($result_1) and !empty($result_2) and !empty($result_3) and !empty($result_4)){
  175. //拼接赛事主队比分 {"1":0,"2":0,"3":0,"4":0}
  176. $home_score = [
  177. '1'=>$result_1['home_score'],
  178. '2'=>$result_2['home_score'] - $result_1['home_score'],
  179. '3'=>$result_3['home_score'] - $result_2['home_score'],
  180. '4'=>$result_4['home_score'] - $result_3['home_score'],
  181. ];
  182. //拼接赛事客队比分
  183. $guest_score = [
  184. '1'=>$result_1['guest_score'],
  185. '2'=>$result_2['guest_score'] - $result_1['guest_score'],
  186. '3'=>$result_3['guest_score'] - $result_2['guest_score'],
  187. '4'=>$result_4['guest_score'] - $result_3['guest_score'],
  188. ];
  189. //获取获胜球队
  190. if(($result_4['home_score']) > ($result_4['guest_score'])){
  191. $match_winer = $v['home_team'];
  192. }else{
  193. $match_winer = $v['guest_team'];
  194. }
  195. //获取总进球数
  196. $all_goal = ($result_4['home_score'])+($result_4['guest_score']);
  197. //赛事待写入赛果数据
  198. $set_match_r[] = [
  199. "match_id"=> $v['id'],
  200. "home_team"=>$v['home_team'],
  201. "guest_team"=>$v['guest_team'],
  202. "lg_id"=>$v['lg_id'],
  203. "status"=>$v['status'],
  204. "tag"=> $v['tag'],
  205. 'match_time'=>$result_4['a_time']?:0,//比赛进行时间
  206. "ctime"=>date('Y-m-d H:i:s'),
  207. "update_time"=>date('Y-m-d H:i:s'),
  208. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  209. "home_rate"=> $result_4['home_rate']?:0, //主队让球
  210. "guest_rate"=> $result_4['guest_rate']?:0, //客队让球
  211. "home_score"=> json_encode($home_score)?:'', //主队进球数
  212. "guest_score"=> json_encode($guest_score)?:'', //客队进球数
  213. "all_goal"=> $all_goal?:0, //总进球数
  214. // "first_score"=> $result_4['first_score']?:'', //最先进球球队
  215. // "last_score"=> $result_4['last_score']?:'', //最后进球球队
  216. "match_score"=> $result_4['match_score']?:0, //赛事比分
  217. "match_winer"=> $match_winer?:'',//获胜球队
  218. "match_process"=> $result_4['match_process']?:'',//比赛进程
  219. "u_home_score"=> $result_2['home_score']?:0,//上半场主队进球数
  220. "u_guest_score"=> $result_2['guest_score']?:0,//上半场客队进球数
  221. ];
  222. }
  223. }
  224. }
  225. //写入赛果
  226. if(!empty($set_match_r)){
  227. $ret = $model['model_result']::insert($set_match_r);
  228. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  229. }
  230. return 1;
  231. }
  232. /**
  233. * 获取自动赛事结果
  234. * 只更新已结束+未手动修改的赛事结果
  235. * $match_id 本地赛事id 为0时 则用于后台手动刷新赛果
  236. */
  237. public static function LQresult_v2($model,$match_id=0){
  238. //如果有赛事id 则只更新当前赛事结果
  239. if($match_id > 0){
  240. $match_ids_str = $match_id;
  241. $match_ids = [$match_id];
  242. }else{
  243. //获取两天内的结束赛事
  244. $matchData = $model['model_match']
  245. ->join('st_lq_result','st_lq_result.match_id','=','st_lq_competition.id')
  246. ->select('st_lq_competition.id','is_correct')
  247. ->where([['st_lq_competition.match_date','>',date('Y-m-d', strtotime("-2 day"))],['st_lq_competition.status','=',2],['st_lq_result.is_correct','=',-1]])
  248. ->get()
  249. ->toArray();
  250. //没有数据,无需操作
  251. if(empty($matchData)) return Response::success();
  252. //获取需更新结果的赛事ID
  253. $match_ids = [];
  254. foreach($matchData as $k =>$v){
  255. //未手动修改比分
  256. if($v['is_correct'] == 0){
  257. $match_ids[] = $v['id'];
  258. }
  259. }
  260. if(empty($match_ids)) return Response::success();
  261. $match_ids_str = implode(",", $match_ids);
  262. }
  263. //如果有赛事id 获取结果中的手动数据
  264. if(!empty($match_ids)){
  265. $manual_result_data = self::select('match_id','manual_result')
  266. ->whereIn('match_id',$match_ids)
  267. ->get()
  268. ->toArray();
  269. }
  270. //拼接sql
  271. $sql_result = "select a.match_id,a.home_team,a.guest_team,a.home_score,a.guest_score,a.match_time as a_time,a.home_rate,a.guest_rate,a.match_process,a.first_score,a.last_score,a.match_score,a.match_winer,a.result_mark from st_lq_result_record a,
  272. (select match_id,max(id) id from st_lq_result_record where match_id IN ($match_ids_str) group by match_id)b
  273. where a.match_id = b.match_id and a.id = b.id ";
  274. //最终结果
  275. $match_result = DB::select($sql_result);
  276. if(!empty($match_result)){
  277. foreach($match_result as $k=>$v){
  278. //获取结果json
  279. $mark_result = json_decode($v->result_mark,true);
  280. //如果为空,设默认值
  281. if($mark_result == []){
  282. $mark_result_json = '{"schedule":"ot","r_time":"00:00","sc_1th_H":0,"sc_1th_C":0,"sc_2th_H":0,"sc_2th_C":0,"sc_3th_H":0,"sc_3th_C":0,"sc_4th_H":0,"sc_4th_C":0,"overtime_H":0,"overtime_C":0,"half_H":0,"half_C":0,"full_H":0,"full_C":0,"whole_H":0,"whole_C":0}';
  283. $mark_result = json_decode($mark_result_json,true);
  284. }
  285. //获取总进球数
  286. $all_goal = $mark_result['whole_H']+$mark_result['whole_C'];
  287. //获取比分
  288. $match_score = $mark_result['whole_H'].':'.$mark_result['whole_C'];
  289. //获取获胜球队
  290. if(($mark_result['whole_H']) > ($mark_result['whole_C'])){
  291. $match_winer = $v->home_team;
  292. }else{
  293. $match_winer = $v->guest_team;
  294. }
  295. //拼接赛事主队比分 {"1":0,"2":0,"3":0,"4":0}
  296. $home_score = [
  297. '1'=>$mark_result['sc_1th_H'],
  298. '2'=>$mark_result['sc_2th_H'],
  299. '3'=>$mark_result['sc_3th_H'],
  300. '4'=>$mark_result['sc_4th_H'],
  301. ];
  302. //拼接赛事客队比分
  303. $guest_score = [
  304. '1'=>$mark_result['sc_1th_C'],
  305. '2'=>$mark_result['sc_2th_C'],
  306. '3'=>$mark_result['sc_3th_C'],
  307. '4'=>$mark_result['sc_4th_C'],
  308. ];
  309. //拼接赛事主客队比分
  310. $inning = [
  311. 1=>[
  312. "home"=>$mark_result['sc_1th_H'],
  313. "guest"=>$mark_result['sc_1th_C'],
  314. ],
  315. 2=>[
  316. "home"=>$mark_result['sc_2th_H'],
  317. "guest"=>$mark_result['sc_2th_C'],
  318. ],
  319. 3=>[
  320. "home"=>$mark_result['sc_3th_H'],
  321. "guest"=>$mark_result['sc_3th_C'],
  322. ],
  323. 4=>[
  324. "home"=>$mark_result['sc_4th_H'],
  325. "guest"=>$mark_result['sc_4th_C'],
  326. ],
  327. 5=>[//上半场
  328. "home"=>$mark_result['sc_1th_H']+$mark_result['sc_2th_H'],
  329. "guest"=>$mark_result['sc_1th_C']+$mark_result['sc_2th_C'],
  330. ],
  331. 6=>[//下半场
  332. "home"=>$mark_result['sc_3th_H']+$mark_result['sc_4th_H'],
  333. "guest"=>$mark_result['sc_3th_C']+$mark_result['sc_4th_C'],
  334. ],
  335. 'all'=>[
  336. "home"=>$mark_result['sc_1th_H']+$mark_result['sc_2th_H']+$mark_result['sc_3th_H']+$mark_result['sc_4th_H'],
  337. "guest"=>$mark_result['sc_1th_C']+$mark_result['sc_2th_C']+$mark_result['sc_3th_C']+$mark_result['sc_4th_C'],
  338. ],
  339. ];
  340. //===追加获取手动结果数据===
  341. $manual_result_json = commonFunction::getManual_result($manual_result_data,$inning,$v->match_id);
  342. //赛事待写入赛果数据
  343. $set_match_r = [
  344. "match_id"=> $v->match_id,
  345. "status"=>2,
  346. "update_time"=>date('Y-m-d H:i:s'),
  347. "home_rate"=> $v->home_rate?:0, //主队让球
  348. "guest_rate"=> $v->guest_rate?:0, //客队让球
  349. "home_score"=> json_encode($home_score)?:'', //主队进球数
  350. "guest_score"=> json_encode($guest_score)?:'', //客队进球数
  351. "inning"=> json_encode($inning)?:'', //主客队进球数
  352. "all_goal"=> $all_goal?:0, //总进球数
  353. // "first_score"=> $result_4['first_score']?:'', //最先进球球队
  354. // "last_score"=> $result_4['last_score']?:'', //最后进球球队
  355. "match_score"=> $match_score?:0, //赛事比分
  356. "match_winer"=> $match_winer?:'',//获胜球队
  357. "match_process"=> $mark_result['schedule']?:'',//比赛进程
  358. "u_home_score"=> $mark_result['half_H']?:0,//上半场主队进球数
  359. "u_guest_score"=> $mark_result['half_C']?:0,//上半场客队进球数
  360. "is_correct"=> -1,//自动比分
  361. "manual_result"=> $manual_result_json,//手动结果
  362. ];
  363. $ret = $model['model_result']::where(['match_id' => $v->match_id,'is_correct'=>-1])
  364. ->update($set_match_r);
  365. // if($ret < 1) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  366. }
  367. }
  368. return 1;
  369. }
  370. //计算滚球 赛事进行时间
  371. public static function secTime($sec=0){
  372. $min = floor($sec/60);
  373. $res = $min.':'.($sec-$min*60);
  374. return $res;
  375. }
  376. }