| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652 |
- <?php
- namespace App\Services;
- use App\Http\Controllers\admin\GameplayRule;
- use App\Models\Rebate;
- use App\Services\BaseService;
- use App\Models\Bet;
- use App\Models\Config;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- use App\Services\GameplayRuleService;
- use App\Services\WalletService;
- use App\Services\IssueService;
- use App\Services\UserService;
- use App\Services\BalanceLogService;
- /**
- * 投注
- */
- class BetService extends BaseService
- {
- /**
- * @description: 模型
- * @return {string}
- */
- public static function model(): string
- {
- return Bet::class;
- }
- /**
- * @description: 枚举
- * @return {*}
- */
- public static function enum(): string
- {
- return '';
- }
- /**
- * @description: 获取查询条件
- * @param {array} $search 查询内容
- * @return {array}
- */
- public static function getWhere(array $search = []): array
- {
- $where = [];
- if (isset($search['issue_no']) && !empty($search['issue_no'])) {
- $where[] = ['issue_no', '=', $search['issue_no']];
- }
- if (isset($search['member_id']) && !empty($search['member_id'])) {
- $where[] = ['member_id', '=', $search['member_id']];
- }
- if (isset($search['keywords']) && !empty($search['keywords'])) {
- $where[] = ['keywords', '=', $search['keywords']];
- }
- if (isset($search['issue_id']) && !empty($search['issue_id'])) {
- $where[] = ['issue_id', '=', $search['issue_id']];
- }
- if (isset($search['id']) && !empty($search['id'])) {
- $where[] = ['id', '=', $search['id']];
- }
- if (isset($search['user_id']) && !empty($search['user_id'])) {
- $where[] = ['user_id', '=', $search['user_id']];
- }
- if (isset($search['start_time']) && !empty($search['status'])) {
- $where[] = ['created_at', '>=', "{$search['start_time']} 00:00:00"];
- $where[] = ['created_at', '<=', "{$search['end_time']} 23:59:59"];
- }
- if (isset($search['is_winner']) && !empty($search['is_winner'])) {
- $where[] = ['status', '=', 2];
- if ($search['is_winner'] == 1) {
- $where[] = ['profit', '>', 0];
- } else {
- $where[] = ['profit', '<=', 0];
- }
- } else {
- if (isset($search['status']) && !empty($search['status'])) {
- $where[] = ['status', '=', $search['status']];
- }
- }
- return $where;
- }
- /**
- * @description: 查询单条数据
- * @param array $search
- * @return \App\Models\Coin|null
- */
- public static function findOne(array $search): ?Bet
- {
- return self::model()::where(self::getWhere($search))->first();
- }
- /**
- * @description: 查询所有数据
- * @param array $search
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public static function findAll(array $search = [])
- {
- return self::model()::where(self::getWhere($search))->get();
- }
- /**
- * @description: 分页查询
- * @param array $search
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
- */
- public static function paginate(array $search = [])
- {
- $limit = isset($search['limit']) ? $search['limit'] : 15;
- $query = self::model()::where(self::getWhere($search))
- ->with(['user']);
- if (isset($search['username']) && !empty($search['username'])) {
- $username = $search['username'];
- $query = $query->whereHas('user', function ($query) use ($username) {
- $query->where('username', $username);
- });
- }
- $paginator = $query->paginate($limit);
- return ['total' => $paginator->total(), 'data' => $paginator->items()];
- }
- /**
- * @description: 投注操作
- * @param {string} $memberId
- * @param {string} $input
- * @return {*}
- */
- public static function bet(string $memberId, string $input, $messageId = 0)
- {
- $msg = [];
- $msg['chat_id'] = $memberId;
- // 钱包生成
- // $walletInfo = WalletService::getUserWallet($memberId);
- // 分解投注的内容
- $betResult = GameplayRuleService::bettingRuleVerify($input);
- $serviceAccount = Config::where('field', 'service_account')->first()->val;
- if ($betResult == null) {
- $text = "消息格式错误!\n";
- $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
- $msg['text'] = $text;
- if ($messageId) {
- $msg['reply_to_message_id'] = $messageId;
- }
- return $msg;
- }
- $keywords = $betResult['rule']; // 玩法
- $amount = $betResult['amount']; // 投注金额
- $gameplayRuleInfo = GameplayRuleService::getGameplayRules($keywords);
- if ($gameplayRuleInfo == null) {
- $text = "玩法未配置!\n";
- $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
- $msg['text'] = $text;
- if ($messageId) {
- $msg['reply_to_message_id'] = $messageId;
- }
- return $msg;
- }
- if ($gameplayRuleInfo['odds'] <= 0) {
- $text = "赔率为0 庄家通吃 禁止投注!\n";
- $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
- $msg['text'] = $text;
- if ($messageId) {
- $msg['reply_to_message_id'] = $messageId;
- }
- return $msg;
- }
- // 期数验证
- $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
- if (empty($issueInfo)) {
- $issueCloseInfo = IssueService::model()::where('status', IssueService::model()::STATUS_CLOSE)->orderBy('id', 'desc')->first();
- if (empty($issueCloseInfo)) {
- $text = "暂无可下注期数,本次下注无效!\n";
- $msg['text'] = $text;
- if ($messageId) {
- $msg['reply_to_message_id'] = $messageId;
- }
- return $msg;
- } else {
- $text = "封盘中,本次下注无效!\n";
- $msg['text'] = $text;
- if ($messageId) {
- $msg['reply_to_message_id'] = $messageId;
- }
- return $msg;
- }
- }
- if (!is_numeric($amount) || $amount <= 0) {
- $text = "投注金额格式不正确!\n";
- $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
- $msg['text'] = $text;
- if ($messageId) {
- $msg['reply_to_message_id'] = $messageId;
- }
- return $msg;
- }
- $now_date = date('Y-m-d H:i:s',time() + 30); // 提前30秒
- if($issueInfo['end_time'] < $now_date){
- $text = "封盘中,本次下注无效!\n";
- $msg['text'] = $text;
- if ($messageId) {
- $msg['reply_to_message_id'] = $messageId;
- }
- return $msg;
- }
- // 投注限制校验
- if ($amount < $gameplayRuleInfo['mininum']) {
- $text = "下注失败,最小金额限制{$gameplayRuleInfo['mininum']}\n";
- $msg['text'] = $text;
- if ($messageId) {
- $msg['reply_to_message_id'] = $messageId;
- }
- return $msg;
- }
- // 投注限制校验
- if ($amount > $gameplayRuleInfo['maxinum']) {
- $text = "下注失败,最大金额限制{$gameplayRuleInfo['maxinum']}\n";
- $msg['text'] = $text;
- if ($messageId) {
- $msg['reply_to_message_id'] = $messageId;
- }
- return $msg;
- }
- // 获取用户余额
- $walletInfo = WalletService::findOne(['member_id' => $memberId]);
- $balance = $walletInfo['available_balance'];
- // 余额计算
- if ($balance < $amount) {
- $text = "余额不足,本次下注无效!\n";
- $msg['text'] = $text;
- if ($messageId) {
- $msg['reply_to_message_id'] = $messageId;
- }
- return $msg;
- }
- $userInfo = UserService::findOne(['member_id' => $memberId]);
- $betInfo = self::findOne(['member_id' => $memberId, 'issue_no' => $issueInfo->issue_no, 'keywords' => $keywords]); // 相同下注
- if ($betInfo) {
- $betInfo->amount = $betInfo->amount + $amount;
- $bet_id = $betInfo->id;
- $betInfo->save();
- } else {
- $data = [];
- $data['amount'] = $amount; // 分数
- $data['keywords'] = $keywords; // 玩法
- $data['member_id'] = $memberId;
- $data['user_id'] = $userInfo->id;
- $data['issue_no'] = $issueInfo->issue_no;
- $data['issue_id'] = $issueInfo->id;
- $data['odds'] = $gameplayRuleInfo['odds'];
- $newBet = self::model()::create($data);
- $bet_id = $newBet->id;
- }
- WalletService::updateBalance($memberId, -$amount);
- BalanceLogService::addLog($memberId, -$amount, $balance, ($balance - $amount), '投注', $bet_id, '');
- $now = Carbon::now('America/New_York')->format('Y-m-d');
- $rebate = Config::where('field', 'rebate')->first()->val;
- Rebate::addOrUpdate([
- 'date' => $now,
- 'member_id' => $memberId,
- 'betting_amount' => $amount,
- 'rebate_ratio' => $rebate,
- 'first_name' => $userInfo->first_name,
- 'username' => $userInfo->username,
- ]);
- // // 返利
- // $rebate = Config::where('field', 'rebate')->first()->val;
- // if($rebate > 0){
- // $rebateAmount = bcmul($amount, $rebate, 2); // 返利金额
- // if($rebateAmount > 0){
- // WalletService::updateBalance($memberId,$rebateAmount);
- // $walletInfo = WalletService::findOne(['member_id' => $memberId]);
- // $balance = $walletInfo['available_balance'];
- // BalanceLogService::addLog($memberId,$rebateAmount,$balance,($balance+$rebateAmount),'返水',$bet_id,'');
- // }
- // }
- $text = "下注期数:{$issueInfo->issue_no}\n";
- $text .= "下注内容\n";
- $text .= "--------\n";
- $text .= "{$input}\n";
- $text .= "--------\n";
- $text .= "下注成功\n";
- $msg['text'] = $text;
- // $lastStr = self::getLastChar($userInfo->first_name, 1);
- $lastStr = self::hideMiddleDigits($userInfo->member_id, 4);
- $groupText = "";
- $groupText .= "私聊下注 【" . $lastStr . "】 \n";
- $groupText .= "下注期数:{$issueInfo->issue_no} \n";
- $groupText .= "下注内容: \n";
- $groupText .= "----------- \n";
- $groupText .= "{$input} \n";
- $groupText .= "----------- \n";
- $inlineButton = self::getOperateButton();
- // 群通知
- self::bettingGroupNotice($groupText, $inlineButton); // 群通知
- return $msg;
- }
- // 模拟下注
- public static function fakeBet()
- {
- $betFake = Config::where('field', 'bet_fake')->first()->val;
- if ($betFake) {
- // 期数验证
- $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
- if ($issueInfo) {
- $betFakeRandAmount = Config::where('field', 'bet_fake_rand_amount')->first()->val;
- $betFakeRandAmount = explode(',', $betFakeRandAmount);
- $betMini = $betFakeRandAmount[0]??10;
- $betMax = $betFakeRandAmount[1]??10000;
- $now_date = date('Y-m-d H:i:s',time() + 30); // 提前30秒
- if($issueInfo['end_time'] > $now_date){
- $fake_bet_list = Cache::get('fake_bet_' . $issueInfo->issue_no, []);
- $gameplayRuleList = GameplayRuleService::model()::where('odds', '>', 0)->get();
- $gameplayRuleList = $gameplayRuleList->toArray();
- $randKey = array_rand($gameplayRuleList, 1);
- $gameplayRuleInfo = $gameplayRuleList[$randKey] ?? [];
- if ($gameplayRuleInfo) {
- $amount = rand($betMini, $betMax);
- $item = [];
- $item['keywords'] = $gameplayRuleInfo['keywords'];
- $item['odds'] = $gameplayRuleInfo['odds'];
- $item['amount'] = $amount;
- $item['first_name'] = self::generateRandomString(6);
- $item['member_id'] = self::generateRandomNumber(10);
- $item['profit'] = 0;
- $input = $item['keywords'] . $item['amount'];
- $fake_bet_list[] = $item;
- $lastStr = self::hideMiddleDigits($item['member_id'], 4);
- $groupText = "";
- $groupText .= "私聊下注 【" . $lastStr . "】 \n";
- $groupText .= "下注期数:{$issueInfo->issue_no} \n";
- $groupText .= "下注内容: \n";
- $groupText .= "----------- \n";
- $groupText .= "{$input} \n";
- $groupText .= "----------- \n";
- $inlineButton = self::getOperateButton();
- // 群通知
- self::bettingGroupNotice($groupText, $inlineButton); // 群通知
- }
- Cache::put('fake_bet_' . $issueInfo->issue_no, $fake_bet_list, 500);
- }
-
- }
- }
- }
- /**
- * @description: 当期下注
- * @param {*} $memberId
- * @return {*}
- */
- public static function currentBet($memberId)
- {
- $msg['chat_id'] = $memberId;
- // 期数验证
- $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
- $issue_no = '';
- if (!empty($issueInfo)) {
- $issue_no = $issueInfo->issue_no;
- } else {
- $issueCloseInfo = IssueService::model()::where('status', IssueService::model()::STATUS_CLOSE)->orderBy('id', 'desc')->first();
- if (empty($issueCloseInfo)) {
- $issue_no = $issueCloseInfo->issue_no;
- }
- }
- if ($issue_no) {
- $text = "当前期号:{$issue_no} \n";
- $text .= "\n";
- $text .= "----------\n";
- $list = self::findAll(['member_id' => $memberId, 'issue_no' => $issue_no]);
- foreach ($list->toArray() as $k => $v) {
- $text .= "{$v['keywords']}{$v['amount']} \n";
- }
- $text .= "\n";
- $text .= "----------\n";
- $msg['text'] = $text;
- } else {
- $msg['text'] = "当前没有开放的投注期数! \n";
- }
- return $msg;
- }
- /**
- * @description: 近期投注
- * @param {*} $memberId
- * @return {*}
- */
- public static function recentlyRecord($memberId, $page = 1, $limit = 5)
- {
- $list = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->orderBy('id', 'desc')->forPage($page, $limit)->get();
- // $text = "```\n";
- $text = "";
- $text .= "期数--内容--盈亏 \n";
- foreach ($list->toArray() as $k => $v) {
- $profit = $v['profit'] - $v['amount'];
- // $text .= $v['issue_no']." ".$v['keywords']." ".$v['amount']." ".$v['profit']."\n";
- $item = $v['issue_no'] . "==" . $v['keywords'] . rtrim(rtrim(number_format($v['amount'], 2, '.', ''), '0'), '.') . "==" . rtrim(rtrim(number_format($profit, 2, '.', ''), '0'), '.') . "\n";
- $text .= $item;
- }
- // $text .= "```\n";
- return $text;
- }
- /**
- * @description: 投注记录
- * @param {*} $memberId
- * @param {*} $page
- * @param {*} $limit
- * @return {*}
- */
- public static function record($memberId, $messageId = null, $page = 1, $limit = 5)
- {
- $type = Cache::get('message_id_bet_record_' . $memberId, 0);
- $msg['chat_id'] = $memberId;
- $list = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->where(self::getWhere(['is_winner' => $type]))->orderBy('id', 'desc')->forPage($page, $limit)->get();
- $count = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->where(self::getWhere(['is_winner' => $type]))->count();
- $keyboard = [];
- $total_amount = BalanceLogService::model()::where('member_id', $memberId)->where('change_type', '中奖')->sum('amount');
- $total_amount = number_format($total_amount, 2);
- $text = "历史注单 \n";
- $text .= "中奖总金额:{$total_amount} \n";
- foreach ($list as $k => $v) {
- if ($v->status == self::model()::STATUS_SETTLED) {
- $phase = $v->profit - $v->amount;
- } else {
- $phase = '待开奖';
- }
- $text .= "-------------------------------------\n";
- $text .= "期数:{$v->issue_no} \n";
- $text .= "内容:{$v->keywords} \n";
- $text .= "金额:{$v->amount} \n";
- $text .= "盈亏:{$phase} \n";
- }
- $msg['text'] = $text;
- $keyboard[] = [
- ['text' => "全部", 'callback_data' => "betRecordType@@0"],
- ['text' => "盈利", 'callback_data' => "betRecordType@@1"],
- ['text' => "亏损", 'callback_data' => "betRecordType@@2"]
- ];
- if ($page > 1) {
- $keyboard[] = [
- ['text' => "👆上一页", 'callback_data' => "betRecordNextPage@@" . ($page - 1)]
- ];
- }
- $allPage = ceil($count / $limit);
- if ($allPage > $page) {
- if ($page > 1) {
- $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)];
- } else {
- $keyboard[] = [
- ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)]
- ];
- }
- }
- if ($messageId) {
- $msg['message_id'] = $messageId;
- }
- if ($keyboard) {
- $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
- }
- return $msg;
- }
- /**
- * @description: 中奖结算
- * @param {*} $issue_no
- * @param {*} $awards
- * @return {*}
- */
- public static function betSettled($issue_no, $awards)
- {
- $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
- $data = [];
- $text = $issue_no . "期开奖结果 \n";
- $text .= "-----本期开奖账单----- \n";
- $bet_num = 0;
- foreach ($list->toArray() as $k => $v) {
- $userInfo = UserService::findAll(['member_id' => $v['member_id']]);
- // $lastStr = self::getLastChar($userInfo->first_name, 1);
- $lastStr = self::hideMiddleDigits($v['member_id'], 4);
- $item = [];
- $item['id'] = $v['id'];
- $item['status'] = self::model()::STATUS_SETTLED;
- if (in_array($v['keywords'], $awards)) {
- $profit = $v['amount'] * $v['odds'];
- if ($profit > 880000) {
- $profit = 880000; // 单注最高奖金880000
- }
- $item['profit'] = $profit;
- $yl = $profit - $v['amount'];
- if ($k + 1 <= 15) {
- $text .= "私聊下注 【" . $lastStr . "】 {$yl}\n";
- $bet_num++;
- }
- // 结算
- WalletService::updateBalance($v['member_id'], $profit);
- $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
- $balance = $walletInfo['available_balance'];
- BalanceLogService::addLog($v['member_id'], $profit, $balance, ($balance + $profit), '中奖', $v['id'], '');
- } else {
- if ($k + 1 <= 15) {
- $text .= "私聊下注 【******" . $lastStr . "】 -{$v['amount']}\n";
- $bet_num++;
- }
- }
- self::model()::where('id', $v['id'])->update($item);
- }
- $inlineButton = self::getOperateButton();
- $rand_num = 30 - $bet_num;
- $text .= self::fakeLotteryDraw($issue_no, $awards, $rand_num);
- // for ($i = 0; $i < $rand_num; $i++) {
- // // 生成 -100000 到 100000 的随机数,但排除 -10 到 10 的范围
- // $randomNumber = random_int(-1000000, 1000000) / 100;
- // if ($randomNumber >= -10 && $randomNumber <= 10) {
- // // 如果落在 -10 到 10 之间,重新生成或调整
- // $randomNumber = $randomNumber < 0 ? -random_int(10, 100000) : random_int(10, 100000);
- // }
- // $text .= "私聊下注 【******】 {$randomNumber}\n";
- // }
- // 群通知
- self::bettingGroupNotice($text, $inlineButton, '');
- }
- // 虚拟开奖
- public static function fakeLotteryDraw($issue_no, $awards, $rand_num = 30)
- {
- $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
- $text = "";
- foreach ($fake_bet_list as $k => $v) {
- // $lastStr = self::getLastChar($v['first_name'], 1);
- $lastStr = self::hideMiddleDigits($v['member_id'], 4);
- if (in_array($v['keywords'], $awards)) {
- $profit = $v['amount'] * $v['odds'];
- if ($profit > 880000) {
- $profit = 880000; // 单注最高奖金880000
- }
- $item['profit'] = $profit;
- $yl = $profit - $v['amount'];
- if ($k + 1 <= $rand_num) {
- $text .= "私聊下注 【" . $lastStr . "】 {$yl}\n";
- }
- } else {
- if ($k + 1 <= $rand_num) {
- $text .= "私聊下注 【" . $lastStr . "】 -{$v['amount']}\n";
- }
- }
- }
- return $text;
- }
- }
|