Betorder.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  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. use Biz\Db\Redis\Cache as Redis;
  9. use Illuminate\Support\Testing\Fakes\QueueFake;
  10. // use Illuminate\Contracts\Queue\Queue;
  11. /*
  12. * Class Register
  13. * @package App\Api\Controller
  14. * User: junghwi
  15. * Date: 2019/3/21
  16. */
  17. class Betorder extends BaseController{
  18. private $simplex_money = 0; //单式投注总额
  19. public function init() {
  20. $this->time = date('Y-m-d H:i:s',time());
  21. $this->MC = new Moneycopy();
  22. $this->M = new Money();
  23. $this->accountManager = new AccountManager();
  24. $this->commonFunction = C()->get('commonFunction');
  25. }
  26. /**
  27. * 测试写队列 任务
  28. */
  29. public function dd(){
  30. //laterOn
  31. //实例化 队列
  32. $Queue = new QueueFake();
  33. //新建队列 任务
  34. $Queue ->push('a','c','123');
  35. /*
  36. $redis = new Redis();
  37. $redis->set('name','pengjun');
  38. $name = $redis->get('name');
  39. dd($name);
  40. */
  41. }
  42. /**
  43. * 单式串式分类
  44. * @param [array] $res 投注数据
  45. * @return [array] $data
  46. *
  47. */
  48. public function Classify(array $res){
  49. if(empty($res)){
  50. Render($res, '7003',lang('Errors','Api')->get('error-7003'));
  51. }
  52. $data['single'] = [];//单式
  53. $data['bunch'] = [];//串式
  54. $last[] = $res[count($res)-1];
  55. unset($res[count($res)-1]);
  56. foreach ($last as $k =>$v){
  57. foreach ($v as $kk =>$vv){
  58. /*
  59. $num = $kk+1;
  60. //验证球类代码是否有效
  61. $this->verify_gameType($vv['ballId'],$num);
  62. //验证联赛是否存在
  63. $this->verify_league($vv['ballId'],(int)$vv['lg_id'],$num);
  64. //验证赛事/赔率代码是否存在,如果match_id = 0 则为冠军联赛赔率,无需验证
  65. if($vv['match_id'] > 0 and $vv['is_champion'] == 0){
  66. $this->verify_match($vv['ballId'],$vv['match_id'],$num);
  67. //验证赛事下赔率玩法是否有效
  68. $this->verify_odds($vv['ballId'],$vv['odds_only'],$vv['odds'],$vv['condition'],$vv['odds_code'],$num);
  69. }
  70. */
  71. }
  72. }
  73. foreach($res as $k => $v){
  74. //根据类型分组
  75. if($v['type'] == '1'){
  76. $data['single'][$k] = array_merge($last[0][$v['index']],$v);
  77. }else{
  78. $data['bunch'][$k] = $v;
  79. }
  80. }
  81. if(!empty($data['bunch'])){
  82. $data['bunch'] = array_merge_recursive($data['bunch'],$last);
  83. }
  84. return $data;
  85. }
  86. /**
  87. * 根据赛事ID分组
  88. * @param [array] $data 球类数据
  89. * @return [array] $data
  90. */
  91. public function Group_ball($data = []){
  92. if(empty($data)){
  93. return $data;
  94. }
  95. foreach($data as $k => $v){
  96. //普通赛事
  97. if($v['match_id'] > 0){
  98. if(!isset($data[$v['match_id']])){
  99. $data[$v['match_id']][] = $v;
  100. unset($data[$k]);
  101. }else{
  102. $data[$v['match_id']][] = $v;
  103. unset($data[$k]);
  104. }
  105. }
  106. //冠军联赛
  107. if($v['match_id'] == 0){
  108. if(!isset($data[$v['lg_id']])){
  109. $data[$v['lg_id']][] = $v;
  110. unset($data[$k]);
  111. }else{
  112. $data[$v['lg_id']][] = $v;
  113. unset($data[$k]);
  114. }
  115. }
  116. }
  117. return $data;
  118. }
  119. /**
  120. * 单式投注数据处理
  121. * @param [array] $res 单式投注数据
  122. * @return [int] 1 成功 其他失败
  123. * @param [int] $batch_id 批量ID
  124. * 弃用
  125. */
  126. public function Simplex_bet(array $res,array $userInfo,$batch_id){
  127. if(empty($res)||empty($userInfo)||empty($batch_id)){
  128. Render('', '7003',lang('Errors','Api')->get('error-7003'));
  129. }
  130. //验证与判断投注了几种
  131. foreach($res as $k => $v){
  132. $num = $k + 1;
  133. if(empty($v['bet_amount'])){
  134. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5105');
  135. Render([], '5105',$msg);
  136. }
  137. //根据类型分组
  138. if(!isset($res[$v['ballId']])){
  139. $res[$v['ballId']][] = $v;
  140. unset($res[$k]);
  141. }else{
  142. $res[$v['ballId']][] = $v;
  143. unset($res[$k]);
  144. }
  145. }
  146. //球类联赛分类并且插入数据库
  147. foreach($res as $k => $v){
  148. $this->Simplex_bet_insert($v,$userInfo,$k,$batch_id);
  149. /*
  150. $gamedate = $this->Group_ball($res[$k]);
  151. $this->Simplex_bet_insert($gamedate,$userInfo,$k,$batch_id);
  152. */
  153. }
  154. return $odds_ids;
  155. }
  156. //弃用 备份
  157. public function Simplex_bet___(array $res,array $userInfo,$batch_id){
  158. if(empty($res)||empty($userInfo)||empty($batch_id)){
  159. Render('', '7003',lang('Errors','Api')->get('error-7003'));
  160. }
  161. //验证与判断投注了几种
  162. foreach($res as $k => $v){
  163. $num = $k + 1;
  164. if(empty($v['bet_amount'])){
  165. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5105');
  166. Render([], '5105',$msg);
  167. }
  168. //根据类型分组
  169. if(!isset($res[$v['ballId']])){
  170. $res[$v['ballId']][] = $v;
  171. unset($res[$k]);
  172. }else{
  173. $res[$v['ballId']][] = $v;
  174. unset($res[$k]);
  175. }
  176. }
  177. //球类联赛分类并且插入数据库
  178. foreach($res as $k => $v){
  179. $gamedate = $this->Group_ball($res[$k]);
  180. $this->Simplex_bet_insert($gamedate,$userInfo,$k,$batch_id);
  181. }
  182. return $odds_ids;
  183. }
  184. /**
  185. * 单式投注单个球类数据插入
  186. * @param [array] $data 单式投注数据
  187. * @param [array] $userInfo 用户信息
  188. * @param [type] $game_code 球类代码
  189. * @param [int] $batch_id 批量ID
  190. * @return [int] 1 成功 其他失败
  191. */
  192. public function Simplex_bet_insert__($data,$userInfo,$game_code,$batch_id){
  193. if(empty($data)||empty($userInfo)||empty($game_code)){
  194. Render('', '7003',lang('Errors','Api')->get('error-7003'));
  195. }
  196. foreach($data as $k => $v){
  197. $OrderID = OrderID('S');//生成订单ID
  198. $UUID = UUID(); //生成信息ID
  199. $bet_money = 0; //赛事总投注额
  200. $prize_money = 0; //可赢额
  201. //获取投注单中是否有冠军联赛数据
  202. $championData = [];
  203. foreach($v as $kk => $vv){
  204. $bet_money += $vv['bet_amount'];
  205. //计算每个投注玩法下的可赢金额
  206. // $prize_money += $this->commonFunction->getEarnMoney($game_code,$vv['p_code'],$vv['odds'],$vv['bet_amount'],1);
  207. $prize_money += $vv['odds']* $vv['bet_amount'];
  208. //是否是滚球投注
  209. $is_rolling = 0;
  210. if($vv['oddsType']=='StRollBall'){
  211. $is_rolling = 1;
  212. }
  213. $matchData =[
  214. 'odds_id' =>$vv['id'],
  215. 'home_team' => $vv['home_team'],
  216. 'guest_team' => $vv['guest_team'],
  217. 'condition' => $vv['condition'],
  218. 'odds' => $vv['odds'],
  219. 'odds_code' => $vv['odds_code'],
  220. 'p_code' => $vv['p_code'],
  221. 'odds_only' => $vv['odds_only'],
  222. 'match_id' => $vv['match_id'],
  223. 'game_code' => $game_code,
  224. 'lg_id' => $vv['lg_id'],
  225. 'batch_id' => $batch_id,
  226. 'bet_type' => 1,
  227. 'bet_money' => $vv['bet_amount'],
  228. 'ctime' => $this->time,
  229. 'utime' => $this->time,
  230. 'is_rolling' =>$is_rolling,
  231. ];
  232. //插入数据源
  233. $moneyBuyMatch = lm('MoneyBuyMatch','Api')->insert($matchData);
  234. if(!$moneyBuyMatch){
  235. Render('','3205',lang('errors','Api')->get('error-3205'));
  236. }
  237. }
  238. $order =[
  239. 'info_identity' => $UUID,
  240. 'account_name' => $userInfo['account'],
  241. 'account_identity' => $userInfo['account_identity'],
  242. 'order_id' => OrderID('S'),//$OrderID,
  243. 'game_code' => $game_code,
  244. 'batch_id' => $batch_id,
  245. 'match_id' => $k,
  246. 'game_no' => "",
  247. 'prize_money'=> $prize_money,
  248. 'money' => $bet_money, //投注金额
  249. 'money_time' => $this->time
  250. ];
  251. $insert = lm("MoneyBuySimplex",'Api')->insert($order);
  252. if(!$insert){
  253. Render('','3205',lang('errors','Api')->get('error-3205'));
  254. }
  255. $insertMoney = $this->M->insertMoney($userInfo['account_identity'],$bet_money,$OrderID,1,"",$UUID,$userInfo);
  256. if($insertMoney!=1){
  257. Render('', $insertMoney,lang('Errors','Api')->get('error'.$insertMoney));
  258. }
  259. $this->M->fanshui($userInfo['account_identity'],$userInfo['account'],$bet_money,$OrderID);//反水
  260. }
  261. }
  262. //按赛事分组 一个赛事一个注单
  263. public function Simplex_bet_insert_v1($data,$userInfo,$game_code,$batch_id){
  264. if(empty($data)||empty($userInfo)||empty($game_code)){
  265. Render('', '7003',lang('Errors','Api')->get('error-7003'));
  266. }
  267. foreach($data as $k => $v){
  268. $OrderID = OrderID('S');//生成订单ID
  269. $UUID = UUID(); //生成信息ID
  270. $bet_money = 0; //赛事总投注额
  271. $prize_money = 0; //可赢额
  272. foreach($v as $kk => $vv){
  273. //是否是冠军投注,如果==0 则为冠军投注
  274. $is_champion = 0;
  275. if($vv['match_id'] == 0){
  276. $is_champion = 1;
  277. }
  278. //获取联赛id lg_id
  279. $lg_id = $vv['lg_id'];
  280. $bet_money += $vv['bet_amount'];
  281. //计算每个投注玩法下的可赢金额
  282. // $prize_money += $this->commonFunction->getEarnMoney($game_code,$vv['p_code'],$vv['odds'],$vv['bet_amount'],1);
  283. $prize_money += $vv['odds']* $vv['bet_amount'];
  284. //是否是滚球投注
  285. $is_rolling = 0;
  286. if($vv['oddsType']=='StRollBall'){
  287. $is_rolling = 1;
  288. }
  289. $matchData =[
  290. 'odds_id' =>$vv['id'],
  291. 'home_team' => $vv['home_team'],
  292. 'guest_team' => $vv['guest_team'],
  293. 'condition' => $vv['condition'],
  294. 'odds' => $vv['odds'],
  295. 'odds_code' => $vv['odds_code'],
  296. 'p_code' => $vv['p_code'],
  297. 'odds_only' => $vv['odds_only'],
  298. 'match_id' => $vv['match_id'],
  299. 'game_code' => $game_code,
  300. 'lg_id' => $vv['lg_id'],
  301. 'batch_id' => $batch_id,
  302. 'bet_type' => 1,
  303. 'bet_money' => $vv['bet_amount'],
  304. 'ctime' => $this->time,
  305. 'utime' => $this->time,
  306. 'is_rolling' =>$is_rolling,
  307. 'is_champion' => $is_champion?:0,//1 冠军投注 0普通赛事投注
  308. 'order_id'=>$OrderID,//追加所属订单id
  309. ];
  310. //插入数据源
  311. $moneyBuyMatch = lm('MoneyBuyMatch','Api')->insert($matchData);
  312. if(!$moneyBuyMatch){
  313. Render('','3205',lang('errors','Api')->get('error-3205'));
  314. }
  315. }
  316. $order = [];
  317. $order =[
  318. 'info_identity' => $UUID,
  319. 'account_name' => $userInfo['account'],
  320. 'account_identity' => $userInfo['account_identity'],
  321. 'order_id' => $OrderID,//$OrderID,
  322. 'game_code' => $game_code,
  323. 'batch_id' => $batch_id,
  324. 'match_id' => $k,
  325. 'lg_id' => $lg_id,
  326. 'game_no' => "",
  327. 'prize_money'=> $prize_money,
  328. 'money' => $bet_money, //投注金额
  329. 'money_time' => $this->time,
  330. 'is_champion' => $is_champion?:0,//1 冠军投注 0普通赛事投注
  331. ];
  332. //如果是冠军投注,追加字段
  333. if($is_champion == 1){
  334. $json_single_result = [
  335. 'lg_id'=> '',
  336. 'game_name' => '',
  337. 'result' => '',
  338. 'updatetime' => '',
  339. ];
  340. $order['single_result'] = json_encode($json_single_result);
  341. }
  342. //如果是滚球投注,追加字段
  343. if($is_rolling == 1 and $game_code == 'zq'){
  344. $order['roll_ratify'] = 2;//未审核滚球投注
  345. }
  346. $insert = lm("MoneyBuySimplex",'Api')->insert($order);
  347. if(!$insert){
  348. Render('','3205',lang('errors','Api')->get('error-3205'));
  349. }
  350. $insertMoney = $this->M->insertMoney($userInfo['account_identity'],$bet_money,$OrderID,1,"",$UUID,$userInfo);
  351. if($insertMoney!=1){
  352. Render('', $insertMoney,lang('Errors','Api')->get('error'.$insertMoney));
  353. }
  354. $this->M->fanshui($userInfo['account_identity'],$userInfo['account'],$bet_money,$OrderID);//反水
  355. }
  356. }
  357. //无需按赛事分组
  358. public function Simplex_bet_insert($data,$userInfo,$game_code,$batch_id){
  359. if(empty($data)||empty($userInfo)||empty($game_code)){
  360. Render('', '7003',lang('Errors','Api')->get('error-7003'));
  361. }
  362. foreach($data as $k => $v){
  363. $OrderID = OrderID('S');//生成订单ID
  364. $UUID = UUID(); //生成信息ID
  365. $bet_money = 0; //赛事总投注额
  366. $prize_money = 0; //可赢额
  367. //====处理 投注数据
  368. //是否是冠军投注,如果==0 则为冠军投注
  369. $is_champion = 0;
  370. if($v['match_id'] == 0){
  371. $is_champion = 1;
  372. }
  373. //获取联赛id lg_id
  374. $lg_id = $v['lg_id'];
  375. $bet_money = $v['bet_amount'];
  376. //计算每个投注玩法下的可赢金额
  377. $prize_money = $v['odds']* $v['bet_amount'];
  378. //是否是滚球投注
  379. $is_rolling = 0;
  380. //追加滚球投注时 赛事进行时间
  381. $roll_time = 0;
  382. //主队得分
  383. $home_score = 0;
  384. //客队得分
  385. $guest_score = 0;
  386. if($v['oddsType']=='StRollBall'){
  387. $is_rolling = 1;
  388. $roll_time=$v['roll_time']?:'';
  389. $home_score = $v['home_score']?:0;
  390. $guest_score = $v['guest_score']?:0;
  391. }
  392. //===追加各球类其他状态码===
  393. //即将
  394. if($v['oddsType']=='StSoon'){
  395. $is_rolling = 2;
  396. }
  397. //今日
  398. if($v['oddsType']=='StToday'){
  399. $is_rolling = 3;
  400. }
  401. //早盘
  402. if($v['oddsType']=='StMorningPlate'){
  403. $is_rolling = 4;
  404. }
  405. //串场
  406. if($v['oddsType']=='StStringScene'){
  407. $is_rolling = 5;
  408. }
  409. //冠军
  410. if($v['oddsType']=='StChampion'){
  411. $is_rolling = 6;
  412. }
  413. //===end===
  414. $matchData =[
  415. 'odds_id' =>$v['id'],
  416. 'home_team' => $v['home_team'],
  417. 'guest_team' => $v['guest_team'],
  418. 'condition' => $v['condition'],
  419. 'odds' => $v['odds'],
  420. 'odds_code' => $v['odds_code'],
  421. 'p_code' => $v['p_code'],
  422. 'odds_only' => $v['odds_only'],
  423. 'match_id' => $v['match_id'],
  424. 'game_code' => $game_code,
  425. 'lg_id' => $lg_id,
  426. 'batch_id' => $batch_id,
  427. 'bet_type' => 1,
  428. 'bet_money' => $bet_money,
  429. 'ctime' => $this->time,
  430. 'utime' => $this->time,
  431. 'is_rolling' =>$is_rolling,
  432. 'is_champion' => $is_champion?:0,//1 冠军投注 0普通赛事投注
  433. 'order_id'=>$OrderID,//追加所属订单id
  434. 'roll_time'=>$roll_time,//追加滚球投注时 赛事进行时间
  435. 'home_score'=>$home_score,//主队得分
  436. 'guest_score'=>$guest_score,//客队得分
  437. ];
  438. //插入数据源
  439. $moneyBuyMatch = lm('MoneyBuyMatch','Api')->insert($matchData);
  440. if(!$moneyBuyMatch){
  441. Render('','3205',lang('errors','Api')->get('error-3205'));
  442. }
  443. //===end===
  444. $order = [];
  445. $order =[
  446. 'info_identity' => $UUID,
  447. 'account_name' => $userInfo['account'],
  448. 'account_identity' => $userInfo['account_identity'],
  449. 'order_id' => $OrderID,//$OrderID,
  450. 'game_code' => $game_code,
  451. 'batch_id' => $batch_id,
  452. 'match_id' => $v['match_id'],
  453. 'lg_id' => $lg_id,
  454. 'game_no' => "",
  455. 'prize_money'=> $prize_money,
  456. 'money' => $bet_money, //投注金额
  457. 'money_time' => $this->time,
  458. 'is_champion' => $is_champion?:0,//1 冠军投注 0普通赛事投注
  459. ];
  460. //如果是冠军投注,追加字段
  461. if($is_champion == 1){
  462. $json_single_result = [
  463. 'lg_id'=> '',
  464. 'game_name' => '',
  465. 'result' => '',
  466. 'updatetime' => '',
  467. ];
  468. $order['single_result'] = json_encode($json_single_result);
  469. }
  470. //如果是滚球投注,追加字段
  471. if($is_rolling == 1 and $game_code == 'zq'){
  472. $order['roll_ratify'] = 2;//未审核滚球投注
  473. }
  474. $insert = lm("MoneyBuySimplex",'Api')->insert($order);
  475. if(!$insert){
  476. Render('','3205',lang('errors','Api')->get('error-3205'));
  477. }
  478. $insertMoney = $this->M->insertMoney($userInfo['account_identity'],$bet_money,$OrderID,1,"",$UUID,$userInfo);
  479. if($insertMoney!=1){
  480. Render('', $insertMoney,lang('Errors','Api')->get('error'.$insertMoney));
  481. }
  482. $this->M->fanshui($userInfo['account_identity'],$userInfo['account'],$bet_money,$OrderID);//反水
  483. }
  484. }
  485. public function Bet(){
  486. /*
  487. // 模拟单式数据
  488. $get_data = [
  489. 'data'=>[
  490. [
  491. 'type'=>1,
  492. 'index'=>0,
  493. 'bet_amount'=>100,
  494. ],
  495. [
  496. 'type'=>1,
  497. 'index'=>1,
  498. 'bet_amount'=>100,
  499. ],
  500. [
  501. [
  502. 'id'=>10350565,
  503. 'home_team'=>'什鲁斯伯里',
  504. 'guest_team'=>'彼德堡',
  505. 'name'=>'大',
  506. 'odds'=>1.09,
  507. 'match_id'=>1463,
  508. 'condition'=>'2.5/3',
  509. 'ballId'=>'zq',
  510. 'odds_code'=>'gsb',
  511. 'p_code'=>'GS',
  512. 'play_name'=>'进球大小',
  513. 'score'=>0,
  514. 'bettingTime'=>date('Y-m-d H:i:s',time()),
  515. 'odds_only'=>'6a003dec710a42849a82dd1ea978f572',
  516. 'lg_id'=>17,
  517. 'match_type'=>'',//滚球投注
  518. 'odds_id'=>10350565,
  519. 'repeat'=>'true',
  520. 'is_champion'=>0,//是否是冠军联赛
  521. ],
  522. [
  523. 'id'=>10781633,
  524. 'home_team'=>'什鲁斯伯里',
  525. 'guest_team'=>'彼德堡',
  526. 'name'=>'彼德堡',
  527. 'odds'=>1,
  528. 'match_id'=>1463,
  529. 'condition'=>'-0/0.5',
  530. 'ballId'=>'zq',
  531. 'odds_code'=>'cog',
  532. 'p_code'=>'CO',
  533. 'play_name'=>'让球',
  534. 'score'=>0,
  535. 'bettingTime'=>date('Y-m-d H:i:s',time()),
  536. 'odds_only'=>'390e2b68992587438c0020179abc94f5',
  537. 'lg_id'=>17,
  538. 'match_type'=>'',//滚球投注
  539. 'odds_id'=>10781633,
  540. 'repeat'=>'true',
  541. 'is_champion'=>0,//是否是冠军联赛
  542. ]
  543. ]
  544. ],
  545. 'token'=>'hdetVs15729386035dc1236b1c189',
  546. 'bet_money'=>200,
  547. ];
  548. */
  549. /*
  550. // 模拟串式数据
  551. $get_data = [
  552. 'data'=>[
  553. [
  554. 'type'=>1,
  555. 'index'=>0,
  556. 'bet_amount'=>1,
  557. ],
  558. [
  559. 'type'=>'3串1',
  560. 'bet_amount' =>1,
  561. ],
  562. [
  563. [
  564. 'id'=>229351,
  565. 'home_team'=>'布琳狮子U20',
  566. 'guest_team'=>'圣奥尔本斯U20',
  567. 'name'=>'圣奥尔本斯U20',
  568. 'odds'=>1.01,
  569. 'match_id'=>0,
  570. 'condition'=>'4/4.5',
  571. 'ballId'=>'zq',
  572. 'odds_code'=>'goal_size_big',
  573. 'p_code'=>'goal_size',
  574. 'play_name'=>'大小',
  575. 'score'=>0,
  576. 'bettingTime'=>date('Y-m-d H:i:s',time()),
  577. 'odds_only'=>'908f8bae2233a0384c96c7b1f42e9ea7',
  578. 'lg_id'=>153,
  579. 'repeat'=>'false',
  580. 'match_type'=>'StRollBall',//滚球投注
  581. ],
  582. [
  583. 'id'=>238940,
  584. 'home_team'=>'格罗兹尼艾哈迈德U20',
  585. 'guest_team'=>'奥伦堡U20',
  586. 'name'=>'格罗兹尼艾哈迈德U20',
  587. 'odds'=>0.94,
  588. 'match_id'=>1247,
  589. 'condition'=>'1/1.5',
  590. 'ballId'=>'zq',
  591. 'odds_code'=>'concede_home',
  592. 'p_code'=>'concede',
  593. 'play_name'=>'让球',
  594. 'score'=>0,
  595. 'bettingTime'=>date('Y-m-d H:i:s',time()),
  596. 'odds_only'=>'9125bf40d6649fcdb663c43d8f55bebd',
  597. 'lg_id'=>20,
  598. 'repeat'=>'false',
  599. 'match_type'=>'StRollBall',//滚球投注
  600. ],
  601. [
  602. 'id'=>238958,
  603. 'home_team'=>'巴扬卡拉',
  604. 'guest_team'=>'马都拉联',
  605. 'name'=>'巴扬卡拉',
  606. 'odds'=>1.13,
  607. 'match_id'=>1254,
  608. 'condition'=>'0.5',
  609. 'ballId'=>'zq',
  610. 'odds_code'=>'concede_home',
  611. 'p_code'=>'concede',
  612. 'play_name'=>'让球',
  613. 'score'=>0,
  614. 'bettingTime'=>date('Y-m-d H:i:s',time()),
  615. 'odds_only'=>'3cf81ac0b9765218845fe8bc4de0ad99',
  616. 'lg_id'=>61,
  617. 'repeat'=>'false',
  618. 'match_type'=>'StRollBall',//滚球投注
  619. ],
  620. ]
  621. ],
  622. 'token'=>'ppWGRW15682541865d79a8eacb0b1',
  623. 'bet_money'=>2
  624. ];
  625. */
  626. //如果获取不到data,则为游戏端提交,需转成数组
  627. if(!empty($_POST['game_data'])){
  628. //获取参数转成数组
  629. $get_data = json_decode($_POST['game_data'],true);
  630. $token = $get_data['token'];
  631. $bet_money = $get_data['bet_money'];
  632. $data = $get_data['data'];
  633. // $bet_money_arr = $get_data['bet_money_arr'];
  634. }else{
  635. $data = $_REQUEST['data'];
  636. $bet_money = $_REQUEST['bet_money'];
  637. $token = $_REQUEST['token'];
  638. // $bet_money_arr = $_REQUEST['bet_money_arr'];
  639. }
  640. /*
  641. $data = $get_data['data'];
  642. $bet_money = $get_data['bet_money'];
  643. $token = $get_data['token'];
  644. */
  645. //验证用户token
  646. $userInfo = $this->getAgent($token);
  647. if(empty($data)||empty($bet_money)){
  648. Render('', '51029',lang('Errors','Api')->get('error-51029'));
  649. }
  650. //获取用户金额信息
  651. if(!$this->M->verifyMoney($bet_money,$userInfo['cash'])){
  652. Render('', '4204',lang('Errors','Api')->get('error-4204'));
  653. }
  654. //追加验证用户投注金额限额
  655. $this->bet_money_limit($data,$userInfo);
  656. //获取订单批次号
  657. $batch_id = OrderID();///strtotime(date('Y-m-d H:i:s',time())).mt_rand('1','99');//批量ID
  658. //执行数据插入
  659. try {
  660. _beginTransaction();//开启事务
  661. $data = $this->Classify($data);
  662. if(!empty($data['single'])){
  663. $this->Simplex_bet($data['single'],$userInfo,$batch_id);//单式数据处理
  664. }
  665. if(!empty($data['bunch'])){
  666. $data_all = $data['bunch'][count($data['bunch'])-1];//获取最后一个数组
  667. unset($data['bunch'][count($data['bunch'])-1]);//删除最后一个元素
  668. $this->stringBet($data['bunch'],$data_all,$userInfo,$batch_id);
  669. }
  670. _commit();//提交
  671. Render('', '1',lang('Errors','Api')->get('error-1'));
  672. } catch (Exception $e) {
  673. _rollBack();//回滚
  674. print $e->getMessage();
  675. }
  676. }
  677. /**
  678. * token获取用户详情
  679. */
  680. public function getAgent($token = '') {
  681. $checkToken = $this->accountManager->checkEffectiveTime($token);
  682. if ($checkToken['status'] != 1) {
  683. Render($checkToken['data'], $checkToken['status'], lang('commons')->get('user does login'));
  684. };
  685. $result = $this->accountManager->refreshToken($token);
  686. return $result['data'];
  687. }
  688. //验证联赛是否存在
  689. public function verify_league($game_code,$lg_id,$num = 1){
  690. if(empty($lg_id)){
  691. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5112');
  692. Render([], '5112',$msg);
  693. }
  694. //根据球类代码 获取相关model
  695. $model = $this->commonFunction->getModels($game_code);
  696. $model_league = $model['model_league'];
  697. $info = lm($model_league,'Sports')
  698. ->select('id')
  699. ->where('id',$lg_id)
  700. ->first();
  701. if(empty($info)){
  702. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5113');
  703. Render([], '5113',$msg);
  704. }
  705. }
  706. /**
  707. * 赛事是否存在或结束
  708. * @param [type] $game_code 球类代码
  709. * @param [type] $match_id 赔率ID
  710. * @param [type] $num 循环键值
  711. * $@param [type]type_str 串式
  712. *
  713. */
  714. public function verify_match($game_code,$match_id,$num = 1){
  715. if(empty($match_id)){
  716. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5109');
  717. Render([], '5109',$msg);
  718. }
  719. //根据球类代码 获取相关model
  720. $models = $this->commonFunction->getModels($game_code);
  721. $model_match = $models['model_match'];
  722. $info = lm($model_match,'Sports')
  723. ->select('status')
  724. ->where('status', '<', '2')
  725. // ->where('us_time','>',qgmdate('Y-m-d H:i:s','', -4))
  726. // ->where('source','hg3535')
  727. ->where('id',$match_id)
  728. ->first();
  729. if(empty($info)){
  730. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5103');
  731. Render([], '5103',$msg);
  732. }
  733. $info = $info->toarray();
  734. if($info['status'] == 2){
  735. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5108');
  736. Render([], '5108',$msg);
  737. }
  738. return $info;
  739. }
  740. /**
  741. * 验证球类是否存在
  742. * @param [type] $game_code 球类代码
  743. * @param [type] $num 第几条
  744. */
  745. public function verify_gameType($game_code,$num){
  746. if(empty($game_code)){
  747. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-5102');
  748. Render([], '5102', $msg);
  749. }
  750. $info = lm('GameType','Sports')->where('game_code',$game_code)->first();
  751. if(empty($info)){
  752. $msg = '第' . $num . '条数据' . lang('Errors', 'Api')->get('error-5102');
  753. Render([], '5102', $msg);
  754. }
  755. return $info;
  756. }
  757. /**
  758. * 验证赔率是否存在
  759. * @param [type] $game_code 球类代码
  760. * @param [type] $odds_id 赔率IDkk
  761. *
  762. */
  763. public function verify_odds($game_code,$odds_only,$odds,$condition,$odds_code,$num = 1){
  764. if (empty($odds_only)){
  765. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-5111');
  766. Render([], '5111', $msg);
  767. }
  768. if(empty($odds_code)){
  769. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7052');
  770. Render([], '7052', $msg);
  771. }
  772. if(empty($odds)){
  773. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7029');
  774. Render([], '7029', $msg);
  775. }
  776. $odds_codeNum = odds_codeModel::getOddsCode($odds_code,$game_code);
  777. if($odds_codeNum < 1){
  778. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7053');
  779. Render([], '7053', $msg);
  780. }
  781. //根据球类代码 获取相关model
  782. $models = $this->commonFunction->getModels($game_code);
  783. $model_odds = $models['model_odds'];
  784. $model_odds_record = $models['model_odds_record'];
  785. //匹配赔率是否过期
  786. $odds_info = lm($model_odds,'Sports')
  787. ->join($model_odds_record,$model_odds_record.'.odds_only',$model_odds.'.odds_only')
  788. ->select($model_odds_record.'.odds',$model_odds_record.'.condition')
  789. ->where($model_odds.'.odds_only',$odds_only)
  790. ->first();
  791. if(empty($odds_info)){
  792. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7051');
  793. Render([], '7051', $msg);
  794. }
  795. $odds_info = $odds_info->toarray();
  796. if($condition != $odds_info['condition']){
  797. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7047');
  798. Render([], '7047', $msg);
  799. }
  800. if($odds != $odds_info['odds']){
  801. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7049');
  802. Render([], '7049', $msg);
  803. }
  804. return $odds_info;
  805. }
  806. /**
  807. * 处理串式投注
  808. * $data_str 各串式 数据
  809. * $data_all 所有赛事 数据
  810. * $userInfo 用户数据
  811. * $batch_id 批次号
  812. */
  813. public function stringBet__($data_str=[],$data_all=[],$userInfo=[],$batch_id=0){
  814. //验证串式投注是否合法
  815. $this->verify_stringType($data_all);
  816. //获取串式 总赛事数量
  817. $groupNum = count($data_all);//总数量
  818. $orderData = [];
  819. foreach ($data_str as $k=>$v){
  820. //字符串替换
  821. $str = str_ireplace("串","_",$v['type']);
  822. //获取type 3_12
  823. $after = substr($str,strpos($str,"_")+1);
  824. //当前串式 值
  825. $sonNum = substr($str,0,strrpos($str,"_"));
  826. //获取倍数
  827. if((int)$after == 1){
  828. $multiple = $this->getMultiple($groupNum,(int)$sonNum);
  829. }else{
  830. $multiple =(int)$after;
  831. }
  832. //获取当前串式投注金额及可赢金额
  833. if($after == 1){
  834. $money_all = $this->getmakeMoney((int)$sonNum,$v['bet_amount'],$multiple,$data_all,$after);
  835. }else{
  836. $money_all = $this->getStrAllMoney((int)$sonNum,$v['bet_amount'],$multiple,$data_all,$after);
  837. }
  838. //投注金额
  839. $moneyData = $money_all['moneyData'];
  840. //可赢金额
  841. $prize_money = $money_all['prize_money'];
  842. $uuid = UUID();
  843. $orderID = OrderID('T');
  844. $orderData[$k]['info_identity'] = $uuid;
  845. $orderData[$k]['account_name'] = $userInfo['account'];
  846. $orderData[$k]['account_identity'] = $userInfo['account_identity'];
  847. $orderData[$k]['order_id'] = $orderID;
  848. $orderData[$k]['game_no'] = "";
  849. $orderData[$k]['money'] = $moneyData;//$pay_money//总投注金额
  850. $orderData[$k]['money_time'] = $this->time;//下注时间
  851. $orderData[$k]['prize_money'] = $prize_money;//$expect_money ;//预期总可赢金额
  852. $orderData[$k]['str_type'] = $v['type'];//串式类型
  853. $orderData[$k]['batch_id'] = $batch_id;//批次号
  854. $orderData[$k]['wait_match_num'] = count($data_all);//批次号
  855. //写资金变动表
  856. $insertMoney = $this->M->insertMoney($userInfo['account_identity'],$moneyData,$orderID,1,"",$uuid,$userInfo);
  857. if($insertMoney!=1){
  858. Render('', $insertMoney,lang('Errors','Api')->get('error'.$insertMoney));
  859. }
  860. $this->M->fanshui($userInfo['account_identity'],$userInfo['account'],$moneyData,$orderID);//反水
  861. }
  862. //写注单 表
  863. $moneyBuy = lm('MoneyBuyStr','Api')->insert($orderData);
  864. if(!$moneyBuy){
  865. Render('','3205',lang('errors','Api')->get('error-3205'));
  866. }
  867. foreach ($data_all as $kk=>$vv){
  868. $getModels = $this->commonFunction->getModels($vv['ballId']);
  869. $model_match = $getModels['model_match'];
  870. //获取是否是滚球投注
  871. $is_rolling = 0;
  872. if($vv['match_type'] == 'StRollBall'){
  873. $is_rolling = 1;
  874. }
  875. $matchData[] = [
  876. 'batch_id'=>$batch_id,
  877. 'odds_id'=>$vv['id'],
  878. 'home_team'=>$vv['home_team'],
  879. 'guest_team'=>$vv['guest_team'],
  880. 'condition'=>$vv['condition'],
  881. 'odds'=>$vv['odds'],
  882. 'odds_code'=>$vv['odds_code'],
  883. 'p_code'=>$vv['p_code'],
  884. 'odds_only'=>$vv['odds_only'],
  885. 'status'=>0,
  886. 'match_id'=>$vv['match_id'],
  887. 'game_code'=>$vv['ballId'],
  888. 'lg_id'=> $vv['lg_id'],
  889. 'ctime'=>$this->time,//写入时间
  890. 'utime' => $this->time,
  891. 'bet_type'=>2,
  892. 'is_rolling'=>$is_rolling,
  893. 'order_id'=> $orderID,
  894. ];
  895. }
  896. //写赔率数据表
  897. $moneyBuyMatch = lm('MoneyBuyMatch','Api')->insert($matchData);
  898. if(!$moneyBuyMatch){
  899. Render('','3205',lang('errors','Api')->get('error-3205'));
  900. }
  901. return true;
  902. }
  903. public function stringBet($data_str=[],$data_all=[],$userInfo=[],$batch_id=0){
  904. //验证串式投注是否合法
  905. $this->verify_stringType($data_all);
  906. //获取串式 总赛事数量
  907. $groupNum = count($data_all);//总数量
  908. $orderData = [];
  909. foreach ($data_str as $k=>$v){
  910. //字符串替换
  911. $str = str_ireplace("串","_",$v['type']);
  912. //获取type 3_12
  913. $after = substr($str,strpos($str,"_")+1);
  914. //当前串式 值
  915. $sonNum = substr($str,0,strrpos($str,"_"));
  916. //获取倍数
  917. if((int)$after == 1){
  918. $multiple = $this->getMultiple($groupNum,(int)$sonNum);
  919. }else{
  920. $multiple =(int)$after;
  921. }
  922. //获取当前串式投注金额及可赢金额
  923. if($after == 1){
  924. $money_all = $this->getmakeMoney((int)$sonNum,$v['bet_amount'],$multiple,$data_all,$after);
  925. }else{
  926. $money_all = $this->getStrAllMoney((int)$sonNum,$v['bet_amount'],$multiple,$data_all,$after);
  927. }
  928. //投注金额
  929. $moneyData = $money_all['moneyData'];
  930. //可赢金额
  931. $prize_money = $money_all['prize_money'];
  932. $uuid = UUID();
  933. $orderID = OrderID('T');
  934. $orderData[$k]['info_identity'] = $uuid;
  935. $orderData[$k]['account_name'] = $userInfo['account'];
  936. $orderData[$k]['account_identity'] = $userInfo['account_identity'];
  937. $orderData[$k]['order_id'] = $orderID;
  938. $orderData[$k]['game_no'] = "";
  939. $orderData[$k]['money'] = $moneyData;//$pay_money//总投注金额
  940. $orderData[$k]['money_time'] = $this->time;//下注时间
  941. $orderData[$k]['prize_money'] = $prize_money;//$expect_money ;//预期总可赢金额
  942. $orderData[$k]['str_type'] = $v['type'];//串式类型
  943. $orderData[$k]['batch_id'] = $batch_id;//批次号
  944. $orderData[$k]['wait_match_num'] = count($data_all);//批次号
  945. //写资金变动表
  946. $insertMoney = $this->M->insertMoney($userInfo['account_identity'],$moneyData,$orderID,1,"",$uuid,$userInfo);
  947. if($insertMoney!=1){
  948. Render('', $insertMoney,lang('Errors','Api')->get('error'.$insertMoney));
  949. }
  950. $this->M->fanshui($userInfo['account_identity'],$userInfo['account'],$moneyData,$orderID);//反水
  951. //===处理赛事数据===
  952. foreach ($data_all as $kk=>$vv){
  953. $getModels = $this->commonFunction->getModels($vv['ballId']);
  954. $model_match = $getModels['model_match'];
  955. //获取是否是滚球投注
  956. $is_rolling = 0;
  957. if($vv['match_type'] == 'StRollBall'){
  958. $is_rolling = 1;
  959. }
  960. $matchData[] = [
  961. 'batch_id'=>$batch_id,
  962. 'odds_id'=>$vv['id'],
  963. 'home_team'=>$vv['home_team'],
  964. 'guest_team'=>$vv['guest_team'],
  965. 'condition'=>$vv['condition'],
  966. 'odds'=>$vv['odds'],
  967. 'odds_code'=>$vv['odds_code'],
  968. 'p_code'=>$vv['p_code'],
  969. 'odds_only'=>$vv['odds_only'],
  970. 'status'=>0,
  971. 'match_id'=>$vv['match_id'],
  972. 'game_code'=>$vv['ballId'],
  973. 'lg_id'=> $vv['lg_id'],
  974. 'ctime'=>$this->time,//写入时间
  975. 'utime' => $this->time,
  976. 'bet_type'=>2,
  977. 'is_rolling'=>$is_rolling,
  978. 'order_id'=> $orderID,
  979. ];
  980. }
  981. }
  982. //写赔率数据表
  983. $moneyBuyMatch = lm('MoneyBuyMatch','Api')->insert($matchData);
  984. if(!$moneyBuyMatch){
  985. Render('','3205',lang('errors','Api')->get('error-3205'));
  986. }
  987. //===end===
  988. //写注单 表
  989. $moneyBuy = lm('MoneyBuyStr','Api')->insert($orderData);
  990. if(!$moneyBuy){
  991. Render('','3205',lang('errors','Api')->get('error-3205'));
  992. }
  993. return true;
  994. }
  995. /**
  996. * 计算串式投注 倍率
  997. * $groupNum 总赛事 数量
  998. * $sonNum 当前串式 数量
  999. */
  1000. public function getMultiple($groupNum = 0,$sonNum = 0){
  1001. if($groupNum and $sonNum){
  1002. $group =1;
  1003. for($i = $groupNum ; $i >($groupNum - $sonNum);$i--) {
  1004. $group = $group * $i;
  1005. }
  1006. $son =1;
  1007. for($i = $sonNum ; $i >0;$i--) {
  1008. $son = $son * $i;
  1009. }
  1010. }
  1011. return $group/$son;
  1012. }
  1013. /**
  1014. * @param int $sonNum 当前串式数量
  1015. * @param int $bet_amount 当前串式投注额
  1016. * @param int $multiple 当前串式倍数
  1017. * @param array $data_all 当前注单 所有投注数据
  1018. * @return mixed
  1019. * 计算当前串式投注总额、可赢总额
  1020. */
  1021. public function getmakeMoney($sonNum=0,$bet_amount=0,$multiple=0,$data_all=[],$after = 0){
  1022. $allNum = count($data_all);//所有数量
  1023. $odds_all = [];//所有投注项数据
  1024. foreach ($data_all as $k=>$v){
  1025. $odds_all[] = $v['odds']+1;
  1026. }
  1027. //投注总额
  1028. $data['moneyData'] = (double)$bet_amount*(double)$multiple;
  1029. if($sonNum == $allNum and (int)$after ==1){//N场赛事 N串一
  1030. //获取所有赛事赔率乘积
  1031. $odds = array_product($odds_all) - 1;
  1032. //获取可赢金额
  1033. $data['prize_money'] = sprintf("%.2f",substr(sprintf("%.3f", $bet_amount*$odds), 0, -1));//floor($bet_amount*$odds*100)/100;//保留两位小数
  1034. }
  1035. if($sonNum < $allNum and (int)$after ==1){//例:N场赛事 <N串一
  1036. //计算 组合赔率
  1037. $group = $this->commonFunction->combination($odds_all, $sonNum);
  1038. foreach ($group as $k=>$v){
  1039. $odds[] = array_product($v);
  1040. }
  1041. $odds = (array_sum($odds))-(count($odds));
  1042. //获取可赢金额
  1043. $data['prize_money'] = sprintf("%.2f",substr(sprintf("%.3f", $bet_amount*$odds), 0, -1));//floor($bet_amount*$odds*100)/100;
  1044. }
  1045. return $data;
  1046. }
  1047. /**
  1048. * 获取所有可用串式 N场赛事 2串1,3串1...N串1,N<11
  1049. * 并计算 其投注可赢金额
  1050. */
  1051. public function getStrAllMoney($sonNum=0,$bet_amount=0,$multiple=0,$data_all=[],$after = 0){
  1052. $d = [];
  1053. for ($i=2;$i<($sonNum+1);$i++){
  1054. $d[] = $i;
  1055. }
  1056. foreach ($d as $k=>$v){
  1057. $multiple = $this->getMultiple($sonNum,$v);
  1058. $money[] = $this->getmakeMoney($v,$bet_amount,$multiple,$data_all,1);
  1059. }
  1060. $data=array();
  1061. foreach($money as $k=>$v){
  1062. $data['moneyData']+=$v['moneyData'];
  1063. $data['prize_money']+=$v['prize_money'];
  1064. }
  1065. return $data;
  1066. }
  1067. /**
  1068. * 验证串式投注 是否合法
  1069. * $data 串式 所有赛事
  1070. */
  1071. public function verify_stringType($data=[]){
  1072. if($data){
  1073. foreach($data as $k=>$v){
  1074. if($v['match_id']==$v['match_id'] ){
  1075. $r[$v['match_id']][] = $v;
  1076. }
  1077. }
  1078. }
  1079. if($r){
  1080. foreach ($r as $k=>$v){
  1081. if(count($v) > 1){
  1082. $msg = lang('Errors','Api')->get('error-50038');
  1083. Render([], '50038',$msg);
  1084. }else if( $k == 0){
  1085. $msg = lang('Errors','Api')->get('error-50043');
  1086. Render([], '50043',$msg);
  1087. }
  1088. }
  1089. }
  1090. }
  1091. /**
  1092. * 更新投注项相关数据
  1093. */
  1094. public function getBetData(){
  1095. $data = $_REQUEST;
  1096. // $data = [['game_code'=>'zq','match_id'=>3095448,'odds_id'=>623078],['game_code'=>'zq','match_id'=>3095448,'odds_id'=>623078]];
  1097. foreach ($data as $k=>$v){
  1098. $getModels = $this->commonFunction->getModels($v['game_code']);
  1099. $model_match = $getModels['model_match'];
  1100. $model_odds = $getModels['model_odds'];
  1101. $oddsData = lm($model_match,"Sports")
  1102. ->join($model_odds,$model_odds.'.match_id',$model_match.'.id')
  1103. ->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')
  1104. // ->where($model_match.'.source',$this->source)
  1105. ->where($model_match.'.id',$v['match_id'])
  1106. ->where($model_odds.'.id',$v['odds_id'])
  1107. //->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  1108. ->first();
  1109. $oddsData->game_code = $v['game_code'];
  1110. $betData[] = $oddsData;
  1111. }
  1112. Render($betData, '1', lang('Tips','Sports')->get('success'));
  1113. }
  1114. //用户投注限额验证
  1115. public function bet_money_limit($bet_money_arr=[],$userInfo=[]){
  1116. if(!empty($bet_money_arr)){
  1117. foreach($bet_money_arr as $k=>$v){
  1118. if($k == (count($bet_money_arr)-1)){
  1119. unset($bet_money_arr[$k]);
  1120. }
  1121. }
  1122. }
  1123. //===追加验证用户投注金额限额===
  1124. if(empty($bet_money_arr)) Render('', '3605',lang('Errors','Api')->get('error-3605'));
  1125. //获取当前用户分组名称
  1126. $substr = substr($userInfo['group_name'],strpos($userInfo['group_name'],'|')+1);
  1127. $role_name = substr($substr, 0, -1);
  1128. //获取用户分组的投注限额
  1129. $bet_money_limit = Dc_user_grade::get_bet_money_limit($role_name);
  1130. foreach($bet_money_arr as $k=>$v){
  1131. 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'));
  1132. 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'));
  1133. }
  1134. //===end==
  1135. }
  1136. //根据token获取用户投注限额
  1137. public function getBetMoneyLimit(){
  1138. //验证用户token
  1139. $token = $_REQUEST['token'];
  1140. $userInfo = $this->getAgent($token);
  1141. //获取当前用户分组名称
  1142. $substr = substr($userInfo['group_name'],strpos($userInfo['group_name'],'|')+1);
  1143. $role_name = substr($substr, 0, -1);
  1144. //获取用户分组的投注限额
  1145. $bet_money_limit = Dc_user_grade::get_bet_money_limit($role_name);
  1146. $data['bet_money_limit']=[
  1147. 'lower_limit'=>$bet_money_limit->lower_limit,
  1148. 'upper_limit'=>$bet_money_limit->upper_limit,
  1149. ];
  1150. Render($data, '1', lang('Tips','Sports')->get('success'));
  1151. }
  1152. /**
  1153. * 用户撤单操作 接口
  1154. * 单订单操作
  1155. */
  1156. public function reOrder(){
  1157. //获取球类代码
  1158. $order_id = $_REQUEST['order_id'];
  1159. $token = $_REQUEST['token'];
  1160. try {
  1161. if(empty($order_id)){
  1162. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  1163. }
  1164. _beginTransaction();//开启事务
  1165. // 单式 S 串式 T
  1166. if(substr($order_id,0,1) == 'S'){
  1167. //单式订单 model
  1168. $order_model = 'money_buy_simplex';
  1169. }else{
  1170. //串式订单 model
  1171. $order_model = 'money_buy_str';
  1172. }
  1173. //查询订单信息
  1174. $order_data = lm($order_model,"Commons")
  1175. ->where('order_id',$order_id)
  1176. ->first();
  1177. //订单不存在
  1178. if(empty($order_data)) throw new \Exception(Render([], '10030', lang('Tips','Sports')->get('order_null')));
  1179. //订单非投注状态 不可撤单
  1180. if($order_data->status != 1) throw new \Exception(Render([], '10031', lang('Tips','Sports')->get('order_type_err')));
  1181. //获取用户数据
  1182. $userInfo = $this->getAgent($token);
  1183. //验证订单是否属于该用户
  1184. if($order_data->account_identity != $userInfo['account_identity']) throw new \Exception(Render([], '10025', lang('Tips','Sports')->get('auth_error').':不是该用户的订单'));
  1185. //获取用户账户余额
  1186. $cash = $userInfo['cash'];
  1187. //获取订单涉及金额
  1188. $where = [
  1189. ['trade_id','=',$order_id],
  1190. ['status','=',1]
  1191. ];
  1192. $odds_money_data = lm('money_details',"Commons")
  1193. ->select('info_identity','trade_id','account_name','account_identity','money','money_type','money_cash','trade_type','status')
  1194. ->where($where)
  1195. ->get();
  1196. if(empty($odds_money_data)) throw new \Exception(Render([], '10026', lang('Tips','Sports')->get('order_money_err')));
  1197. $odds_money_data = $odds_money_data -> toArray();
  1198. //获取订单投注金额
  1199. $odds_money_bet = 0;
  1200. //获取订单反水金额
  1201. $odds_money_fs = 0;
  1202. foreach($odds_money_data as $k=>$v){
  1203. if($v['trade_type'] == 1){
  1204. $odds_money_bet = $v['money'];
  1205. }
  1206. if($v['trade_type'] == 7){
  1207. $odds_money_fs = $v['money'];
  1208. }
  1209. }
  1210. //更新订单状态为已取消
  1211. $up_order = lm($order_model,"Commons")
  1212. ->where('order_id',$order_id)
  1213. ->update(['status'=>3,'use_mark'=>'用户撤单']);
  1214. if($up_order < 1) throw new \Exception(Render([], '10027', lang('Tips','Sports')->get('order_status_up_err')));
  1215. //===退款===
  1216. //需退回金额 = 投注金额-反水金额
  1217. $money = abs($odds_money_bet - $odds_money_fs);
  1218. //用户剩余金额
  1219. $new_available_cash = $cash + $money;
  1220. //组装新增数据
  1221. $set_money_data = [
  1222. 'info_identity' => UUID(),
  1223. 'trade_id' => $order_id,
  1224. 'account_name' => $userInfo['account'],
  1225. 'account_identity' => $userInfo['account_identity'],
  1226. 'money' => $money,
  1227. 'money_time' => date("Y-m-d H:i:s", time()),
  1228. 'money_type' => 1,
  1229. 'money_cash' => $new_available_cash,
  1230. 'trade_type' => 3,
  1231. 'trade_desc' => '用户撤单,退回投注资金:¥'.$money.';订单号:'.$order_id.'',
  1232. 'status' => 1,
  1233. ];
  1234. //更新用户数据
  1235. $ret_user = lm('account_detailed',"Commons")
  1236. ->where('account_identity', $v['account_identity'])
  1237. ->update(['available_cash' => $new_available_cash, 'cash' => $new_available_cash]);
  1238. if($ret_user < 1) throw new \Exception(Render([], '10028', lang('Tips','Sports')->get('up_user_err')));
  1239. //更新用户资金数据
  1240. $set_money = lm('money_details',"Commons")->insert($set_money_data);
  1241. if($set_money != true) throw new \Exception(Render([], '10029', lang('Tips','Sports')->get('add_money_err')));
  1242. //===end===
  1243. _commit();//提交
  1244. Render('', '1', lang('Tips','Sports')->get('success'));
  1245. } catch (\Exception $e) {
  1246. echo $e->getMessage();
  1247. }
  1248. }
  1249. }