BetService.php 20 KB

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