BetService.php 21 KB

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