Betorder.php 45 KB

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