Betorder.php 52 KB

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