| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219 |
- <?php
- namespace App\Services;
- use App\Http\Controllers\admin\GameplayRule;
- use App\Models\Rebate;
- use App\Models\User;
- use App\Services\BalanceLogService;
- use App\Services\BaseService;
- use App\Models\Bet;
- use App\Models\Config;
- use App\Services\GameplayRuleService;
- use App\Services\IssueService;
- use App\Services\UserService;
- use App\Services\WalletService;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\App;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- use App\Jobs\SendTelegramMessageJob;
- use App\Jobs\SendTelegramGroupMessageJob;
- use Exception;
- /**
- * 投注
- */
- class BetService extends BaseService
- {
- public static $OTHER_BET_1 = [
- '大',
- '小',
- '单',
- '双',
- ];
- public static $OTHER_BET_2 = [
- '大单',
- '小双',
- '小单',
- '小双',
- ];
- /**
- * @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['start_time'])) {
- $where[] = ['created_at', '>=', "{$search['start_time']} 00:00:00"];
- $where[] = ['created_at', '<=', "{$search['end_time']} 23:59:59"];
- }
- if (isset($search['is_winner']) && $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);
- });
- }
- $query->orderBy('id', 'desc');
- $paginator = $query->paginate($limit);
- return ['total' => $paginator->total(), 'data' => $paginator->items()];
- }
- /**
- * @description: 投注操作
- * @param string $memberId
- * @param string $input
- * @param int $messageId
- * @return array
- */
- public static function bet(string $memberId, string $input, int $messageId = 0): array
- {
- $msg = [];
- $msg['chat_id'] = $memberId;
- // 分解投注的内容
- $betResult = GameplayRuleService::bettingRuleVerify($input);
- $serviceAccount = Config::where('field', 'service_customer')->first()->val;
- $maintenanceSwitch = Config::where('field', 'maintenance_switch')->first()->val;
- if ($maintenanceSwitch != 0) {
- $text = lang("系统维护中!") . "\n";
- $text .= lang("任何疑问都可以联系唯一客服") . ":@{$serviceAccount}";
- $msg['text'] = $text;
- if ($messageId) {
- $msg['reply_to_message_id'] = $messageId;
- }
- return $msg;
- }
- if ($betResult == null) {
- $text = lang("消息格式错误!") . "\n";
- $text .= lang("任何疑问都可以联系唯一客服") . ":@{$serviceAccount}";
- $msg['text'] = $text;
- if ($messageId) {
- $msg['reply_to_message_id'] = $messageId;
- }
- return $msg;
- }
- DB::beginTransaction();
- try {
- $text = lang('下注期数') . ":{issue_no}\n";
- $text .= lang("下注内容") . "\n";
- $text .= "--------\n";
- $errText = "";
- $success = false;
- foreach ($betResult as $item) {
- $keywords = $item['rule'];
- $amount = $item['amount'];
- $error = $issueNo = "";
- $b = static::singleNote($memberId, $keywords, $amount, $issueNo, $text, $error);
- if ($b) {
- $text = str_replace("{issue_no}", $issueNo, $text);
- $success = true;
- $text .= "\n";
- } else {
- $errText .= $error;
- $errText .= "\n";
- }
- }
- $text .= "--------\n";
- $text .= lang("下注成功");
- if (!$success) {
- $msg['text'] = $errText;
- if ($messageId) {
- $msg['reply_to_message_id'] = $messageId;
- }
- } else {
- $msg['text'] = $text;
- $msg['text'] .= "\n";
- if (!empty($errText)) {
- $msg['text'] .= "------------------------\n";
- $msg['text'] .= $errText;
- }
- }
- DB::commit();
- } catch (Exception $e) {
- DB::rollBack();
- $msg['text'] = "投注失败,请联系管理员";
- if ($messageId) {
- $msg['reply_to_message_id'] = $messageId;
- }
- }
- return $msg;
- }
- private static function singleNote($memberId, $keywords, $amount, &$issueNo, &$text, &$errText): bool
- {
- $errText .= "【{$keywords}{$amount}】\n";
- $serviceAccount = Config::where('field', 'service_customer')->first()->val;
- $gameplayRuleInfo = GameplayRuleService::getGameplayRules($keywords);
- if ($gameplayRuleInfo == null) {
- $errText .= lang("玩法未配置!") . "\n";
- $errText .= lang("任何疑问都可以联系唯一财务") . ":@{$serviceAccount}";
- return false;
- }
- if ($gameplayRuleInfo['odds'] <= 0) {
- $errText .= lang("赔率为0 庄家通吃 禁止投注!") . "\n";
- $errText .= lang("任何疑问都可以联系唯一财务") . ":@{$serviceAccount}";
- return false;
- }
- // 期数验证
- $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)) {
- $errText .= lang("暂无可下注期数,本次下注无效!");
- return false;
- } else {
- $errText .= lang("封盘中,本次下注无效!");
- return false;
- }
- }
- if (!is_numeric($amount) || $amount <= 0) {
- $errText .= lang("投注金额格式不正确!");
- $errText .= lang("任何疑问都可以联系唯一财务") . ":@{$serviceAccount}";
- return false;
- }
- $now_date = date('Y-m-d H:i:s', time() + 30); // 提前30秒
- if ($issueInfo['end_time'] < $now_date) {
- $errText .= lang("封盘中,本次下注无效!");
- return false;
- }
- // 投注限制校验
- if ($amount < $gameplayRuleInfo['mininum']) {
- $errText .= lang("下注失败,最小金额限制") . "{$gameplayRuleInfo['mininum']}\n";
- return false;
- }
- // 投注限制校验
- if ($amount > $gameplayRuleInfo['maxinum']) {
- $errText .= lang("下注失败,最大金额限制") . "{$gameplayRuleInfo['maxinum']}\n";
- return false;
- }
- // 获取用户余额
- $walletInfo = WalletService::findOne(['member_id' => $memberId]);
- $balance = $walletInfo['available_balance'];
- // 余额计算
- if ($balance < $amount) {
- $errText .= lang("余额不足,本次下注无效!\n");
- return false;
- }
- $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;
- $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
- $huishui_percentage = Config::where('field', 'huishui_percentage')->first()->val;
- $rebate = Rebate::addOrUpdate([
- 'date' => $now,
- 'member_id' => $memberId,
- 'betting_amount' => $amount,
- 'rebate_ratio' => $rebate,
- 'first_name' => $userInfo->first_name,
- 'username' => $userInfo->username,
- 'huishui_restriction' => $huishui_restriction,
- 'huishui_percentage' => $huishui_percentage,
- ]);
- if (!RebateService::BibiReturn($rebate, $amount)) {
- $errText .= lang("比比返失败");
- return false;
- }
- $text .= "{$keywords}{$amount}";
- // $lastStr = self::getLastChar($userInfo->first_name, 1);
- $lastStr = self::hideMiddleDigits($userInfo->member_id, 4);
- $issueNo = $issueInfo->issue_no;
- $groupText = lang("会员下注") . " 【" . $lastStr . "】 \n";
- $groupText .= lang('下注期数') . ":{$issueInfo->issue_no} \n";
- $groupText .= lang("下注内容") . ": \n";
- $groupText .= "----------- \n";
- $groupText .= "{$keywords}{$amount} \n";
- $groupText .= "----------- \n";
- $inlineButton = self::getOperateButton();
- // 群通知
- // self::bettingGroupNotice($groupText, $inlineButton); // 群通知
- self::asyncBettingGroupNotice($groupText, $inlineButton); // 异步群通知
- return true;
- }
- // 模拟下注
- public static function fakeBet()
- {
- $noRule = ['0操', '27操'];
- // 防止一开就虚拟投注
- $cache = Cache::get('new_issue_no');
- if ($cache) {
- return;
- }
- $maintenanceSwitch = Config::where('field', 'maintenance_switch')->first()->val;
- if ($maintenanceSwitch != 0) {
- return;
- }
- $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() + 38); // 提前45秒
- 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();
- $member_id = self::generateRandomNumber(10);
- $betTimes = rand(1, 4); // 每次下注次数
- for ($i = 0; $i < $betTimes; $i++) {
- $randKey = array_rand($gameplayRuleList, 1);
- $gameplayRuleInfo = $gameplayRuleList[$randKey] ?? [];
- if ($gameplayRuleInfo) {
- if (in_array($gameplayRuleInfo['keywords'], $noRule)) {
- return;
- }
- if ($gameplayRuleInfo['maxinum'] < $betMax) {
- $betMax = $gameplayRuleInfo['maxinum'];
- }
- if ($gameplayRuleInfo['mininum'] > $betMini) {
- $betMini = $gameplayRuleInfo['mininum'];
- }
- $amount = rand($betMini, $betMax);
- $amount = floor($amount / 10) * 10;
- $input = $gameplayRuleInfo['keywords'] . $amount;
- // $amount = number_format($amount,2);
- $item = [];
- $item['keywords'] = $gameplayRuleInfo['keywords'];
- $item['odds'] = $gameplayRuleInfo['odds'];
- $item['amount'] = $amount;
- $item['first_name'] = self::generateRandomString(6);
- $item['member_id'] = $member_id;
- $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); // 群通知
- self::asyncBettingGroupNotice($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 = lang("期数") . " {$issue_no} \n";
- // $text .= "\n";
- // $text .= "----------\n";
- $list = self::findAll(['member_id' => $memberId, 'issue_no' => $issue_no]);
- $list = $list->toArray();
- if (empty($list)) {
- $text .= lang("本期暂未下注") . "! \n";
- } else {
- $keywords = implode(',', array_column($list, 'keywords'));
- $amounts = implode(',', array_column($list, 'amount'));
- $text .= lang("下注类型") . ":[" . $keywords . "] \n";
- $text .= lang("下注金额") . ":" . $amounts . " \n";
- $text .= lang("下注总额") . ":" . array_sum(array_column($list, 'amount')) . " \n";
- $text .= lang("开奖状态") . ":" . lang("等待开奖") . " \n";
- }
- // foreach ($list->toArray() as $k => $v) {
- // $text .= "{$v['keywords']}{$v['amount']} \n";
- // }
- // $text .= "\n";
- // $text .= "----------\n";
- $msg['text'] = $text;
- } else {
- $msg['text'] = lang("当前没有开放的投注期数") . "! \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);
- if ($type == 0) {
- $type = '';
- }
- $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 = lang("历史注单") . " \n";
- $text .= lang("中奖总派彩") . ":{$total_amount} \n";
- foreach ($list as $k => $v) {
- if ($v->status == self::model()::STATUS_SETTLED) {
- $phase = $v->profit - $v->amount;
- } else {
- $phase = lang('待开奖');
- }
- $text .= "-------------------------------------\n";
- $text .= lang("期数") . ":{$v->issue_no} \n";
- $text .= lang("内容") . ":{$v->keywords} \n";
- $text .= lang("金额") . ":{$v->amount} \n";
- $text .= lang("盈亏") . ":{$phase} \n";
- }
- $msg['text'] = $text;
- $keyboard[] = [
- ['text' => lang("全部"), 'callback_data' => "betRecordType@@0"],
- ['text' => lang("盈利"), 'callback_data' => "betRecordType@@1"],
- ['text' => lang("亏损"), 'callback_data' => "betRecordType@@2"]
- ];
- if ($page > 1) {
- $keyboard[] = [
- ['text' => "👆" . lang("上一页"), 'callback_data' => "betRecordNextPage@@" . ($page - 1)]
- ];
- }
- $allPage = ceil($count / $limit);
- if ($allPage > $page) {
- if ($page > 1) {
- $keyboard[count($keyboard) - 1][] = ['text' => "👇" . lang("下一页"), 'callback_data' => "betRecordNextPage@@" . ($page + 1)];
- } else {
- $keyboard[] = [
- ['text' => "👇" . lang("下一页"), '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
- * @return {*}
- */
- public static function betFail($issue_no)
- {
- $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
- foreach ($list->toArray() as $k => $v) {
- $profit = $v['amount'];
- 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'], '');
- $text = $issue_no . "期开奖失败 \n";
- $text .= "投注类型:{$v['keywords']} \n";
- $text .= "投注金额:{$v['amount']} \n";
- $text .= "投注的资金已退回您的钱包 \n";
- self::asyncSendMessage($v['member_id'], $text);
- $item = [];
- $iem['status'] = self::model()::STATUS_SETTLED;
- self::model()::where('id', $v['id'])->update($item);
- }
- }
- /**
- * @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]);
- // 大小单双的
- $otherSum = self::model()::where('issue_no', $issue_no)->where('status', self::model()::STATUS_STAY)->whereIn('keywords', ['大', '小', '单', '双'])->sum('amount');
- $fakeOpenData = self::fakeLotteryDraw($issue_no, $awards, 0);
- $keywordsList = $fakeOpenData['keywordsList'];
- $fakeOtherSum = $fakeOpenData['sum'];
- $sum = $otherSum + $fakeOtherSum;
- $betNoticeNum = Config::where('field', 'bet_notice_num')->first()->val;
- $betNoticeNum = explode(',', $betNoticeNum);
- $betNoticeMini = $betNoticeNum[0] ?? 26;
- $betNoticeMax = $betNoticeNum[1] ?? 38;
- $noticeNum = rand($betNoticeMini, $betNoticeMax);
- $realNoticeNum = ceil($noticeNum / 2);
- $openList = [];
- $memberList = [];
- $bet_num = 0;
- foreach ($list->toArray() as $k => $v) {
- if (isset($keywordsList[$v['keywords']])) {
- $keywordsList[$v['keywords']] += $v['amount'];
- } else {
- $keywordsList[$v['keywords']] = $v['amount'];
- }
- // $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'];
- $amount = $v['amount'];
- // $amount = rtrim($amount, '0'); // 去掉右侧的 0
- // $amount = rtrim($amount, '.'); // 如果末尾是 . 就去掉
- $odds = $v['odds'];
- if (in_array('13操', $awards) || in_array('14操', $awards)) {
- // 13 14特殊处理倍率
- if (in_array($v['keywords'], self::$OTHER_BET_1)) {
- if ($sum < 10000) {
- $odds = 1.5;
- } else {
- $odds = 1;
- }
- }
- if (in_array($v['keywords'], self::$OTHER_BET_2)) {
- $odds = 1;
- }
- }
- $profit = bcmul($amount, $odds, 2); // 保留两位小数
- if ($profit > 1000000) {
- $profit = 1000000; // 单注最高奖金1000000
- }
- $item['profit'] = $profit;
- // $yl = $profit - $amount;
- $yl = bcsub($profit, $amount, 2); // 盈利
- if (!in_array('13操', $awards) && !in_array('14操', $awards)) {
- Rebate::updateProfit([
- 'member_id' => $v['member_id'],
- 'profit' => $yl,
- ]);
- }
- $memberList[$v['member_id']][] = [
- 'member_id' => $v['member_id'],
- 'keywords' => $v['keywords'],
- 'amount' => $v['amount'],
- 'profit' => $profit,
- 'yl' => $yl,
- ];
- // 结算
- 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'], '');
- if (isset($openList[$v['member_id']])) {
- $openList[$v['member_id']]['member_id'] = $v['member_id'];
- $openList[$v['member_id']]['amount'] += $v['amount'];
- $openList[$v['member_id']]['profit'] += $profit;
- $openList[$v['member_id']]['lastStr'] = $lastStr;
- $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
- $openList[$v['member_id']]['keywords'][] = $v['keywords'];
- $openList[$v['member_id']]['win_amount'] += $v['amount'];
- } else {
- $openList[$v['member_id']]['member_id'] = $v['member_id'];
- $openList[$v['member_id']]['amount'] = $v['amount'];
- $openList[$v['member_id']]['profit'] = $profit;
- $openList[$v['member_id']]['lastStr'] = $lastStr;
- $openList[$v['member_id']]['openKeywords'] = [];
- $openList[$v['member_id']]['keywords'] = [];
- $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
- $openList[$v['member_id']]['keywords'][] = $v['keywords'];
- $openList[$v['member_id']]['win_amount'] = $v['amount'];
- $openList[$v['member_id']]['is_send'] = true;
- }
- } else {
- if (!in_array('13操', $awards) && !in_array('14操', $awards)) {
- Rebate::updateProfit([
- 'member_id' => $v['member_id'],
- 'profit' => ($v['amount'] * -1),
- ]);
- }
- $profit = 0;
- if (in_array('13操', $awards) || in_array('14操', $awards)) {
- $amount = $v['amount'];
- $odds = 0;
- // 13 14特殊处理倍率
- if (in_array($v['keywords'], self::$OTHER_BET_1)) {
- if ($sum < 10000) {
- $odds = 1.5;
- } else {
- $odds = 1;
- }
- }
- if (in_array($v['keywords'], self::$OTHER_BET_2)) {
- $odds = 1;
- }
- $profit = bcmul($amount, $odds, 2); // 保留两位小数
- if ($profit > 1000000) {
- $profit = 1000000; // 单注最高奖金1000000
- }
- $item['profit'] = $profit;
- $yl = bcsub($profit, $amount, 2); // 盈利
- 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'], '');
- }
- if (isset($openList[$v['member_id']])) {
- $openList[$v['member_id']]['member_id'] = $v['member_id'];
- $openList[$v['member_id']]['amount'] += $v['amount'];
- $openList[$v['member_id']]['profit'] += $profit;
- $openList[$v['member_id']]['lastStr'] = $lastStr;
- $openList[$v['member_id']]['keywords'][] = $v['keywords'];
- } else {
- $openList[$v['member_id']]['member_id'] = $v['member_id'];
- $openList[$v['member_id']]['amount'] = $v['amount'];
- $openList[$v['member_id']]['profit'] = $profit;
- $openList[$v['member_id']]['lastStr'] = $lastStr;
- $openList[$v['member_id']]['openKeywords'] = [];
- $openList[$v['member_id']]['keywords'] = [];
- $openList[$v['member_id']]['keywords'][] = $v['keywords'];
- $openList[$v['member_id']]['win_amount'] = 0;
- $openList[$v['member_id']]['is_send'] = true;
- }
- }
- self::model()::where('id', $v['id'])->update($item);
- }
- $openList = array_merge($openList, $fakeOpenData['list']);
- self::lotteryNotice($openList, $issue_no, $keywordsList);
- }
- /**
- * @description: 中奖结算
- * @param {*} $issue_no
- * @param {*} $awards
- * @return {*}
- */
- public static function betSettled2($issue_no, $awards)
- {
- $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
- $data = [];
- $text = $issue_no . "期开奖结果 \n";
- $text .= "-----本期开奖账单----- \n";
- // $text .=" 中奖类型 用户 投注金额 中奖金额 盈亏 \n";
- $text .= "\n";
- $betNoticeNum = Config::where('field', 'bet_notice_num')->first()->val;
- $betNoticeNum = explode(',', $betNoticeNum);
- $betNoticeMini = $betNoticeNum[0] ?? 26;
- $betNoticeMax = $betNoticeNum[1] ?? 38;
- $noticeNum = rand($betNoticeMini, $betNoticeMax);
- $realNoticeNum = ceil($noticeNum / 2);
- $openList = [];
- $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'];
- $amount = $v['amount'];
- // $amount = rtrim($amount, '0'); // 去掉右侧的 0
- // $amount = rtrim($amount, '.'); // 如果末尾是 . 就去掉
- $odds = $v['odds'];
- $profit = bcmul($amount, $odds, 2); // 保留两位小数
- if ($profit > 880000) {
- $profit = 880000; // 单注最高奖金880000
- }
- $item['profit'] = $profit;
- // $yl = $profit - $amount;
- $yl = bcsub($profit, $amount, 2); // 盈利
- // if ($k + 1 <= $realNoticeNum) {
- // // $text .= "会员下注 【" . $lastStr . "】{$v['amount']} {$profit} {$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'], '');
- if (isset($openList[$v['member_id']])) {
- $openList[$v['member_id']]['member_id'] = $v['member_id'];
- $openList[$v['member_id']]['amount'] += $v['amount'];
- $openList[$v['member_id']]['profit'] += $profit;
- $openList[$v['member_id']]['lastStr'] = $lastStr;
- $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
- $openList[$v['member_id']]['keywords'][] = $v['keywords'];
- $openList[$v['member_id']]['win_amount'] += $v['amount'];
- } else {
- $openList[$v['member_id']]['member_id'] = $v['member_id'];
- $openList[$v['member_id']]['amount'] = $v['amount'];
- $openList[$v['member_id']]['profit'] = $profit;
- $openList[$v['member_id']]['lastStr'] = $lastStr;
- $openList[$v['member_id']]['openKeywords'] = [];
- $openList[$v['member_id']]['keywords'] = [];
- $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
- $openList[$v['member_id']]['keywords'][] = $v['keywords'];
- $openList[$v['member_id']]['win_amount'] = $v['amount'];
- $openList[$v['member_id']]['is_send'] = true;
- }
- } else {
- if (isset($openList[$v['member_id']])) {
- $openList[$v['member_id']]['member_id'] = $v['member_id'];
- $openList[$v['member_id']]['amount'] += $v['amount'];
- $openList[$v['member_id']]['lastStr'] = $lastStr;
- $openList[$v['member_id']]['keywords'][] = $v['keywords'];
- } else {
- $openList[$v['member_id']]['member_id'] = $v['member_id'];
- $openList[$v['member_id']]['amount'] = $v['amount'];
- $openList[$v['member_id']]['profit'] = 0;
- $openList[$v['member_id']]['lastStr'] = $lastStr;
- $openList[$v['member_id']]['openKeywords'] = [];
- $openList[$v['member_id']]['keywords'] = [];
- $openList[$v['member_id']]['keywords'][] = $v['keywords'];
- $openList[$v['member_id']]['win_amount'] = 0;
- $openList[$v['member_id']]['is_send'] = true;
- }
- // if ($k + 1 <= $realNoticeNum) {
- // // $text .= "会员下注 【" . $lastStr . "】{$v['amount']} {$v['profit']} -{$v['amount']}\n";
- // $bet_num++;
- // }
- }
- // self::model()::where('id', $v['id'])->update($item);
- }
- // foreach($openList as $k => $v){
- // $amount = $v['amount'];
- // // if($v['profit'] >= 0){
- // $profit = number_format($v['profit'],2);
- // $yl = bcsub($v['profit'], $v['amount'], 2); // 盈利
- // // $text .= "会员下注 【" . $v['lastStr'] . "】 {$amount} {$profit} {$yl}\n";
- // if(++$bet_num <= $realNoticeNum){
- // $text .= "用户ID:{$v['lastStr']} \n";
- // $text .= "下注类型:[".implode(',', $v['keywords'])."] \n";
- // $text .= "中奖类型:[".implode(',', $v['openKeywords'])."] \n";
- // $text .= "投注金额:{$amount} \n";
- // $text .= "中奖金额:{$v['win_amount']} \n";
- // $text .= "派彩金额:{$profit} \n";
- // $text .= "盈亏金额:{$yl} \n";
- // $text .= "-------------------------------- \n";
- // }
- // $text2 = "{$issue_no}期开奖结果 \n";
- // $text2 .= "下注类型:[".implode(',', $v['keywords'])."] \n";
- // $text2 .= "中奖类型:[".implode(',', $v['openKeywords'])."] \n";
- // $text2 .= "投注金额:{$amount} \n";
- // $text2 .= "中奖金额:{$v['win_amount']} \n";
- // $text2 .= "派彩金额:{$profit} \n";
- // $text2 .= "盈亏金额:{$yl} \n";
- // $keyboard = [];
- // $keyboard[] = [
- // ['text' => "开奖历史", 'callback_data' => "showLotteryHistory@@" . $issue_no]
- // ];
- // // SendTelegramMessageJob::dispatch($v['member_id'],$text2);
- // self::sendMessage($v['member_id'],$text2,$keyboard);
- // // }else{
- // // $text .= "会员下注 【" . $v['lastStr'] . "】 {$amount} 0 -{$amount}\n";
- // // }
- // }
- $inlineButton = self::getOperateButton();
- $rand_num = $noticeNum - $bet_num;
- $fakeOpenData = self::fakeLotteryDraw($issue_no, $awards, $rand_num);
- $openList = array_merge($openList, $fakeOpenData['list']);
- // 群通知
- // self::bettingGroupNotice($text, $inlineButton, '');
- // SendTelegramGroupMessageJob::dispatch($text,$inlineButton,'');
- self::lotteryNotice($openList, $issue_no);
- }
- // 虚拟开奖
- public static function fakeLotteryDraw($issue_no, $awards, $rand_num = 30)
- {
- $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
- $text = "";
- $keywordsList = [];
- $fakeOtherSum = 0;
- $openList = [];
- foreach ($fake_bet_list as $k => $v) {
- if (in_array($v['keywords'], ['大', '小', '单', '双'])) {
- $fakeOtherSum += $v['amount'];
- }
- if (isset($keywordsList[$v['keywords']])) {
- $keywordsList[$v['keywords']] += $v['amount'];
- } else {
- $keywordsList[$v['keywords']] = $v['amount'];
- }
- // $lastStr = self::getLastChar($v['first_name'], 1);
- $lastStr = self::hideMiddleDigits($v['member_id'], 4);
- if (in_array($v['keywords'], $awards)) {
- $amount = $v['amount'];
- $odds = $v['odds'];
- $profit = bcmul($amount, $odds, 2); // 保留两位小数
- if ($profit > 880000) {
- $profit = 880000; // 单注最高奖金880000
- }
- $item['profit'] = $profit;
- // $v['amount'] = number_format($amount,2);
- if (isset($openList[$v['member_id']])) {
- $openList[$v['member_id']]['member_id'] = $v['member_id'];
- $openList[$v['member_id']]['amount'] += $v['amount'];
- $openList[$v['member_id']]['profit'] += $profit;
- $openList[$v['member_id']]['lastStr'] = $lastStr;
- $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
- $openList[$v['member_id']]['keywords'][] = $v['keywords'];
- $openList[$v['member_id']]['win_amount'] += $v['amount'];
- } else {
- $openList[$v['member_id']]['member_id'] = $v['member_id'];
- $openList[$v['member_id']]['amount'] = $v['amount'];
- $openList[$v['member_id']]['profit'] = $profit;
- $openList[$v['member_id']]['lastStr'] = $lastStr;
- $openList[$v['member_id']]['openKeywords'] = [];
- $openList[$v['member_id']]['keywords'] = [];
- $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
- $openList[$v['member_id']]['keywords'][] = $v['keywords'];
- $openList[$v['member_id']]['win_amount'] = $v['amount'];
- $openList[$v['member_id']]['is_send'] = false;
- }
- } else {
- if (isset($openList[$v['member_id']])) {
- $openList[$v['member_id']]['member_id'] = $v['member_id'];
- $openList[$v['member_id']]['amount'] += $v['amount'];
- $openList[$v['member_id']]['lastStr'] = $lastStr;
- $openList[$v['member_id']]['keywords'][] = $v['keywords'];
- } else {
- $openList[$v['member_id']]['member_id'] = $v['member_id'];
- $openList[$v['member_id']]['amount'] = $v['amount'];
- $openList[$v['member_id']]['profit'] = 0;
- $openList[$v['member_id']]['lastStr'] = $lastStr;
- $openList[$v['member_id']]['openKeywords'] = [];
- $openList[$v['member_id']]['keywords'] = [];
- $openList[$v['member_id']]['keywords'][] = $v['keywords'];
- $openList[$v['member_id']]['win_amount'] = 0;
- $openList[$v['member_id']]['is_send'] = false;
- }
- }
- }
- return ['sum' => $fakeOtherSum, 'list' => $openList, 'keywordsList' => $keywordsList];
- }
- // 开奖通知
- public static function lotteryNotice($openList, $issue_no, $keywordsList = [])
- {
- $betNoticeNum = Config::where('field', 'bet_notice_num')->first()->val;
- $betNoticeNum = explode(',', $betNoticeNum);
- $betNoticeMini = $betNoticeNum[0] ?? 26;
- $betNoticeMax = $betNoticeNum[1] ?? 38;
- $noticeNum = rand($betNoticeMini, $betNoticeMax);
- // shuffle($openList);
- // Log::error('lotteryNotice openList', $openList);
- $text = "{$issue_no}期开奖结果";
- $text .= "\n-----本期开奖账单----- \n";
- foreach ($openList as $k => $v) {
- $amount = number_format($v['amount'], 2);
- $v['win_amount'] = number_format($v['win_amount'], 2);
- $profit = number_format($v['profit'], 2);
- $yl = bcsub($v['profit'], $v['amount'], 2); // 盈利
- if (empty($v['openKeywords'])) {
- $openKeyword = '-';
- } else {
- $openKeyword = implode(',', $v['openKeywords']);
- }
- if (($k + 1) <= $noticeNum) {
- $text .= "用户ID:{$v['lastStr']} \n";
- $text .= "下注类型:[" . implode(',', $v['keywords']) . "] \n";
- $text .= "中奖类型:[" . $openKeyword . "] \n";
- $text .= "投注金额:{$amount} \n";
- $text .= "中奖金额:{$v['win_amount']} \n";
- $text .= "派彩金额:{$profit} \n";
- $text .= "盈亏金额:{$yl} \n";
- $text .= "-------------------------------- \n";
- }
- if ($v['is_send']) {
- $language = User::where('member_id', $v['member_id'])->first()->language;
- App::setLocale($language);
- $text2 = lang("{issue_no}期开奖结果");
- $text2 = str_replace("{issue_no}", $issue_no, $text2);
- $text2 .= "\n";
- $text2 .= lang('下注类型') . ":[" . implode(',', $v['keywords']) . "] \n";
- $text2 .= lang('中奖类型') . ":[" . $openKeyword . "] \n";
- $text2 .= lang('投注金额') . ":{$amount} \n";
- $text2 .= lang('中奖金额') . ":{$v['win_amount']} \n";
- $text2 .= lang('派彩金额') . ":{$profit} \n";
- $text2 .= lang("盈亏金额") . ":{$yl} \n";
- $keyboard = [];
- $keyboard[] = [
- ['text' => lang("开奖历史"), 'callback_data' => "showLotteryHistory@@" . $issue_no]
- ];
- // self::sendMessage($v['member_id'],$text2,$keyboard);
- SendTelegramMessageJob::dispatch($v['member_id'], $text2, $keyboard);
- }
- }
- $inlineButton = self::getOperateButton();
- // 群通知
- // self::bettingGroupNotice($text, $inlineButton, '');
- // Log::error('lotteryNotice Group:'.$text);
- // $pos = strrpos($text, '--------------------------------');
- //
- // if ($pos !== false) {
- // // 使用 substr_replace 来删除最后一个 "ne"
- // $text = substr_replace($text, "", $pos, strlen('--------------------------------'));
- // }
- SendTelegramGroupMessageJob::dispatch($text, $inlineButton, '', false, '--------------------------------');
- }
- /**
- * @description: 统计投注情况通知
- * @param {*} $issue_no
- * @return {*}
- */
- public static function statNotice($issue_no)
- {
- $keywordsList = [];
- // 虚拟投注情况
- $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
- foreach ($fake_bet_list as $k => $v) {
- if (isset($keywordsList[$v['keywords']])) {
- $keywordsList[$v['keywords']] += $v['amount'];
- } else {
- $keywordsList[$v['keywords']] = $v['amount'];
- }
- }
- // 真实投注
- $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
- foreach ($list->toArray() as $k => $v) {
- if (isset($keywordsList[$v['keywords']])) {
- $keywordsList[$v['keywords']] += $v['amount'];
- } else {
- $keywordsList[$v['keywords']] = $v['amount'];
- }
- }
- $text3 = "📝 {$issue_no}期投注统计 \n";
- if ($keywordsList) {
- ksort($keywordsList);
- foreach ($keywordsList as $k => $v) {
- $text3 .= "玩法:{$k} \n";
- $text3 .= "总注:{$v} \n";
- $text3 .= "-------------- \n";
- }
- }
- $inlineButton = self::getOperateButton();
- SendTelegramGroupMessageJob::dispatch($text3, $inlineButton, '');
- }
- public static function todayExchangeRate($chatId)
- {
- $exchangeRate = Config::where('field', 'exchange_rate_rmb')->first()->val;
- $text = lang("今日汇率") . ":1USDT = {$exchangeRate} RMB \n";
- // $botMsg = [
- // 'chat_id' => "@{$chatId}",
- // 'text' => $text
- // ];
- self::sendMessage($chatId, $text);
- // return $botMsg;
- }
- }
|