MoneyBuy.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. /**
  3. *------Create thems Model------
  4. *------SCWPHP Version 1.0.0------
  5. *------Dev Model Jions------
  6. *------Create Time 2017-06-12 05:08:18------
  7. */
  8. namespace App\Api\Model;
  9. use \System\Model;
  10. use Biz\Account\AccountManager;
  11. class MoneyBuy extends Model {
  12. protected $table = 'money_buy';
  13. //晋级流水统计
  14. function getstreamAmount($params=array(),$startime='',$endtime='')
  15. {
  16. if (empty($params['account_identity'])) {
  17. return -51005;
  18. }
  19. $gradeinfo = lm('UserGrade', 'Api')->select('name', 'rule', 'rule_details')->where('grade', '=', 0)->first();
  20. if (empty($gradeinfo)) {
  21. return -5002;
  22. }
  23. $gradeinfo = $gradeinfo->toArray();
  24. $gradeinfo['rule_details'] = explode(',', $gradeinfo['rule_details']);
  25. $bet = lm('MoneyBuy', 'Api')->where('account_identity', $params['account_identity'])->where('status', '<', 4)->where('game_status', '>', 0);
  26. $uservip = lm('User_vip', 'Api')->select('audittime')->where('account_identity', $params['account_identity'])->first();
  27. if ($gradeinfo['rule_details'][0] == 'year') {
  28. $startime = date('Y-01-01', time()) . ' 00:00:00';
  29. $endtime = date('Y-12-31', time()) . ' 23:59:59';
  30. $bet = $bet->where('money_time', '>=', $startime)->where('money_time', '<=', $endtime);
  31. } elseif ($gradeinfo['rule_details'][0] == 'month') {
  32. $startime = date('Y-m-01', strtotime(date('Y', time()) . '-' . (date('m', time()) - 1) . '-01')) . ' 00:00:00';
  33. $endtime = date('Y-m-d', strtotime("$startime +1 month -1 day")) . ' 23:59:59';
  34. $bet = $bet->where('money_time', '>=', $startime)->where('money_time', '<=', $endtime);
  35. } elseif ($gradeinfo['rule_details'][0] == 'week') {
  36. $now_day = date('W', time()) - 1;
  37. $sunday_str = time() - $now_day * 60 * 60 * 24;
  38. $sunday = date('Y-m-d 00:00:00', $sunday_str);
  39. $strday_str = time() + (6 - $now_day) * 60 * 60 * 24;
  40. $strday = date('Y-m-d 23:59:59', $strday_str);
  41. $bet = $bet->where('money_time', '>=', $sunday)->where('money_time', '<=', $strday);
  42. } elseif ($gradeinfo['rule_details'][0] == 'day') {
  43. $firstday = date('Y-m-d 00:00:00', strtotime("-" . $gradeinfo['rule_details'][3] . " day"));
  44. $bet = $bet->where('money_time', '>=', $firstday)->where('money_time', '<=', date('Y-m-d 23:59:59', strtotime('-1 day')));
  45. } elseif ($gradeinfo['rule_details'][0] == 'ymd') {
  46. $firstday = $gradeinfo['rule_details'][3] . ' 00:00:00';
  47. $bet = $bet->where('money_time', '>=', $firstday);
  48. }
  49. if(!empty($uservip)){
  50. $bet = $uservip->audittime?$bet->where ('money_time', '>=', $uservip->audittime):$bet;
  51. }
  52. $bet=$startime?$bet->where ('money_time', '>=', $startime):$bet;
  53. $bet=$endtime?$bet->where ('money_time', '<', $endtime):$bet;
  54. $bet = $bet->sum('money');
  55. return $bet;
  56. }
  57. /**
  58. * 单式投注记录
  59. *
  60. * @access public
  61. * @return String
  62. */
  63. public function singleBetRecord()
  64. {
  65. // 获取用户信息
  66. $accountManagerClass = new AccountManager;
  67. $userInfo = $accountManagerClass -> getCurrentUser();
  68. if (empty($userInfo['identity'])) {
  69. Render([], '2001', lang('Common','Api') -> get('user does login'));
  70. }
  71. // 用户ID
  72. $moneyBuySpxWhere['account_identity'] = $userInfo['identity'];
  73. // 时间区间
  74. $startTime = isset($_POST['startTime'])? $_POST['startTime'] : '1900-01-01 00:00:00';
  75. $endTime = isset($_POST['endTime'])? $_POST['endTime'] : '3000-12-12 00:00:00';
  76. $moneyBuySpxBetween = ['money_time' => [$startTime, $endTime]];
  77. $moneyBuySpxMdl = lm('money_buy_simplex', "commons");
  78. // 获取订单记录
  79. $moneyBuySpxSlt = ['batch_id'];
  80. $getMoneyBuySpx = $moneyBuySpxMdl -> moneyBuySimplex($moneyBuySpxSlt, $moneyBuySpxWhere, $moneyBuySpxBetween, '', '', 1);
  81. // 获取全部订单id
  82. $moneyBuyMthOr = [];
  83. $allBatchId = array_unique($getMoneyBuySpx, SORT_REGULAR);
  84. if (!$allBatchId) {
  85. // 获取记录总数
  86. $result['total'] = 0;
  87. $result['list'] = [];
  88. // 总页数计算
  89. $result['countPage'] = 1;
  90. $result['currentPage'] = 1;
  91. return $result;
  92. }
  93. foreach ($allBatchId as $key => $value) {
  94. $moneyBuyMthOr[0][$key][0] = 'batch_id';
  95. $moneyBuyMthOr[0][$key][1] = $value['batch_id'];
  96. }
  97. $moneyBuyMthWhere['bet_type'] = 1;
  98. // 输赢
  99. if (!empty($_POST['result']) && $_POST['result'] == 1) {
  100. $moneyBuyMthOr[1][0][0] = 'result';
  101. $moneyBuyMthOr[1][0][1] = 1;
  102. $moneyBuyMthOr[1][1][0] = 'result';
  103. $moneyBuyMthOr[1][1][1] = 3;
  104. $moneyBuyMthOr[1][2][0] = 'result';
  105. $moneyBuyMthOr[1][2][1] = 2;
  106. } elseif (!empty($_POST['result']) && $_POST['result'] == 2) {
  107. $moneyBuyMthOr[1][0][0] = 'result';
  108. $moneyBuyMthOr[1][0][1] = -1;
  109. $moneyBuyMthOr[1][1][0] = 'result';
  110. $moneyBuyMthOr[1][1][1] = 4;
  111. } elseif ($_POST['result'] === '0') {
  112. $moneyBuyMthOr[1][0][0] = 'result';
  113. $moneyBuyMthOr[1][0][1] = 0;
  114. }
  115. // 当前页
  116. $currentPage = isset($_POST['currentPage']) ? $_POST['currentPage'] : "1";
  117. // 分页大小
  118. $pageSize = isset($_POST['pageSize']) ? $_POST['pageSize'] : "10";
  119. // 起始查询位置
  120. $begin = ($currentPage - 1) * $pageSize;
  121. // 获取注单
  122. $moneyBuyMthMdl = lm('Money_buy_match', "commons");
  123. $moneyBuyMthSlt = ['status', 'bet_money', 'matchResult', 'result', 'home_team', 'batch_id', 'guest_team', 'condition', 'ctime', 'odds', 'odds_name', 'match_id', 'money_buy_match.game_code', 'money_buy_match.odds_code'];
  124. $getMoneyBuyMth = $moneyBuyMthMdl -> moneyBuyMatch($moneyBuyMthSlt, $moneyBuyMthWhere, $moneyBuyMthOr, $begin, $pageSize);
  125. $allCount = $moneyBuyMthMdl -> moneyBuyMatchTotal($moneyBuyMthWhere, $moneyBuyMthOr);
  126. // 获取可加注所需要的查询条件
  127. $commonFunction = C() -> get('commonFunction');
  128. $stOddsWheres = [];
  129. foreach ($getMoneyBuyMth as $key => $value) {
  130. $match_id = $value -> match_id;
  131. $odds_code = $value -> odds_code;
  132. $gameType = $value -> game_code;
  133. $where = [
  134. 'match_id' => $match_id,
  135. 'odds_code' => $odds_code,
  136. ];
  137. // 判断是否重复
  138. if (!$commonFunction -> judgeEqual($where, $stOddsWheres[$gameType])) {
  139. $stOddsWheres[$gameType][] = $where;
  140. }
  141. }
  142. // 查询数据下所有可加注的游戏
  143. $stOddsModel = lm('stOdds','commons');
  144. $stOddsSelect = ['match_id', 'odds_code'];
  145. $getSstOdds = [];
  146. // 循环查询每个运动的可加注游戏 注:由于查询不同表所以必须循环查询
  147. foreach ($stOddsWheres as $key => $value) {
  148. if ($value) {
  149. $getSstOdds[$key] = $stOddsModel -> stOdds($stOddsSelect, $value, $key);
  150. }
  151. }
  152. // 循环添加玩法和判断玩法是否可加注
  153. foreach ($getMoneyBuyMth as $key => $value) {
  154. $getMoneyBuyMth[$key]->willMoney = $value->bet_money * $value->odds;
  155. if ($value->result) {
  156. switch (intval($value->result)) {
  157. case 1:
  158. $oddsDiscount = 1 + $value->odds;
  159. break;
  160. case 2:
  161. $oddsDiscount = 1;
  162. break;
  163. case 3:
  164. $oddsDiscount = 1 + $value->odds * 0.5;
  165. break;
  166. case 4:
  167. $oddsDiscount = 0.5;
  168. break;
  169. default:
  170. $oddsDiscount = 0;
  171. }
  172. $getMoneyBuyMth[$key]->winMoney = $value->bet_money * ($oddsDiscount - 1);
  173. } else {
  174. $getMoneyBuyMth[$key]->winMoney = 0;
  175. }
  176. $typeSstOdds = $getSstOdds[$value -> game_code];
  177. $thisSstOdds = [
  178. 'match_id' => $value -> match_id,
  179. 'odds_code' => $value -> odds_code,
  180. ];
  181. // 判断如果在数组中则为可加注
  182. if (!!$commonFunction->judgeEqual($thisSstOdds, $typeSstOdds)) {
  183. $getMoneyBuyMth[$key] -> addBet = 0;
  184. } else {
  185. $getMoneyBuyMth[$key] -> addBet = 1;
  186. }
  187. }
  188. // 获取记录总数
  189. $result['total'] = $allCount;
  190. $result['list'] = $getMoneyBuyMth;
  191. // 总页数计算
  192. $result['countPage'] = ceil($result['total'] / $pageSize);
  193. $result['currentPage'] = $currentPage;
  194. return $result;
  195. }
  196. /**
  197. * 串式投注记录
  198. *
  199. * @access public
  200. * @return String
  201. */
  202. public function stringBetRecord()
  203. {
  204. // 获取用户信息
  205. $accountManagerClass = new AccountManager;
  206. $userInfo = $accountManagerClass -> getCurrentUser();
  207. if (empty($userInfo['identity'])) {
  208. Render([], '2001', lang('Common','Api') -> get('user does login'));
  209. }
  210. // 用户ID
  211. $moneyBuyStrWhere['account_identity'] = $userInfo['identity'];
  212. // 输赢
  213. if (!empty($_POST['result'])) {
  214. $moneyBuyStrWhere['game_status'] = $_POST['result'];
  215. }
  216. // 时间区间
  217. $startTime = isset($_POST['startTime'])? $_POST['startTime'] : '1900-01-01 00:00:00';
  218. $endTime = isset($_POST['endTime'])? $_POST['endTime'] : '3000-12-12 00:00:00';
  219. $moneyBuyStrBetween = ['money_time' => [$startTime, $endTime]];
  220. $moneyBuyStrMdl = lm('money_buy_str', "commons");
  221. // 当前页
  222. $currentPage = isset($_POST['currentPage']) ? $_POST['currentPage'] : "1";
  223. // 分页大小
  224. $pageSize = isset($_POST['pageSize']) ? $_POST['pageSize'] : "10";
  225. // 起始查询位置
  226. $begin = ($currentPage - 1) * $pageSize;
  227. // 获取订单记录
  228. $moneyBuyStrSlt = ['batch_id', 'gain_money', 'game_status', 'money', 'prize_money', 'money_time'];
  229. $getMoneyBuyStr = $moneyBuyStrMdl -> moneyBuyStr($moneyBuyStrSlt, $moneyBuyStrWhere, $moneyBuyStrBetween, $begin, $pageSize, 1);
  230. $allCount = $moneyBuyStrMdl -> moneyBuyStrTotal($moneyBuyStrWhere, $moneyBuyStrBetween);
  231. // 获取全部订单id
  232. $moneyBuyMthOr = [];
  233. $allBatchId = array_unique($getMoneyBuyStr, SORT_REGULAR);
  234. if (!$allBatchId) {
  235. // 获取记录总数
  236. $result['total'] = 0;
  237. $result['list'] = [];
  238. // 总页数计算
  239. $result['countPage'] = 1;
  240. $result['currentPage'] = 1;
  241. return $result;
  242. }
  243. foreach ($allBatchId as $key => $value) {
  244. $moneyBuyMthOr[0][$key][0] = 'batch_id';
  245. $moneyBuyMthOr[0][$key][1] = $value['batch_id'];
  246. }
  247. $moneyBuyMthWhere['bet_type'] = 2;
  248. // 获取注单
  249. $moneyBuyMthMdl = lm('Money_buy_match', "commons");
  250. $moneyBuyMthSlt = ['status', 'matchResult', 'bet_money', 'home_team', 'guest_team', 'condition', 'odds', 'odds_name', 'match_id', 'batch_id', 'money_buy_match.game_code', 'money_buy_match.odds_code'];
  251. $getMoneyBuyMth = $moneyBuyMthMdl -> moneyBuyMatch($moneyBuyMthSlt, $moneyBuyMthWhere, $moneyBuyMthOr, $begin, $pageSize);
  252. // 获取可加注所需要的查询条件
  253. $commonFunction = C() -> get('commonFunction');
  254. $stOddsWheres = [];
  255. foreach ($getMoneyBuyMth as $key => $value) {
  256. $match_id = $value -> match_id;
  257. $odds_code = $value -> odds_code;
  258. $gameType = $value -> game_code;
  259. $where = [
  260. 'match_id' => $match_id,
  261. 'odds_code' => $odds_code,
  262. ];
  263. // 判断是否重复
  264. if (!$commonFunction -> judgeEqual($where, $stOddsWheres[$gameType])) {
  265. $stOddsWheres[$gameType][] = $where;
  266. }
  267. }
  268. // 查询数据下所有可加注的游戏
  269. $stOddsModel = lm('stOdds','commons');
  270. $stOddsSelect = ['match_id', 'odds_code'];
  271. $getSstOdds = [];
  272. // 循环查询每个运动的可加注游戏 注:由于查询不同表所以必须循环查询
  273. foreach ($stOddsWheres as $key => $value) {
  274. if ($value) {
  275. $getSstOdds[$key] = $stOddsModel -> stOdds($stOddsSelect, $value, $key);
  276. }
  277. }
  278. // 循环添加玩法和判断玩法是否可加注
  279. foreach ($getMoneyBuyMth as $key => $value) {
  280. $typeSstOdds = $getSstOdds[$value -> game_code];
  281. $thisSstOdds = [
  282. 'match_id' => $value -> match_id,
  283. 'odds_code' => $value -> odds_code,
  284. ];
  285. // 判断如果在数组中则为可加注
  286. if (!!$commonFunction->judgeEqual($thisSstOdds, $typeSstOdds)) {
  287. $getMoneyBuyMth[$key] -> addBet = 0;
  288. } else {
  289. $getMoneyBuyMth[$key] -> addBet = 1;
  290. }
  291. }
  292. foreach ($getMoneyBuyStr as $key => $value) {
  293. // 计算所赢金额.
  294. if ($value->gain_money) {
  295. $getMoneyBuyStr[$key]->winMoney = $value->gain_money - $value->money;
  296. } else {
  297. $getMoneyBuyStr[$key]->winMoney = 0;
  298. }
  299. foreach ($getMoneyBuyMth as $k => $v) {
  300. if ($value['batch_id'] == $v['batch_id']) {
  301. $getMoneyBuyStr[$key]['money_buy'][$k] = $v;
  302. }
  303. }
  304. }
  305. // 获取记录总数
  306. $result['total'] = $allCount;
  307. $result['list'] = $getMoneyBuyStr;
  308. // 总页数计算
  309. $result['countPage'] = ceil($result['total'] / $pageSize);
  310. $result['currentPage'] = $currentPage;
  311. return $result;
  312. }
  313. /**
  314. * 盈亏记录
  315. *
  316. * @access public
  317. * @return String
  318. */
  319. public function moneyRecord()
  320. {
  321. /*$moneyRecord = lm('Money_buy_simplex', "commons");
  322. $moneyRecord->aa();*/
  323. // 获取用户信息
  324. $accountManagerClass = new AccountManager;
  325. $userInfo = $accountManagerClass -> getCurrentUser();
  326. if (empty($userInfo['identity'])) {
  327. Render([], '2001', lang('Common','Api') -> get('user does login'));
  328. }
  329. $moneyRecordWhere = array();
  330. // 用户ID
  331. $moneyRecordWhere['account_identity'] = $userInfo['identity'];
  332. $moneyRecordWhere['status'] = 1;
  333. // 时间区间
  334. $startTime = isset($_POST['startTime'])? $_POST['startTime'] : '1900-01-01 00:00:00';
  335. $endTime = isset($_POST['endTime'])? $_POST['endTime'] : '3000-12-12 00:00:00';
  336. $moneyRecordBetween = [$startTime, $endTime];
  337. // 当前页
  338. $currentPage = isset($_POST['currentPage']) ? $_POST['currentPage'] : "1";
  339. // 分页大小
  340. $pageSize = isset($_POST['pageSize']) ? $_POST['pageSize'] : "10";
  341. // 起始查询位置
  342. $begin = ($currentPage - 1) * $pageSize;
  343. $moneyRecord = lm('Money_buy_simplex', "commons");
  344. $moneyRecordSelect = ['info_identity', 'trade_id', 'account_identity', 'money', 'money_time', 'money_type'];
  345. // 获取盈亏记录
  346. $result['list'] = $moneyRecord -> winLoseRecord($moneyRecordWhere, $moneyRecordBetween, $begin, $pageSize);
  347. // 获取记录总数
  348. $result['total'] = $moneyRecord -> winLoseCount($moneyRecordWhere, $moneyRecordBetween);
  349. // 总页数计算
  350. $result['countPage'] = ceil($result['total'] / $pageSize);
  351. $result['currentPage'] = $currentPage;
  352. return $result;
  353. }
  354. /**
  355. * 统计记录
  356. *
  357. * @access public
  358. * @return String
  359. */
  360. public function monthRecord()
  361. {
  362. $accountManagerClass = new AccountManager;
  363. $userInfo = $accountManagerClass -> getCurrentUser();
  364. if (empty($userInfo['identity'])) {
  365. Render([], '2001', lang('Common','Api') -> get('user does login'));
  366. }
  367. // 用户ID
  368. $moneyBuyWhere['account_identity'] = $userInfo['identity'];
  369. // 时间区间
  370. $endTime = date('Y-m-d H:i:s');
  371. if ($userInfo['statisticsTime']) {
  372. $startTime = $userInfo['statisticsTime'];
  373. } else {
  374. $startTime = '1900-01-01 00:00:00';
  375. }
  376. $moneyBuyBetween = [$startTime, $endTime];
  377. $moneyRecordWhere['settle_status'] = 2;
  378. // 获取单式投注订单
  379. $moneyBuySimplex = lm('money_buy_simplex', "commons");
  380. $mnyBuySpxData = $moneyBuySimplex
  381. ->select(['order_id', 'money', 'gain_money', 'batch_id'])
  382. ->where($moneyBuyWhere)
  383. ->whereBetween('settlementTime', $moneyBuyBetween)
  384. ->get()
  385. ->toArray();
  386. // 获取全部订单id
  387. $moneyBuyMthOr = [];
  388. $allBatchId = array_unique($mnyBuySpxData, SORT_REGULAR);
  389. if ($allBatchId) {
  390. foreach ($allBatchId as $key => $value) {
  391. $moneyBuyMthOr[0][$key][0] = 'batch_id';
  392. $moneyBuyMthOr[0][$key][1] = $value['batch_id'];
  393. }
  394. $moneyBuyMthWhere['bet_type'] = 1;
  395. // 获取注单
  396. $moneyBuyMthMdl = lm('Money_buy_match', "commons");
  397. $moneyBuyMthSlt = ['bet_money', 'odds', 'result'];
  398. $getMoneyBuyMth = $moneyBuyMthMdl -> moneyBuyMatchAll($moneyBuyMthSlt, $moneyBuyMthWhere, $moneyBuyMthOr);
  399. } else {
  400. $getMoneyBuyMth = [];
  401. }
  402. // 获取串式投注订单
  403. $moneyBuyStr = lm('money_buy_str', "commons");
  404. $mnyBuyStrData = $moneyBuyStr
  405. ->select(['order_id', 'money', 'gain_money'])
  406. ->where($moneyBuyWhere)
  407. ->whereBetween('settlementTime', $moneyBuyBetween)
  408. ->get();
  409. $winMoney = $userInfo['allWin'];
  410. $loseMoney = $userInfo['allLose'];
  411. // 单式盈利.
  412. foreach ($getMoneyBuyMth as $key => $value) {
  413. // 判断订单是否盈利
  414. if ($value->result == 1) {
  415. $winMoney += $value->bet_money * $value->odds;
  416. } else if ($value->result == 3) {
  417. $winMoney += $value->bet_money * $value->odds * 0.5;
  418. } else if ($value->result == 4) {
  419. $loseMoney += $value->bet_money * 0.5;
  420. } else if ($value->result == -1) {
  421. $loseMoney += $value->bet_money;
  422. }
  423. }
  424. // 串式盈利.
  425. foreach ($mnyBuyStrData as $key => $value) {
  426. // 判断订单是否盈利
  427. if ($value->money >= $value->gain_money) {
  428. $loseMoney += $value->money - $value->gain_money; //本钱减回款
  429. } else {
  430. $winMoney += $value->gain_money - $value->money; //回款减本钱
  431. }
  432. }
  433. // 修改用户统计
  434. $updateActDtd = [
  435. 'allWin' => $winMoney,
  436. 'allLose' => $loseMoney,
  437. 'statisticsTime' => date('Y-m-d H:i:s'),
  438. ];
  439. $accountDetailed = lm('account_detailed', "commons");
  440. $accountDetailed->where(['account_identity'=>$userInfo['identity']])->update($updateActDtd);
  441. $result = [
  442. 'winMoney' => $winMoney ?? 0,
  443. 'loseMoney' => $loseMoney ?? 0,
  444. 'allMoney' => $userInfo['available_cash'],
  445. ];
  446. return $result;
  447. }
  448. }