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