StWqResult.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 StWqResult
  9. * @package App\Http\Model
  10. * 网球 赛事 结果
  11. */
  12. class StWqResult extends Model
  13. {
  14. protected $table = 'st_wq_result';
  15. public $timestamps = false;
  16. /*
  17. * 写赛事结果
  18. * 弃用
  19. */
  20. public static function WQresult__($model){
  21. //获取赛事表7天内所有赛事
  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. 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("-7 day"))]])
  31. ->get()
  32. ->toarray();
  33. //结果表无数据,直接插入
  34. if(empty($matchData_r)){
  35. foreach ($matchData as $k=>$v){
  36. $start_time = ($v['match_date'].' '.$v['match_time']);
  37. $time = time()-strtotime($v['match_time']);
  38. $match_time = self::secTime($time);
  39. $set_match_r[] = [
  40. "match_id"=> $v['id'],
  41. "home_player_name"=>$v['home_team'],
  42. "guest_player_name"=>$v['guest_team'],
  43. "lg_id"=>$v['lg_id'],
  44. "status"=>$v['status'],
  45. "tag"=> $v['tag'],
  46. 'match_time'=>$match_time,
  47. "ctime"=>date('Y-m-d H:i:s'),
  48. "update_time"=>date('Y-m-d H:i:s'),
  49. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time))
  50. ];
  51. }
  52. $ret = $model['model_result']::insert($set_match_r);
  53. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  54. }else {
  55. //如果结果表有数据,则获取结果表没有的赛事
  56. foreach ($matchData as $k => $v) {
  57. foreach ($matchData_r as $kk => $vv) {
  58. if ($v['id'] == $vv['match_id']) {
  59. unset($matchData[$k]);
  60. }
  61. }
  62. }
  63. //如果还有未写入赛事
  64. if (!empty($matchData)) {
  65. //写入结果表不存在赛事
  66. foreach ($matchData as $k => $v) {
  67. $start_time = ($v['match_date'] . ' ' . $v['match_time']);
  68. $time = time() - strtotime($v['match_time']);
  69. $match_time = self::secTime($time);
  70. $set_match_r[] = [
  71. "match_id" => $v['id'],
  72. "home_player_name" => $v['home_team'],
  73. "guest_player_name" => $v['guest_team'],
  74. "lg_id" => $v['lg_id'],
  75. "status" => $v['status'],
  76. "tag" => $v['tag'],
  77. 'match_time' => $match_time,
  78. "ctime" => date('Y-m-d H:i:s'),
  79. "update_time" => date('Y-m-d H:i:s'),
  80. "start_time" => date('Y-m-d H:i:s', strtotime($start_time))
  81. ];
  82. }
  83. $ret = $model['model_result']::insert($set_match_r);
  84. if ($ret != true) throw new \Exception(Response::generate('', Response::ADD_MATCH_R_ERROR));//Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
  85. }
  86. }
  87. }
  88. public static function WQresult($model){
  89. //获取赛事表7天内所有赛事
  90. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  91. ->where([['match_date','>',date('Y-m-d',strtotime("-2 day"))],['status','=',2]])
  92. ->get()
  93. ->toArray();
  94. //获取赛事结果表 一天内
  95. $matchData_r = $model['model_result']::select('match_id')
  96. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-2 day"))]])
  97. ->get()
  98. ->toArray();
  99. if(!empty($matchData_r)){
  100. //如果结果表有数据,则获取结果表没有的赛事
  101. foreach ($matchData as $k => $v) {
  102. foreach ($matchData_r as $kk => $vv) {
  103. if ($v['id'] == $vv['match_id']) {
  104. unset($matchData[$k]);
  105. }
  106. }
  107. }
  108. }
  109. //没有数据,无需操作
  110. if(empty($matchData)) return Response::success();
  111. //获取赛事id 用于获取赛事对应结果比分
  112. $match_ids = [];
  113. foreach($matchData as $k =>$v){
  114. //只获取赛事已结束的
  115. if($v['status'] == 2){
  116. $match_ids[] = $v['id'];
  117. }
  118. }
  119. $match_ids_str = implode(",", $match_ids);
  120. //组装sql match_process = '第一节' and
  121. //获取赛事最终赛果
  122. $sql_result = "select a.match_id,a.home_player_score,a.guest_player_score,a.match_time as a_time,a.home_player_let_plate,a.guest_player_let_plate,a.home_player_let_inning,a.guest_player_let_inning,a.all_inning,a.match_process,a.first_score_player,a.last_score_player,a.first_inning_score,a.second_inning_score,a.third_inning_score,a.match_winer_player,a.result_mark from st_wq_result_record a,
  123. (select match_id,max(id) id from st_wq_result_record where match_id IN ($match_ids_str) group by match_id)b
  124. where a.match_id = b.match_id and a.id = b.id ";
  125. //赛事最终结果
  126. $match_result = DB::select($sql_result);
  127. //拼装赛事结果数据
  128. $set_match_r = [];
  129. if(!empty($match_result)){
  130. foreach($matchData as $k =>$v ){
  131. //获取开赛时间
  132. $start_time = ($v['match_date'].' '.$v['match_time']);
  133. $time = time()-strtotime($v['match_time']);
  134. $match_time = self::secTime($time);
  135. //获取赛事每节的赛果
  136. $resultData = commonFunction::filter_by_value($match_result,'match_id',$v['id']);
  137. if(!empty($resultData)){
  138. //获取赛果json
  139. $result_mark = json_decode($resultData['result_mark'],true);
  140. //获取进程
  141. $match_process = $result_mark['schedule'];
  142. //获胜球员
  143. $match_winer = '';
  144. if($result_mark['game_num_H'] > $result_mark['game_num_C']){
  145. $match_winer = $v['home_team'];
  146. }else{
  147. $match_winer = $v['guest_team'];
  148. }
  149. dd($result_mark['best']);
  150. //组装输赢结果json
  151. if($result_mark['best'] == 3)
  152. $inning = [
  153. 1=>[
  154. "home"=>[
  155. 1=>1,
  156. 2=>0,
  157. 3=>1,
  158. 4=>1,
  159. ],
  160. "guest"=>[
  161. 1=>1,
  162. 2=>0,
  163. 3=>1,
  164. 4=>1,
  165. ],
  166. ],
  167. 2=>[
  168. "home"=>[
  169. 1=>1,
  170. 2=>0,
  171. 3=>1,
  172. 4=>1,
  173. ],
  174. "guest"=>[
  175. 1=>1,
  176. 2=>0,
  177. 3=>1,
  178. 4=>1,
  179. ],
  180. ],
  181. 3=>[
  182. "home"=>[
  183. 1=>1,
  184. 2=>0,
  185. 3=>1,
  186. 4=>1,
  187. ],
  188. "guest"=>[
  189. 1=>1,
  190. 2=>0,
  191. 3=>1,
  192. 4=>1,
  193. ],
  194. ],
  195. ];
  196. dd(json_encode($inning));
  197. //赛事待写入赛果数据
  198. $set_match_r[] = [
  199. "match_id"=> $v['id'],
  200. "home_player_name"=>$v['home_team'],
  201. "guest_player_name"=>$v['guest_team'],
  202. "lg_id"=>$v['lg_id'],
  203. "status"=>$v['status'],
  204. "tag"=> $v['tag'],
  205. 'match_time'=>$resultData['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_player_let_plate"=> $resultData['home_player_let_plate']?:0, //主队让盘
  210. "guest_player_let_plate"=> $resultData['guest_player_let_plate']?:0, //客队让盘
  211. "home_player_let_inning"=> $resultData['home_player_let_inning']?:0, //主队让局
  212. "guest_player_let_inning"=> $resultData['guest_player_let_inning']?:0, //客队让局
  213. "home_player_score"=> json_encode($home_score)?:'', //主队得分
  214. "guest_player_score"=> json_encode($guest_score)?:'', //客队得分
  215. "all_inning"=> $resultData['all_inning']?:0, //总局数
  216. "first_score_player"=> $resultData['first_score']?:'', //最先得分
  217. "last_score_player"=> $resultData['last_score']?:'', //最后得分
  218. "first_inning_score"=> $resultData['first_inning_score']?:0, //第一局比分
  219. "second_inning_score"=> $resultData['second_inning_score']?:0,//第二局比分
  220. "third_inning_score"=> $resultData['third_inning_score']?:0,//第三局比分
  221. // "match_score"=> ,//赛事比分
  222. "match_winer_player"=> $match_winer?:'',//获胜队员
  223. "match_process"=> $match_process?:'',//比赛进程
  224. // "inning"=> ,//每局输赢结果
  225. ];
  226. }
  227. }
  228. }
  229. //写入赛果
  230. if(!empty($set_match_r)){
  231. $ret = $model['model_result']::insert($set_match_r);
  232. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  233. }
  234. return 1;
  235. /*
  236. //没有数据,无需操作
  237. if(empty($matchData)) return Response::success();
  238. //获取赛事结果表 所有当月
  239. $matchData_r = $model['model_result']::select('match_id')
  240. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-7 day"))]])
  241. ->get()
  242. ->toarray();
  243. //结果表无数据,直接插入
  244. if(empty($matchData_r)){
  245. foreach ($matchData as $k=>$v){
  246. $start_time = ($v['match_date'].' '.$v['match_time']);
  247. $time = time()-strtotime($v['match_time']);
  248. $match_time = self::secTime($time);
  249. $set_match_r[] = [
  250. "match_id"=> $v['id'],
  251. "home_player_name"=>$v['home_team'],
  252. "guest_player_name"=>$v['guest_team'],
  253. "lg_id"=>$v['lg_id'],
  254. "status"=>$v['status'],
  255. "tag"=> $v['tag'],
  256. 'match_time'=>$match_time,
  257. "ctime"=>date('Y-m-d H:i:s'),
  258. "update_time"=>date('Y-m-d H:i:s'),
  259. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time))
  260. ];
  261. }
  262. $ret = $model['model_result']::insert($set_match_r);
  263. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  264. }else {
  265. //如果结果表有数据,则获取结果表没有的赛事
  266. foreach ($matchData as $k => $v) {
  267. foreach ($matchData_r as $kk => $vv) {
  268. if ($v['id'] == $vv['match_id']) {
  269. unset($matchData[$k]);
  270. }
  271. }
  272. }
  273. //如果还有未写入赛事
  274. if (!empty($matchData)) {
  275. //写入结果表不存在赛事
  276. foreach ($matchData as $k => $v) {
  277. $start_time = ($v['match_date'] . ' ' . $v['match_time']);
  278. $time = time() - strtotime($v['match_time']);
  279. $match_time = self::secTime($time);
  280. $set_match_r[] = [
  281. "match_id" => $v['id'],
  282. "home_player_name" => $v['home_team'],
  283. "guest_player_name" => $v['guest_team'],
  284. "lg_id" => $v['lg_id'],
  285. "status" => $v['status'],
  286. "tag" => $v['tag'],
  287. 'match_time' => $match_time,
  288. "ctime" => date('Y-m-d H:i:s'),
  289. "update_time" => date('Y-m-d H:i:s'),
  290. "start_time" => date('Y-m-d H:i:s', strtotime($start_time))
  291. ];
  292. }
  293. $ret = $model['model_result']::insert($set_match_r);
  294. if ($ret != true) throw new \Exception(Response::generate('', Response::ADD_MATCH_R_ERROR));//Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
  295. }
  296. }
  297. */
  298. }
  299. //计算滚球 赛事进行时间
  300. public static function secTime($sec=0){
  301. $min = floor($sec/60);
  302. $res = $min.':'.($sec-$min*60);
  303. return $res;
  304. }
  305. }