Betorder.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  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. //首页
  414. if($v['oddsType']=='home'){
  415. $is_rolling = 7;
  416. }
  417. //===end===
  418. $matchData =[
  419. 'odds_id' =>$v['id'],
  420. 'home_team' => $v['home_team'],
  421. 'guest_team' => $v['guest_team'],
  422. 'condition' => $v['condition'],
  423. 'odds' => $v['odds'],
  424. 'odds_code' => $v['odds_code'],
  425. 'p_code' => $v['p_code'],
  426. 'odds_only' => $v['odds_only'],
  427. 'match_id' => $v['match_id'],
  428. 'game_code' => $game_code,
  429. 'lg_id' => $lg_id,
  430. 'batch_id' => $batch_id,
  431. 'bet_type' => 1,
  432. 'bet_money' => $bet_money,
  433. 'ctime' => $this->time,
  434. 'utime' => $this->time,
  435. 'is_rolling' =>$is_rolling,
  436. 'is_champion' => $is_champion?:0,//1 冠军投注 0普通赛事投注
  437. 'order_id'=>$OrderID,//追加所属订单id
  438. 'roll_time'=>$roll_time,//追加滚球投注时 赛事进行时间
  439. 'home_score'=>$home_score,//主队得分
  440. 'guest_score'=>$guest_score,//客队得分
  441. ];
  442. //插入数据源
  443. $moneyBuyMatch = lm('MoneyBuyMatch','Api')->insert($matchData);
  444. if(!$moneyBuyMatch){
  445. Render('','3205',lang('errors','Api')->get('error-3205'));
  446. }
  447. //===end===
  448. $order = [];
  449. $order =[
  450. 'info_identity' => $UUID,
  451. 'account_name' => $userInfo['account'],
  452. 'account_identity' => $userInfo['account_identity'],
  453. 'order_id' => $OrderID,//$OrderID,
  454. 'game_code' => $game_code,
  455. 'batch_id' => $batch_id,
  456. 'match_id' => $v['match_id'],
  457. 'lg_id' => $lg_id,
  458. 'game_no' => "",
  459. 'prize_money'=> $prize_money,
  460. 'money' => $bet_money, //投注金额
  461. 'money_time' => $this->time,
  462. 'is_champion' => $is_champion?:0,//1 冠军投注 0普通赛事投注
  463. ];
  464. //如果是冠军投注,追加字段
  465. if($is_champion == 1){
  466. $json_single_result = [
  467. 'lg_id'=> '',
  468. 'game_name' => '',
  469. 'result' => '',
  470. 'updatetime' => '',
  471. ];
  472. $order['single_result'] = json_encode($json_single_result);
  473. }
  474. //如果是滚球投注,追加字段
  475. if($is_rolling == 1 and $game_code == 'zq'){
  476. $order['roll_ratify'] = 2;//未审核滚球投注
  477. }
  478. $insert = lm("MoneyBuySimplex",'Api')->insert($order);
  479. if(!$insert){
  480. Render('','3205',lang('errors','Api')->get('error-3205'));
  481. }
  482. $insertMoney = $this->M->insertMoney($userInfo['account_identity'],$bet_money,$OrderID,1,"",$UUID,$userInfo);
  483. if($insertMoney!=1){
  484. Render('', $insertMoney,lang('Errors','Api')->get('error'.$insertMoney));
  485. }
  486. $this->M->fanshui($userInfo['account_identity'],$userInfo['account'],$bet_money,$OrderID);//反水
  487. //===追加写处理队列===
  488. //订单号 比赛id188 开赛日期时间 比赛状态(早盘,今日,串场,滚球), 父级玩法, 子级玩法,排序
  489. //获取开赛时间
  490. //根据球类代码 获取相关model
  491. $models = $this->commonFunction->getModels($v['ballId']);
  492. $model_match = $models['model_match'];
  493. $info = lm($model_match,'Sports')
  494. ->select('match_date','match_time','match_identity')
  495. ->where('id',$v['match_id'])
  496. ->first();
  497. $match_time = $info->match_date.' '.$info->match_time;
  498. $orderData = [
  499. 'order_type'=>1,//订单类型 1单式 2串式
  500. 'order_id'=>$OrderID,//订单ID
  501. 'bet_money'=>$bet_money,//投注金额
  502. 'matchData'=>[
  503. [
  504. 'game_code'=>$v['ballId'],//球类代码
  505. 'other_match_id'=>$v['others_match_id'],//第三方赛事id 188
  506. 'match_id'=>$v['match_id'],//本地赛事id
  507. 'match_uuid'=> $info->match_identity,//赛事uuid
  508. 'odds_code'=>$v['odds_code'],//子玩法代码
  509. 'p_code'=>$v['p_code'],//父玩法代码
  510. 'match_time'=>$match_time,//开赛时间
  511. 'match_type'=>$is_rolling,//比赛盘口
  512. 'sort'=>$v['sort'],//排序
  513. ]
  514. ],
  515. ];
  516. $this->commonFunction->setOrderQueue($orderData);
  517. }
  518. }
  519. public function Bet(){
  520. /*
  521. // 模拟单式数据
  522. $get_data = [
  523. 'data'=>[
  524. [
  525. 'type'=>1,
  526. 'index'=>0,
  527. 'bet_amount'=>100,
  528. ],
  529. [
  530. 'type'=>1,
  531. 'index'=>1,
  532. 'bet_amount'=>100,
  533. ],
  534. [
  535. [
  536. 'id'=>10350565,
  537. 'home_team'=>'什鲁斯伯里',
  538. 'guest_team'=>'彼德堡',
  539. 'name'=>'大',
  540. 'odds'=>1.09,
  541. 'match_id'=>1463,
  542. 'condition'=>'2.5/3',
  543. 'ballId'=>'zq',
  544. 'odds_code'=>'gsb',
  545. 'p_code'=>'GS',
  546. 'play_name'=>'进球大小',
  547. 'score'=>0,
  548. 'bettingTime'=>date('Y-m-d H:i:s',time()),
  549. 'odds_only'=>'6a003dec710a42849a82dd1ea978f572',
  550. 'lg_id'=>17,
  551. 'match_type'=>'',//滚球投注
  552. 'odds_id'=>10350565,
  553. 'repeat'=>'true',
  554. 'is_champion'=>0,//是否是冠军联赛
  555. ],
  556. [
  557. 'id'=>10781633,
  558. 'home_team'=>'什鲁斯伯里',
  559. 'guest_team'=>'彼德堡',
  560. 'name'=>'彼德堡',
  561. 'odds'=>1,
  562. 'match_id'=>1463,
  563. 'condition'=>'-0/0.5',
  564. 'ballId'=>'zq',
  565. 'odds_code'=>'cog',
  566. 'p_code'=>'CO',
  567. 'play_name'=>'让球',
  568. 'score'=>0,
  569. 'bettingTime'=>date('Y-m-d H:i:s',time()),
  570. 'odds_only'=>'390e2b68992587438c0020179abc94f5',
  571. 'lg_id'=>17,
  572. 'match_type'=>'',//滚球投注
  573. 'odds_id'=>10781633,
  574. 'repeat'=>'true',
  575. 'is_champion'=>0,//是否是冠军联赛
  576. ]
  577. ]
  578. ],
  579. 'token'=>'Zj2aXC15738846155dcf92c78385c',
  580. 'bet_money'=>200,
  581. ];
  582. */
  583. /*
  584. // 模拟串式数据
  585. $get_data = [
  586. 'data'=>[
  587. [
  588. 'type'=>1,
  589. 'index'=>0,
  590. 'bet_amount'=>1,
  591. ],
  592. [
  593. 'type'=>'3串1',
  594. 'bet_amount' =>1,
  595. ],
  596. [
  597. [
  598. 'id'=>229351,
  599. 'home_team'=>'布琳狮子U20',
  600. 'guest_team'=>'圣奥尔本斯U20',
  601. 'name'=>'圣奥尔本斯U20',
  602. 'odds'=>1.01,
  603. 'match_id'=>0,
  604. 'condition'=>'4/4.5',
  605. 'ballId'=>'zq',
  606. 'odds_code'=>'goal_size_big',
  607. 'p_code'=>'goal_size',
  608. 'play_name'=>'大小',
  609. 'score'=>0,
  610. 'bettingTime'=>date('Y-m-d H:i:s',time()),
  611. 'odds_only'=>'908f8bae2233a0384c96c7b1f42e9ea7',
  612. 'lg_id'=>153,
  613. 'repeat'=>'false',
  614. 'match_type'=>'StRollBall',//滚球投注
  615. ],
  616. [
  617. 'id'=>238940,
  618. 'home_team'=>'格罗兹尼艾哈迈德U20',
  619. 'guest_team'=>'奥伦堡U20',
  620. 'name'=>'格罗兹尼艾哈迈德U20',
  621. 'odds'=>0.94,
  622. 'match_id'=>1247,
  623. 'condition'=>'1/1.5',
  624. 'ballId'=>'zq',
  625. 'odds_code'=>'concede_home',
  626. 'p_code'=>'concede',
  627. 'play_name'=>'让球',
  628. 'score'=>0,
  629. 'bettingTime'=>date('Y-m-d H:i:s',time()),
  630. 'odds_only'=>'9125bf40d6649fcdb663c43d8f55bebd',
  631. 'lg_id'=>20,
  632. 'repeat'=>'false',
  633. 'match_type'=>'StRollBall',//滚球投注
  634. ],
  635. [
  636. 'id'=>238958,
  637. 'home_team'=>'巴扬卡拉',
  638. 'guest_team'=>'马都拉联',
  639. 'name'=>'巴扬卡拉',
  640. 'odds'=>1.13,
  641. 'match_id'=>1254,
  642. 'condition'=>'0.5',
  643. 'ballId'=>'zq',
  644. 'odds_code'=>'concede_home',
  645. 'p_code'=>'concede',
  646. 'play_name'=>'让球',
  647. 'score'=>0,
  648. 'bettingTime'=>date('Y-m-d H:i:s',time()),
  649. 'odds_only'=>'3cf81ac0b9765218845fe8bc4de0ad99',
  650. 'lg_id'=>61,
  651. 'repeat'=>'false',
  652. 'match_type'=>'StRollBall',//滚球投注
  653. ],
  654. ]
  655. ],
  656. 'token'=>'ppWGRW15682541865d79a8eacb0b1',
  657. 'bet_money'=>2
  658. ];
  659. */
  660. //如果获取不到data,则为游戏端提交,需转成数组
  661. if(!empty($_POST['game_data'])){
  662. //获取参数转成数组
  663. $get_data = json_decode($_POST['game_data'],true);
  664. $token = $get_data['token'];
  665. $bet_money = $get_data['bet_money'];
  666. $data = $get_data['data'];
  667. // $bet_money_arr = $get_data['bet_money_arr'];
  668. }else{
  669. $data = $_REQUEST['data'];
  670. $bet_money = $_REQUEST['bet_money'];
  671. $token = $_REQUEST['token'];
  672. // $bet_money_arr = $_REQUEST['bet_money_arr'];
  673. }
  674. /*
  675. $data = $get_data['data'];
  676. $bet_money = $get_data['bet_money'];
  677. $token = $get_data['token'];
  678. */
  679. //验证用户token
  680. $userInfo = $this->getAgent($token);
  681. if(empty($data)||empty($bet_money)){
  682. Render('', '51029',lang('Errors','Api')->get('error-51029'));
  683. }
  684. //获取用户金额信息
  685. if(!$this->M->verifyMoney($bet_money,$userInfo['cash'])){
  686. Render('', '4204',lang('Errors','Api')->get('error-4204'));
  687. }
  688. //追加验证用户投注金额限额
  689. $this->bet_money_limit($data,$userInfo);
  690. //获取订单批次号
  691. $batch_id = OrderID();///strtotime(date('Y-m-d H:i:s',time())).mt_rand('1','99');//批量ID
  692. //执行数据插入
  693. try {
  694. _beginTransaction();//开启事务
  695. $data = $this->Classify($data);
  696. if(!empty($data['single'])){
  697. $this->Simplex_bet($data['single'],$userInfo,$batch_id);//单式数据处理
  698. }
  699. if(!empty($data['bunch'])){
  700. $data_all = $data['bunch'][count($data['bunch'])-1];//获取最后一个数组
  701. unset($data['bunch'][count($data['bunch'])-1]);//删除最后一个元素
  702. $this->stringBet($data['bunch'],$data_all,$userInfo,$batch_id);
  703. }
  704. _commit();//提交
  705. Render('', '1',lang('Errors','Api')->get('error-1'));
  706. } catch (Exception $e) {
  707. _rollBack();//回滚
  708. print $e->getMessage();
  709. }
  710. }
  711. /**
  712. * token获取用户详情
  713. */
  714. public function getAgent($token = '') {
  715. $checkToken = $this->accountManager->checkEffectiveTime($token);
  716. if ($checkToken['status'] != 1) {
  717. Render($checkToken['data'], $checkToken['status'], lang('commons')->get('user does login'));
  718. };
  719. $result = $this->accountManager->refreshToken($token);
  720. return $result['data'];
  721. }
  722. //验证联赛是否存在
  723. public function verify_league($game_code,$lg_id,$num = 1){
  724. if(empty($lg_id)){
  725. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5112');
  726. Render([], '5112',$msg);
  727. }
  728. //根据球类代码 获取相关model
  729. $model = $this->commonFunction->getModels($game_code);
  730. $model_league = $model['model_league'];
  731. $info = lm($model_league,'Sports')
  732. ->select('id')
  733. ->where('id',$lg_id)
  734. ->first();
  735. if(empty($info)){
  736. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5113');
  737. Render([], '5113',$msg);
  738. }
  739. }
  740. /**
  741. * 赛事是否存在或结束
  742. * @param [type] $game_code 球类代码
  743. * @param [type] $match_id 赔率ID
  744. * @param [type] $num 循环键值
  745. * $@param [type]type_str 串式
  746. *
  747. */
  748. public function verify_match($game_code,$match_id,$num = 1){
  749. if(empty($match_id)){
  750. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5109');
  751. Render([], '5109',$msg);
  752. }
  753. //根据球类代码 获取相关model
  754. $models = $this->commonFunction->getModels($game_code);
  755. $model_match = $models['model_match'];
  756. $info = lm($model_match,'Sports')
  757. ->select('status')
  758. ->where('status', '<', '2')
  759. // ->where('us_time','>',qgmdate('Y-m-d H:i:s','', -4))
  760. // ->where('source','hg3535')
  761. ->where('id',$match_id)
  762. ->first();
  763. if(empty($info)){
  764. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5103');
  765. Render([], '5103',$msg);
  766. }
  767. $info = $info->toarray();
  768. if($info['status'] == 2){
  769. $msg = '第'.$num.'条'.lang('Errors','Api')->get('error-5108');
  770. Render([], '5108',$msg);
  771. }
  772. return $info;
  773. }
  774. /**
  775. * 验证球类是否存在
  776. * @param [type] $game_code 球类代码
  777. * @param [type] $num 第几条
  778. */
  779. public function verify_gameType($game_code,$num){
  780. if(empty($game_code)){
  781. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-5102');
  782. Render([], '5102', $msg);
  783. }
  784. $info = lm('GameType','Sports')->where('game_code',$game_code)->first();
  785. if(empty($info)){
  786. $msg = '第' . $num . '条数据' . lang('Errors', 'Api')->get('error-5102');
  787. Render([], '5102', $msg);
  788. }
  789. return $info;
  790. }
  791. /**
  792. * 验证赔率是否存在
  793. * @param [type] $game_code 球类代码
  794. * @param [type] $odds_id 赔率IDkk
  795. *
  796. */
  797. public function verify_odds($game_code,$odds_only,$odds,$condition,$odds_code,$num = 1){
  798. if (empty($odds_only)){
  799. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-5111');
  800. Render([], '5111', $msg);
  801. }
  802. if(empty($odds_code)){
  803. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7052');
  804. Render([], '7052', $msg);
  805. }
  806. if(empty($odds)){
  807. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7029');
  808. Render([], '7029', $msg);
  809. }
  810. $odds_codeNum = odds_codeModel::getOddsCode($odds_code,$game_code);
  811. if($odds_codeNum < 1){
  812. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7053');
  813. Render([], '7053', $msg);
  814. }
  815. //根据球类代码 获取相关model
  816. $models = $this->commonFunction->getModels($game_code);
  817. $model_odds = $models['model_odds'];
  818. $model_odds_record = $models['model_odds_record'];
  819. //匹配赔率是否过期
  820. $odds_info = lm($model_odds,'Sports')
  821. ->join($model_odds_record,$model_odds_record.'.odds_only',$model_odds.'.odds_only')
  822. ->select($model_odds_record.'.odds',$model_odds_record.'.condition')
  823. ->where($model_odds.'.odds_only',$odds_only)
  824. ->first();
  825. if(empty($odds_info)){
  826. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7051');
  827. Render([], '7051', $msg);
  828. }
  829. $odds_info = $odds_info->toarray();
  830. if($condition != $odds_info['condition']){
  831. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7047');
  832. Render([], '7047', $msg);
  833. }
  834. if($odds != $odds_info['odds']){
  835. $msg = '第'.$num.'条数据'.lang('Errors','Api')->get('error-7049');
  836. Render([], '7049', $msg);
  837. }
  838. return $odds_info;
  839. }
  840. /**
  841. * 处理串式投注
  842. * $data_str 各串式 数据
  843. * $data_all 所有赛事 数据
  844. * $userInfo 用户数据
  845. * $batch_id 批次号
  846. */
  847. public function stringBet__($data_str=[],$data_all=[],$userInfo=[],$batch_id=0){
  848. //验证串式投注是否合法
  849. $this->verify_stringType($data_all);
  850. //获取串式 总赛事数量
  851. $groupNum = count($data_all);//总数量
  852. $orderData = [];
  853. foreach ($data_str as $k=>$v){
  854. //字符串替换
  855. $str = str_ireplace("串","_",$v['type']);
  856. //获取type 3_12
  857. $after = substr($str,strpos($str,"_")+1);
  858. //当前串式 值
  859. $sonNum = substr($str,0,strrpos($str,"_"));
  860. //获取倍数
  861. if((int)$after == 1){
  862. $multiple = $this->getMultiple($groupNum,(int)$sonNum);
  863. }else{
  864. $multiple =(int)$after;
  865. }
  866. //获取当前串式投注金额及可赢金额
  867. if($after == 1){
  868. $money_all = $this->getmakeMoney((int)$sonNum,$v['bet_amount'],$multiple,$data_all,$after);
  869. }else{
  870. $money_all = $this->getStrAllMoney((int)$sonNum,$v['bet_amount'],$multiple,$data_all,$after);
  871. }
  872. //投注金额
  873. $moneyData = $money_all['moneyData'];
  874. //可赢金额
  875. $prize_money = $money_all['prize_money'];
  876. $uuid = UUID();
  877. $orderID = OrderID('T');
  878. $orderData[$k]['info_identity'] = $uuid;
  879. $orderData[$k]['account_name'] = $userInfo['account'];
  880. $orderData[$k]['account_identity'] = $userInfo['account_identity'];
  881. $orderData[$k]['order_id'] = $orderID;
  882. $orderData[$k]['game_no'] = "";
  883. $orderData[$k]['money'] = $moneyData;//$pay_money//总投注金额
  884. $orderData[$k]['money_time'] = $this->time;//下注时间
  885. $orderData[$k]['prize_money'] = $prize_money;//$expect_money ;//预期总可赢金额
  886. $orderData[$k]['str_type'] = $v['type'];//串式类型
  887. $orderData[$k]['batch_id'] = $batch_id;//批次号
  888. $orderData[$k]['wait_match_num'] = count($data_all);//批次号
  889. //写资金变动表
  890. $insertMoney = $this->M->insertMoney($userInfo['account_identity'],$moneyData,$orderID,1,"",$uuid,$userInfo);
  891. if($insertMoney!=1){
  892. Render('', $insertMoney,lang('Errors','Api')->get('error'.$insertMoney));
  893. }
  894. $this->M->fanshui($userInfo['account_identity'],$userInfo['account'],$moneyData,$orderID);//反水
  895. }
  896. //写注单 表
  897. $moneyBuy = lm('MoneyBuyStr','Api')->insert($orderData);
  898. if(!$moneyBuy){
  899. Render('','3205',lang('errors','Api')->get('error-3205'));
  900. }
  901. foreach ($data_all as $kk=>$vv){
  902. $getModels = $this->commonFunction->getModels($vv['ballId']);
  903. $model_match = $getModels['model_match'];
  904. //获取是否是滚球投注
  905. $is_rolling = 0;
  906. if($vv['match_type'] == 'StRollBall'){
  907. $is_rolling = 1;
  908. }
  909. $matchData[] = [
  910. 'batch_id'=>$batch_id,
  911. 'odds_id'=>$vv['id'],
  912. 'home_team'=>$vv['home_team'],
  913. 'guest_team'=>$vv['guest_team'],
  914. 'condition'=>$vv['condition'],
  915. 'odds'=>$vv['odds'],
  916. 'odds_code'=>$vv['odds_code'],
  917. 'p_code'=>$vv['p_code'],
  918. 'odds_only'=>$vv['odds_only'],
  919. 'status'=>0,
  920. 'match_id'=>$vv['match_id'],
  921. 'game_code'=>$vv['ballId'],
  922. 'lg_id'=> $vv['lg_id'],
  923. 'ctime'=>$this->time,//写入时间
  924. 'utime' => $this->time,
  925. 'bet_type'=>2,
  926. 'is_rolling'=>$is_rolling,
  927. 'order_id'=> $orderID,
  928. ];
  929. }
  930. //写赔率数据表
  931. $moneyBuyMatch = lm('MoneyBuyMatch','Api')->insert($matchData);
  932. if(!$moneyBuyMatch){
  933. Render('','3205',lang('errors','Api')->get('error-3205'));
  934. }
  935. return true;
  936. }
  937. public function stringBet($data_str=[],$data_all=[],$userInfo=[],$batch_id=0){
  938. //验证串式投注是否合法
  939. $this->verify_stringType($data_all);
  940. //获取串式 总赛事数量
  941. $groupNum = count($data_all);//总数量
  942. $orderData = [];
  943. foreach ($data_str as $k=>$v){
  944. //字符串替换
  945. $str = str_ireplace("串","_",$v['type']);
  946. //获取type 3_12
  947. $after = substr($str,strpos($str,"_")+1);
  948. //当前串式 值
  949. $sonNum = substr($str,0,strrpos($str,"_"));
  950. //获取倍数
  951. if((int)$after == 1){
  952. $multiple = $this->getMultiple($groupNum,(int)$sonNum);
  953. }else{
  954. $multiple =(int)$after;
  955. }
  956. //获取当前串式投注金额及可赢金额
  957. if($after == 1){
  958. $money_all = $this->getmakeMoney((int)$sonNum,$v['bet_amount'],$multiple,$data_all,$after);
  959. }else{
  960. $money_all = $this->getStrAllMoney((int)$sonNum,$v['bet_amount'],$multiple,$data_all,$after);
  961. }
  962. //投注金额
  963. $moneyData = $money_all['moneyData'];
  964. //可赢金额
  965. $prize_money = $money_all['prize_money'];
  966. $uuid = UUID();
  967. $orderID = OrderID('T');
  968. $orderData[$k]['info_identity'] = $uuid;
  969. $orderData[$k]['account_name'] = $userInfo['account'];
  970. $orderData[$k]['account_identity'] = $userInfo['account_identity'];
  971. $orderData[$k]['order_id'] = $orderID;
  972. $orderData[$k]['game_no'] = "";
  973. $orderData[$k]['money'] = $moneyData;//$pay_money//总投注金额
  974. $orderData[$k]['money_time'] = $this->time;//下注时间
  975. $orderData[$k]['prize_money'] = $prize_money;//$expect_money ;//预期总可赢金额
  976. $orderData[$k]['str_type'] = $v['type'];//串式类型
  977. $orderData[$k]['batch_id'] = $batch_id;//批次号
  978. $orderData[$k]['wait_match_num'] = count($data_all);//批次号
  979. //写资金变动表
  980. $insertMoney = $this->M->insertMoney($userInfo['account_identity'],$moneyData,$orderID,1,"",$uuid,$userInfo);
  981. if($insertMoney!=1){
  982. Render('', $insertMoney,lang('Errors','Api')->get('error'.$insertMoney));
  983. }
  984. $this->M->fanshui($userInfo['account_identity'],$userInfo['account'],$moneyData,$orderID);//反水
  985. //===处理赛事数据===
  986. foreach ($data_all as $kk=>$vv){
  987. $getModels = $this->commonFunction->getModels($vv['ballId']);
  988. $model_match = $getModels['model_match'];
  989. //获取是否是滚球投注
  990. $is_rolling = 0;
  991. if($vv['match_type'] == 'StRollBall'){
  992. $is_rolling = 1;
  993. }
  994. $matchData[] = [
  995. 'batch_id'=>$batch_id,
  996. 'odds_id'=>$vv['id'],
  997. 'home_team'=>$vv['home_team'],
  998. 'guest_team'=>$vv['guest_team'],
  999. 'condition'=>$vv['condition'],
  1000. 'odds'=>$vv['odds'],
  1001. 'odds_code'=>$vv['odds_code'],
  1002. 'p_code'=>$vv['p_code'],
  1003. 'odds_only'=>$vv['odds_only'],
  1004. 'status'=>0,
  1005. 'match_id'=>$vv['match_id'],
  1006. 'game_code'=>$vv['ballId'],
  1007. 'lg_id'=> $vv['lg_id'],
  1008. 'ctime'=>$this->time,//写入时间
  1009. 'utime' => $this->time,
  1010. 'bet_type'=>2,
  1011. 'is_rolling'=>$is_rolling,
  1012. 'order_id'=> $orderID,
  1013. ];
  1014. //=====拼接赛事数据===
  1015. //获取开赛时间
  1016. //根据球类代码 获取相关model
  1017. $models = $this->commonFunction->getModels($vv['ballId']);
  1018. $model_match = $models['model_match'];
  1019. $info = lm($model_match,'Sports')
  1020. ->select('match_date','match_time','match_identity')
  1021. ->where('id',$vv['match_id'])
  1022. ->first();
  1023. $match_time = $info->match_date.' '.$info->match_time;
  1024. $match_data_order[] = [
  1025. 'game_code'=>$vv['ballId'],//球类代码
  1026. 'other_match_id'=>$vv['others_match_id'],//第三方赛事id 188
  1027. 'match_id'=>$vv['match_id'],//本地赛事id
  1028. 'match_uuid'=> $info->match_identity,//赛事uuid
  1029. 'odds_code'=>$vv['odds_code'],//子玩法代码
  1030. 'p_code'=>$vv['p_code'],//父玩法代码
  1031. 'match_time'=>$match_time,//开赛时间
  1032. 'match_type'=>$is_rolling,//比赛盘口
  1033. 'sort'=>$vv['sort'],//排序
  1034. ];
  1035. //===end===
  1036. }
  1037. //===追加写处理队列===
  1038. //订单号 比赛id188 开赛日期时间 比赛状态(早盘,今日,串场,滚球), 父级玩法, 子级玩法,排序
  1039. //赛事去重
  1040. $match_data_order = $this->commonFunction->array_unset_tt($match_data_order,'match_id');
  1041. sort($match_data_order);
  1042. $orderData_queue[] = [
  1043. 'order_type'=>2,//订单类型 1单式 2串式
  1044. 'order_id'=>$orderID,//订单ID
  1045. 'bet_money' =>$moneyData,//投注金额
  1046. 'matchData'=>$match_data_order
  1047. ];
  1048. }
  1049. //写赔率数据表
  1050. $moneyBuyMatch = lm('MoneyBuyMatch','Api')->insert($matchData);
  1051. if(!$moneyBuyMatch){
  1052. Render('','3205',lang('errors','Api')->get('error-3205'));
  1053. }
  1054. //===end===
  1055. //写注单 表
  1056. $moneyBuy = lm('MoneyBuyStr','Api')->insert($orderData);
  1057. if(!$moneyBuy){
  1058. Render('','3205',lang('errors','Api')->get('error-3205'));
  1059. }
  1060. //写队列
  1061. foreach($orderData_queue as $k=>$v){
  1062. $this->commonFunction->setOrderQueue($v);
  1063. }
  1064. return true;
  1065. }
  1066. /**
  1067. * 计算串式投注 倍率
  1068. * $groupNum 总赛事 数量
  1069. * $sonNum 当前串式 数量
  1070. */
  1071. public function getMultiple($groupNum = 0,$sonNum = 0){
  1072. if($groupNum and $sonNum){
  1073. $group =1;
  1074. for($i = $groupNum ; $i >($groupNum - $sonNum);$i--) {
  1075. $group = $group * $i;
  1076. }
  1077. $son =1;
  1078. for($i = $sonNum ; $i >0;$i--) {
  1079. $son = $son * $i;
  1080. }
  1081. }
  1082. return $group/$son;
  1083. }
  1084. /**
  1085. * @param int $sonNum 当前串式数量
  1086. * @param int $bet_amount 当前串式投注额
  1087. * @param int $multiple 当前串式倍数
  1088. * @param array $data_all 当前注单 所有投注数据
  1089. * @return mixed
  1090. * 计算当前串式投注总额、可赢总额
  1091. */
  1092. public function getmakeMoney($sonNum=0,$bet_amount=0,$multiple=0,$data_all=[],$after = 0){
  1093. $allNum = count($data_all);//所有数量
  1094. $odds_all = [];//所有投注项数据
  1095. foreach ($data_all as $k=>$v){
  1096. $odds_all[] = $v['odds']+1;
  1097. }
  1098. //投注总额
  1099. $data['moneyData'] = (double)$bet_amount*(double)$multiple;
  1100. if($sonNum == $allNum and (int)$after ==1){//N场赛事 N串一
  1101. //获取所有赛事赔率乘积
  1102. $odds = array_product($odds_all) - 1;
  1103. //获取可赢金额
  1104. $data['prize_money'] = sprintf("%.2f",substr(sprintf("%.3f", $bet_amount*$odds), 0, -1));//floor($bet_amount*$odds*100)/100;//保留两位小数
  1105. }
  1106. if($sonNum < $allNum and (int)$after ==1){//例:N场赛事 <N串一
  1107. //计算 组合赔率
  1108. $group = $this->commonFunction->combination($odds_all, $sonNum);
  1109. foreach ($group as $k=>$v){
  1110. $odds[] = array_product($v);
  1111. }
  1112. $odds = (array_sum($odds))-(count($odds));
  1113. //获取可赢金额
  1114. $data['prize_money'] = sprintf("%.2f",substr(sprintf("%.3f", $bet_amount*$odds), 0, -1));//floor($bet_amount*$odds*100)/100;
  1115. }
  1116. return $data;
  1117. }
  1118. /**
  1119. * 获取所有可用串式 N场赛事 2串1,3串1...N串1,N<11
  1120. * 并计算 其投注可赢金额
  1121. */
  1122. public function getStrAllMoney($sonNum=0,$bet_amount=0,$multiple=0,$data_all=[],$after = 0){
  1123. $d = [];
  1124. for ($i=2;$i<($sonNum+1);$i++){
  1125. $d[] = $i;
  1126. }
  1127. foreach ($d as $k=>$v){
  1128. $multiple = $this->getMultiple($sonNum,$v);
  1129. $money[] = $this->getmakeMoney($v,$bet_amount,$multiple,$data_all,1);
  1130. }
  1131. $data=array();
  1132. foreach($money as $k=>$v){
  1133. $data['moneyData']+=$v['moneyData'];
  1134. $data['prize_money']+=$v['prize_money'];
  1135. }
  1136. return $data;
  1137. }
  1138. /**
  1139. * 验证串式投注 是否合法
  1140. * $data 串式 所有赛事
  1141. */
  1142. public function verify_stringType($data=[]){
  1143. if($data){
  1144. foreach($data as $k=>$v){
  1145. if($v['match_id']==$v['match_id'] ){
  1146. $r[$v['match_id']][] = $v;
  1147. }
  1148. }
  1149. }
  1150. if($r){
  1151. foreach ($r as $k=>$v){
  1152. if(count($v) > 1){
  1153. $msg = lang('Errors','Api')->get('error-50038');
  1154. Render([], '50038',$msg);
  1155. }else if( $k == 0){
  1156. $msg = lang('Errors','Api')->get('error-50043');
  1157. Render([], '50043',$msg);
  1158. }
  1159. }
  1160. }
  1161. }
  1162. /**
  1163. * 更新投注项相关数据
  1164. */
  1165. public function getBetData(){
  1166. $data = $_REQUEST;
  1167. // $data = [['game_code'=>'zq','match_id'=>3095448,'odds_id'=>623078],['game_code'=>'zq','match_id'=>3095448,'odds_id'=>623078]];
  1168. foreach ($data as $k=>$v){
  1169. $getModels = $this->commonFunction->getModels($v['game_code']);
  1170. $model_match = $getModels['model_match'];
  1171. $model_odds = $getModels['model_odds'];
  1172. $oddsData = lm($model_match,"Sports")
  1173. ->join($model_odds,$model_odds.'.match_id',$model_match.'.id')
  1174. ->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')
  1175. // ->where($model_match.'.source',$this->source)
  1176. ->where($model_match.'.id',$v['match_id'])
  1177. ->where($model_odds.'.id',$v['odds_id'])
  1178. //->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  1179. ->first();
  1180. $oddsData->game_code = $v['game_code'];
  1181. $betData[] = $oddsData;
  1182. }
  1183. Render($betData, '1', lang('Tips','Sports')->get('success'));
  1184. }
  1185. //用户投注限额验证
  1186. public function bet_money_limit($bet_money_arr=[],$userInfo=[]){
  1187. if(!empty($bet_money_arr)){
  1188. foreach($bet_money_arr as $k=>$v){
  1189. if($k == (count($bet_money_arr)-1)){
  1190. unset($bet_money_arr[$k]);
  1191. }
  1192. }
  1193. }
  1194. //===追加验证用户投注金额限额===
  1195. if(empty($bet_money_arr)) Render('', '3605',lang('Errors','Api')->get('error-3605'));
  1196. //获取当前用户分组名称
  1197. $substr = substr($userInfo['group_name'],strpos($userInfo['group_name'],'|')+1);
  1198. $role_name = substr($substr, 0, -1);
  1199. //获取用户分组的投注限额
  1200. $bet_money_limit = Dc_user_grade::get_bet_money_limit($role_name);
  1201. foreach($bet_money_arr as $k=>$v){
  1202. 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'));
  1203. 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'));
  1204. }
  1205. //===end==
  1206. }
  1207. //根据token获取用户投注限额
  1208. public function getBetMoneyLimit(){
  1209. //验证用户token
  1210. $token = $_REQUEST['token'];
  1211. $userInfo = $this->getAgent($token);
  1212. //获取当前用户分组名称
  1213. $substr = substr($userInfo['group_name'],strpos($userInfo['group_name'],'|')+1);
  1214. $role_name = substr($substr, 0, -1);
  1215. //获取用户分组的投注限额
  1216. $bet_money_limit = Dc_user_grade::get_bet_money_limit($role_name);
  1217. $data['bet_money_limit']=[
  1218. 'lower_limit'=>$bet_money_limit->lower_limit,
  1219. 'upper_limit'=>$bet_money_limit->upper_limit,
  1220. ];
  1221. Render($data, '1', lang('Tips','Sports')->get('success'));
  1222. }
  1223. /**
  1224. * 用户撤单操作 接口
  1225. * 单订单操作
  1226. */
  1227. public function reOrder(){
  1228. //获取球类代码
  1229. $order_id = $_REQUEST['order_id'];
  1230. $token = $_REQUEST['token'];
  1231. try {
  1232. if(empty($order_id)){
  1233. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  1234. }
  1235. _beginTransaction();//开启事务
  1236. // 单式 S 串式 T
  1237. if(substr($order_id,0,1) == 'S'){
  1238. //单式订单 model
  1239. $order_model = 'money_buy_simplex';
  1240. }else{
  1241. //串式订单 model
  1242. $order_model = 'money_buy_str';
  1243. }
  1244. //查询订单信息
  1245. $order_data = lm($order_model,"Commons")
  1246. ->where('order_id',$order_id)
  1247. ->first();
  1248. //订单不存在
  1249. if(empty($order_data)) throw new \Exception(Render([], '10030', lang('Tips','Sports')->get('order_null')));
  1250. //订单非投注状态 不可撤单
  1251. if($order_data->status != 1) throw new \Exception(Render([], '10031', lang('Tips','Sports')->get('order_type_err')));
  1252. //获取用户数据
  1253. $userInfo = $this->getAgent($token);
  1254. //验证订单是否属于该用户
  1255. if($order_data->account_identity != $userInfo['account_identity']) throw new \Exception(Render([], '10025', lang('Tips','Sports')->get('auth_error').':不是该用户的订单'));
  1256. //获取用户账户余额
  1257. $cash = $userInfo['cash'];
  1258. //获取订单涉及金额
  1259. $where = [
  1260. ['trade_id','=',$order_id],
  1261. ['status','=',1]
  1262. ];
  1263. $odds_money_data = lm('money_details',"Commons")
  1264. ->select('info_identity','trade_id','account_name','account_identity','money','money_type','money_cash','trade_type','status')
  1265. ->where($where)
  1266. ->get();
  1267. if(empty($odds_money_data)) throw new \Exception(Render([], '10026', lang('Tips','Sports')->get('order_money_err')));
  1268. $odds_money_data = $odds_money_data -> toArray();
  1269. //获取订单投注金额
  1270. $odds_money_bet = 0;
  1271. //获取订单反水金额
  1272. $odds_money_fs = 0;
  1273. foreach($odds_money_data as $k=>$v){
  1274. if($v['trade_type'] == 1){
  1275. $odds_money_bet = $v['money'];
  1276. }
  1277. if($v['trade_type'] == 7){
  1278. $odds_money_fs = $v['money'];
  1279. }
  1280. }
  1281. //更新订单状态为已取消
  1282. $up_order = lm($order_model,"Commons")
  1283. ->where('order_id',$order_id)
  1284. ->update(['status'=>3,'use_mark'=>'用户撤单']);
  1285. if($up_order < 1) throw new \Exception(Render([], '10027', lang('Tips','Sports')->get('order_status_up_err')));
  1286. //===退款===
  1287. //需退回金额 = 投注金额-反水金额
  1288. $money = abs($odds_money_bet - $odds_money_fs);
  1289. //用户剩余金额
  1290. $new_available_cash = $cash + $money;
  1291. //组装新增数据
  1292. $set_money_data = [
  1293. 'info_identity' => UUID(),
  1294. 'trade_id' => $order_id,
  1295. 'account_name' => $userInfo['account'],
  1296. 'account_identity' => $userInfo['account_identity'],
  1297. 'money' => $money,
  1298. 'money_time' => date("Y-m-d H:i:s", time()),
  1299. 'money_type' => 1,
  1300. 'money_cash' => $new_available_cash,
  1301. 'trade_type' => 3,
  1302. 'trade_desc' => '用户撤单,退回投注资金:¥'.$money.';订单号:'.$order_id.'',
  1303. 'status' => 1,
  1304. ];
  1305. //更新用户数据
  1306. $ret_user = lm('account_detailed',"Commons")
  1307. ->where('account_identity', $v['account_identity'])
  1308. ->update(['available_cash' => $new_available_cash, 'cash' => $new_available_cash]);
  1309. if($ret_user < 1) throw new \Exception(Render([], '10028', lang('Tips','Sports')->get('up_user_err')));
  1310. //更新用户资金数据
  1311. $set_money = lm('money_details',"Commons")->insert($set_money_data);
  1312. if($set_money != true) throw new \Exception(Render([], '10029', lang('Tips','Sports')->get('add_money_err')));
  1313. //===end===
  1314. _commit();//提交
  1315. Render('', '1', lang('Tips','Sports')->get('success'));
  1316. } catch (\Exception $e) {
  1317. echo $e->getMessage();
  1318. }
  1319. }
  1320. }