Betorder.php 55 KB

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