Betorder.php 35 KB

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