Betorder.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. <?php
  2. namespace App\Api\Controller;
  3. use Biz\Money\Moneycopy;
  4. use Biz\Money\Money;
  5. use Biz\Account\AccountManager;
  6. use App\Api\Model\St_odds_code as odds_codeModel;
  7. use App\Api\Model\Dc_user_grade;
  8. /*
  9. * Class Register
  10. * @package App\Api\Controller
  11. * User: junghwi
  12. * Date: 2019/3/21
  13. */
  14. class Betorder extends BaseController{
  15. private $simplex_money = 0; //单式投注总额
  16. public function init() {
  17. $this->time = date('Y-m-d H:i:s',time());
  18. $this->MC = new Moneycopy();
  19. $this->M = new Money();
  20. $this->accountManager = new AccountManager();
  21. $this->commonFunction = C()->get('commonFunction');
  22. }
  23. /**
  24. * 单式串式分类
  25. * @param [array] $res 投注数据
  26. * @return [array] $data
  27. *
  28. */
  29. public function Classify(array $res){
  30. if(empty($res)){
  31. Render($res, '7003',lang('Errors','Api')->get('error-7003'));
  32. }
  33. $data['single'] = [];//单式
  34. $data['bunch'] = [];//串式
  35. $last[] = $res[count($res)-1];
  36. unset($res[count($res)-1]);
  37. foreach ($last as $k =>$v){
  38. foreach ($v as $kk =>$vv){
  39. /*
  40. $num = $kk+1;
  41. //验证球类代码是否有效
  42. $this->verify_gameType($vv['ballId'],$num);
  43. //验证联赛是否存在
  44. $this->verify_league($vv['ballId'],(int)$vv['lg_id'],$num);
  45. //验证赛事/赔率代码是否存在,如果match_id = 0 则为冠军联赛赔率,无需验证
  46. if($vv['match_id'] > 0 and $vv['is_champion'] == 0){
  47. $this->verify_match($vv['ballId'],$vv['match_id'],$num);
  48. //验证赛事下赔率玩法是否有效
  49. $this->verify_odds($vv['ballId'],$vv['odds_only'],$vv['odds'],$vv['condition'],$vv['odds_code'],$num);
  50. }
  51. */
  52. }
  53. }
  54. foreach($res as $k => $v){
  55. //根据类型分组
  56. if($v['type'] == '1'){
  57. $data['single'][$k] = array_merge($last[0][$v['index']],$v);
  58. }else{
  59. $data['bunch'][$k] = $v;
  60. }
  61. }
  62. if(!empty($data['bunch'])){
  63. $data['bunch'] = array_merge_recursive($data['bunch'],$last);
  64. }
  65. return $data;
  66. }
  67. /**
  68. * 根据赛事ID分组
  69. * @param [array] $data 球类数据
  70. * @return [array] $data
  71. */
  72. public function Group_ball($data = []){
  73. if(empty($data)){
  74. return $data;
  75. }
  76. foreach($data as $k => $v){
  77. //普通赛事
  78. if($v['match_id'] > 0){
  79. if(!isset($data[$v['match_id']])){
  80. $data[$v['match_id']][] = $v;
  81. unset($data[$k]);
  82. }else{
  83. $data[$v['match_id']][] = $v;
  84. unset($data[$k]);
  85. }
  86. }
  87. //冠军联赛
  88. if($v['match_id'] == 0){
  89. if(!isset($data[$v['lg_id']])){
  90. $data[$v['lg_id']][] = $v;
  91. unset($data[$k]);
  92. }else{
  93. $data[$v['lg_id']][] = $v;
  94. unset($data[$k]);
  95. }
  96. }
  97. }
  98. return $data;
  99. }
  100. /**
  101. * 单式投注数据处理
  102. * @param [array] $res 单式投注数据
  103. * @return [int] 1 成功 其他失败
  104. * @param [int] $batch_id 批量ID
  105. */
  106. public function Simplex_bet(array $res,array $userInfo,$batch_id){
  107. if(empty($res)||empty($userInfo)||empty($batch_id)){
  108. Render('', '7003',lang('Errors','Api')->get('error-7003'));
  109. }
  110. //验证与判断投注了几种
  111. foreach($res as $k => $v){
  112. $num = $k + 1;
  113. if(empty($v['bet_amount'])){
  114. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5105');
  115. Render([], '5105',$msg);
  116. }
  117. //根据类型分组
  118. if(!isset($res[$v['ballId']])){
  119. $res[$v['ballId']][] = $v;
  120. unset($res[$k]);
  121. }else{
  122. $res[$v['ballId']][] = $v;
  123. unset($res[$k]);
  124. }
  125. }
  126. //球类联赛分类并且插入数据库
  127. foreach($res as $k => $v){
  128. $gamedate = $this->Group_ball($res[$k]);
  129. $this->Simplex_bet_insert($gamedate,$userInfo,$k,$batch_id);
  130. }
  131. }
  132. /**
  133. * 单式投注单个球类数据插入
  134. * @param [array] $data 单式投注数据
  135. * @param [array] $userInfo 用户信息
  136. * @param [type] $game_code 球类代码
  137. * @param [int] $batch_id 批量ID
  138. * @return [int] 1 成功 其他失败
  139. */
  140. public function Simplex_bet_insert__($data,$userInfo,$game_code,$batch_id){
  141. if(empty($data)||empty($userInfo)||empty($game_code)){
  142. Render('', '7003',lang('Errors','Api')->get('error-7003'));
  143. }
  144. foreach($data as $k => $v){
  145. $OrderID = OrderID('S');//生成订单ID
  146. $UUID = UUID(); //生成信息ID
  147. $bet_money = 0; //赛事总投注额
  148. $prize_money = 0; //可赢额
  149. //获取投注单中是否有冠军联赛数据
  150. $championData = [];
  151. foreach($v as $kk => $vv){
  152. $bet_money += $vv['bet_amount'];
  153. //计算每个投注玩法下的可赢金额
  154. // $prize_money += $this->commonFunction->getEarnMoney($game_code,$vv['p_code'],$vv['odds'],$vv['bet_amount'],1);
  155. $prize_money += $vv['odds']* $vv['bet_amount'];
  156. //是否是滚球投注
  157. $is_rolling = 0;
  158. if($vv['oddsType']=='StRollBall'){
  159. $is_rolling = 1;
  160. }
  161. $matchData =[
  162. 'odds_id' =>$vv['id'],
  163. 'home_team' => $vv['home_team'],
  164. 'guest_team' => $vv['guest_team'],
  165. 'condition' => $vv['condition'],
  166. 'odds' => $vv['odds'],
  167. 'odds_code' => $vv['odds_code'],
  168. 'p_code' => $vv['p_code'],
  169. 'odds_only' => $vv['odds_only'],
  170. 'match_id' => $vv['match_id'],
  171. 'game_code' => $game_code,
  172. 'lg_id' => $vv['lg_id'],
  173. 'batch_id' => $batch_id,
  174. 'bet_type' => 1,
  175. 'bet_money' => $vv['bet_amount'],
  176. 'ctime' => $this->time,
  177. 'utime' => $this->time,
  178. 'is_rolling' =>$is_rolling,
  179. ];
  180. //插入数据源
  181. $moneyBuyMatch = lm('MoneyBuyMatch','Api')->insert($matchData);
  182. if(!$moneyBuyMatch){
  183. Render('','3205',lang('errors','Api')->get('error-3205'));
  184. }
  185. }
  186. $order =[
  187. 'info_identity' => $UUID,
  188. 'account_name' => $userInfo['account'],
  189. 'account_identity' => $userInfo['account_identity'],
  190. 'order_id' => OrderID('S'),//$OrderID,
  191. 'game_code' => $game_code,
  192. 'batch_id' => $batch_id,
  193. 'match_id' => $k,
  194. 'game_no' => "",
  195. 'prize_money'=> $prize_money,
  196. 'money' => $bet_money, //投注金额
  197. 'money_time' => $this->time
  198. ];
  199. $insert = lm("MoneyBuySimplex",'Api')->insert($order);
  200. if(!$insert){
  201. Render('','3205',lang('errors','Api')->get('error-3205'));
  202. }
  203. $insertMoney = $this->M->insertMoney($userInfo['account_identity'],$bet_money,$OrderID,1,"",$UUID,$userInfo);
  204. if($insertMoney!=1){
  205. Render('', $insertMoney,lang('Errors','Api')->get('error'.$insertMoney));
  206. }
  207. $this->M->fanshui($userInfo['account_identity'],$userInfo['account'],$bet_money,$OrderID);//反水
  208. }
  209. }
  210. public function Simplex_bet_insert($data,$userInfo,$game_code,$batch_id){
  211. if(empty($data)||empty($userInfo)||empty($game_code)){
  212. Render('', '7003',lang('Errors','Api')->get('error-7003'));
  213. }
  214. foreach($data as $k => $v){
  215. $OrderID = OrderID('S');//生成订单ID
  216. $UUID = UUID(); //生成信息ID
  217. $bet_money = 0; //赛事总投注额
  218. $prize_money = 0; //可赢额
  219. foreach($v as $kk => $vv){
  220. //是否是冠军投注,如果==0 则为冠军投注
  221. $is_champion = 0;
  222. if($vv['match_id'] == 0){
  223. $is_champion = 1;
  224. }
  225. //获取联赛id lg_id
  226. $lg_id = $vv['lg_id'];
  227. $bet_money += $vv['bet_amount'];
  228. //计算每个投注玩法下的可赢金额
  229. // $prize_money += $this->commonFunction->getEarnMoney($game_code,$vv['p_code'],$vv['odds'],$vv['bet_amount'],1);
  230. $prize_money += $vv['odds']* $vv['bet_amount'];
  231. //是否是滚球投注
  232. $is_rolling = 0;
  233. if($vv['oddsType']=='StRollBall'){
  234. $is_rolling = 1;
  235. }
  236. $matchData[] =[
  237. 'odds_id' =>$vv['id'],
  238. 'home_team' => $vv['home_team'],
  239. 'guest_team' => $vv['guest_team'],
  240. 'condition' => $vv['condition'],
  241. 'odds' => $vv['odds'],
  242. 'odds_code' => $vv['odds_code'],
  243. 'p_code' => $vv['p_code'],
  244. 'odds_only' => $vv['odds_only'],
  245. 'match_id' => $vv['match_id'],
  246. 'game_code' => $game_code,
  247. 'lg_id' => $vv['lg_id'],
  248. 'batch_id' => $batch_id,
  249. 'bet_type' => 1,
  250. 'bet_money' => $vv['bet_amount'],
  251. 'ctime' => $this->time,
  252. 'utime' => $this->time,
  253. 'is_rolling' =>$is_rolling,
  254. ];
  255. }
  256. //插入数据源
  257. $moneyBuyMatch = lm('MoneyBuyMatch','Api')->insert($matchData);
  258. if(!$moneyBuyMatch){
  259. Render('','3205',lang('errors','Api')->get('error-3205'));
  260. }
  261. $order =[
  262. 'info_identity' => $UUID,
  263. 'account_name' => $userInfo['account'],
  264. 'account_identity' => $userInfo['account_identity'],
  265. 'order_id' => OrderID('S'),//$OrderID,
  266. 'game_code' => $game_code,
  267. 'batch_id' => $batch_id,
  268. 'match_id' => $k,
  269. 'lg_id' => $lg_id,
  270. 'game_no' => "",
  271. 'prize_money'=> $prize_money,
  272. 'money' => $bet_money, //投注金额
  273. 'money_time' => $this->time,
  274. 'is_champion' => $is_champion?:0,//1 冠军投注 0普通赛事投注
  275. ];
  276. $insert = lm("MoneyBuySimplex",'Api')->insert($order);
  277. if(!$insert){
  278. Render('','3205',lang('errors','Api')->get('error-3205'));
  279. }
  280. $insertMoney = $this->M->insertMoney($userInfo['account_identity'],$bet_money,$OrderID,1,"",$UUID,$userInfo);
  281. if($insertMoney!=1){
  282. Render('', $insertMoney,lang('Errors','Api')->get('error'.$insertMoney));
  283. }
  284. $this->M->fanshui($userInfo['account_identity'],$userInfo['account'],$bet_money,$OrderID);//反水
  285. }
  286. }
  287. public function Bet(){
  288. /*
  289. // 模拟单式数据
  290. $get_data = [
  291. 'data'=>[
  292. [
  293. 'type'=>1,
  294. 'index'=>0,
  295. 'bet_amount'=>1,
  296. ],
  297. [
  298. 'type'=>1,
  299. 'index'=>1,
  300. 'bet_amount'=>1,
  301. ],
  302. [
  303. [
  304. 'id'=>226063,
  305. 'home_team'=>'内蒙古草上飞',
  306. 'guest_team'=>'吉林百嘉',
  307. 'name'=>'内蒙古草上飞',
  308. 'odds'=>0.81,
  309. 'match_id'=>0,
  310. 'condition'=>'0.5/1',
  311. 'ballId'=>'zq',
  312. 'odds_code'=>'concede_home',
  313. 'p_code'=>'concede',
  314. 'play_name'=>'让球',
  315. 'score'=>0,
  316. 'bettingTime'=>date('Y-m-d H:i:s',time()),
  317. 'odds_only'=>'272af2647b112b7eb337a2ed16753c6f',
  318. 'lg_id'=>29,
  319. 'repeat'=>'false',
  320. 'match_type'=>'StRollBall',//滚球投注
  321. // 'is_champion'=>1,//是否是冠军联赛
  322. ],
  323. [
  324. 'id'=>2260632,
  325. 'home_team'=>'内蒙古草上飞1',
  326. 'guest_team'=>'吉林百嘉2',
  327. 'name'=>'内蒙古草上飞',
  328. 'odds'=>0.81,
  329. 'match_id'=>1249,
  330. 'condition'=>'0.5/1',
  331. 'ballId'=>'zq',
  332. 'odds_code'=>'concede_home',
  333. 'p_code'=>'concede',
  334. 'play_name'=>'让球',
  335. 'score'=>0,
  336. 'bettingTime'=>date('Y-m-d H:i:s',time()),
  337. 'odds_only'=>'272af2647b112b7eb337a2ed16753c6f1',
  338. 'lg_id'=>29,
  339. 'repeat'=>'false',
  340. 'match_type'=>'StRollBall',//滚球投注
  341. 'is_champion'=>0,//是否是冠军联赛
  342. ],
  343. ]
  344. ],
  345. 'token'=>'ppWGRW15682541865d79a8eacb0b1',
  346. 'bet_money'=>900,
  347. 'bet_money_arr'=>[100,200,300],
  348. ];
  349. */
  350. /*
  351. // 模拟串式数据
  352. $get_data = [
  353. 'data'=>[
  354. [
  355. 'type'=>1,
  356. 'index'=>0,
  357. 'bet_amount'=>1,
  358. ],
  359. [
  360. 'type'=>'3串1',
  361. 'bet_amount' =>1,
  362. ],
  363. [
  364. [
  365. 'id'=>229351,
  366. 'home_team'=>'布琳狮子U20',
  367. 'guest_team'=>'圣奥尔本斯U20',
  368. 'name'=>'圣奥尔本斯U20',
  369. 'odds'=>1.01,
  370. 'match_id'=>0,
  371. 'condition'=>'4/4.5',
  372. 'ballId'=>'zq',
  373. 'odds_code'=>'goal_size_big',
  374. 'p_code'=>'goal_size',
  375. 'play_name'=>'大小',
  376. 'score'=>0,
  377. 'bettingTime'=>date('Y-m-d H:i:s',time()),
  378. 'odds_only'=>'908f8bae2233a0384c96c7b1f42e9ea7',
  379. 'lg_id'=>153,
  380. 'repeat'=>'false',
  381. 'match_type'=>'StRollBall',//滚球投注
  382. ],
  383. [
  384. 'id'=>238940,
  385. 'home_team'=>'格罗兹尼艾哈迈德U20',
  386. 'guest_team'=>'奥伦堡U20',
  387. 'name'=>'格罗兹尼艾哈迈德U20',
  388. 'odds'=>0.94,
  389. 'match_id'=>1247,
  390. 'condition'=>'1/1.5',
  391. 'ballId'=>'zq',
  392. 'odds_code'=>'concede_home',
  393. 'p_code'=>'concede',
  394. 'play_name'=>'让球',
  395. 'score'=>0,
  396. 'bettingTime'=>date('Y-m-d H:i:s',time()),
  397. 'odds_only'=>'9125bf40d6649fcdb663c43d8f55bebd',
  398. 'lg_id'=>20,
  399. 'repeat'=>'false',
  400. 'match_type'=>'StRollBall',//滚球投注
  401. ],
  402. [
  403. 'id'=>238958,
  404. 'home_team'=>'巴扬卡拉',
  405. 'guest_team'=>'马都拉联',
  406. 'name'=>'巴扬卡拉',
  407. 'odds'=>1.13,
  408. 'match_id'=>1254,
  409. 'condition'=>'0.5',
  410. 'ballId'=>'zq',
  411. 'odds_code'=>'concede_home',
  412. 'p_code'=>'concede',
  413. 'play_name'=>'让球',
  414. 'score'=>0,
  415. 'bettingTime'=>date('Y-m-d H:i:s',time()),
  416. 'odds_only'=>'3cf81ac0b9765218845fe8bc4de0ad99',
  417. 'lg_id'=>61,
  418. 'repeat'=>'false',
  419. 'match_type'=>'StRollBall',//滚球投注
  420. ],
  421. ]
  422. ],
  423. 'token'=>'ppWGRW15682541865d79a8eacb0b1',
  424. 'bet_money'=>2
  425. ];
  426. */
  427. //如果获取不到data,则为游戏端提交,需转成数组
  428. if(!empty($_POST['game_data'])){
  429. //获取参数转成数组
  430. $get_data = json_decode($_POST['game_data'],true);
  431. $token = $get_data['token'];
  432. $bet_money = $get_data['bet_money'];
  433. $data = $get_data['data'];
  434. // $bet_money_arr = $get_data['bet_money_arr'];
  435. }else{
  436. $data = $_REQUEST['data'];
  437. $bet_money = $_REQUEST['bet_money'];
  438. $token = $_REQUEST['token'];
  439. // $bet_money_arr = $_REQUEST['bet_money_arr'];
  440. }
  441. /*
  442. $data = $get_data['data'];
  443. $bet_money = $get_data['bet_money'];
  444. $token = $get_data['token'];
  445. */
  446. //验证用户token
  447. $userInfo = $this->getAgent($token);
  448. if(empty($data)||empty($bet_money)){
  449. Render('', '51029',lang('Errors','Api')->get('error-51029'));
  450. }
  451. //获取用户金额信息
  452. if(!$this->M->verifyMoney($bet_money,$userInfo['cash'])){
  453. Render('', '4204',lang('Errors','Api')->get('error-4204'));
  454. }
  455. //追加验证用户投注金额限额
  456. $this->bet_money_limit($data,$userInfo);
  457. //获取订单批次号
  458. $batch_id = OrderID();///strtotime(date('Y-m-d H:i:s',time())).mt_rand('1','99');//批量ID
  459. //执行数据插入
  460. try {
  461. _beginTransaction();//开启事务
  462. $data = $this->Classify($data);
  463. if(!empty($data['single'])){
  464. $this->Simplex_bet($data['single'],$userInfo,$batch_id);//单式数据处理
  465. }
  466. if(!empty($data['bunch'])){
  467. $data_all = $data['bunch'][count($data['bunch'])-1];//获取最后一个数组
  468. unset($data['bunch'][count($data['bunch'])-1]);//删除最后一个元素
  469. $this->stringBet($data['bunch'],$data_all,$userInfo,$batch_id);
  470. }
  471. _commit();//提交
  472. Render('', '1',lang('Errors','Api')->get('error-1'));
  473. } catch (Exception $e) {
  474. _rollBack();//回滚
  475. print $e->getMessage();
  476. }
  477. }
  478. /**
  479. * token获取用户详情
  480. */
  481. public function getAgent($token = '') {
  482. $checkToken = $this->accountManager->checkEffectiveTime($token);
  483. if ($checkToken['status'] != 1) {
  484. Render($checkToken['data'], $checkToken['status'], lang('commons')->get('user does login'));
  485. };
  486. $result = $this->accountManager->refreshToken($token);
  487. return $result['data'];
  488. }
  489. //验证联赛是否存在
  490. public function verify_league($game_code,$lg_id,$num = 1){
  491. if(empty($lg_id)){
  492. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5112');
  493. Render([], '5112',$msg);
  494. }
  495. //根据球类代码 获取相关model
  496. $model = $this->commonFunction->getModels($game_code);
  497. $model_league = $model['model_league'];
  498. $info = lm($model_league,'Sports')
  499. ->select('id')
  500. ->where('id',$lg_id)
  501. ->first();
  502. if(empty($info)){
  503. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5113');
  504. Render([], '5113',$msg);
  505. }
  506. }
  507. /**
  508. * 赛事是否存在或结束
  509. * @param [type] $game_code 球类代码
  510. * @param [type] $match_id 赔率ID
  511. * @param [type] $num 循环键值
  512. * $@param [type]type_str 串式
  513. *
  514. */
  515. public function verify_match($game_code,$match_id,$num = 1){
  516. if(empty($match_id)){
  517. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5109');
  518. Render([], '5109',$msg);
  519. }
  520. //根据球类代码 获取相关model
  521. $models = $this->commonFunction->getModels($game_code);
  522. $model_match = $models['model_match'];
  523. $info = lm($model_match,'Sports')
  524. ->select('status')
  525. ->where('status', '<', '2')
  526. // ->where('us_time','>',qgmdate('Y-m-d H:i:s','', -4))
  527. // ->where('source','hg3535')
  528. ->where('id',$match_id)
  529. ->first();
  530. if(empty($info)){
  531. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5103');
  532. Render([], '5103',$msg);
  533. }
  534. $info = $info->toarray();
  535. if($info['status'] == 2){
  536. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5108');
  537. Render([], '5108',$msg);
  538. }
  539. return $info;
  540. }
  541. /**
  542. * 验证球类是否存在
  543. * @param [type] $game_code 球类代码
  544. * @param [type] $num 第几条
  545. */
  546. public function verify_gameType($game_code,$num){
  547. if(empty($game_code)){
  548. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-5102');
  549. Render([], '5102', $msg);
  550. }
  551. $info = lm('GameType','Sports')->where('game_code',$game_code)->first();
  552. if(empty($info)){
  553. $msg = '第' . $num . '条数据' . lang('Errors', 'Api')->get('error-5102');
  554. Render([], '5102', $msg);
  555. }
  556. return $info;
  557. }
  558. /**
  559. * 验证赔率是否存在
  560. * @param [type] $game_code 球类代码
  561. * @param [type] $odds_id 赔率IDkk
  562. *
  563. */
  564. public function verify_odds($game_code,$odds_only,$odds,$condition,$odds_code,$num = 1){
  565. if (empty($odds_only)){
  566. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-5111');
  567. Render([], '5111', $msg);
  568. }
  569. if(empty($odds_code)){
  570. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7052');
  571. Render([], '7052', $msg);
  572. }
  573. if(empty($odds)){
  574. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7029');
  575. Render([], '7029', $msg);
  576. }
  577. $odds_codeNum = odds_codeModel::getOddsCode($odds_code,$game_code);
  578. if($odds_codeNum < 1){
  579. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7053');
  580. Render([], '7053', $msg);
  581. }
  582. //根据球类代码 获取相关model
  583. $models = $this->commonFunction->getModels($game_code);
  584. $model_odds = $models['model_odds'];
  585. $model_odds_record = $models['model_odds_record'];
  586. //匹配赔率是否过期
  587. $odds_info = lm($model_odds,'Sports')
  588. ->join($model_odds_record,$model_odds_record.'.odds_only',$model_odds.'.odds_only')
  589. ->select($model_odds_record.'.odds',$model_odds_record.'.condition')
  590. ->where($model_odds.'.odds_only',$odds_only)
  591. ->first();
  592. if(empty($odds_info)){
  593. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7051');
  594. Render([], '7051', $msg);
  595. }
  596. $odds_info = $odds_info->toarray();
  597. if($condition != $odds_info['condition']){
  598. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7047');
  599. Render([], '7047', $msg);
  600. }
  601. if($odds != $odds_info['odds']){
  602. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7049');
  603. Render([], '7049', $msg);
  604. }
  605. return $odds_info;
  606. }
  607. /**
  608. * 处理串式投注
  609. * $data_str 各串式 数据
  610. * $data_all 所有赛事 数据
  611. * $userInfo 用户数据
  612. * $batch_id 批次号
  613. */
  614. public function stringBet($data_str=[],$data_all=[],$userInfo=[],$batch_id=0){
  615. //验证串式投注是否合法
  616. $this->verify_stringType($data_all);
  617. //获取串式 总赛事数量
  618. $groupNum = count($data_all);//总数量
  619. $orderData = [];
  620. foreach ($data_str as $k=>$v){
  621. //字符串替换
  622. $str = str_ireplace("串","_",$v['type']);
  623. //获取type 3_12
  624. $after = substr($str,strpos($str,"_")+1);
  625. //当前串式 值
  626. $sonNum = substr($str,0,strrpos($str,"_"));
  627. //获取倍数
  628. if((int)$after == 1){
  629. $multiple = $this->getMultiple($groupNum,(int)$sonNum);
  630. }else{
  631. $multiple =(int)$after;
  632. }
  633. //获取当前串式投注金额及可赢金额
  634. if($after == 1){
  635. $money_all = $this->getmakeMoney((int)$sonNum,$v['bet_amount'],$multiple,$data_all,$after);
  636. }else{
  637. $money_all = $this->getStrAllMoney((int)$sonNum,$v['bet_amount'],$multiple,$data_all,$after);
  638. }
  639. //投注金额
  640. $moneyData = $money_all['moneyData'];
  641. //可赢金额
  642. $prize_money = $money_all['prize_money'];
  643. $uuid = UUID();
  644. $orderID = OrderID('T');
  645. $orderData[$k]['info_identity'] = $uuid;
  646. $orderData[$k]['account_name'] = $userInfo['account'];
  647. $orderData[$k]['account_identity'] = $userInfo['account_identity'];
  648. $orderData[$k]['order_id'] = $orderID;
  649. $orderData[$k]['game_no'] = "";
  650. $orderData[$k]['money'] = $moneyData;//$pay_money//总投注金额
  651. $orderData[$k]['money_time'] = $this->time;//下注时间
  652. $orderData[$k]['prize_money'] = $prize_money;//$expect_money ;//预期总可赢金额
  653. $orderData[$k]['str_type'] = $v['type'];//串式类型
  654. $orderData[$k]['batch_id'] = $batch_id;//批次号
  655. $orderData[$k]['wait_match_num'] = count($data_all);//批次号
  656. //写资金变动表
  657. $insertMoney = $this->M->insertMoney($userInfo['account_identity'],$moneyData,$orderID,1,"",$uuid,$userInfo);
  658. if($insertMoney!=1){
  659. Render('', $insertMoney,lang('Errors','Api')->get('error'.$insertMoney));
  660. }
  661. $this->M->fanshui($userInfo['account_identity'],$userInfo['account'],$moneyData,$orderID);//反水
  662. }
  663. //写注单 表
  664. $moneyBuy = lm('MoneyBuyStr','Api')->insert($orderData);
  665. if(!$moneyBuy){
  666. Render('','3205',lang('errors','Api')->get('error-3205'));
  667. }
  668. foreach ($data_all as $kk=>$vv){
  669. $getModels = $this->commonFunction->getModels($vv['ballId']);
  670. $model_match = $getModels['model_match'];
  671. //获取是否是滚球投注
  672. $is_rolling = 0;
  673. if($vv['match_type'] == 'StRollBall'){
  674. $is_rolling = 1;
  675. }
  676. $matchData[] = [
  677. 'batch_id'=>$batch_id,
  678. 'odds_id'=>$vv['id'],
  679. 'home_team'=>$vv['home_team'],
  680. 'guest_team'=>$vv['guest_team'],
  681. 'condition'=>$vv['condition'],
  682. 'odds'=>$vv['odds'],
  683. 'odds_code'=>$vv['odds_code'],
  684. 'p_code'=>$vv['p_code'],
  685. 'odds_only'=>$vv['odds_only'],
  686. 'status'=>0,
  687. 'match_id'=>$vv['match_id'],
  688. 'game_code'=>$vv['ballId'],
  689. 'lg_id'=> $vv['lg_id'],
  690. 'ctime'=>$this->time,//写入时间
  691. 'utime' => $this->time,
  692. 'bet_type'=>2,
  693. 'is_rolling'=>$is_rolling,
  694. ];
  695. }
  696. //写赔率数据表
  697. $moneyBuyMatch = lm('MoneyBuyMatch','Api')->insert($matchData);
  698. if(!$moneyBuyMatch){
  699. Render('','3205',lang('errors','Api')->get('error-3205'));
  700. }
  701. return true;
  702. }
  703. /**
  704. * 计算串式投注 倍率
  705. * $groupNum 总赛事 数量
  706. * $sonNum 当前串式 数量
  707. */
  708. public function getMultiple($groupNum = 0,$sonNum = 0){
  709. if($groupNum and $sonNum){
  710. $group =1;
  711. for($i = $groupNum ; $i >($groupNum - $sonNum);$i--) {
  712. $group = $group * $i;
  713. }
  714. $son =1;
  715. for($i = $sonNum ; $i >0;$i--) {
  716. $son = $son * $i;
  717. }
  718. }
  719. return $group/$son;
  720. }
  721. /**
  722. * @param int $sonNum 当前串式数量
  723. * @param int $bet_amount 当前串式投注额
  724. * @param int $multiple 当前串式倍数
  725. * @param array $data_all 当前注单 所有投注数据
  726. * @return mixed
  727. * 计算当前串式投注总额、可赢总额
  728. */
  729. public function getmakeMoney($sonNum=0,$bet_amount=0,$multiple=0,$data_all=[],$after = 0){
  730. $allNum = count($data_all);//所有数量
  731. $odds_all = [];//所有投注项数据
  732. foreach ($data_all as $k=>$v){
  733. $odds_all[] = $v['odds']+1;
  734. }
  735. //投注总额
  736. $data['moneyData'] = (double)$bet_amount*(double)$multiple;
  737. if($sonNum == $allNum and (int)$after ==1){//N场赛事 N串一
  738. //获取所有赛事赔率乘积
  739. $odds = array_product($odds_all) - 1;
  740. //获取可赢金额
  741. $data['prize_money'] = sprintf("%.2f",substr(sprintf("%.3f", $bet_amount*$odds), 0, -1));//floor($bet_amount*$odds*100)/100;//保留两位小数
  742. }
  743. if($sonNum < $allNum and (int)$after ==1){//例:N场赛事 <N串一
  744. //计算 组合赔率
  745. $group = $this->commonFunction->combination($odds_all, $sonNum);
  746. foreach ($group as $k=>$v){
  747. $odds[] = array_product($v);
  748. }
  749. $odds = (array_sum($odds))-(count($odds));
  750. //获取可赢金额
  751. $data['prize_money'] = sprintf("%.2f",substr(sprintf("%.3f", $bet_amount*$odds), 0, -1));//floor($bet_amount*$odds*100)/100;
  752. }
  753. return $data;
  754. }
  755. /**
  756. * 获取所有可用串式 N场赛事 2串1,3串1...N串1,N<11
  757. * 并计算 其投注可赢金额
  758. */
  759. public function getStrAllMoney($sonNum=0,$bet_amount=0,$multiple=0,$data_all=[],$after = 0){
  760. $d = [];
  761. for ($i=2;$i<($sonNum+1);$i++){
  762. $d[] = $i;
  763. }
  764. foreach ($d as $k=>$v){
  765. $multiple = $this->getMultiple($sonNum,$v);
  766. $money[] = $this->getmakeMoney($v,$bet_amount,$multiple,$data_all,1);
  767. }
  768. $data=array();
  769. foreach($money as $k=>$v){
  770. $data['moneyData']+=$v['moneyData'];
  771. $data['prize_money']+=$v['prize_money'];
  772. }
  773. return $data;
  774. }
  775. /**
  776. * 验证串式投注 是否合法
  777. * $data 串式 所有赛事
  778. */
  779. public function verify_stringType($data=[]){
  780. if($data){
  781. foreach($data as $k=>$v){
  782. if($v['match_id']==$v['match_id'] ){
  783. $r[$v['match_id']][] = $v;
  784. }
  785. }
  786. }
  787. if($r){
  788. foreach ($r as $k=>$v){
  789. if(count($v) > 1){
  790. $msg = lang('Errors','Api')->get('error-50038');
  791. Render([], '50038',$msg);
  792. }else if( $k == 0){
  793. $msg = lang('Errors','Api')->get('error-50043');
  794. Render([], '50043',$msg);
  795. }
  796. }
  797. }
  798. }
  799. /**
  800. * 更新投注项相关数据
  801. */
  802. public function getBetData(){
  803. $data = $_REQUEST;
  804. // $data = [['game_code'=>'zq','match_id'=>3095448,'odds_id'=>623078],['game_code'=>'zq','match_id'=>3095448,'odds_id'=>623078]];
  805. foreach ($data as $k=>$v){
  806. $getModels = $this->commonFunction->getModels($v['game_code']);
  807. $model_match = $getModels['model_match'];
  808. $model_odds = $getModels['model_odds'];
  809. $oddsData = lm($model_match,"Sports")
  810. ->join($model_odds,$model_odds.'.match_id',$model_match.'.id')
  811. ->select($model_match.'.id as match_id',$model_match.'.match_date',$model_match.'.home_team',$model_match.'.guest_team',$model_odds.'.id as odds_id',$model_odds.'.p_code',$model_odds.'.odds_code',$model_odds.'.condition',$model_odds.'.odds',$model_odds.'.odds_only',$model_odds.'.status',$model_odds.'.sort')
  812. // ->where($model_match.'.source',$this->source)
  813. ->where($model_match.'.id',$v['match_id'])
  814. ->where($model_odds.'.id',$v['odds_id'])
  815. //->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  816. ->first();
  817. $oddsData->game_code = $v['game_code'];
  818. $betData[] = $oddsData;
  819. }
  820. Render($betData, '1', lang('Tips','Sports')->get('success'));
  821. }
  822. //用户投注限额验证
  823. public function bet_money_limit($bet_money_arr=[],$userInfo=[]){
  824. if(!empty($bet_money_arr)){
  825. foreach($bet_money_arr as $k=>$v){
  826. if($k == (count($bet_money_arr)-1)){
  827. unset($bet_money_arr[$k]);
  828. }
  829. }
  830. }
  831. //===追加验证用户投注金额限额===
  832. if(empty($bet_money_arr)) Render('', '3605',lang('Errors','Api')->get('error-3605'));
  833. //获取当前用户分组名称
  834. $substr = substr($userInfo['group_name'],strpos($userInfo['group_name'],'|')+1);
  835. $role_name = substr($substr, 0, -1);
  836. //获取用户分组的投注限额
  837. $bet_money_limit = Dc_user_grade::get_bet_money_limit($role_name);
  838. foreach($bet_money_arr as $k=>$v){
  839. if($v['bet_amount'] < $bet_money_limit->lower_limit) Render(['bet_amount'=>$v['bet_amount'],'lower_limit'=>$bet_money_limit->lower_limit], '3406','第'.($k+1).'条'.lang('Errors','Api')->get('error-3406'));
  840. if($v['bet_amount'] > $bet_money_limit->upper_limit) Render(['bet_amount'=>$v['bet_amount'],'upper_limit'=>$bet_money_limit->upper_limit], '3401','第'.($k+1).'条'.lang('Errors','Api')->get('error-3401'));
  841. }
  842. //===end==
  843. }
  844. //根据token获取用户投注限额
  845. public function getBetMoneyLimit(){
  846. //验证用户token
  847. $token = $_REQUEST['token'];
  848. $userInfo = $this->getAgent($token);
  849. //获取当前用户分组名称
  850. $substr = substr($userInfo['group_name'],strpos($userInfo['group_name'],'|')+1);
  851. $role_name = substr($substr, 0, -1);
  852. //获取用户分组的投注限额
  853. $bet_money_limit = Dc_user_grade::get_bet_money_limit($role_name);
  854. $data['bet_money_limit']=[
  855. 'lower_limit'=>$bet_money_limit->lower_limit,
  856. 'upper_limit'=>$bet_money_limit->upper_limit,
  857. ];
  858. Render($data, '1', lang('Tips','Sports')->get('success'));
  859. }
  860. }