BetService.php 19 KB

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