BetService.php 44 KB

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