BetService.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. <?php
  2. namespace App\Services;
  3. use App\Models\PcIssue;
  4. use App\Models\Rebate;
  5. use App\Models\User;
  6. use App\Models\Bet;
  7. use App\Models\Config;
  8. use Illuminate\Support\Facades\App;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Cache;
  11. use App\Jobs\SendTelegramMessageJob;
  12. use App\Jobs\SendTelegramGroupMessageJob;
  13. use Exception;
  14. use Illuminate\Support\Facades\Log;
  15. /**
  16. * 投注
  17. */
  18. class BetService extends BaseService
  19. {
  20. public static $OTHER_BET_1 = [
  21. '大',
  22. '小',
  23. '单',
  24. '双',
  25. ];
  26. public static $OTHER_BET_2 = [
  27. '大单',
  28. '小双',
  29. '小单',
  30. '小双',
  31. ];
  32. /**
  33. * @description: 模型
  34. * @return {string}
  35. */
  36. public static function model(): string
  37. {
  38. return Bet::class;
  39. }
  40. /**
  41. * @description: 枚举
  42. * @return {*}
  43. */
  44. public static function enum(): string
  45. {
  46. return '';
  47. }
  48. /**
  49. * @description: 获取查询条件
  50. * @param {array} $search 查询内容
  51. * @return {array}
  52. */
  53. public static function getWhere(array $search = []): array
  54. {
  55. $where = [];
  56. if (isset($search['issue_no']) && !empty($search['issue_no'])) {
  57. $where[] = ['issue_no', '=', $search['issue_no']];
  58. }
  59. if (isset($search['member_id']) && !empty($search['member_id'])) {
  60. $where[] = ['member_id', '=', $search['member_id']];
  61. }
  62. if (isset($search['keywords']) && !empty($search['keywords'])) {
  63. $where[] = ['keywords', '=', $search['keywords']];
  64. }
  65. if (isset($search['issue_id']) && !empty($search['issue_id'])) {
  66. $where[] = ['issue_id', '=', $search['issue_id']];
  67. }
  68. if (isset($search['id']) && !empty($search['id'])) {
  69. $where[] = ['id', '=', $search['id']];
  70. }
  71. if (isset($search['user_id']) && !empty($search['user_id'])) {
  72. $where[] = ['user_id', '=', $search['user_id']];
  73. }
  74. if (isset($search['start_time']) && !empty($search['start_time'])) {
  75. $where[] = ['created_at', '>=', "{$search['start_time']} 00:00:00"];
  76. $where[] = ['created_at', '<=', "{$search['end_time']} 23:59:59"];
  77. }
  78. if (isset($search['is_winner']) && $search['is_winner'] != '') {
  79. $where[] = ['status', '=', 2];
  80. if ($search['is_winner'] == 1) {
  81. $where[] = ['profit', '>', 0];
  82. } else {
  83. $where[] = ['profit', '<=', 0];
  84. }
  85. } else {
  86. if (isset($search['status']) && !empty($search['status'])) {
  87. $where[] = ['status', '=', $search['status']];
  88. }
  89. }
  90. return $where;
  91. }
  92. /**
  93. * @description: 查询单条数据
  94. * @param array $search
  95. * @return \App\Models\Coin|null
  96. */
  97. public static function findOne(array $search): ?Bet
  98. {
  99. return self::model()::where(self::getWhere($search))->first();
  100. }
  101. /**
  102. * @description: 查询所有数据
  103. * @param array $search
  104. * @return \Illuminate\Database\Eloquent\Collection
  105. */
  106. public static function findAll(array $search = [])
  107. {
  108. return self::model()::where(self::getWhere($search))->get();
  109. }
  110. /**
  111. * @description: 分页查询
  112. * @param array $search
  113. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  114. */
  115. public static function paginate(array $search = [])
  116. {
  117. $limit = isset($search['limit']) ? $search['limit'] : 15;
  118. $query = self::model()::where(self::getWhere($search))
  119. ->with(['user']);
  120. if (isset($search['username']) && !empty($search['username'])) {
  121. $username = $search['username'];
  122. $query = $query->whereHas('user', function ($query) use ($username) {
  123. $query->where('username', $username);
  124. });
  125. }
  126. $query->orderBy('id', 'desc');
  127. $paginator = $query->paginate($limit);
  128. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  129. }
  130. /**
  131. * @description: 投注操作
  132. * @param string $memberId
  133. * @param string $input
  134. * @param int $messageId
  135. * @return array
  136. */
  137. public static function bet(string $memberId, string $input, int $messageId = 0): array
  138. {
  139. $msg = [];
  140. $msg['chat_id'] = $memberId;
  141. // 分解投注的内容
  142. $betResult = GameplayRuleService::bettingRuleVerify($input);
  143. $serviceAccount = Config::where('field', 'service_customer')->first()->val;
  144. $maintenanceSwitch = Config::where('field', 'maintenance_switch')->first()->val;
  145. if ($maintenanceSwitch != 0) {
  146. $text = lang("系统维护中") . "\n";
  147. $text .= lang("任何疑问都可以联系唯一客服") . ":@{$serviceAccount}";
  148. $msg['text'] = $text;
  149. if ($messageId) {
  150. $msg['reply_to_message_id'] = $messageId;
  151. }
  152. return $msg;
  153. }
  154. if ($betResult == null) {
  155. $text = lang("消息格式错误!") . "\n";
  156. $text .= lang("任何疑问都可以联系唯一客服") . ":@{$serviceAccount}";
  157. $msg['text'] = $text;
  158. if ($messageId) {
  159. $msg['reply_to_message_id'] = $messageId;
  160. }
  161. return $msg;
  162. }
  163. DB::beginTransaction();
  164. try {
  165. $text = lang('下注期数') . ":{issue_no}\n";
  166. $text .= lang("下注内容") . "\n";
  167. $text .= "--------\n";
  168. $errText = "";
  169. $success = false;
  170. $sumAmount = 0;
  171. $groupTexts = [];
  172. $groupText = "";
  173. foreach ($betResult as $index =>$item) {
  174. $keywords = $item['rule'];
  175. $amount = $item['amount'];
  176. $error = $issueNo = "";
  177. $b = static::singleNote($memberId, $keywords, $amount, $issueNo, $text, $error, $groupText,$index);
  178. if ($b) {
  179. $sumAmount += $amount;
  180. $text = str_replace("{issue_no}", $issueNo, $text);
  181. $success = true;
  182. $text .= "\n";
  183. } else {
  184. $errText .= $error;
  185. $errText .= "\n";
  186. }
  187. }
  188. $sumAmount = number_format($sumAmount, 2);
  189. $text .= "--------\n";
  190. $text .= lang("合计金额") . ":{$sumAmount}\n";
  191. $text .= lang("下注成功");
  192. if (!$success) {
  193. $msg['text'] = $errText;
  194. if ($messageId) {
  195. $msg['reply_to_message_id'] = $messageId;
  196. }
  197. } else {
  198. $msg['text'] = $text;
  199. $msg['text'] .= "\n";
  200. if (!empty($errText)) {
  201. $msg['text'] .= "------------------------\n";
  202. $msg['text'] .= $errText;
  203. }
  204. }
  205. // 群通知
  206. $inlineButton = self::getOperateButton();
  207. $groupText.="\n--------\n";
  208. self::asyncBettingGroupNotice($groupText, $inlineButton); // 异步群通知
  209. DB::commit();
  210. } catch (Exception $e) {
  211. DB::rollBack();
  212. Log::error('错误信息', [
  213. 'message' => $e->getMessage(),
  214. 'file' => $e->getFile(),
  215. 'line' => $e->getLine(),
  216. 'trace' => $e->getTraceAsString()
  217. ]);
  218. $msg['text'] = "投注失败,请联系管理员";
  219. if ($messageId) {
  220. $msg['reply_to_message_id'] = $messageId;
  221. }
  222. }
  223. return $msg;
  224. }
  225. private static function singleNote($memberId, $keywords, $amount, &$issueNo, &$text, &$errText, &$groupText,$index): bool
  226. {
  227. $errText .= "【{$keywords}{$amount}】\n";
  228. //客服号
  229. $serviceAccount = Config::where('field', 'service_customer')->first()->val;
  230. $gameplayRuleInfo = GameplayRuleService::getGameplayRules($keywords);
  231. if ($gameplayRuleInfo == null) {
  232. $errText .= lang("玩法未配置!") . "\n";
  233. $errText .= lang("任何疑问都可以联系唯一财务") . ":@{$serviceAccount}";
  234. return false;
  235. }
  236. if ($gameplayRuleInfo['odds'] <= 0) {
  237. $errText .= lang("赔率为0 庄家通吃 禁止投注!") . "\n";
  238. $errText .= lang("任何疑问都可以联系唯一财务") . ":@{$serviceAccount}";
  239. return false;
  240. }
  241. // 期数验证
  242. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  243. if ($pc28Switch == 1) {
  244. $issueInfo = PcIssue::where('status', PcIssue::STATUS_BETTING)->orderBy('id', 'desc')->first();
  245. } else {
  246. $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
  247. }
  248. if (empty($issueInfo)) {
  249. if ($pc28Switch == 1) {
  250. $issueCloseInfo = PcIssue::where('status', PcIssue::STATUS_CLOSE)->orderBy('id', 'desc')->first();
  251. } else {
  252. $issueCloseInfo = IssueService::model()::where('status', IssueService::model()::STATUS_CLOSE)->orderBy('id', 'desc')->first();
  253. }
  254. if (empty($issueCloseInfo)) {
  255. $errText .= lang("暂无可下注期数,本次下注无效!");
  256. return false;
  257. } else {
  258. $errText .= lang("封盘中,本次下注无效!");
  259. return false;
  260. }
  261. }
  262. if (!is_numeric($amount) || $amount <= 0) {
  263. $errText .= lang("投注金额格式不正确!");
  264. $errText .= lang("任何疑问都可以联系唯一财务") . ":@{$serviceAccount}";
  265. return false;
  266. }
  267. $now_date = date('Y-m-d H:i:s', time() + 30); // 提前30秒
  268. if ($issueInfo['end_time'] < $now_date) {
  269. $errText .= lang("封盘中,本次下注无效!");
  270. return false;
  271. }
  272. // 投注限制校验
  273. if ($amount < $gameplayRuleInfo['mininum']) {
  274. $errText .= lang("下注失败,最小金额限制") . "{$gameplayRuleInfo['mininum']}\n";
  275. return false;
  276. }
  277. // 投注限制校验
  278. if ($amount > $gameplayRuleInfo['maxinum']) {
  279. $errText .= lang("下注失败,最大金额限制") . "{$gameplayRuleInfo['maxinum']}\n";
  280. return false;
  281. }
  282. // 获取用户余额
  283. $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  284. $balance = $walletInfo['available_balance'];
  285. // 余额计算
  286. if ($balance < $amount) {
  287. $errText .= lang("余额不足,本次下注无效!\n");
  288. return false;
  289. }
  290. $userInfo = UserService::findOne(['member_id' => $memberId]);
  291. $betInfo = self::findOne(['member_id' => $memberId, 'issue_no' => $issueInfo->issue_no, 'keywords' => $keywords]); // 相同下注
  292. if ($betInfo) {
  293. $betInfo->amount = $betInfo->amount + $amount;
  294. $bet_id = $betInfo->id;
  295. $betInfo->save();
  296. } else {
  297. $data = [];
  298. $data['amount'] = $amount; // 分数
  299. $data['keywords'] = $keywords; // 玩法
  300. $data['member_id'] = $memberId;
  301. $data['user_id'] = $userInfo->id;
  302. $data['issue_no'] = $issueInfo->issue_no;
  303. $data['issue_id'] = $issueInfo->id;
  304. $data['odds'] = $gameplayRuleInfo['odds'];
  305. $newBet = self::model()::create($data);
  306. $bet_id = $newBet->id;
  307. }
  308. WalletService::updateBalance($memberId, -$amount);
  309. BalanceLogService::addLog($memberId, -$amount, $balance, ($balance - $amount), '投注', $bet_id, '');
  310. //记录反水
  311. $rebate = Rebate::addOrUpdate([
  312. 'member_id' => $memberId,
  313. 'betting_amount' => $amount,
  314. 'first_name' => $userInfo->first_name,
  315. 'username' => $userInfo->username,
  316. ]);
  317. if (!RebateService::BibiReturn($rebate, $amount)) {
  318. $errText .= lang("比比返失败");
  319. return false;
  320. }
  321. $text .= "{$keywords} {$amount}";
  322. // $lastStr = self::getLastChar($userInfo->first_name, 1);
  323. $lastStr = self::hideMiddleDigits($userInfo->member_id, 4);
  324. $issueNo = $issueInfo->issue_no;
  325. $lang = App::getLocale();
  326. $group_language = Config::where('field', 'group_language')->first()->val;
  327. App::setLocale($group_language);
  328. if($index ===0){
  329. $groupText .= lang("第") . "{$issueNo}" . lang("期") . "\n";
  330. $groupText .= lang("开奖时间") . ":" . date("H:i:s", strtotime($issueInfo['end_time'])) . "\n";
  331. $groupText .= lang('投注截止') . ":" . date("H:i:s", (strtotime($issueInfo['end_time'])) - 30) . "\n";;
  332. $groupText.=lang("成功")."\n";
  333. $groupText .= lang('玩家') . ": 【{$lastStr}】 \n";
  334. $groupText .= "{$keywords} {$amount} ({$gameplayRuleInfo['odds']}".lang("倍率").") ";
  335. }else{
  336. $groupText .= "{$keywords} {$amount} ({$gameplayRuleInfo['odds']}".lang("倍率").") ";
  337. }
  338. App::setLocale($lang);
  339. // $inlineButton = self::getOperateButton();
  340. // 群通知
  341. // self::asyncBettingGroupNotice($groupText, $inlineButton); // 异步群通知
  342. return true;
  343. }
  344. //随机虚拟下注
  345. public static function randomVirtualBetting($maxPeople = 1): void
  346. {
  347. $maxPeople = intval($maxPeople);
  348. $maxPeople = max($maxPeople, 1);
  349. $maxPeople = min($maxPeople, 20);
  350. $num = rand(1, $maxPeople);
  351. for ($i = 0; $i < $num; $i++) {
  352. static::fakeBet(4);
  353. }
  354. }
  355. // 模拟下注
  356. private static function fakeBet($betNumber = 1): void
  357. {
  358. $noRule = ['0操', '27操'];
  359. // 防止一开就虚拟投注
  360. $cache = Cache::get('new_issue_no');
  361. if ($cache) {
  362. return;
  363. }
  364. $maintenanceSwitch = Config::where('field', 'maintenance_switch')->first()->val;
  365. if ($maintenanceSwitch != 0) {
  366. return;
  367. }
  368. $betFake = Config::where('field', 'bet_fake')->first()->val;
  369. if ($betFake) {
  370. // 期数验证
  371. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  372. if ($pc28Switch == 1) {
  373. $issueInfo = PcIssue::where('status', PcIssue::STATUS_BETTING)->orderBy('id', 'desc')->first();
  374. } else {
  375. $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
  376. }
  377. if ($issueInfo) {
  378. $betFakeRandAmount = Config::where('field', 'bet_fake_rand_amount')->first()->val;
  379. $betFakeRandAmount = explode(',', $betFakeRandAmount);
  380. $betMini = $betFakeRandAmount[0] ?? 10;
  381. $betMax = $betFakeRandAmount[1] ?? 10000;
  382. $now_date = date('Y-m-d H:i:s', time() + 38); // 提前45秒
  383. if ($issueInfo['end_time'] > $now_date) {
  384. $fake_bet_list = Cache::get('fake_bet_' . $issueInfo->issue_no, []);
  385. $gameplayRuleList = GameplayRuleService::model()::where('odds', '>', 0)->get();
  386. $gameplayRuleList = $gameplayRuleList->toArray();
  387. $member_id = self::generateRandomNumber(10);
  388. $betTimes = rand(1, $betNumber); // 每次下注次数
  389. Cache::add("封盘后下注单数_{$issueInfo->issue_no}", rand(3, 5), 4);
  390. $lastStr = self::hideMiddleDigits($member_id, 4);
  391. $lang = App::getLocale();
  392. $group_language = Config::where('field', 'group_language')->first()->val;
  393. $first_name = self::generateRandomString(6);
  394. App::setLocale($group_language);
  395. $groupText = lang("第") . "{$issueInfo->issue_no}" . lang("期") . "\n";
  396. $groupText .= lang("开奖时间") . ":" . date("H:i:s", strtotime($issueInfo['end_time'])) . "\n";
  397. $groupText .= lang('投注截止') . ":" . date("H:i:s", (strtotime($issueInfo['end_time'])) - 30) . "\n";;
  398. $groupText.=lang("成功")."\n";
  399. $groupText .= lang('玩家') . ": 【{$lastStr}】 \n";
  400. App::setLocale($lang);
  401. for ($i = 0; $i < $betTimes; $i++) {
  402. if (strtotime($issueInfo['end_time']) - time() < IssueService::COUNTDOWN_TO_CLOSING_THE_MARKET) {
  403. $fake_bet_count = Cache::get("fake_bet_count_{$issueInfo->issue_no}", 0);
  404. $cc = Cache::get("封盘后下注单数_{$issueInfo->issue_no}");
  405. if ($fake_bet_count >= $cc) return;
  406. }
  407. $randKey = array_rand($gameplayRuleList, 1);
  408. $gameplayRuleInfo = $gameplayRuleList[$randKey] ?? [];
  409. if ($gameplayRuleInfo) {
  410. if (in_array($gameplayRuleInfo['keywords'], $noRule)) {
  411. return;
  412. }
  413. if ($gameplayRuleInfo['maxinum'] < $betMax) {
  414. $betMax = $gameplayRuleInfo['maxinum'];
  415. }
  416. if ($gameplayRuleInfo['mininum'] > $betMini) {
  417. $betMini = $gameplayRuleInfo['mininum'];
  418. }
  419. $amount = rand($betMini, $betMax);
  420. $amount = floor($amount / 10) * 10;
  421. $input = $gameplayRuleInfo['keywords'] . ' ' . $amount;
  422. // $amount = number_format($amount,2);
  423. $item = [];
  424. $item['keywords'] = $gameplayRuleInfo['keywords'];
  425. $item['odds'] = floatval($gameplayRuleInfo['odds']);
  426. $item['amount'] = $amount;
  427. $item['first_name'] = $first_name;
  428. $item['member_id'] = $member_id;
  429. $item['profit'] = 0;
  430. // $input = $item['keywords'] . $item['amount'];
  431. $fake_bet_list[] = $item;
  432. $groupText .= "{$input} ({$item['odds']}".lang("倍率").") ";
  433. if (strtotime($issueInfo['end_time']) - strtotime($now_date) < 22) {
  434. $fake_bet_count = Cache::get("fake_bet_count_{$issueInfo->issue_no}", 0);
  435. Cache::put("fake_bet_count_{$issueInfo->issue_no}", $fake_bet_count + 1, 500);
  436. }
  437. }
  438. }
  439. if($groupText!=""){
  440. $groupText .= "\n-------- \n";
  441. $inlineButton = self::getOperateButton();
  442. // 群通知 暂停2秒再发送,避免同时发送多个消息而超过 Telegram 的频率限制
  443. self::asyncBettingGroupNotice($groupText, $inlineButton); // 异步群通知
  444. sleep(2);
  445. }
  446. Cache::put('fake_bet_' . $issueInfo->issue_no, $fake_bet_list, 500);
  447. }
  448. }
  449. }
  450. }
  451. /**
  452. * @description: 当期下注
  453. * @param {*} $memberId
  454. * @return {*}
  455. */
  456. public static function currentBet($memberId)
  457. {
  458. $msg['chat_id'] = $memberId;
  459. // 期数验证
  460. $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
  461. $issue_no = '';
  462. if (!empty($issueInfo)) {
  463. $issue_no = $issueInfo->issue_no;
  464. } else {
  465. $issueCloseInfo = IssueService::model()::where('status', IssueService::model()::STATUS_CLOSE)->orderBy('id', 'desc')->first();
  466. if (empty($issueCloseInfo)) {
  467. $issue_no = $issueCloseInfo->issue_no;
  468. }
  469. }
  470. if ($issue_no) {
  471. $text = lang("期数") . " {$issue_no} \n";
  472. // $text .= "\n";
  473. // $text .= "----------\n";
  474. $list = self::findAll(['member_id' => $memberId, 'issue_no' => $issue_no]);
  475. $list = $list->toArray();
  476. if (empty($list)) {
  477. $text .= lang("本期暂未下注") . "! \n";
  478. } else {
  479. $keywords = implode(',', array_column($list, 'keywords'));
  480. $amounts = implode(',', array_column($list, 'amount'));
  481. $text .= lang("下注类型") . ":[" . $keywords . "] \n";
  482. $text .= lang("下注金额") . ":" . $amounts . " \n";
  483. $text .= lang("下注总额") . ":" . array_sum(array_column($list, 'amount')) . " \n";
  484. $text .= lang("开奖状态") . ":" . lang("等待开奖") . " \n";
  485. }
  486. // foreach ($list->toArray() as $k => $v) {
  487. // $text .= "{$v['keywords']}{$v['amount']} \n";
  488. // }
  489. // $text .= "\n";
  490. // $text .= "----------\n";
  491. $msg['text'] = $text;
  492. } else {
  493. $msg['text'] = lang("当前没有开放的投注期数") . "! \n";
  494. }
  495. return $msg;
  496. }
  497. /**
  498. * @description: 近期投注
  499. * @param {*} $memberId
  500. * @return {*}
  501. */
  502. public static function recentlyRecord($memberId, $page = 1, $limit = 5)
  503. {
  504. $list = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->orderBy('id', 'desc')->forPage($page, $limit)->get();
  505. // $text = "```\n";
  506. $text = "";
  507. $text .= "期数--内容--盈亏 \n";
  508. foreach ($list->toArray() as $k => $v) {
  509. $profit = $v['profit'] - $v['amount'];
  510. // $text .= $v['issue_no']." ".$v['keywords']." ".$v['amount']." ".$v['profit']."\n";
  511. $item = $v['issue_no'] . "==" . $v['keywords'] . rtrim(rtrim(number_format($v['amount'], 2, '.', ''), '0'), '.') . "==" . rtrim(rtrim(number_format($profit, 2, '.', ''), '0'), '.') . "\n";
  512. $text .= $item;
  513. }
  514. // $text .= "```\n";
  515. return $text;
  516. }
  517. /**
  518. * @description: 投注记录
  519. * @param {*} $memberId
  520. * @param {*} $page
  521. * @param {*} $limit
  522. * @return {*}
  523. */
  524. public static function record($memberId, $messageId = null, $page = 1, $limit = 5)
  525. {
  526. $type = Cache::get('message_id_bet_record_' . $memberId, 0);
  527. if ($type == 0) {
  528. $type = '';
  529. }
  530. $msg['chat_id'] = $memberId;
  531. $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();
  532. $count = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->where(self::getWhere(['is_winner' => $type]))->count();
  533. $keyboard = [];
  534. $total_amount = BalanceLogService::model()::where('member_id', $memberId)->where('change_type', '中奖')->sum('amount');
  535. $total_amount = number_format($total_amount, 2);
  536. $text = lang("历史注单") . " \n";
  537. $text .= lang("中奖总派彩") . ":{$total_amount} \n";
  538. foreach ($list as $k => $v) {
  539. if ($v->status == self::model()::STATUS_SETTLED) {
  540. $phase = $v->profit - $v->amount;
  541. } else {
  542. $phase = lang('待开奖');
  543. }
  544. $text .= "-------------------------------------\n";
  545. $text .= lang("期数") . ":{$v->issue_no} \n";
  546. $text .= lang("内容") . ":{$v->keywords} \n";
  547. $text .= lang("金额") . ":{$v->amount} \n";
  548. $text .= lang("盈亏") . ":{$phase} \n";
  549. }
  550. $msg['text'] = $text;
  551. $keyboard[] = [
  552. ['text' => lang("全部"), 'callback_data' => "betRecordType@@0"],
  553. ['text' => lang("盈利"), 'callback_data' => "betRecordType@@1"],
  554. ['text' => lang("亏损"), 'callback_data' => "betRecordType@@2"]
  555. ];
  556. if ($page > 1) {
  557. $keyboard[] = [
  558. ['text' => "👆" . lang("上一页"), 'callback_data' => "betRecordNextPage@@" . ($page - 1)]
  559. ];
  560. }
  561. $allPage = ceil($count / $limit);
  562. if ($allPage > $page) {
  563. if ($page > 1) {
  564. $keyboard[count($keyboard) - 1][] = ['text' => "👇" . lang("下一页"), 'callback_data' => "betRecordNextPage@@" . ($page + 1)];
  565. } else {
  566. $keyboard[] = [
  567. ['text' => "👇" . lang("下一页"), 'callback_data' => "betRecordNextPage@@" . ($page + 1)]
  568. ];
  569. }
  570. }
  571. if ($messageId) {
  572. $msg['message_id'] = $messageId;
  573. }
  574. if ($keyboard) {
  575. $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  576. }
  577. return $msg;
  578. }
  579. /**
  580. * @description: 开奖失败退回投注的
  581. * @param {*} $issue_no
  582. * @return {*}
  583. */
  584. public static function betFail($issue_no)
  585. {
  586. $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
  587. foreach ($list->toArray() as $k => $v) {
  588. $profit = $v['amount'];
  589. WalletService::updateBalance($v['member_id'], $profit);
  590. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  591. $balance = $walletInfo['available_balance'];
  592. BalanceLogService::addLog($v['member_id'], $profit, $balance, ($balance + $profit), '中奖', $v['id'], '');
  593. $text = $issue_no . "期开奖失败 \n";
  594. $text .= "投注类型:{$v['keywords']} \n";
  595. $text .= "投注金额:{$v['amount']} \n";
  596. $text .= "投注的资金已退回您的钱包 \n";
  597. self::asyncSendMessage($v['member_id'], $text);
  598. $item = [];
  599. $iem['status'] = self::model()::STATUS_SETTLED;
  600. self::model()::where('id', $v['id'])->update($item);
  601. }
  602. }
  603. /**
  604. * @description: 中奖结算
  605. * @param {*} $issue_no
  606. * @param {*} $awards
  607. * @return {*}
  608. */
  609. public static function betSettled($issue_no, $awards)
  610. {
  611. $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
  612. // 大小单双的
  613. $otherSum = self::model()::where('issue_no', $issue_no)->where('status', self::model()::STATUS_STAY)->whereIn('keywords', ['大', '小', '单', '双'])->sum('amount');
  614. $fakeOpenData = self::fakeLotteryDraw($issue_no, $awards, 0);
  615. $keywordsList = $fakeOpenData['keywordsList'];
  616. $fakeOtherSum = $fakeOpenData['sum'];
  617. $sum = $otherSum + $fakeOtherSum;
  618. $betNoticeNum = Config::where('field', 'bet_notice_num')->first()->val;
  619. $betNoticeNum = explode(',', $betNoticeNum);
  620. $betNoticeMini = $betNoticeNum[0] ?? 26;
  621. $betNoticeMax = $betNoticeNum[1] ?? 38;
  622. $noticeNum = rand($betNoticeMini, $betNoticeMax);
  623. $realNoticeNum = ceil($noticeNum / 2);
  624. $openList = [];
  625. $memberList = [];
  626. $bet_num = 0;
  627. foreach ($list->toArray() as $k => $v) {
  628. if (isset($keywordsList[$v['keywords']])) {
  629. $keywordsList[$v['keywords']] += $v['amount'];
  630. } else {
  631. $keywordsList[$v['keywords']] = $v['amount'];
  632. }
  633. // $userInfo = UserService::findAll(['member_id' => $v['member_id']]);
  634. // $lastStr = self::getLastChar($userInfo->first_name, 1);
  635. $lastStr = self::hideMiddleDigits($v['member_id'], 4);
  636. $item = [];
  637. $item['id'] = $v['id'];
  638. $item['status'] = self::model()::STATUS_SETTLED;
  639. if (in_array($v['keywords'], $awards)) {
  640. // $profit = $v['amount'] * $v['odds'];
  641. $amount = $v['amount'];
  642. // $amount = rtrim($amount, '0'); // 去掉右侧的 0
  643. // $amount = rtrim($amount, '.'); // 如果末尾是 . 就去掉
  644. $odds = $v['odds'];
  645. if (in_array('13操', $awards) || in_array('14操', $awards)) {
  646. // 13 14特殊处理倍率
  647. if (in_array($v['keywords'], self::$OTHER_BET_1)) {
  648. if ($sum < 10000) {
  649. $odds = 1.5;
  650. } else {
  651. $odds = 1;
  652. }
  653. }
  654. if (in_array($v['keywords'], self::$OTHER_BET_2)) {
  655. $odds = 1;
  656. }
  657. }
  658. $profit = bcmul($amount, $odds, 2); // 保留两位小数
  659. if ($profit > 1000000) {
  660. $profit = 1000000; // 单注最高奖金1000000
  661. }
  662. $item['profit'] = $profit;
  663. // $yl = $profit - $amount;
  664. $yl = bcsub($profit, $amount, 2); // 盈利
  665. if (!in_array('13操', $awards) && !in_array('14操', $awards)) {
  666. Rebate::updateProfit([
  667. 'member_id' => $v['member_id'],
  668. 'profit' => $yl,
  669. ]);
  670. }
  671. $memberList[$v['member_id']][] = [
  672. 'member_id' => $v['member_id'],
  673. 'keywords' => $v['keywords'],
  674. 'amount' => $v['amount'],
  675. 'profit' => $profit,
  676. 'yl' => $yl,
  677. ];
  678. // 结算
  679. WalletService::updateBalance($v['member_id'], $profit);
  680. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  681. $balance = $walletInfo['available_balance'];
  682. BalanceLogService::addLog($v['member_id'], $profit, $balance, ($balance + $profit), '中奖', $v['id'], '');
  683. if (isset($openList[$v['member_id']])) {
  684. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  685. $openList[$v['member_id']]['amount'] += $v['amount'];
  686. $openList[$v['member_id']]['profit'] += $profit;
  687. $openList[$v['member_id']]['lastStr'] = $lastStr;
  688. $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
  689. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  690. $openList[$v['member_id']]['win_amount'] += $v['amount'];
  691. } else {
  692. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  693. $openList[$v['member_id']]['amount'] = $v['amount'];
  694. $openList[$v['member_id']]['profit'] = $profit;
  695. $openList[$v['member_id']]['lastStr'] = $lastStr;
  696. $openList[$v['member_id']]['openKeywords'] = [];
  697. $openList[$v['member_id']]['keywords'] = [];
  698. $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
  699. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  700. $openList[$v['member_id']]['win_amount'] = $v['amount'];
  701. $openList[$v['member_id']]['is_send'] = true;
  702. }
  703. } else {
  704. if (!in_array('13操', $awards) && !in_array('14操', $awards)) {
  705. Rebate::updateProfit([
  706. 'member_id' => $v['member_id'],
  707. 'profit' => ($v['amount'] * -1),
  708. ]);
  709. }
  710. $profit = 0;
  711. if (in_array('13操', $awards) || in_array('14操', $awards)) {
  712. $amount = $v['amount'];
  713. $odds = 0;
  714. // 13 14特殊处理倍率
  715. if (in_array($v['keywords'], self::$OTHER_BET_1)) {
  716. if ($sum < 10000) {
  717. $odds = 1.5;
  718. } else {
  719. $odds = 1;
  720. }
  721. }
  722. if (in_array($v['keywords'], self::$OTHER_BET_2)) {
  723. $odds = 1;
  724. }
  725. $profit = bcmul($amount, $odds, 2); // 保留两位小数
  726. if ($profit > 1000000) {
  727. $profit = 1000000; // 单注最高奖金1000000
  728. }
  729. $item['profit'] = $profit;
  730. $yl = bcsub($profit, $amount, 2); // 盈利
  731. WalletService::updateBalance($v['member_id'], $profit);
  732. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  733. $balance = $walletInfo['available_balance'];
  734. BalanceLogService::addLog($v['member_id'], $profit, $balance, ($balance + $profit), '中奖', $v['id'], '');
  735. }
  736. if (isset($openList[$v['member_id']])) {
  737. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  738. $openList[$v['member_id']]['amount'] += $v['amount'];
  739. $openList[$v['member_id']]['profit'] += $profit;
  740. $openList[$v['member_id']]['lastStr'] = $lastStr;
  741. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  742. } else {
  743. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  744. $openList[$v['member_id']]['amount'] = $v['amount'];
  745. $openList[$v['member_id']]['profit'] = $profit;
  746. $openList[$v['member_id']]['lastStr'] = $lastStr;
  747. $openList[$v['member_id']]['openKeywords'] = [];
  748. $openList[$v['member_id']]['keywords'] = [];
  749. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  750. $openList[$v['member_id']]['win_amount'] = 0;
  751. $openList[$v['member_id']]['is_send'] = true;
  752. }
  753. }
  754. self::model()::where('id', $v['id'])->update($item);
  755. }
  756. $openList = array_merge($openList, $fakeOpenData['list']);
  757. self::lotteryNotice($openList, $issue_no, $keywordsList);
  758. }
  759. // 虚拟开奖
  760. public static function fakeLotteryDraw($issue_no, $awards, $rand_num = 30)
  761. {
  762. $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
  763. $text = "";
  764. $keywordsList = [];
  765. $fakeOtherSum = 0;
  766. $openList = [];
  767. foreach ($fake_bet_list as $k => $v) {
  768. if (in_array($v['keywords'], ['大', '小', '单', '双'])) {
  769. $fakeOtherSum += $v['amount'];
  770. }
  771. if (isset($keywordsList[$v['keywords']])) {
  772. $keywordsList[$v['keywords']] += $v['amount'];
  773. } else {
  774. $keywordsList[$v['keywords']] = $v['amount'];
  775. }
  776. // $lastStr = self::getLastChar($v['first_name'], 1);
  777. $lastStr = self::hideMiddleDigits($v['member_id'], 4);
  778. if (in_array($v['keywords'], $awards)) {
  779. $amount = $v['amount'];
  780. $odds = $v['odds'];
  781. $profit = bcmul($amount, $odds, 2); // 保留两位小数
  782. if ($profit > 880000) {
  783. $profit = 880000; // 单注最高奖金880000
  784. }
  785. $item['profit'] = $profit;
  786. // $v['amount'] = number_format($amount,2);
  787. if (isset($openList[$v['member_id']])) {
  788. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  789. $openList[$v['member_id']]['amount'] += $v['amount'];
  790. $openList[$v['member_id']]['profit'] += $profit;
  791. $openList[$v['member_id']]['lastStr'] = $lastStr;
  792. $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
  793. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  794. $openList[$v['member_id']]['win_amount'] += $v['amount'];
  795. } else {
  796. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  797. $openList[$v['member_id']]['amount'] = $v['amount'];
  798. $openList[$v['member_id']]['profit'] = $profit;
  799. $openList[$v['member_id']]['lastStr'] = $lastStr;
  800. $openList[$v['member_id']]['openKeywords'] = [];
  801. $openList[$v['member_id']]['keywords'] = [];
  802. $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
  803. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  804. $openList[$v['member_id']]['win_amount'] = $v['amount'];
  805. $openList[$v['member_id']]['is_send'] = false;
  806. }
  807. } else {
  808. if (isset($openList[$v['member_id']])) {
  809. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  810. $openList[$v['member_id']]['amount'] += $v['amount'];
  811. $openList[$v['member_id']]['lastStr'] = $lastStr;
  812. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  813. } else {
  814. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  815. $openList[$v['member_id']]['amount'] = $v['amount'];
  816. $openList[$v['member_id']]['profit'] = 0;
  817. $openList[$v['member_id']]['lastStr'] = $lastStr;
  818. $openList[$v['member_id']]['openKeywords'] = [];
  819. $openList[$v['member_id']]['keywords'] = [];
  820. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  821. $openList[$v['member_id']]['win_amount'] = 0;
  822. $openList[$v['member_id']]['is_send'] = false;
  823. }
  824. }
  825. }
  826. return ['sum' => $fakeOtherSum, 'list' => $openList, 'keywordsList' => $keywordsList];
  827. }
  828. // 开奖通知
  829. public static function lotteryNotice($openList, $issue_no, $keywordsList = [])
  830. {
  831. $betNoticeNum = Config::where('field', 'bet_notice_num')->first()->val;
  832. $betNoticeNum = explode(',', $betNoticeNum);
  833. $betNoticeMini = $betNoticeNum[0] ?? 26;
  834. $betNoticeMax = $betNoticeNum[1] ?? 38;
  835. $noticeNum = rand($betNoticeMini, $betNoticeMax);
  836. // shuffle($openList);
  837. // Log::error('lotteryNotice openList', $openList);
  838. $lang = App::getLocale();
  839. $group_language = Config::where('field', 'group_language')->first()->val;
  840. App::setLocale($group_language);
  841. $text = "{$issue_no} " . lang("期开奖结果");
  842. $text .= "\n-----" . lang("本期开奖账单") . "----- \n";
  843. App::setLocale($lang);
  844. foreach ($openList as $k => $v) {
  845. $amount = number_format($v['amount'], 2);
  846. $v['win_amount'] = number_format($v['win_amount'], 2);
  847. $profit = number_format($v['profit'], 2);
  848. $yl = bcsub($v['profit'], $v['amount'], 2); // 盈利
  849. if (empty($v['openKeywords'])) {
  850. $openKeyword = '-';
  851. } else {
  852. $openKeyword = implode(',', $v['openKeywords']);
  853. }
  854. if (($k + 1) <= $noticeNum) {
  855. $lang = App::getLocale();
  856. $group_language = Config::where('field', 'group_language')->first()->val;
  857. App::setLocale($group_language);
  858. $text .= lang("用户ID") . ":{$v['lastStr']} \n";
  859. $text .= lang("下注类型") . ":[" . implode(',', $v['keywords']) . "] \n";
  860. $text .= lang('中奖类型') . ":[" . $openKeyword . "] \n";
  861. $text .= lang('投注金额') . ":{$amount} \n";
  862. $text .= lang('中奖金额') . ":{$v['win_amount']} \n";
  863. $text .= lang('派彩金额') . ":{$profit} \n";
  864. $text .= lang("盈亏金额") . ":{$yl} \n";
  865. $text .= "-------------------------------- \n";
  866. App::setLocale($lang);
  867. }
  868. if ($v['is_send']) {
  869. $language = User::where('member_id', $v['member_id'])->first()->language;
  870. App::setLocale($language);
  871. $text2 = lang("{issue_no}期开奖结果");
  872. $text2 = str_replace("{issue_no}", $issue_no, $text2);
  873. $text2 .= "\n";
  874. $text2 .= lang('下注类型') . ":[" . implode(',', $v['keywords']) . "] \n";
  875. $text2 .= lang('中奖类型') . ":[" . $openKeyword . "] \n";
  876. $text2 .= lang('投注金额') . ":{$amount} \n";
  877. $text2 .= lang('中奖金额') . ":{$v['win_amount']} \n";
  878. $text2 .= lang('派彩金额') . ":{$profit} \n";
  879. $text2 .= lang("盈亏金额") . ":{$yl} \n";
  880. $keyboard = [];
  881. $keyboard[] = [
  882. ['text' => lang("开奖历史"), 'callback_data' => "showLotteryHistory@@" . $issue_no]
  883. ];
  884. // self::sendMessage($v['member_id'],$text2,$keyboard);
  885. SendTelegramMessageJob::dispatch($v['member_id'], $text2, $keyboard);
  886. }
  887. }
  888. $inlineButton = self::getOperateButton();
  889. // 群通知
  890. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  891. if (($pc28Switch == 0 && is_numeric($issue_no)) || $pc28Switch == 1 && !is_numeric($issue_no)) {
  892. SendTelegramGroupMessageJob::dispatch($text, $inlineButton, '', false, '--------------------------------');
  893. }
  894. }
  895. /**
  896. * @description: 统计投注情况通知
  897. * @param {*} $issue_no
  898. * @return {*}
  899. */
  900. public static function statNotice($issue_no)
  901. {
  902. $keywordsList = [];
  903. // 虚拟投注情况
  904. $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
  905. foreach ($fake_bet_list as $k => $v) {
  906. if (isset($keywordsList[$v['keywords']])) {
  907. $keywordsList[$v['keywords']] += $v['amount'];
  908. } else {
  909. $keywordsList[$v['keywords']] = $v['amount'];
  910. }
  911. }
  912. // 真实投注
  913. $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
  914. foreach ($list->toArray() as $k => $v) {
  915. if (isset($keywordsList[$v['keywords']])) {
  916. $keywordsList[$v['keywords']] += $v['amount'];
  917. } else {
  918. $keywordsList[$v['keywords']] = $v['amount'];
  919. }
  920. }
  921. $lang = App::getLocale();
  922. $group_language = Config::where('field', 'group_language')->first()->val;
  923. App::setLocale($group_language);
  924. $text3 = "📝 {$issue_no}" . lang('期投注统计') . " \n";
  925. $text3 .= lang("玩法") . " " . lang("总投") . " \n";
  926. App::setLocale($lang);
  927. if ($keywordsList) {
  928. uksort($keywordsList, 'custom_sort');
  929. // ksort($keywordsList);
  930. foreach ($keywordsList as $k => $v) {
  931. $text3 .= "{$k} {$v} \n";
  932. }
  933. }
  934. $inlineButton = self::getOperateButton();
  935. // 投注统计的消息不发了
  936. // SendTelegramGroupMessageJob::dispatch($text3, $inlineButton, '');
  937. }
  938. public static function todayExchangeRate($chatId)
  939. {
  940. $exchangeRate = Config::where('field', 'exchange_rate_rmb')->first()->val;
  941. $text = lang("今日汇率") . ":1USDT = {$exchangeRate} RMB \n";
  942. // $botMsg = [
  943. // 'chat_id' => "@{$chatId}",
  944. // 'text' => $text
  945. // ];
  946. self::sendMessage($chatId, $text);
  947. // return $botMsg;
  948. }
  949. }