BetService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. <?php
  2. namespace App\Services;
  3. use App\Http\Controllers\admin\GameplayRule;
  4. use App\Models\Rebate;
  5. use App\Services\BaseService;
  6. use App\Models\Bet;
  7. use App\Models\Config;
  8. use Carbon\Carbon;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Collection;
  11. use Illuminate\Support\Facades\Cache;
  12. use Illuminate\Support\Facades\Log;
  13. use App\Services\GameplayRuleService;
  14. use App\Services\WalletService;
  15. use App\Services\IssueService;
  16. use App\Services\UserService;
  17. use App\Services\BalanceLogService;
  18. /**
  19. * 投注
  20. */
  21. class BetService extends BaseService
  22. {
  23. /**
  24. * @description: 模型
  25. * @return {string}
  26. */
  27. public static function model(): string
  28. {
  29. return Bet::class;
  30. }
  31. /**
  32. * @description: 枚举
  33. * @return {*}
  34. */
  35. public static function enum(): string
  36. {
  37. return '';
  38. }
  39. /**
  40. * @description: 获取查询条件
  41. * @param {array} $search 查询内容
  42. * @return {array}
  43. */
  44. public static function getWhere(array $search = []): array
  45. {
  46. $where = [];
  47. if (isset($search['issue_no']) && !empty($search['issue_no'])) {
  48. $where[] = ['issue_no', '=', $search['issue_no']];
  49. }
  50. if (isset($search['member_id']) && !empty($search['member_id'])) {
  51. $where[] = ['member_id', '=', $search['member_id']];
  52. }
  53. if (isset($search['keywords']) && !empty($search['keywords'])) {
  54. $where[] = ['keywords', '=', $search['keywords']];
  55. }
  56. if (isset($search['issue_id']) && !empty($search['issue_id'])) {
  57. $where[] = ['issue_id', '=', $search['issue_id']];
  58. }
  59. if (isset($search['id']) && !empty($search['id'])) {
  60. $where[] = ['id', '=', $search['id']];
  61. }
  62. if (isset($search['user_id']) && !empty($search['user_id'])) {
  63. $where[] = ['user_id', '=', $search['user_id']];
  64. }
  65. if (isset($search['status']) && !empty($search['status'])) {
  66. $where[] = ['status', '=', $search['status']];
  67. }
  68. if (isset($search['start_time']) && !empty($search['status'])) {
  69. $where[] = ['created_at', '>=', "{$search['start_time']} 00:00:00"];
  70. $where[] = ['created_at', '<=', "{$search['end_time']} 23:59:59"];
  71. }
  72. if (isset($search['is_winner']) && !empty($search['is_winner'])) {
  73. $where[] = ['status', '=', 1];
  74. if ($search['is_winner'] == 1) {
  75. $where[] = ['profit', '>', 0];
  76. } else {
  77. $where[] = ['profit', '<=', 0];
  78. }
  79. }
  80. return $where;
  81. }
  82. /**
  83. * @description: 查询单条数据
  84. * @param array $search
  85. * @return \App\Models\Coin|null
  86. */
  87. public static function findOne(array $search): ?Bet
  88. {
  89. return self::model()::where(self::getWhere($search))->first();
  90. }
  91. /**
  92. * @description: 查询所有数据
  93. * @param array $search
  94. * @return \Illuminate\Database\Eloquent\Collection
  95. */
  96. public static function findAll(array $search = [])
  97. {
  98. return self::model()::where(self::getWhere($search))->get();
  99. }
  100. /**
  101. * @description: 分页查询
  102. * @param array $search
  103. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  104. */
  105. public static function paginate(array $search = [])
  106. {
  107. $limit = isset($search['limit']) ? $search['limit'] : 15;
  108. $query = self::model()::where(self::getWhere($search));
  109. if (isset($search['username']) && !empty($search['username'])) {
  110. $username = $search['username'];
  111. $query = $query->whereHas('user', function ($query) use ($username) {
  112. $query->where('username', $username);
  113. });
  114. }
  115. $paginator = $query->paginate($limit);
  116. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  117. }
  118. /**
  119. * @description: 投注操作
  120. * @param {string} $memberId
  121. * @param {string} $input
  122. * @return {*}
  123. */
  124. public static function bet(string $memberId, string $input, $messageId = 0)
  125. {
  126. $msg = [];
  127. $msg['chat_id'] = $memberId;
  128. // 钱包生成
  129. // $walletInfo = WalletService::getUserWallet($memberId);
  130. // 分解投注的内容
  131. $betResult = GameplayRuleService::bettingRuleVerify($input);
  132. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  133. if ($betResult == null) {
  134. $text = "消息格式错误!\n";
  135. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  136. $msg['text'] = $text;
  137. if ($messageId) {
  138. $msg['reply_to_message_id'] = $messageId;
  139. }
  140. return $msg;
  141. }
  142. $keywords = $betResult['rule']; // 玩法
  143. $amount = $betResult['amount']; // 投注金额
  144. $gameplayRuleInfo = GameplayRuleService::getGameplayRules($keywords);
  145. if ($gameplayRuleInfo == null) {
  146. $text = "玩法未配置!\n";
  147. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  148. $msg['text'] = $text;
  149. if ($messageId) {
  150. $msg['reply_to_message_id'] = $messageId;
  151. }
  152. return $msg;
  153. }
  154. if ($gameplayRuleInfo['odds'] <= 0) {
  155. $text = "赔率为0 庄家通吃 禁止投注!\n";
  156. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  157. $msg['text'] = $text;
  158. if ($messageId) {
  159. $msg['reply_to_message_id'] = $messageId;
  160. }
  161. return $msg;
  162. }
  163. // 期数验证
  164. $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
  165. if (empty($issueInfo)) {
  166. $issueCloseInfo = IssueService::model()::where('status', IssueService::model()::STATUS_CLOSE)->orderBy('id', 'desc')->first();
  167. if (empty($issueCloseInfo)) {
  168. $text = "暂无可下注期数,本次下注无效!\n";
  169. $msg['text'] = $text;
  170. if ($messageId) {
  171. $msg['reply_to_message_id'] = $messageId;
  172. }
  173. return $msg;
  174. } else {
  175. $text = "封盘中,本次下注无效!\n";
  176. $msg['text'] = $text;
  177. if ($messageId) {
  178. $msg['reply_to_message_id'] = $messageId;
  179. }
  180. return $msg;
  181. }
  182. }
  183. if (!is_numeric($amount) || $amount <= 0) {
  184. $text = "投注金额格式不正确!\n";
  185. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  186. $msg['text'] = $text;
  187. if ($messageId) {
  188. $msg['reply_to_message_id'] = $messageId;
  189. }
  190. return $msg;
  191. }
  192. // 投注限制校验
  193. if ($amount < $gameplayRuleInfo['mininum']) {
  194. $text = "下注失败,最小金额限制{$gameplayRuleInfo['mininum']}\n";
  195. $msg['text'] = $text;
  196. if ($messageId) {
  197. $msg['reply_to_message_id'] = $messageId;
  198. }
  199. return $msg;
  200. }
  201. // 投注限制校验
  202. if ($amount > $gameplayRuleInfo['maxinum']) {
  203. $text = "下注失败,最大金额限制{$gameplayRuleInfo['maxinum']}\n";
  204. $msg['text'] = $text;
  205. if ($messageId) {
  206. $msg['reply_to_message_id'] = $messageId;
  207. }
  208. return $msg;
  209. }
  210. // 获取用户余额
  211. $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  212. $balance = $walletInfo['available_balance'];
  213. // 余额计算
  214. if ($balance < $amount) {
  215. $text = "余额不足,本次下注无效!\n";
  216. $msg['text'] = $text;
  217. if ($messageId) {
  218. $msg['reply_to_message_id'] = $messageId;
  219. }
  220. return $msg;
  221. }
  222. $userInfo = UserService::findOne(['member_id' => $memberId]);
  223. $betInfo = self::findOne(['member_id' => $memberId, 'issue_no' => $issueInfo->issue_no, 'keywords' => $keywords]); // 相同下注
  224. if ($betInfo) {
  225. $betInfo->amount = $betInfo->amount + $amount;
  226. $bet_id = $betInfo->id;
  227. $betInfo->save();
  228. } else {
  229. $data = [];
  230. $data['amount'] = $amount; // 分数
  231. $data['keywords'] = $keywords; // 玩法
  232. $data['member_id'] = $memberId;
  233. $data['user_id'] = $userInfo->id;
  234. $data['issue_no'] = $issueInfo->issue_no;
  235. $data['issue_id'] = $issueInfo->id;
  236. $data['odds'] = $gameplayRuleInfo['odds'];
  237. $newBet = self::model()::create($data);
  238. $bet_id = $newBet->id;
  239. }
  240. WalletService::updateBalance($memberId, -$amount);
  241. BalanceLogService::addLog($memberId, -$amount, $balance, ($balance - $amount), '投注', $bet_id, '');
  242. $now = Carbon::now('America/New_York')->format('Y-m-d');
  243. $rebate = Config::where('field', 'rebate')->first()->val;
  244. Rebate::addOrUpdate([
  245. 'date' => $now,
  246. 'member_id' => $memberId,
  247. 'betting_amount' => $amount,
  248. 'rebate_ratio' => $rebate,
  249. 'first_name' => $userInfo->first_name,
  250. 'username' => $userInfo->username,
  251. ]);
  252. // // 返利
  253. // $rebate = Config::where('field', 'rebate')->first()->val;
  254. // if($rebate > 0){
  255. // $rebateAmount = bcmul($amount, $rebate, 2); // 返利金额
  256. // if($rebateAmount > 0){
  257. // WalletService::updateBalance($memberId,$rebateAmount);
  258. // $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  259. // $balance = $walletInfo['available_balance'];
  260. // BalanceLogService::addLog($memberId,$rebateAmount,$balance,($balance+$rebateAmount),'返水',$bet_id,'');
  261. // }
  262. // }
  263. $text = "下注期数:{$issueInfo->issue_no}\n";
  264. $text .= "下注内容\n";
  265. $text .= "--------\n";
  266. $text .= "{$input}\n";
  267. $text .= "--------\n";
  268. $text .= "下注成功\n";
  269. $msg['text'] = $text;
  270. $lastStr = self::getLastChar($userInfo->first_name, 1);
  271. $groupText = "";
  272. $groupText .= "私聊下注 【xxxxxx" . $lastStr . "】 \n";
  273. $groupText .= "下注期数:{$issueInfo->issue_no} \n";
  274. $groupText .= "下注内容: \n";
  275. $groupText .= "----------- \n";
  276. $groupText .= "{$input} \n";
  277. $groupText .= "----------- \n";
  278. $inlineButton = self::getOperateButton();
  279. // 群通知
  280. self::bettingGroupNotice($groupText, $inlineButton); // 群通知
  281. return $msg;
  282. }
  283. // 模拟下注
  284. public static function fakeBet()
  285. {
  286. $betFake = Config::where('field', 'bet_fake')->first()->val;
  287. if ($betFake) {
  288. // 期数验证
  289. $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
  290. if ($issueInfo) {
  291. $fake_bet_list = Cache::get('fake_bet_' . $issueInfo->issue_no, []);
  292. $gameplayRuleList = GameplayRuleService::model()::where('odds', '>', 0)->get();
  293. $gameplayRuleList = $gameplayRuleList->toArray();
  294. $randKey = array_rand($gameplayRuleList, 1);
  295. $gameplayRuleInfo = $gameplayRuleList[$randKey] ?? [];
  296. if ($gameplayRuleInfo) {
  297. $item = [];
  298. $item['keywords'] = $gameplayRuleInfo['keywords'];
  299. $item['odds'] = $gameplayRuleInfo['odds'];
  300. $item['amount'] = rand($gameplayRuleInfo['mininum'], $gameplayRuleInfo['maxinum']);
  301. $item['first_name'] = self::generateRandomString(6);
  302. $item['profit'] = 0;
  303. $input = $item['keywords'] . $item['amount'];
  304. $fake_bet_list[] = $item;
  305. $lastStr = self::getLastChar($item['first_name'], 1);
  306. $groupText = "";
  307. $groupText .= "私聊下注 【xxxxxx" . $lastStr . "】 \n";
  308. $groupText .= "下注期数:{$issueInfo->issue_no} \n";
  309. $groupText .= "下注内容: \n";
  310. $groupText .= "----------- \n";
  311. $groupText .= "{$input} \n";
  312. $groupText .= "----------- \n";
  313. $inlineButton = self::getOperateButton();
  314. // 群通知
  315. self::bettingGroupNotice($groupText, $inlineButton); // 群通知
  316. }
  317. Cache::put('fake_bet_' . $issueInfo->issue_no, $fake_bet_list, 500);
  318. }
  319. }
  320. }
  321. /**
  322. * @description: 当期下注
  323. * @param {*} $memberId
  324. * @return {*}
  325. */
  326. public static function currentBet($memberId)
  327. {
  328. $msg['chat_id'] = $memberId;
  329. // 期数验证
  330. $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
  331. $issue_no = '';
  332. if (!empty($issueInfo)) {
  333. $issue_no = $issueInfo->issue_no;
  334. } else {
  335. $issueCloseInfo = IssueService::model()::where('status', IssueService::model()::STATUS_CLOSE)->orderBy('id', 'desc')->first();
  336. if (empty($issueCloseInfo)) {
  337. $issue_no = $issueCloseInfo->issue_no;
  338. }
  339. }
  340. if ($issue_no) {
  341. $text = "当前期号:{$issue_no} \n";
  342. $text .= "\n";
  343. $text .= "----------\n";
  344. $list = self::findAll(['member_id' => $memberId, 'issue_no' => $issue_no]);
  345. foreach ($list->toArray() as $k => $v) {
  346. $text .= "{$v['keywords']}{$v['amount']} \n";
  347. }
  348. $text .= "\n";
  349. $text .= "----------\n";
  350. $msg['text'] = $text;
  351. } else {
  352. $msg['text'] = "当前没有开放的投注期数! \n";
  353. }
  354. return $msg;
  355. }
  356. /**
  357. * @description: 近期投注
  358. * @param {*} $memberId
  359. * @return {*}
  360. */
  361. public static function recentlyRecord($memberId, $page = 1, $limit = 5)
  362. {
  363. $list = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->orderBy('id', 'desc')->forPage($page, $limit)->get();
  364. // $text = "```\n";
  365. $text = "";
  366. $text .= "期数--内容--盈亏 \n";
  367. foreach ($list->toArray() as $k => $v) {
  368. $profit = $v['profit'] - $v['amount'];
  369. // $text .= $v['issue_no']." ".$v['keywords']." ".$v['amount']." ".$v['profit']."\n";
  370. $item = $v['issue_no'] . "==" . $v['keywords'] . rtrim(rtrim(number_format($v['amount'], 2, '.', ''), '0'), '.') . "==" . rtrim(rtrim(number_format($profit, 2, '.', ''), '0'), '.') . "\n";
  371. $text .= $item;
  372. }
  373. // $text .= "```\n";
  374. return $text;
  375. }
  376. /**
  377. * @description: 投注记录
  378. * @param {*} $memberId
  379. * @param {*} $page
  380. * @param {*} $limit
  381. * @return {*}
  382. */
  383. public static function record($memberId, $messageId = null, $page = 1, $limit = 5)
  384. {
  385. $msg['chat_id'] = $memberId;
  386. $list = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->orderBy('id', 'desc')->forPage($page, $limit)->get();
  387. $count = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->count();
  388. $keyboard = [];
  389. $total_amount = BalanceLogService::model()::where('member_id',$memberId)->where('change_type','中奖')->sum('amount');
  390. $total_amount = number_format($total_amount, 2);
  391. $text = "历史注单 \n";
  392. $text .="中奖总金额:{$total_amount} \n";
  393. foreach ($list as $k => $v) {
  394. if ($v->status == self::model()::STATUS_SETTLED) {
  395. $phase = $v->profit - $v->amount;
  396. } else {
  397. $phase = '待开奖';
  398. }
  399. $text .= "-------------------------------------\n";
  400. $text .= "期数:{$v->issue_no} \n";
  401. $text .= "内容:{$v->keywords} \n";
  402. $text .= "金额:{$v->amount} \n";
  403. $text .= "盈亏:{$phase} \n";
  404. }
  405. $msg['text'] = $text;
  406. if ($page > 1) {
  407. $keyboard[] = [
  408. ['text' => "👆上一页", 'callback_data' => "betRecordNextPage@@" . ($page - 1)]
  409. ];
  410. }
  411. $allPage = ceil($count / $limit);
  412. if ($allPage > $page) {
  413. if ($page > 1) {
  414. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)];
  415. } else {
  416. $keyboard[] = [
  417. ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)]
  418. ];
  419. }
  420. }
  421. if ($messageId) {
  422. $msg['message_id'] = $messageId;
  423. }
  424. if ($keyboard) {
  425. $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  426. }
  427. return $msg;
  428. }
  429. /**
  430. * @description: 中奖结算
  431. * @param {*} $issue_no
  432. * @param {*} $awards
  433. * @return {*}
  434. */
  435. public static function betSettled($issue_no, $awards)
  436. {
  437. $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
  438. $data = [];
  439. $text = $issue_no . "期开奖结果 \n";
  440. $text .= "-----本期开奖账单----- \n";
  441. $bet_num = 0;
  442. foreach ($list->toArray() as $k => $v) {
  443. $userInfo = UserService::findAll(['member_id' => $v['member_id']]);
  444. $lastStr = self::getLastChar($userInfo->first_name, 1);
  445. $item = [];
  446. $item['id'] = $v['id'];
  447. $item['status'] = self::model()::STATUS_SETTLED;
  448. if (in_array($v['keywords'], $awards)) {
  449. $profit = $v['amount'] * $v['odds'];
  450. if ($profit > 880000) {
  451. $profit = 880000; // 单注最高奖金880000
  452. }
  453. $item['profit'] = $profit;
  454. $yl = $profit - $v['amount'];
  455. if ($k + 1 <= 15) {
  456. $text .= "私聊下注 【******" . $lastStr . "】 {$yl}\n";
  457. $bet_num++;
  458. }
  459. // 结算
  460. WalletService::updateBalance($v['member_id'], $profit);
  461. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  462. $balance = $walletInfo['available_balance'];
  463. BalanceLogService::addLog($v['member_id'], $profit, $balance, ($balance + $profit), '中奖', $v['id'], '');
  464. } else {
  465. if ($k + 1 <= 15) {
  466. $text .= "私聊下注 【******" . $lastStr . "】 -{$v['amount']}\n";
  467. $bet_num++;
  468. }
  469. }
  470. self::model()::where('id', $v['id'])->update($item);
  471. }
  472. $inlineButton = self::getOperateButton();
  473. $rand_num = 30 - $bet_num;
  474. $text .= self::fakeLotteryDraw($issue_no, $awards, $rand_num);
  475. // for ($i = 0; $i < $rand_num; $i++) {
  476. // // 生成 -100000 到 100000 的随机数,但排除 -10 到 10 的范围
  477. // $randomNumber = random_int(-1000000, 1000000) / 100;
  478. // if ($randomNumber >= -10 && $randomNumber <= 10) {
  479. // // 如果落在 -10 到 10 之间,重新生成或调整
  480. // $randomNumber = $randomNumber < 0 ? -random_int(10, 100000) : random_int(10, 100000);
  481. // }
  482. // $text .= "私聊下注 【******】 {$randomNumber}\n";
  483. // }
  484. // 群通知
  485. self::bettingGroupNotice($text, $inlineButton, '');
  486. }
  487. // 虚拟开奖
  488. public static function fakeLotteryDraw($issue_no, $awards, $rand_num = 30)
  489. {
  490. $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
  491. $text = "";
  492. foreach ($fake_bet_list as $k => $v) {
  493. $lastStr = self::getLastChar($v['first_name'], 1);
  494. if (in_array($v['keywords'], $awards)) {
  495. $profit = $v['amount'] * $v['odds'];
  496. if ($profit > 880000) {
  497. $profit = 880000; // 单注最高奖金880000
  498. }
  499. $item['profit'] = $profit;
  500. $yl = $profit - $v['amount'];
  501. if ($k + 1 <= $rand_num) {
  502. $text .= "私聊下注 【******" . $lastStr . "】 {$yl}\n";
  503. }
  504. } else {
  505. if ($k + 1 <= $rand_num) {
  506. $text .= "私聊下注 【******" . $lastStr . "】 -{$v['amount']}\n";
  507. }
  508. }
  509. }
  510. return $text;
  511. }
  512. }