MoneyBuy.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. // 获取用户信息
  322. $accountManagerClass = new AccountManager;
  323. $userInfo = $accountManagerClass -> getCurrentUser();
  324. if (empty($userInfo['identity'])) {
  325. Render([], '2001', lang('Common','Api') -> get('user does login'));
  326. }
  327. $moneyRecordWhere = array();
  328. // 用户ID
  329. $moneyRecordWhere['account_identity'] = $userInfo['identity'];
  330. $moneyRecordWhere['status'] = 1;
  331. $moneyRecordWhere['settle_status'] = 2;
  332. if (empty($_POST['result']) === false) {
  333. $moneyRecordWhere['game_status'] = empty($_POST['result']);
  334. }
  335. // 时间区间
  336. $startTime = isset($_POST['startTime'])? $_POST['startTime'] : '1900-01-01 00:00:00';
  337. $endTime = isset($_POST['endTime'])? $_POST['endTime'] : '3000-12-12 00:00:00';
  338. $moneyRecordBetween = [$startTime, $endTime];
  339. // 当前页
  340. $currentPage = isset($_POST['currentPage']) ? $_POST['currentPage'] : "1";
  341. // 分页大小
  342. $pageSize = isset($_POST['pageSize']) ? $_POST['pageSize'] : "10";
  343. // 起始查询位置
  344. $begin = ($currentPage - 1) * $pageSize;
  345. $moneyRecord = lm('Money_buy_simplex', "commons");
  346. $moneyRecordSelect = ['info_identity', 'trade_id', 'account_identity', 'money', 'money_time', 'money_type'];
  347. // 获取盈亏记录
  348. $result['list'] = $moneyRecord -> winLoseRecord($moneyRecordWhere, $moneyRecordBetween, $begin, $pageSize);
  349. // 获取记录总数
  350. $result['total'] = $moneyRecord -> winLoseCount($moneyRecordWhere, $moneyRecordBetween);
  351. // 总页数计算
  352. $result['countPage'] = ceil($result['total'] / $pageSize);
  353. $result['currentPage'] = $currentPage;
  354. return $result;
  355. }
  356. /**
  357. * 统计记录
  358. *
  359. * @access public
  360. * @return String
  361. */
  362. public function monthRecord()
  363. {
  364. $accountManagerClass = new AccountManager;
  365. $userInfo = $accountManagerClass -> getCurrentUser();
  366. if (empty($userInfo['identity'])) {
  367. Render([], '2001', lang('Common','Api') -> get('user does login'));
  368. }
  369. // 用户ID
  370. $moneyBuyWhere['account_identity'] = $userInfo['identity'];
  371. // 时间区间
  372. $endTime = date('Y-m-d H:i:s');
  373. if ($userInfo['statisticsTime']) {
  374. $startTime = $userInfo['statisticsTime'];
  375. } else {
  376. $startTime = '1900-01-01 00:00:00';
  377. }
  378. $moneyBuyBetween = [$startTime, $endTime];
  379. $moneyRecordWhere['settle_status'] = 2;
  380. // 获取单式投注订单
  381. $moneyBuySimplex = lm('money_buy_simplex', "commons");
  382. $mnyBuySpxData = $moneyBuySimplex
  383. ->select(['order_id', 'money', 'gain_money', 'batch_id'])
  384. ->where($moneyBuyWhere)
  385. ->whereBetween('settlementTime', $moneyBuyBetween)
  386. ->get()
  387. ->toArray();
  388. // 获取全部订单id
  389. $moneyBuyMthOr = [];
  390. $allBatchId = array_unique($mnyBuySpxData, SORT_REGULAR);
  391. if ($allBatchId) {
  392. foreach ($allBatchId as $key => $value) {
  393. $moneyBuyMthOr[0][$key][0] = 'batch_id';
  394. $moneyBuyMthOr[0][$key][1] = $value['batch_id'];
  395. }
  396. $moneyBuyMthWhere['bet_type'] = 1;
  397. // 获取注单
  398. $moneyBuyMthMdl = lm('Money_buy_match', "commons");
  399. $moneyBuyMthSlt = ['bet_money', 'odds', 'result'];
  400. $getMoneyBuyMth = $moneyBuyMthMdl -> moneyBuyMatchAll($moneyBuyMthSlt, $moneyBuyMthWhere, $moneyBuyMthOr);
  401. } else {
  402. $getMoneyBuyMth = [];
  403. }
  404. // 获取串式投注订单
  405. $moneyBuyStr = lm('money_buy_str', "commons");
  406. $mnyBuyStrData = $moneyBuyStr
  407. ->select(['order_id', 'money', 'gain_money'])
  408. ->where($moneyBuyWhere)
  409. ->whereBetween('settlementTime', $moneyBuyBetween)
  410. ->get();
  411. $winMoney = $userInfo['allWin'];
  412. $loseMoney = $userInfo['allLose'];
  413. // 单式盈利.
  414. foreach ($getMoneyBuyMth as $key => $value) {
  415. // 判断订单是否盈利
  416. if ($value->result == 1) {
  417. $winMoney += $value->bet_money * $value->odds;
  418. } else if ($value->result == 3) {
  419. $winMoney += $value->bet_money * $value->odds * 0.5;
  420. } else if ($value->result == 4) {
  421. $loseMoney += $value->bet_money * 0.5;
  422. } else if ($value->result == -1) {
  423. $loseMoney += $value->bet_money;
  424. }
  425. }
  426. // 串式盈利.
  427. foreach ($mnyBuyStrData as $key => $value) {
  428. // 判断订单是否盈利
  429. if ($value->money >= $value->gain_money) {
  430. $loseMoney += $value->money - $value->gain_money; //本钱减回款
  431. } else {
  432. $winMoney += $value->gain_money - $value->money; //回款减本钱
  433. }
  434. }
  435. // 修改用户统计
  436. $updateActDtd = [
  437. 'allWin' => $winMoney,
  438. 'allLose' => $loseMoney,
  439. 'statisticsTime' => date('Y-m-d H:i:s'),
  440. ];
  441. $accountDetailed = lm('account_detailed', "commons");
  442. $accountDetailed->where(['account_identity'=>$userInfo['identity']])->update($updateActDtd);
  443. $result = [
  444. 'winMoney' => $winMoney ?? 0,
  445. 'loseMoney' => $loseMoney ?? 0,
  446. 'allMoney' => $userInfo['available_cash'],
  447. ];
  448. return $result;
  449. }
  450. }