WinfailLogic.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/7/15
  6. * Time: 16:51
  7. */
  8. namespace App\Logic;
  9. use Illuminate\Database\Capsule\Manager as DB;
  10. use App\Lib\Settlement\Adapter\ZqRule;
  11. use App\Lib\Settlement\Adapter\LqRule;
  12. use App\Lib\Settlement\Adapter\WqRule;
  13. use App\Lib\Settlement\Adapter\BqRule;
  14. use datainf\pplus\Instance;
  15. class WinfailLogic
  16. {
  17. use Instance;
  18. //类型映射
  19. public $gameAllMap = [
  20. 'zq' => 'ZqRule',
  21. 'lq' => 'LqRule',
  22. 'wq' => 'WqRule',
  23. 'bq' => 'BqRule',
  24. ];
  25. public function getNoticeDate($id)
  26. {
  27. $noticeMode = DB::table('comendnotice')->where(['id' => $id])->first();
  28. if (empty($noticeMode)) {
  29. throw new \Exception("没有找到notice记录-" . $id);
  30. }
  31. return $noticeMode;
  32. }
  33. //r按赛事 全部重计算输赢时,要清除手动设置的结果
  34. public function Reset_manualData($game_code, $match_id)
  35. {
  36. DB::table('money_buy_simplex')->where(['match_id' => $match_id, 'game_code' => $game_code])->update(['is_manual' => 0]);
  37. }
  38. //得到比赛最终结果记录
  39. public function getCompResult($type, $match_id)
  40. {
  41. $model = null;
  42. switch ($type) {
  43. case 'bq':
  44. $model = DB::table('st_bq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
  45. break;
  46. case 'lq':
  47. $model = DB::table('st_lq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
  48. break;
  49. case 'wq':
  50. $model = DB::table('st_wq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
  51. break;
  52. case 'zq':
  53. $model = DB::table('st_zq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
  54. break;
  55. }
  56. if (empty($model)) {
  57. throw new \Exception("没有找到赛事结果记录-" . $type . '-' . $match_id);
  58. }
  59. //按赛事结算时,存在手动更改赛事结果的问题,如果有手动设置结果,就用手动结果覆盖真实结果
  60. $this->FormatResult_all($type, $model['0']);
  61. return $model;
  62. }
  63. //格式化比赛结果为兼容以前的格式(主要因为有手动修改结果和自然采集到数据的问題)
  64. //如果比赛结果表中的 is_correct=1时 手动输入结果覆盖旧字段的数据
  65. //开始1-->
  66. public function FormatResult_all($type, $resultObj)
  67. {
  68. switch ($type) {
  69. case 'zq':
  70. $this->FormatResult_zq($resultObj);
  71. break;
  72. case 'lq':
  73. $this->FormatResult_lq($resultObj);
  74. break;
  75. case 'wq':
  76. $this->FormatResult_wq($resultObj);
  77. break;
  78. case 'bq':
  79. $this->FormatResult_bq($resultObj);
  80. break;
  81. default:
  82. break;
  83. }
  84. return;
  85. }
  86. public function FormatResult_zq($resultObj)
  87. {
  88. if ($resultObj->is_correct != 1) {
  89. return;
  90. }
  91. $handres = json_decode($resultObj->manual_result);
  92. $resultObj->u_home_score = intval($handres->half->home);
  93. $resultObj->u_guest_score = intval($handres->half->guest);
  94. $resultObj->home_score = intval($handres->all->home);
  95. $resultObj->guest_score = intval($handres->all->guest);
  96. $tmp = ["home" => intval($handres->all_corner->home), "guest" => intval($handres->all_corner->guest), "home_half" => intval($handres->half_corner->home), "guest_half" => intval($handres->half_corner->home)];
  97. $resultObj->corner_ball = json_encode($tmp, 256);
  98. //其它结果因为废弃其玩法,就不用管了
  99. return;
  100. }
  101. public function FormatResult_lq($resultObj)
  102. {
  103. if ($resultObj->is_correct != 1) {
  104. return;
  105. }
  106. $resultObj->inning = $resultObj->manual_result;
  107. return;
  108. }
  109. public function FormatResult_wq($resultObj)
  110. {
  111. if ($resultObj->is_correct != 1) {
  112. return;
  113. }
  114. $resultObj->inning = $resultObj->manual_result;
  115. return;
  116. }
  117. public function FormatResult_bq($resultObj)
  118. {
  119. if ($resultObj->is_correct != 1) {
  120. return;
  121. }
  122. $resultObj->match_score_t = $resultObj->manual_result;
  123. return;
  124. }
  125. //结束1 -->
  126. //单式订单存在 单独设置比赛结果的情况,要分开处理
  127. public function getSimplexData($type, $match_id)
  128. {
  129. $return = [];
  130. $ret = DB::table("money_buy_simplex")->where(['game_code' => $type, 'match_id' => $match_id])->get();
  131. if (count($ret) <= 0) {
  132. return;
  133. }
  134. foreach ($ret as $val) {
  135. $return[$val->order_id][$val->match_id] = ['result_flag' => $val->result_flag, 'single_result' => $val->single_result];
  136. }
  137. return $return;
  138. }
  139. //找到订单详细数据
  140. public
  141. function getMatchRecords($type, $match_id, $bet_type)
  142. {
  143. $return = [];
  144. $ret = DB::table("money_buy_match")->where(['game_code' => $type, 'match_id' => $match_id, 'bet_type' => $bet_type])->get();
  145. if (count($ret) <= 0) {
  146. return [];
  147. }
  148. foreach ($ret as $val) {
  149. $return[] = $val;
  150. }
  151. return $return;
  152. }
  153. public
  154. function getAdapterObj($game_type)
  155. {
  156. $game_type = strtolower($game_type);
  157. if (!isset($this->gameAllMap)) {
  158. throw new \Exception('赛事类型错误-' . $game_type, 4002);
  159. return false;
  160. }
  161. switch ($game_type) {
  162. case 'bq':
  163. $apd = new BqRule();
  164. break;
  165. case 'lq':
  166. $apd = new LqRule();
  167. break;
  168. case 'wq':
  169. $apd = new WqRule();
  170. break;
  171. case 'zq':
  172. $apd = new ZqRule();
  173. break;
  174. }
  175. return $apd;
  176. }
  177. //冠军比赛结果
  178. public
  179. function getGjDatas($matchModel)
  180. {
  181. $table = 'st_' . $matchModel->game_code . '_league_result';
  182. $where = ['lg_id' => $matchModel->lg_id, 'game_name' => $matchModel->game_code];
  183. $model = DB::table($table)->where($where)->first();
  184. if (empty($model)) {
  185. return false;
  186. }
  187. return $model;
  188. }
  189. //冠军比赛结果
  190. public
  191. function getGjDatasV2($game_code, $lg_id)
  192. {
  193. $table = 'st_' . $game_code . '_league_result';
  194. $where = ['lg_id' => $lg_id, 'game_name' => $game_code];
  195. $model = DB::table($table)->where($where)->first();
  196. if (empty($model)) {
  197. return false;
  198. }
  199. return $model;
  200. }
  201. public
  202. function getOddsTypeData($game_code, $match_id)
  203. {
  204. $ret = DB::table('money_buy_match')->select("p_code", "odds_code", "condition", "bet_type", "is_champion")->where(['match_id' => $match_id, 'game_code' => $game_code])->groupBy('p_code', 'odds_code', 'condition', 'bet_type', 'is_champion')->get();
  205. return $ret;
  206. }
  207. //胜负判断 NotiematchModel赛事结束通知model $result普通赛事结果对像数组[冠军赛事下可能为空] $GJresult冠军赛事结果对像数组[非冠军赛事为空]
  208. public
  209. function doLogic($NotiematchModel, $result, $GJresult)
  210. {
  211. $game_code = $NotiematchModel->game_code;
  212. $match_id = $NotiematchModel->match_id;
  213. $AdapterObj = $this->getAdapterObj($game_code);
  214. $groupDatas = $this->getOddsTypeData($game_code, $match_id);
  215. $fristRet = $this->getFristRecord($game_code, $match_id, $groupDatas);
  216. $RefClass = new \ReflectionClass($AdapterObj);
  217. $noticeId = $NotiematchModel->id;
  218. $nowtime = date("Y-m-d H:i:s");
  219. $winorfalsedef = ['result' => 2, 'matchResult' => 'noRuleOrError'];
  220. foreach ($fristRet as $matchModel) {
  221. $is_champion = $matchModel->is_champion;
  222. $fun1 = $matchModel->odds_code;
  223. $fun2 = $matchModel->p_code;
  224. if ($is_champion == 0) {
  225. //普通赛事
  226. $resultTypeNow = $result;
  227. if (empty($resultTypeNow)) {
  228. throw new \Exception("赛事结果不能为空! --lg_id=" . $matchModel->game_code . '_' . $matchModel->lg_id);
  229. }
  230. } else {
  231. //冠军赛事;
  232. $resultTypeNow = $GJresult;
  233. if (empty($resultTypeNow)) {
  234. throw new \Exception("冠军赛事结果不能为空! --lg_id=" . $matchModel->game_code . '_' . $matchModel->lg_id);
  235. }
  236. }
  237. try {
  238. if ($RefClass->hasMethod($fun1)) {
  239. $winorfalse = $AdapterObj->$fun1($matchModel, $resultTypeNow, []);
  240. } elseif ($RefClass->hasMethod($fun2)) {
  241. $winorfalse = $AdapterObj->$fun2($matchModel, $resultTypeNow, []);
  242. }
  243. } catch (\Exception $e) {
  244. }
  245. if (!isset($winorfalse['result']) || !isset($winorfalse['matchResult']) || !in_array($winorfalse['result'], [-1, 1, 2, 3, 4])) {
  246. $winorfalse = $winorfalsedef;
  247. }
  248. $win = intval($winorfalse['result']);
  249. $rwin = ($win == 1) ? 1 : ($win == -1 ? 2 : 3);
  250. $matword = $winorfalse['matchResult'];
  251. $odds_code = $fun1;
  252. $p_code = $fun2;
  253. $condition = $matchModel->condition;
  254. $bet_type = $matchModel->bet_type;
  255. DB::update('update money_buy_match set "result"=?,matchresult=? where match_id=? and game_code=? and odds_code=? and p_code=? and condition=? and is_champion=?', [$win, $matword, $match_id, $game_code, $odds_code, $p_code, $condition, $is_champion]);
  256. $sql = "select order_id from money_buy_match where match_id=$match_id and game_code='$game_code' and p_code='$p_code'and odds_code='$odds_code'and condition='$condition' and is_champion=$is_champion ";
  257. if ($bet_type == 1) {
  258. DB::update("update money_buy_simplex set game_status=$rwin where game_code='$game_code' and match_id=$match_id and order_id in ( $sql)");
  259. } else {
  260. //串式订单不能通过单个比赛来判断输赢,因为有可能其它是输或未开始
  261. //DB::update("update money_buy_str set game_status=$rwin where order_id in ( $sql)");
  262. }
  263. }
  264. DB::update("update comendnotice set status=1,result=1,done_time='$nowtime',pcount=pcount+1 where id=$noticeId ");
  265. return true;
  266. }
  267. //得到每个玩法第一条记录 根据规则计算输赢
  268. public
  269. function getFristRecord($game_code, $match_id, $ret)
  270. {
  271. $return = [];
  272. foreach ($ret as $val) {
  273. $p_code = $val->p_code;
  274. $odds_code = $val->odds_code;
  275. $condition = $val->condition;
  276. $is_champion = $val->is_champion;
  277. $ret = DB::table('money_buy_match')->where(['match_id' => $match_id, 'game_code' => $game_code, 'p_code' => $p_code, 'odds_code' => $odds_code, 'condition' => $condition, 'is_champion' => $is_champion])->first();
  278. $return[] = $ret;
  279. }
  280. return $return;
  281. }
  282. //对单式订单 单个更改过比赛结果的订单进行输赢判断 $orderInfoArray一维数组 $matchArray二维数组
  283. public
  284. function WinFailOneOrder($orderInfo, $matchArray, $is_champion)
  285. {
  286. $game_code = $orderInfo->game_code;
  287. $match_id = $orderInfo->match_id;
  288. $order_id = $orderInfo->order_id;
  289. $AdapterObj = $this->getAdapterObj($game_code);
  290. $RefClass = new \ReflectionClass($AdapterObj);
  291. $result = json_decode($orderInfo->single_result);
  292. if ($is_champion == 0) {
  293. //还原回原来的类型,以兼容之前的处理
  294. switch (strtolower($orderInfo->game_code)) {
  295. case 'zq':
  296. $result['0']->first_score = json_encode($result['0']->first_score, 256);
  297. $result['0']->corner_ball = json_encode($result['0']->corner_ball, 256);
  298. $result['0']->penalty_card = json_encode($result['0']->penalty_card, 256);
  299. break;
  300. case 'lq':
  301. $result['0']->home_score = json_encode($result['0']->home_score, 256);
  302. $result['0']->guest_score = json_encode($result['0']->guest_score, 256);
  303. break;
  304. case 'wq':
  305. $result['0']->inning = json_encode($result['0']->inning, 256);
  306. break;
  307. case 'bq':
  308. $result['0']->match_score_t = json_encode($result['0']->match_score_t, 256);
  309. break;
  310. }
  311. $result['0']->warn_more = json_encode($result['0']->warn_more, 256);
  312. }
  313. $winorfalsedef = ['result' => 2, 'matchResult' => 'noRuleOrError'];
  314. foreach ($matchArray as $matchModel) {
  315. $fun1 = $matchModel->odds_code;
  316. $fun2 = $matchModel->p_code;
  317. $mid = $matchModel->id;
  318. try {
  319. if ($RefClass->hasMethod($fun1)) {
  320. $winorfalse = $AdapterObj->$fun1($matchModel, $result, []);
  321. } elseif ($RefClass->hasMethod($fun2)) {
  322. $winorfalse = $AdapterObj->$fun2($matchModel, $result, []);
  323. }
  324. } catch (\Exception $e) {
  325. echo 'excetption: ' . print_r([$order_id, $e->getMessage(), $e->getFile(), $e->getLine()], true) . "\n";
  326. }
  327. if (!isset($winorfalse['result']) || !isset($winorfalse['matchResult']) || !in_array($winorfalse['result'], [-1, 1, 2, 3, 4])) {
  328. $winorfalse = $winorfalsedef;
  329. }
  330. $win = intval($winorfalse['result']);
  331. $rwin = ($win == 1) ? 1 : ($win == -1 ? 2 : 3);
  332. $matword = $winorfalse['matchResult'];
  333. DB::update('update money_buy_match set "result"=?,matchresult=? where match_id=? and game_code=? and id=?', [$win, $matword, $match_id, $game_code, $mid]);
  334. }
  335. DB::update("update money_buy_simplex set game_status=$rwin where order_id='$order_id' and game_code='$game_code' and match_id='$match_id' ");
  336. return true;
  337. }
  338. //普通单式或串式订单输赢处理
  339. public function WinFailOneOrderNomal($order_id, $bet_type = 1)
  340. {
  341. if ($bet_type == 1) {
  342. $orderInfo = DB::table('money_buy_simplex')->where('order_id', $order_id)->first();
  343. } else {
  344. $orderInfo = DB::table('money_buy_str')->where('order_id', $order_id)->first();
  345. }
  346. $matchArray = DB::table('money_buy_match')->where('order_id', $order_id)->get();
  347. if (empty($orderInfo) || empty($matchArray)) {
  348. return 0;
  349. }
  350. if ($bet_type == 1) {
  351. $game_code = $orderInfo->game_code;
  352. $match_id = $orderInfo->match_id;
  353. $table = 'st_' . $game_code . '_result';
  354. $result = DB::table($table)->where('match_id', $match_id)->get();
  355. if (empty($result)) {
  356. return 0;
  357. }
  358. $this->FormatResult_all($orderInfo->game_code, $result['0']);
  359. }
  360. $winorfalsedef = ['result' => 2, 'matchResult' => 'noRuleOrError'];
  361. $chuanSkip = 0; //串式跳过输赢结果处理标识
  362. foreach ($matchArray as $matchModel) {
  363. if ($bet_type == 2) {
  364. $game_code = $matchModel->game_code;
  365. $match_id = $matchModel->match_id;
  366. $table = 'st_' . $game_code . '_result';
  367. $result = DB::table($table)->where('match_id', $match_id)->get();
  368. if (empty($result)) {
  369. $chuanSkip = 1;
  370. continue;
  371. }
  372. if ($result['0']->status == 1) {
  373. //还未结束,暂不用输赢处理
  374. $chuanSkip = 1;
  375. continue;
  376. }
  377. $this->FormatResult_all($game_code, $result['0']);
  378. }
  379. $AdapterObj = $this->getAdapterObj($game_code);
  380. $RefClass = new \ReflectionClass($AdapterObj);
  381. $fun1 = $matchModel->odds_code;
  382. $fun2 = $matchModel->p_code;
  383. $mid = $matchModel->id;
  384. if ($result['0']->status == 4) {
  385. $winorfalse = ['result' => 2, 'matchResult' => '赛事取消和'];
  386. } else {
  387. try {
  388. if ($RefClass->hasMethod($fun1)) {
  389. $winorfalse = $AdapterObj->$fun1($matchModel, $result, []);
  390. } elseif ($RefClass->hasMethod($fun2)) {
  391. $winorfalse = $AdapterObj->$fun2($matchModel, $result, []);
  392. }
  393. } catch (\Exception $e) {
  394. echo 'excetption: ' . print_r([$order_id, $e->getMessage(), $e->getFile(), $e->getLine()], true) . "\n";
  395. }
  396. if (!isset($winorfalse['result']) || !isset($winorfalse['matchResult']) || !in_array($winorfalse['result'], [-1, 1, 2, 3, 4])) {
  397. $winorfalse = $winorfalsedef;
  398. }
  399. }
  400. $win = intval($winorfalse['result']);
  401. $rwin = ($win == 1) ? 1 : ($win == -1 ? 2 : 3);
  402. if ($bet_type == 2) {
  403. $rwinarr[] = $rwin;
  404. }
  405. $matword = $winorfalse['matchResult'];
  406. DB::update('update money_buy_match set "result"=?,matchresult=? where match_id=? and game_code=? and id=?', [$win, $matword, $match_id, $game_code, $mid]);
  407. }
  408. if ($bet_type == 1) {
  409. DB::update("update money_buy_simplex set game_status=$rwin where order_id='$order_id' ");
  410. } else {
  411. $rwin = 1;
  412. if ($chuanSkip) {
  413. return 1;
  414. }
  415. foreach ($rwinarr as $nwin) {
  416. if ($nwin == 2) {
  417. $rwin = 3;
  418. break;
  419. }
  420. }
  421. DB::update("update money_buy_str set game_status=$rwin where order_id='$order_id' ");
  422. }
  423. return 1;
  424. }
  425. }