Betorder.php 30 KB

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