MoneyBuy.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. }
  112. // 当前页
  113. $currentPage = isset($_POST['currentPage']) ? $_POST['currentPage'] : "1";
  114. // 分页大小
  115. $pageSize = isset($_POST['pageSize']) ? $_POST['pageSize'] : "10";
  116. // 起始查询位置
  117. $begin = ($currentPage - 1) * $pageSize;
  118. // 获取注单
  119. $moneyBuyMthMdl = lm('Money_buy_match', "commons");
  120. $moneyBuyMthSlt = ['status', 'bet_money', 'home_team', 'guest_team', 'condition', 'odds', 'odds_name', 'match_id', 'money_buy_match.game_code', 'money_buy_match.odds_code'];
  121. $getMoneyBuyMth = $moneyBuyMthMdl -> moneyBuyMatch($moneyBuyMthSlt, $moneyBuyMthWhere, $moneyBuyMthOr, $begin, $pageSize);
  122. $allCount = $moneyBuyMthMdl -> moneyBuyMatchTotal($moneyBuyMthWhere, $moneyBuyMthOr);
  123. // 获取可加注所需要的查询条件
  124. $commonFunction = C() -> get('commonFunction');
  125. $stOddsWheres = [];
  126. foreach ($getMoneyBuyMth as $key => $value) {
  127. $match_id = $value -> match_id;
  128. $odds_code = $value -> odds_code;
  129. $gameType = $value -> game_code;
  130. $where = [
  131. 'match_id' => $match_id,
  132. 'odds_code' => $odds_code,
  133. ];
  134. // 判断是否重复
  135. if (!$commonFunction -> judgeEqual($where, $stOddsWheres[$gameType])) {
  136. $stOddsWheres[$gameType][] = $where;
  137. }
  138. }
  139. // 查询数据下所有可加注的游戏
  140. $stOddsModel = lm('stOdds','commons');
  141. $stOddsSelect = ['match_id', 'odds_code'];
  142. $getSstOdds = [];
  143. // 循环查询每个运动的可加注游戏 注:由于查询不同表所以必须循环查询
  144. foreach ($stOddsWheres as $key => $value) {
  145. if ($value) {
  146. $getSstOdds[$key] = $stOddsModel -> stOdds($stOddsSelect, $value, $key);
  147. }
  148. }
  149. // 循环添加玩法和判断玩法是否可加注
  150. foreach ($getMoneyBuyMth as $key => $value) {
  151. $typeSstOdds = $getSstOdds[$value -> game_code];
  152. $thisSstOdds = [
  153. 'match_id' => $value -> match_id,
  154. 'odds_code' => $value -> odds_code,
  155. ];
  156. // 判断如果在数组中则为可加注
  157. if (!!$commonFunction->judgeEqual($thisSstOdds, $typeSstOdds)) {
  158. $getMoneyBuyMth[$key] -> addBet = 0;
  159. } else {
  160. $getMoneyBuyMth[$key] -> addBet = 1;
  161. }
  162. }
  163. // 获取记录总数
  164. $result['total'] = $allCount;
  165. $result['list'] = $getMoneyBuyMth;
  166. // 总页数计算
  167. $result['countPage'] = ceil($result['total'] / $pageSize);
  168. $result['currentPage'] = $currentPage;
  169. return $result;
  170. }
  171. /**
  172. * 串式投注记录
  173. *
  174. * @access public
  175. * @return String
  176. */
  177. public function stringBetRecord()
  178. {
  179. // 获取用户信息
  180. $accountManagerClass = new AccountManager;
  181. $userInfo = $accountManagerClass -> getCurrentUser();
  182. if (empty($userInfo['identity'])) {
  183. Render([], '2001', lang('Common','Api') -> get('user does login'));
  184. }
  185. // 用户ID
  186. $moneyBuyStrWhere['account_identity'] = $userInfo['identity'];
  187. // 输赢
  188. if (!empty($_POST['result'])) {
  189. $moneyBuyStrWhere['game_status'] = $_POST['result'];
  190. }
  191. // 时间区间
  192. $startTime = isset($_POST['startTime'])? $_POST['startTime'] : '1900-01-01 00:00:00';
  193. $endTime = isset($_POST['endTime'])? $_POST['endTime'] : '3000-12-12 00:00:00';
  194. $moneyBuyStrBetween = ['money_time' => [$startTime, $endTime]];
  195. $moneyBuyStrMdl = lm('money_buy_str', "commons");
  196. // 当前页
  197. $currentPage = isset($_POST['currentPage']) ? $_POST['currentPage'] : "1";
  198. // 分页大小
  199. $pageSize = isset($_POST['pageSize']) ? $_POST['pageSize'] : "10";
  200. // 起始查询位置
  201. $begin = ($currentPage - 1) * $pageSize;
  202. // 获取订单记录
  203. $moneyBuyStrSlt = ['batch_id', 'money'];
  204. $getMoneyBuyStr = $moneyBuyStrMdl -> moneyBuyStr($moneyBuyStrSlt, $moneyBuyStrWhere, $moneyBuyStrBetween, $begin, $pageSize, 1);
  205. $allCount = $moneyBuyStrMdl -> moneyBuyStrTotal($moneyBuyStrWhere, $moneyBuyStrBetween);
  206. // 获取全部订单id
  207. $moneyBuyMthOr = [];
  208. $allBatchId = array_unique($getMoneyBuyStr, SORT_REGULAR);
  209. if (!$allBatchId) {
  210. // 获取记录总数
  211. $result['total'] = 0;
  212. $result['list'] = [];
  213. // 总页数计算
  214. $result['countPage'] = 1;
  215. $result['currentPage'] = 1;
  216. return $result;
  217. }
  218. foreach ($allBatchId as $key => $value) {
  219. $moneyBuyMthOr[0][$key][0] = 'batch_id';
  220. $moneyBuyMthOr[0][$key][1] = $value['batch_id'];
  221. }
  222. $moneyBuyMthWhere['bet_type'] = 2;
  223. // 获取注单
  224. $moneyBuyMthMdl = lm('Money_buy_match', "commons");
  225. $moneyBuyMthSlt = ['status', 'bet_money', 'home_team', 'guest_team', 'condition', 'odds', 'odds_name', 'match_id', 'batch_id', 'money_buy_match.game_code', 'money_buy_match.odds_code'];
  226. $getMoneyBuyMth = $moneyBuyMthMdl -> moneyBuyMatch($moneyBuyMthSlt, $moneyBuyMthWhere, $moneyBuyMthOr, $begin, $pageSize);
  227. // 获取可加注所需要的查询条件
  228. $commonFunction = C() -> get('commonFunction');
  229. $stOddsWheres = [];
  230. foreach ($getMoneyBuyMth as $key => $value) {
  231. $match_id = $value -> match_id;
  232. $odds_code = $value -> odds_code;
  233. $gameType = $value -> game_code;
  234. $where = [
  235. 'match_id' => $match_id,
  236. 'odds_code' => $odds_code,
  237. ];
  238. // 判断是否重复
  239. if (!$commonFunction -> judgeEqual($where, $stOddsWheres[$gameType])) {
  240. $stOddsWheres[$gameType][] = $where;
  241. }
  242. }
  243. // 查询数据下所有可加注的游戏
  244. $stOddsModel = lm('stOdds','commons');
  245. $stOddsSelect = ['match_id', 'odds_code'];
  246. $getSstOdds = [];
  247. // 循环查询每个运动的可加注游戏 注:由于查询不同表所以必须循环查询
  248. foreach ($stOddsWheres as $key => $value) {
  249. if ($value) {
  250. $getSstOdds[$key] = $stOddsModel -> stOdds($stOddsSelect, $value, $key);
  251. }
  252. }
  253. // 循环添加玩法和判断玩法是否可加注
  254. foreach ($getMoneyBuyMth as $key => $value) {
  255. $typeSstOdds = $getSstOdds[$value -> game_code];
  256. $thisSstOdds = [
  257. 'match_id' => $value -> match_id,
  258. 'odds_code' => $value -> odds_code,
  259. ];
  260. // 判断如果在数组中则为可加注
  261. if (!!$commonFunction->judgeEqual($thisSstOdds, $typeSstOdds)) {
  262. $getMoneyBuyMth[$key] -> addBet = 0;
  263. } else {
  264. $getMoneyBuyMth[$key] -> addBet = 1;
  265. }
  266. }
  267. foreach ($getMoneyBuyStr as $key => $value) {
  268. foreach ($getMoneyBuyMth as $k => $v) {
  269. if ($value['batch_id'] == $v['batch_id']) {
  270. $getMoneyBuyStr[$key]['money_buy'][$k] = $v;
  271. }
  272. }
  273. }
  274. // 获取记录总数
  275. $result['total'] = $allCount;
  276. $result['list'] = $getMoneyBuyStr;
  277. // 总页数计算
  278. $result['countPage'] = ceil($result['total'] / $pageSize);
  279. $result['currentPage'] = $currentPage;
  280. return $result;
  281. }
  282. /**
  283. * 盈亏记录
  284. *
  285. * @access public
  286. * @return String
  287. */
  288. public function moneyRecord()
  289. {
  290. /*$moneyRecord = lm('Money_buy_simplex', "commons");
  291. $moneyRecord->aa();*/
  292. // 获取用户信息
  293. $accountManagerClass = new AccountManager;
  294. $userInfo = $accountManagerClass -> getCurrentUser();
  295. if (empty($userInfo['identity'])) {
  296. Render([], '2001', lang('Common','Api') -> get('user does login'));
  297. }
  298. $moneyRecordWhere = array();
  299. // 用户ID
  300. $moneyRecordWhere['account_identity'] = $userInfo['identity'];
  301. $moneyRecordWhere['status'] = 1;
  302. // 时间区间
  303. $startTime = isset($_POST['startTime'])? $_POST['startTime'] : '1900-01-01 00:00:00';
  304. $endTime = isset($_POST['endTime'])? $_POST['endTime'] : '3000-12-12 00:00:00';
  305. $moneyRecordBetween = [$startTime, $endTime];
  306. // 当前页
  307. $currentPage = isset($_POST['currentPage']) ? $_POST['currentPage'] : "1";
  308. // 分页大小
  309. $pageSize = isset($_POST['pageSize']) ? $_POST['pageSize'] : "10";
  310. // 起始查询位置
  311. $begin = ($currentPage - 1) * $pageSize;
  312. $moneyRecord = lm('Money_buy_simplex', "commons");
  313. $moneyRecordSelect = ['info_identity', 'trade_id', 'account_identity', 'money', 'money_time', 'money_type'];
  314. // 获取盈亏记录
  315. $result['list'] = $moneyRecord -> winLoseRecord($moneyRecordWhere, $moneyRecordBetween, $begin, $pageSize);
  316. // 获取记录总数
  317. $result['total'] = $moneyRecord -> winLoseCount($moneyRecordWhere, $moneyRecordBetween);
  318. // 总页数计算
  319. $result['countPage'] = ceil($result['total'] / $pageSize);
  320. $result['currentPage'] = $currentPage;
  321. return $result;
  322. }
  323. /**
  324. * 统计记录
  325. *
  326. * @access public
  327. * @return String
  328. */
  329. public function monthRecord()
  330. {
  331. $accountManagerClass = new AccountManager;
  332. $userInfo = $accountManagerClass -> getCurrentUser();
  333. if (empty($userInfo['identity'])) {
  334. Render([], '2001', lang('Common','Api') -> get('user does login'));
  335. }
  336. // 用户ID
  337. $moneyBuyWhere['account_identity'] =$userInfo['identity'];
  338. // 时间区间
  339. $endTime = date('Y-m-d H:i:s');
  340. if ($userInfo['statisticsTime']) {
  341. $startTime = $userInfo['statisticsTime'];
  342. } else {
  343. $startTime = '1900-01-01 00:00:00';
  344. }
  345. $moneyBuyBetween = [$startTime, $endTime];
  346. $moneyRecordWhere['settle_status'] = 2;
  347. // 获取单式投注订单
  348. $moneyBuySimplex = lm('money_buy_simplex', "commons");
  349. $mnyBuySpxData = $moneyBuySimplex
  350. ->select(['order_id', 'money', 'gain_money', 'batch_id'])
  351. ->where($moneyBuyWhere)
  352. ->whereBetween('settlementTime', $moneyBuyBetween)
  353. ->get()
  354. ->toArray();
  355. // 获取全部订单id
  356. $moneyBuyMthOr = [];
  357. $allBatchId = array_unique($mnyBuySpxData, SORT_REGULAR);
  358. if ($allBatchId) {
  359. foreach ($allBatchId as $key => $value) {
  360. $moneyBuyMthOr[0][$key][0] = 'batch_id';
  361. $moneyBuyMthOr[0][$key][1] = $value['batch_id'];
  362. }
  363. $moneyBuyMthWhere['bet_type'] = 1;
  364. // 获取注单
  365. $moneyBuyMthMdl = lm('Money_buy_match', "commons");
  366. $moneyBuyMthSlt = ['bet_money', 'odds', 'result'];
  367. $getMoneyBuyMth = $moneyBuyMthMdl -> moneyBuyMatchAll($moneyBuyMthSlt, $moneyBuyMthWhere, $moneyBuyMthOr);
  368. } else {
  369. $getMoneyBuyMth = [];
  370. }
  371. // 获取串式投注订单
  372. $moneyBuyStr = lm('money_buy_str', "commons");
  373. $mnyBuyStrData = $moneyBuyStr
  374. ->select(['order_id', 'money', 'gain_money'])
  375. ->where($moneyBuyWhere)
  376. ->whereBetween('settlementTime', $moneyBuyBetween)
  377. ->get();
  378. $winMoney = $userInfo['allWin'];
  379. $loseMoney = $userInfo['allLose'];
  380. // 单式盈利.
  381. foreach ($getMoneyBuyMth as $key => $value) {
  382. // 判断订单是否盈利
  383. if ($value->result == 1) {
  384. $winMoney += $value->bet_money * $value->odds;
  385. } else if ($value->result == 3) {
  386. $winMoney += $value->bet_money * $value->odds * 0.5;
  387. } else if ($value->result == 4) {
  388. $loseMoney += $value->bet_money * 0.5;
  389. } else if ($value->result == -1) {
  390. $loseMoney += $value->bet_money;
  391. }
  392. }
  393. // 串式盈利.
  394. foreach ($mnyBuyStrData as $key => $value) {
  395. // 判断订单是否盈利
  396. if ($value->money >= $value->gain_money) {
  397. $loseMoney += $value->money - $value->gain_money; //本钱减回款
  398. } else {
  399. $winMoney += $value->gain_money - $value->money; //回款减本钱
  400. }
  401. }
  402. // 修改用户统计
  403. $updateActDtd = [
  404. 'allWin' => $winMoney,
  405. 'allLose' => $loseMoney,
  406. 'statisticsTime' => date('Y-m-d H:i:s'),
  407. ];
  408. $accountDetailed = lm('account_detailed', "commons");
  409. $accountDetailed->where(['account_identity'=>$userInfo['identity']])->update($updateActDtd);
  410. $result = [
  411. 'winMoney' => $winMoney ?? 0,
  412. 'loseMoney' => $loseMoney ?? 0,
  413. 'allMoney' => $userInfo['available_cash'],
  414. ];
  415. return $result;
  416. }
  417. }