Betorder.php 51 KB

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