StZqResult.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. */
  21. public static function ZQresult__($model){
  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. //获取赛事结果表 所有当月
  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. //获取赛事id
  36. $match_ids = [];
  37. foreach($matchData as $k =>$v){
  38. //只获取赛事已结束的
  39. if($v['status'] == 2){
  40. $match_ids[] = $v['id'];
  41. }
  42. }
  43. $match_ids_str = implode(",", $match_ids);
  44. //上半场
  45. $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,
  46. (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
  47. where a.match_id = b.match_id and a.id = b.id ";
  48. //全场
  49. $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,
  50. (select match_id,max(id) id from st_zq_result_record where match_id IN ($match_ids_str) group by match_id)b
  51. where a.match_id = b.match_id and a.id = b.id ";
  52. //上半场最终结果
  53. $match_result_h = DB::select($sql_h);
  54. //全场最终结果
  55. $match_result_f = DB::select($sql_f);
  56. //拼装赛事结果数据
  57. $match_result_record = [];
  58. if(!empty($match_result_h) and !empty($match_result_f)){
  59. foreach($match_result_h as $k=>$v){
  60. foreach($match_result_f as $kk=>$vv){
  61. if($v->match_id == $vv->match_id){
  62. $match_result_record[$k] = [
  63. "match_id"=>$v->match_id,
  64. "home_score"=> $vv->home_score,
  65. "guest_score"=> $vv->guest_score,
  66. "a_time"=> $vv->a_time,
  67. "match_process"=> $vv->match_process,
  68. "all_goal"=> $vv->all_goal,
  69. "first_score"=> $vv->first_score,
  70. "last_score"=> $vv->last_score,
  71. "match_score"=> $vv->match_score,
  72. "match_winer"=> $vv->match_winer,
  73. "u_home_score"=> $v->home_score,
  74. "u_guest_score"=> $v->guest_score,
  75. "home_rate"=> $vv->home_rate,
  76. "guest_rate"=> $vv->guest_rate,
  77. ];
  78. }
  79. }
  80. }
  81. }
  82. //结果表无数据,直接插入
  83. if(empty($matchData_r)){
  84. foreach ($matchData as $k=>$v){
  85. $start_time = ($v['match_date'].' '.$v['match_time']);
  86. $time = time()-strtotime($v['match_time']);
  87. $match_time = self::secTime($time);
  88. foreach($match_result_record as $kk =>$vv){
  89. if($v['id'] == $vv['match_id']){
  90. $set_match_r[] = [
  91. "match_id"=> $v['id'],
  92. "home_team"=>$v['home_team'],
  93. "guest_team"=>$v['guest_team'],
  94. "lg_id"=>$v['lg_id'],
  95. "status"=>$v['status'],
  96. "tag"=> $v['tag'],
  97. 'match_time'=>$vv['a_time']?:0,//比赛进行时间
  98. "ctime"=>date('Y-m-d H:i:s'),
  99. "update_time"=>date('Y-m-d H:i:s'),
  100. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  101. "home_rate"=> $vv['home_rate']?:0, //主队让球
  102. "guest_rate"=> $vv['guest_rate']?:0, //客队让球
  103. "home_score"=> $vv['home_score']?:0, //主队进球数
  104. "guest_score"=> $vv['guest_score']?:0, //客队进球数
  105. "all_goal"=> $vv['all_goal']?:0, //总进球数
  106. "first_score"=> $vv['first_score']?:'', //最先进球球队
  107. "last_score"=> $vv['last_score']?:'', //最后进球球队
  108. "match_score"=> $vv['match_score']?:0, //赛事比分
  109. "match_winer"=> $vv['match_winer']?:'',//获胜球队
  110. "match_process"=> $vv['match_process']?:'',//比赛进程
  111. "u_home_score"=> $vv['u_home_score']?:0,//上半场主队进球数
  112. "u_guest_score"=> $vv['u_guest_score']?:0,//上半场客队进球数
  113. ];
  114. }
  115. }
  116. }
  117. /*
  118. if(!empty($set_match_r)){
  119. $ret = $model['model_result']::insert($set_match_r);
  120. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  121. }
  122. */
  123. }else {
  124. //如果结果表有数据,则获取结果表没有的赛事
  125. foreach ($matchData as $k => $v) {
  126. foreach ($matchData_r as $kk => $vv) {
  127. if ($v['id'] == $vv['match_id']) {
  128. unset($matchData[$k]);
  129. }
  130. }
  131. }
  132. //如果还有未写入赛事
  133. if (!empty($matchData)) {
  134. //写入结果表不存在赛事
  135. foreach ($matchData as $k => $v) {
  136. $start_time = ($v['match_date'].' '.$v['match_time']);
  137. $time = time()-strtotime($v['match_time']);
  138. // $match_time = self::secTime($time);
  139. foreach($match_result_record as $kk =>$vv){
  140. if($v['id'] == $vv['match_id']){
  141. $set_match_r[] = [
  142. "match_id"=> $v['id'],
  143. "home_team"=>$v['home_team'],
  144. "guest_team"=>$v['guest_team'],
  145. "lg_id"=>$v['lg_id'],
  146. "status"=>$v['status'],
  147. "tag"=> $v['tag'],
  148. 'match_time'=>$vv['a_time']?:0,//比赛进行时间
  149. "ctime"=>date('Y-m-d H:i:s'),
  150. "update_time"=>date('Y-m-d H:i:s'),
  151. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  152. "home_rate"=> $vv['home_rate']?:0, //主队让球
  153. "guest_rate"=> $vv['guest_rate']?:0, //客队让球
  154. "home_score"=> $vv['home_score']?:0, //主队进球数
  155. "guest_score"=> $vv['guest_score']?:0, //客队进球数
  156. "all_goal"=> $vv['all_goal']?:0, //总进球数
  157. "first_score"=> $vv['first_score']?:'', //最先进球球队
  158. "last_score"=> $vv['last_score']?:'', //最后进球球队
  159. "match_score"=> $vv['match_score']?:0, //赛事比分
  160. "match_winer"=> $vv['match_winer']?:'',//获胜球队
  161. "match_process"=> $vv['match_process']?:'',//比赛进程
  162. "u_home_score"=> $vv['u_home_score']?:0,//上半场主队进球数
  163. "u_guest_score"=> $vv['u_guest_score']?:0,//上半场客队进球数
  164. ];
  165. }
  166. }
  167. }
  168. }
  169. }
  170. if(!empty($set_match_r)){
  171. $ret = $model['model_result']::insert($set_match_r);
  172. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  173. }
  174. return 1;
  175. }
  176. /*
  177. * 写赛事结果
  178. */
  179. public static function ZQresult_v1($model){
  180. //获取当天开始并且已结束的所有赛事
  181. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_date','match_time')
  182. ->where([['match_date','>',date('Y-m-d', strtotime("-2 day"))],['status','=',2]])
  183. ->get()
  184. ->toArray();
  185. //获取赛事结果表 一天内
  186. $matchData_r = $model['model_result']::select('match_id')
  187. ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-2 day"))]])
  188. ->get()
  189. ->toArray();
  190. if(!empty($matchData_r)){
  191. //如果结果表有数据,则获取结果表没有的赛事
  192. foreach ($matchData as $k => $v) {
  193. foreach ($matchData_r as $kk => $vv) {
  194. if ($v['id'] == $vv['match_id']) {
  195. unset($matchData[$k]);
  196. }
  197. }
  198. }
  199. }
  200. //没有数据,无需操作
  201. if(empty($matchData)) return Response::success();
  202. //获取赛事id 用于获取赛事对应结果比分
  203. $match_ids = [];
  204. foreach($matchData as $k =>$v){
  205. //只获取赛事已结束的
  206. if($v['status'] == 2){
  207. $match_ids[] = $v['id'];
  208. }
  209. }
  210. $match_ids_str = implode(",", $match_ids);
  211. //上半场
  212. $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,
  213. (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
  214. where a.match_id = b.match_id and a.id = b.id ";
  215. //全场
  216. $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,
  217. (select match_id,max(id) id from st_zq_result_record where match_id IN ($match_ids_str) group by match_id)b
  218. where a.match_id = b.match_id and a.id = b.id ";
  219. //上半场最终结果
  220. $match_result_h = DB::select($sql_h);
  221. //全场最终结果
  222. $match_result_f = DB::select($sql_f);
  223. //拼装赛事结果数据
  224. $match_result_record = [];
  225. if(!empty($match_result_h) and !empty($match_result_f)){
  226. foreach($match_result_h as $k=>$v){
  227. foreach($match_result_f as $kk=>$vv){
  228. if($v->match_id == $vv->match_id){
  229. $match_result_record[$k] = [
  230. "match_id"=>$v->match_id,
  231. "home_score"=> $vv->home_score,
  232. "guest_score"=> $vv->guest_score,
  233. "a_time"=> $vv->a_time,
  234. "match_process"=> $vv->match_process,
  235. "all_goal"=> $vv->all_goal,
  236. "first_score"=> $vv->first_score,
  237. "last_score"=> $vv->last_score,
  238. "match_score"=> $vv->match_score,
  239. "match_winer"=> $vv->match_winer,
  240. "u_home_score"=> $v->home_score,
  241. "u_guest_score"=> $v->guest_score,
  242. "home_rate"=> $vv->home_rate,
  243. "guest_rate"=> $vv->guest_rate,
  244. ];
  245. }
  246. }
  247. }
  248. }
  249. //组装赛果数据
  250. if(!empty($match_result_record)){
  251. foreach ($matchData as $k=>$v){
  252. $start_time = ($v['match_date'].' '.$v['match_time']);
  253. $time = time()-strtotime($v['match_time']);
  254. $match_time = self::secTime($time);
  255. foreach($match_result_record as $kk =>$vv){
  256. if($v['id'] == $vv['match_id']){
  257. //获取获胜球队
  258. if(($vv['home_score']) > ($vv['guest_score'])){
  259. $match_winer = $v['home_team'];
  260. }else{
  261. $match_winer = $v['guest_team'];
  262. }
  263. $set_match_r[] = [
  264. "match_id"=> $v['id'],
  265. "home_team"=>$v['home_team'],
  266. "guest_team"=>$v['guest_team'],
  267. "lg_id"=>$v['lg_id'],
  268. "status"=>$v['status'],
  269. "tag"=> $v['tag'],
  270. 'match_time'=>$vv['a_time']?:0,//比赛进行时间
  271. "ctime"=>date('Y-m-d H:i:s'),
  272. "update_time"=>date('Y-m-d H:i:s'),
  273. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  274. "home_rate"=> $vv['home_rate']?:0, //主队让球
  275. "guest_rate"=> $vv['guest_rate']?:0, //客队让球
  276. "home_score"=> $vv['home_score']?:0, //主队进球数
  277. "guest_score"=> $vv['guest_score']?:0, //客队进球数
  278. "all_goal"=> $vv['all_goal']?:0, //总进球数
  279. // "first_score"=> $vv['first_score']?:'', //最先进球球队
  280. // "last_score"=> $vv['last_score']?:'', //最后进球球队
  281. "match_score"=> $vv['match_score']?:0, //赛事比分
  282. "match_winer"=> $match_winer?:'',//获胜球队
  283. "match_process"=> $vv['match_process']?:'',//比赛进程
  284. "u_home_score"=> $vv['u_home_score']?:0,//上半场主队进球数
  285. "u_guest_score"=> $vv['u_guest_score']?:0,//上半场客队进球数
  286. ];
  287. }
  288. }
  289. }
  290. }
  291. //写入赛果
  292. if(!empty($set_match_r)){
  293. $ret = $model['model_result']::insert($set_match_r);
  294. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  295. }
  296. return 1;
  297. }
  298. /**
  299. * 获取自动赛事结果
  300. * 只更新已结束+未手动修改的赛事结果
  301. * $match_id 本地赛事id 为0时 则用于后台手动刷新赛果
  302. */
  303. public static function ZQresult_v2($model,$match_id = 0){
  304. //如果有赛事id 则只更新当前赛事结果
  305. if($match_id > 0){
  306. $match_ids_str = $match_id;
  307. $match_ids = [$match_id];
  308. }else{
  309. //获取两天内的结束赛事
  310. $matchData = $model['model_match']
  311. ->join('st_zq_result','st_zq_result.match_id','=','st_zq_competition.id')
  312. ->select('st_zq_competition.id','is_correct','manual_result')
  313. ->where([['st_zq_competition.match_date','>',date('Y-m-d', strtotime("-2 day"))],['st_zq_competition.status','=',2],['st_zq_result.is_correct','=',-1]])//
  314. ->get()
  315. ->toArray();
  316. //没有数据,无需操作
  317. if(empty($matchData)) return Response::success();
  318. //获取需更新结果的赛事ID
  319. $match_ids = [];
  320. foreach($matchData as $k =>$v){
  321. //未手动修改比分
  322. if($v['is_correct'] == 0){
  323. $match_ids[] = $v['id'];
  324. }
  325. }
  326. if(empty($match_ids)) return Response::success();
  327. $match_ids_str = implode(",", $match_ids);
  328. }
  329. //如果有赛事id 获取结果中的手动数据
  330. if(!empty($match_ids)){
  331. $manual_result_data = self::select('match_id','manual_result')
  332. ->whereIn('match_id',$match_ids)
  333. ->get()
  334. ->toArray();
  335. }
  336. //上半场
  337. $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,
  338. (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
  339. where a.match_id = b.match_id and a.id = b.id ";
  340. //全场
  341. $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,
  342. (select match_id,max(id) id from st_zq_result_record where match_id IN ($match_ids_str) group by match_id)b
  343. where a.match_id = b.match_id and a.id = b.id ";
  344. //上半场最终结果
  345. $match_result_h = DB::select($sql_h);
  346. //全场最终结果
  347. $match_result_f = DB::select($sql_f);
  348. //拼装赛事结果数据
  349. $match_result_record = [];
  350. if(!empty($match_result_h) and !empty($match_result_f)){
  351. foreach($match_result_h as $k=>$v){
  352. foreach($match_result_f as $kk=>$vv){
  353. if($v->match_id == $vv->match_id){
  354. $match_result_record[$k] = [
  355. "match_id"=>$v->match_id,
  356. "home_team"=>$v->home_team,
  357. "guest_team"=>$v->guest_team,
  358. "home_score"=> $vv->home_score,
  359. "guest_score"=> $vv->guest_score,
  360. "a_time"=> $vv->a_time,
  361. "match_process"=> $vv->match_process,
  362. "all_goal"=> $vv->all_goal,
  363. "first_score"=> $vv->first_score,
  364. "last_score"=> $vv->last_score,
  365. "match_score"=> $vv->match_score,
  366. "match_winer"=> $vv->match_winer,
  367. "u_home_score"=> $v->home_score,
  368. "u_guest_score"=> $v->guest_score,
  369. "home_rate"=> $vv->home_rate,
  370. "guest_rate"=> $vv->guest_rate,
  371. "corner_result" => $vv->corner_result,
  372. "penalty_result" => $vv->penalty_result,
  373. ];
  374. }
  375. }
  376. }
  377. }
  378. if(!empty($match_result_record)){
  379. foreach($match_result_record as $k=>$v){
  380. //获取获胜球队
  381. if(($v['home_score']) > ($v['guest_score'])){
  382. $match_winer = $v['home_team'];
  383. }else{
  384. $match_winer = $v['guest_team'];
  385. }
  386. //===追加获取手动结果数据===
  387. $manual_result_json = self::getManual_result($manual_result_data,$v,$v['match_id']);
  388. $set_match_r = [
  389. "match_id"=> $v['match_id'],
  390. "update_time"=>date('Y-m-d H:i:s'),
  391. "status"=>2,//已结束
  392. "home_rate"=> $v['home_rate']?:0, //主队让球
  393. "guest_rate"=> $v['guest_rate']?:0, //客队让球
  394. "home_score"=> $v['home_score']?:0, //主队进球数
  395. "guest_score"=> $v['guest_score']?:0, //客队进球数
  396. "all_goal"=> $v['all_goal']?:0, //总进球数
  397. // "first_score"=> $vv['first_score']?:'', //最先进球球队
  398. // "last_score"=> $vv['last_score']?:'', //最后进球球队
  399. "match_score"=> $v['match_score']?:0, //赛事比分
  400. "match_winer"=> $match_winer?:'',//获胜球队
  401. "match_process"=> $v['match_process']?:'',//比赛进程
  402. "u_home_score"=> $v['u_home_score']?:0,//上半场主队进球数
  403. "u_guest_score"=> $v['u_guest_score']?:0,//上半场客队进球数
  404. "is_correct"=> -1,//自动比分
  405. "manual_result"=> $manual_result_json,//手动结果
  406. "corner_ball" => $v['corner_result'],//角球结果
  407. "penalty_card" => $v['penalty_result'],//罚牌结果
  408. ];
  409. $ret = $model['model_result']::where(['match_id' => $v['match_id'],'is_correct'=>-1])
  410. ->update($set_match_r);
  411. // if($ret < 1) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  412. }
  413. }
  414. return 1;
  415. }
  416. /**
  417. * 处理 拼接手动结果数据
  418. * $manual_result_data arr 多个赛事手动结果数据
  419. * $match_id int 赛事id
  420. */
  421. public static function getManual_result($manual_result_data,$auto_result,$match_id){
  422. $resultData = commonFunction::filter_by_value($manual_result_data,'match_id',$match_id);
  423. //手动结果数据 json
  424. $manual_result_json = $resultData['manual_result'];
  425. //手动结果数据 arr
  426. $manual_result_arr = json_decode($manual_result_json,true);
  427. //角球数据
  428. $corner_result_arr = json_decode($auto_result['corner_result'],true);
  429. //罚牌数据
  430. $penalty_result_arr = json_decode($auto_result['penalty_result'],true);
  431. //拼接手动数据
  432. foreach($manual_result_arr as $kk=>$vv){
  433. //上半场角球
  434. if($kk == 'half_corner'){
  435. //赋值 上半场角球
  436. if(empty($vv['home']) || empty($vv['guest'])){
  437. $vv['home'] = $corner_result_arr['home_half']?:0;
  438. $vv['guest'] = $corner_result_arr['guest_half']?:0;
  439. }
  440. }
  441. //全场角球
  442. if($kk == 'all_corner'){
  443. if(empty($vv['home']) || empty($vv['guest'])){
  444. $vv['home'] = $corner_result_arr['home']?:0;
  445. $vv['guest'] = $corner_result_arr['guest']?:0;
  446. }
  447. }
  448. //上半场比分
  449. if($kk == 'half'){
  450. if(empty($vv['home']) || empty($vv['guest'])){
  451. $vv['home'] = $auto_result['u_home_score']?:0;
  452. $vv['guest'] = $auto_result['u_guest_score']?:0;
  453. }
  454. }
  455. //全场比分
  456. if($kk == 'all'){
  457. if(empty($vv['home']) || empty($vv['guest'])){
  458. $vv['home'] = $auto_result['home_score']?:0;
  459. $vv['guest'] = $auto_result['guest_score']?:0;
  460. }
  461. }
  462. //上半场罚牌
  463. if($kk == 'half_penalty'){
  464. if(empty($vv['home']) || empty($vv['guest'])){
  465. $vv['home'] = $penalty_result_arr['home_half']?:0;
  466. $vv['guest'] = $penalty_result_arr['guest_half']?:0;
  467. }
  468. }
  469. //全场罚牌
  470. if($kk == 'all_penalty'){
  471. if(empty($vv['home']) || empty($vv['guest'])){
  472. $vv['home'] = $penalty_result_arr['home_half']?:0;
  473. $vv['guest'] = $penalty_result_arr['guest_half']?:0;
  474. }
  475. }
  476. //首进球时间
  477. if($kk == 'first_score'){
  478. if(empty($vv['home']) || empty($vv['guest'])){
  479. $vv['time'] = 0;
  480. }
  481. }
  482. $manual_result_arr[$kk] =$vv;
  483. }
  484. //转回json
  485. $manual_result_json = json_encode($manual_result_arr);
  486. return $manual_result_json;
  487. }
  488. //计算滚球 赛事进行时间
  489. public static function secTime($sec=0){
  490. $min = floor($sec/60);
  491. $res = $min.':'.($sec-$min*60);
  492. return $res;
  493. }
  494. /**
  495. * 写入预植赛果
  496. */
  497. public static function set_result($opt = []){
  498. if(!empty($opt)){
  499. //赛事数据
  500. $data = $opt['data'];
  501. //获取开始时间
  502. $start_time = ($data['match_date'].' '.$data['match_time']);
  503. $match_result = [
  504. "match_id"=> $opt['match_id'],
  505. "lg_id"=>$opt['lg_id'],
  506. "status"=>$data['status'],
  507. "tag"=> 0,
  508. 'match_time'=>0,//比赛进行时间
  509. "ctime"=>date('Y-m-d H:i:s'),
  510. "update_time"=>date('Y-m-d H:i:s'),
  511. "is_correct" => -1,
  512. "start_time"=>date('Y-m-d H:i:s',strtotime($start_time)),
  513. ];
  514. //根据球类赋值
  515. if($opt['game_code'] == 'zq'){//足球
  516. $match_result['home_team'] = $data['home_team'];
  517. $match_result['guest_team'] = $data['guest_team'];
  518. }
  519. if($opt['game_code'] == 'lq'){//篮球
  520. $match_result['home_team'] = $data['home_team'];
  521. $match_result['guest_team'] = $data['guest_team'];
  522. }
  523. if($opt['game_code'] == 'wq'){//网球
  524. $match_result['home_player_name'] = $data['home_team'];
  525. $match_result['guest_player_name'] = $data['guest_team'];
  526. }
  527. if($opt['game_code'] == 'bq'){//棒球
  528. $match_result['home_team'] = $data['home_team'];
  529. $match_result['guest_team'] = $data['guest_team'];
  530. }
  531. //获取model
  532. $models = $opt['models'];
  533. //执行写入
  534. $ret_id = $models['model_result']::insertGetId($match_result);
  535. //如果失败,返回异常
  536. if ($ret_id < 1) throw new \Exception(Response::generate($opt['gameName'] . '赛事-match_id:' . $opt['match_id'] . ';', Response::SET_MATCH_ERR));
  537. }
  538. }
  539. /**
  540. * 更新赛事 危险球 数据
  541. */
  542. public static function set_result_warn($match_id = 0,$warn_data=[]){
  543. //获取当前赛事已有危险球数据 json
  544. $warn_json = self::where(['match_id' => $match_id])->SELECT('warn_more')->first()->warn_more;
  545. //转数组
  546. $warn_arr = json_decode($warn_json,true);
  547. //删除初始 危险球 数据
  548. foreach($warn_arr as $k=>$v){
  549. if($v['rtype'] == 0) unset($warn_arr[$k]);
  550. }
  551. if($match_id > 0 and !empty($warn_data)){
  552. $data = [];
  553. foreach($warn_data as $k=>$v){
  554. $data[$k]['timei'] = $v['find_time'];//危险球时间
  555. $data[$k]['rtype'] = $v['warn_name'];//危险球名称
  556. $data[$k]['warn_type'] = $v['warn_type'];//危险球类型 1 进球无效 2红卡无效 3无效(用于角球)
  557. $data[$k]['timep'] = 90;
  558. }
  559. //合并危险球数据
  560. $warn_data_new = array_merge($warn_arr,$data);
  561. //转json
  562. $warn_data = json_encode($warn_data_new);
  563. //更新
  564. $ret = self::where(['match_id' => $match_id])
  565. ->update(['warn_more'=>$warn_data,'update_time'=>date('Y-m-d H:i:s')]);
  566. }
  567. return $ret;
  568. }
  569. }