BetService.php 44 KB

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