StLqResult.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 App\Lib\Biz\Sport\Common as commonFunction;
  7. /**
  8. * Class StLqResult
  9. * @package App\Http\Model
  10. * 篮球 赛事 结果
  11. */
  12. class StLqResult extends Model
  13. {
  14. protected $table = 'st_lq_result';
  15. public $timestamps = false;
  16. /*
  17. * 写赛事结果
  18. * 弃用
  19. */
  20. public static function LQresult___($model){
  21. dd(123);
  22. //获取赛事表7天内所有赛事
  23. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  24. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-1 day"))]])
  25. ->get()
  26. ->toarray();
  27. //没有数据,无需操作
  28. if(empty($matchData)) return Response::success();
  29. //获取赛事结果表 15天内
  30. $matchData_r = $model['model_result']::select('match_id')
  31. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-1 day"))]])
  32. ->get()
  33. ->toarray();
  34. //结果表无数据,直接插入
  35. if(empty($matchData_r)){
  36. foreach ($matchData as $k=>$v){
  37. $process = 0;
  38. if((int)$v['status'] == 2){
  39. $process = 4;
  40. }
  41. if((int)$v['status'] == 1){
  42. $process = 1;
  43. }
  44. $start_time = ($v['match_date'].' '.$v['match_time']);
  45. $set_match_r[] = [
  46. "match_id"=> $v['id'],
  47. "home_team"=>$v['home_team'],
  48. "guest_team"=>$v['guest_team'],
  49. "lg_id"=>$v['lg_id'],
  50. "status"=>$v['status'],
  51. "tag"=> $v['tag'],
  52. "match_process"=>$process,
  53. 'match_time'=>12,
  54. "ctime"=>date('Y-m-d H:i:s'),
  55. "update_time"=>date('Y-m-d H:i:s'),
  56. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time))
  57. ];
  58. }
  59. $ret = $model['model_result']::insert($set_match_r);
  60. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  61. }else{
  62. //如果结果表有数据,则获取结果表没有的赛事
  63. foreach ($matchData as $k=>$v){
  64. foreach ($matchData_r as $kk=>$vv){
  65. if($v['id'] == $vv['match_id']){
  66. unset($matchData[$k]);
  67. }
  68. }
  69. }
  70. //如果还有未写入赛事
  71. if(!empty($matchData)){
  72. //写入结果表不存在赛事
  73. foreach ($matchData as $k=>$v){
  74. $process = 0;
  75. if((int)$v['status'] == 2){
  76. $process = 4;
  77. }
  78. if((int)$v['status'] == 1){
  79. $process = 1;
  80. }
  81. $start_time = ($v['match_date'].' '.$v['match_time']);
  82. $set_match_r[] = [
  83. "match_id"=> $v['id'],
  84. "home_team"=>$v['home_team'],
  85. "guest_team"=>$v['guest_team'],
  86. "lg_id"=>$v['lg_id'],
  87. "status"=>$v['status'],
  88. "tag"=> $v['tag'],
  89. "match_process"=>$process,
  90. 'match_time'=>12,
  91. "ctime"=>date('Y-m-d H:i:s'),
  92. "update_time"=>date('Y-m-d H:i:s'),
  93. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time))
  94. ];
  95. }
  96. $ret = $model['model_result']::insert($set_match_r);
  97. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));//Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
  98. }
  99. }
  100. }
  101. /*
  102. * 写赛事结果
  103. */
  104. public static function LQresult($model){
  105. //获取当天开始并且已结束的所有赛事
  106. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  107. ->where([['match_date','>',date('Y-m-d',strtotime("-2 day"))],['status','=',2]])
  108. ->get()
  109. ->toArray();
  110. //获取赛事结果表 一天内
  111. $matchData_r = $model['model_result']::select('match_id')
  112. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-2 day"))]])
  113. ->get()
  114. ->toArray();
  115. if(!empty($matchData_r)){
  116. //如果结果表有数据,则获取结果表没有的赛事
  117. foreach ($matchData as $k => $v) {
  118. foreach ($matchData_r as $kk => $vv) {
  119. if ($v['id'] == $vv['match_id']) {
  120. unset($matchData[$k]);
  121. }
  122. }
  123. }
  124. }
  125. //没有数据,无需操作
  126. if(empty($matchData)) return Response::success();
  127. //获取赛事id 用于获取赛事对应结果比分
  128. $match_ids = [];
  129. foreach($matchData as $k =>$v){
  130. //只获取赛事已结束的
  131. if($v['status'] == 2){
  132. $match_ids[] = $v['id'];
  133. }
  134. }
  135. $match_ids_str = implode(",", $match_ids);
  136. //第一节
  137. $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,
  138. (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
  139. where a.match_id = b.match_id and a.id = b.id ";
  140. //第二节
  141. $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,
  142. (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
  143. where a.match_id = b.match_id and a.id = b.id ";
  144. //第三节
  145. $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,
  146. (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
  147. where a.match_id = b.match_id and a.id = b.id ";
  148. //第四节
  149. $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,
  150. (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
  151. where a.match_id = b.match_id and a.id = b.id ";
  152. //第一节最终结果
  153. $match_result_1 = DB::select($sql_1);
  154. //第二节最终结果
  155. $match_result_2 = DB::select($sql_2);
  156. //第三节最终结果
  157. $match_result_3 = DB::select($sql_3);
  158. //第四节最终结果
  159. $match_result_4 = DB::select($sql_4);
  160. //拼装赛事结果数据
  161. $set_match_r = [];
  162. if(!empty($match_result_1) and !empty($match_result_2) and !empty($match_result_3) and !empty($match_result_4)){
  163. foreach($matchData as $k =>$v ){
  164. //获取开赛时间
  165. $start_time = ($v['match_date'].' '.$v['match_time']);
  166. $time = time()-strtotime($v['match_time']);
  167. $match_time = self::secTime($time);
  168. //获取赛事每节的赛果
  169. $result_1 = commonFunction::filter_by_value($match_result_1,'match_id',$v['id']);
  170. $result_2 = commonFunction::filter_by_value($match_result_2,'match_id',$v['id']);
  171. $result_3 = commonFunction::filter_by_value($match_result_3,'match_id',$v['id']);
  172. $result_4 = commonFunction::filter_by_value($match_result_4,'match_id',$v['id']);
  173. if(!empty($result_1) and !empty($result_2) and !empty($result_3) and !empty($result_4)){
  174. //拼接赛事主队比分 {"1":0,"2":0,"3":0,"4":0}
  175. $home_score = [
  176. '1'=>$result_1['home_score'],
  177. '2'=>$result_2['home_score'] - $result_1['home_score'],
  178. '3'=>$result_3['home_score'] - $result_2['home_score'],
  179. '4'=>$result_4['home_score'] - $result_3['home_score'],
  180. ];
  181. //拼接赛事客队比分
  182. $guest_score = [
  183. '1'=>$result_1['guest_score'],
  184. '2'=>$result_2['guest_score'] - $result_1['guest_score'],
  185. '3'=>$result_3['guest_score'] - $result_2['guest_score'],
  186. '4'=>$result_4['guest_score'] - $result_3['guest_score'],
  187. ];
  188. //获取获胜球队
  189. if(($result_4['home_score']) > ($result_4['guest_score'])){
  190. $match_winer = $v['home_team'];
  191. }else{
  192. $match_winer = $v['guest_team'];
  193. }
  194. //获取总进球数
  195. $all_goal = ($result_4['home_score'])+($result_4['guest_score']);
  196. //赛事待写入赛果数据
  197. $set_match_r[] = [
  198. "match_id"=> $v['id'],
  199. "home_team"=>$v['home_team'],
  200. "guest_team"=>$v['guest_team'],
  201. "lg_id"=>$v['lg_id'],
  202. "status"=>$v['status'],
  203. "tag"=> $v['tag'],
  204. 'match_time'=>$result_4['a_time']?:0,//比赛进行时间
  205. "ctime"=>date('Y-m-d H:i:s'),
  206. "update_time"=>date('Y-m-d H:i:s'),
  207. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  208. "home_rate"=> $result_4['home_rate']?:0, //主队让球
  209. "guest_rate"=> $result_4['guest_rate']?:0, //客队让球
  210. "home_score"=> json_encode($home_score)?:'', //主队进球数
  211. "guest_score"=> json_encode($guest_score)?:'', //客队进球数
  212. "all_goal"=> $all_goal?:0, //总进球数
  213. // "first_score"=> $result_4['first_score']?:'', //最先进球球队
  214. // "last_score"=> $result_4['last_score']?:'', //最后进球球队
  215. "match_score"=> $result_4['match_score']?:0, //赛事比分
  216. "match_winer"=> $match_winer?:'',//获胜球队
  217. "match_process"=> $result_4['match_process']?:'',//比赛进程
  218. "u_home_score"=> $result_2['home_score']?:0,//上半场主队进球数
  219. "u_guest_score"=> $result_2['guest_score']?:0,//上半场客队进球数
  220. ];
  221. }
  222. }
  223. }
  224. //写入赛果
  225. if(!empty($set_match_r)){
  226. $ret = $model['model_result']::insert($set_match_r);
  227. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  228. }
  229. return 1;
  230. }
  231. //计算滚球 赛事进行时间
  232. public static function secTime($sec=0){
  233. $min = floor($sec/60);
  234. $res = $min.':'.($sec-$min*60);
  235. return $res;
  236. }
  237. }