BetService.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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. $text .= "{$keywords} {$amount} (" . $gameplayRuleInfo['odds'] . lang('倍率') . ")";
  323. // $lastStr = self::getLastChar($userInfo->first_name, 1);
  324. $lastStr = self::hideMiddleDigits($userInfo->member_id, 4);
  325. $issueNo = $issueInfo->issue_no;
  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. $groupText .= "{$keywords} {$amount} ({$gameplayRuleInfo['odds']}" . lang("倍率") . ") ";
  336. } else {
  337. $groupText .= "{$keywords} {$amount} ({$gameplayRuleInfo['odds']}" . lang("倍率") . ") ";
  338. }
  339. App::setLocale($lang);
  340. // $inlineButton = self::getOperateButton();
  341. // 群通知
  342. // self::asyncBettingGroupNotice($groupText, $inlineButton); // 异步群通知
  343. return true;
  344. }
  345. //随机虚拟下注
  346. public static function randomVirtualBetting($maxPeople = 1): void
  347. {
  348. $maxPeople = intval($maxPeople);
  349. $maxPeople = max($maxPeople, 1);
  350. $maxPeople = min($maxPeople, 20);
  351. $num = rand(1, $maxPeople);
  352. for ($i = 0; $i < $num; $i++) {
  353. static::fakeBet(4);
  354. }
  355. }
  356. // 模拟下注
  357. private static function fakeBet($betNumber = 1): void
  358. {
  359. $noRule = ['0操', '27操'];
  360. // 防止一开就虚拟投注
  361. $cache = Cache::get('new_issue_no');
  362. if ($cache) {
  363. return;
  364. }
  365. $maintenanceSwitch = Config::where('field', 'maintenance_switch')->first()->val;
  366. if ($maintenanceSwitch != 0) {
  367. return;
  368. }
  369. $betFake = Config::where('field', 'bet_fake')->first()->val;
  370. if ($betFake) {
  371. // 期数验证
  372. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  373. if ($pc28Switch == 1) {
  374. $issueInfo = PcIssue::where('status', PcIssue::STATUS_BETTING)->orderBy('id', 'desc')->first();
  375. } else {
  376. $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
  377. }
  378. if ($issueInfo) {
  379. $betFakeRandAmount = Config::where('field', 'bet_fake_rand_amount')->first()->val;
  380. $betFakeRandAmount = explode(',', $betFakeRandAmount);
  381. $betMini = $betFakeRandAmount[0] ?? 10;
  382. $betMax = $betFakeRandAmount[1] ?? 10000;
  383. $now_date = date('Y-m-d H:i:s', time() + 38); // 提前45秒
  384. if ($issueInfo['end_time'] > $now_date) {
  385. $fake_bet_list = Cache::get('fake_bet_' . $issueInfo->issue_no, []);
  386. $gameplayRuleList = GameplayRuleService::model()::where('odds', '>', 0)->get();
  387. $gameplayRuleList = $gameplayRuleList->toArray();
  388. $member_id = self::generateRandomNumber(10);
  389. $betTimes = rand(1, $betNumber); // 每次下注次数
  390. Cache::add("封盘后下注单数_{$issueInfo->issue_no}", rand(3, 5), 4);
  391. $lastStr = self::hideMiddleDigits($member_id, 4);
  392. $lang = App::getLocale();
  393. $group_language = Config::where('field', 'group_language')->first()->val;
  394. $first_name = self::generateRandomString(6);
  395. App::setLocale($group_language);
  396. $groupText = lang("第") . "{$issueInfo->issue_no}" . lang("期") . "\n";
  397. $groupText .= lang("开奖时间") . ":" . date("H:i:s", strtotime($issueInfo['end_time'])) . "\n";
  398. $groupText .= lang('投注截止') . ":" . date("H:i:s", (strtotime($issueInfo['end_time'])) - 30) . "\n";;
  399. $groupText .= lang("成功") . "\n";
  400. $groupText .= lang('玩家') . ": 【{$lastStr}】 \n";
  401. App::setLocale($lang);
  402. for ($i = 0; $i < $betTimes; $i++) {
  403. if (strtotime($issueInfo['end_time']) - time() < IssueService::COUNTDOWN_TO_CLOSING_THE_MARKET) {
  404. $fake_bet_count = Cache::get("fake_bet_count_{$issueInfo->issue_no}", 0);
  405. $cc = Cache::get("封盘后下注单数_{$issueInfo->issue_no}");
  406. if ($fake_bet_count >= $cc) return;
  407. }
  408. $randKey = array_rand($gameplayRuleList, 1);
  409. $gameplayRuleInfo = $gameplayRuleList[$randKey] ?? [];
  410. if ($gameplayRuleInfo) {
  411. if (in_array($gameplayRuleInfo['keywords'], $noRule)) {
  412. return;
  413. }
  414. if ($gameplayRuleInfo['maxinum'] < $betMax) {
  415. $betMax = $gameplayRuleInfo['maxinum'];
  416. }
  417. if ($gameplayRuleInfo['mininum'] > $betMini) {
  418. $betMini = $gameplayRuleInfo['mininum'];
  419. }
  420. $amount = rand($betMini, $betMax);
  421. $amount = floor($amount / 10) * 10;
  422. $input = $gameplayRuleInfo['keywords'] . ' ' . $amount;
  423. // $amount = number_format($amount,2);
  424. $item = [];
  425. $item['keywords'] = $gameplayRuleInfo['keywords'];
  426. $item['odds'] = floatval($gameplayRuleInfo['odds']);
  427. $item['amount'] = $amount;
  428. $item['first_name'] = $first_name;
  429. $item['member_id'] = $member_id;
  430. $item['profit'] = 0;
  431. // $input = $item['keywords'] . $item['amount'];
  432. $fake_bet_list[] = $item;
  433. $lang = App::getLocale();
  434. $group_language = Config::where('field', 'group_language')->first()->val;
  435. $first_name = self::generateRandomString(6);
  436. App::setLocale($group_language);
  437. $groupText .= "{$input} (" . $item['odds'] . lang("倍率") . ") ";
  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 ($groupText != "") {
  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. /**
  458. * @description: 当期下注
  459. * @param {*} $memberId
  460. * @return {*}
  461. */
  462. public static function currentBet($memberId)
  463. {
  464. $msg['chat_id'] = $memberId;
  465. // 期数验证
  466. $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
  467. $issue_no = '';
  468. if (!empty($issueInfo)) {
  469. $issue_no = $issueInfo->issue_no;
  470. } else {
  471. $issueCloseInfo = IssueService::model()::where('status', IssueService::model()::STATUS_CLOSE)->orderBy('id', 'desc')->first();
  472. if (empty($issueCloseInfo)) {
  473. $issue_no = $issueCloseInfo->issue_no;
  474. }
  475. }
  476. if ($issue_no) {
  477. $text = lang("期数") . " {$issue_no} \n";
  478. // $text .= "\n";
  479. // $text .= "----------\n";
  480. $list = self::findAll(['member_id' => $memberId, 'issue_no' => $issue_no]);
  481. $list = $list->toArray();
  482. if (empty($list)) {
  483. $text .= lang("本期暂未下注") . "! \n";
  484. } else {
  485. $keywords = implode(',', array_column($list, 'keywords'));
  486. $amounts = implode(',', array_column($list, 'amount'));
  487. $text .= lang("下注类型") . ":[" . $keywords . "] \n";
  488. $text .= lang("下注金额") . ":" . $amounts . " \n";
  489. $text .= lang("下注总额") . ":" . array_sum(array_column($list, 'amount')) . " \n";
  490. $text .= lang("开奖状态") . ":" . lang("等待开奖") . " \n";
  491. }
  492. // foreach ($list->toArray() as $k => $v) {
  493. // $text .= "{$v['keywords']}{$v['amount']} \n";
  494. // }
  495. // $text .= "\n";
  496. // $text .= "----------\n";
  497. $msg['text'] = $text;
  498. } else {
  499. $msg['text'] = lang("当前没有开放的投注期数") . "! \n";
  500. }
  501. return $msg;
  502. }
  503. /**
  504. * @description: 近期投注
  505. * @param {*} $memberId
  506. * @return {*}
  507. */
  508. public static function recentlyRecord($memberId, $page = 1, $limit = 5)
  509. {
  510. $list = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->orderBy('id', 'desc')->forPage($page, $limit)->get();
  511. // $text = "```\n";
  512. $text = "";
  513. $text .= "期数--内容--盈亏 \n";
  514. foreach ($list->toArray() as $k => $v) {
  515. $profit = $v['profit'] - $v['amount'];
  516. // $text .= $v['issue_no']." ".$v['keywords']." ".$v['amount']." ".$v['profit']."\n";
  517. $item = $v['issue_no'] . "==" . $v['keywords'] . rtrim(rtrim(number_format($v['amount'], 2, '.', ''), '0'), '.') . "==" . rtrim(rtrim(number_format($profit, 2, '.', ''), '0'), '.') . "\n";
  518. $text .= $item;
  519. }
  520. // $text .= "```\n";
  521. return $text;
  522. }
  523. /**
  524. * @description: 投注记录
  525. * @param {*} $memberId
  526. * @param {*} $page
  527. * @param {*} $limit
  528. * @return {*}
  529. */
  530. public static function record($memberId, $messageId = null, $page = 1, $limit = 5)
  531. {
  532. $type = Cache::get('message_id_bet_record_' . $memberId, 0);
  533. if ($type == 0) {
  534. $type = '';
  535. }
  536. $msg['chat_id'] = $memberId;
  537. $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();
  538. $count = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->where(self::getWhere(['is_winner' => $type]))->count();
  539. $keyboard = [];
  540. $total_amount = BalanceLogService::model()::where('member_id', $memberId)->where('change_type', '中奖')->sum('amount');
  541. $total_amount = number_format($total_amount, 2);
  542. $text = lang("历史注单") . " \n";
  543. $text .= lang("中奖总派彩") . ":{$total_amount} \n";
  544. foreach ($list as $k => $v) {
  545. if ($v->status == self::model()::STATUS_SETTLED) {
  546. $phase = $v->profit - $v->amount;
  547. } else {
  548. $phase = lang('待开奖');
  549. }
  550. $text .= "-------------------------------------\n";
  551. $text .= lang("期数") . ":{$v->issue_no} \n";
  552. $text .= lang("内容") . ":{$v->keywords} \n";
  553. $text .= lang("金额") . ":{$v->amount} \n";
  554. $text .= lang("盈亏") . ":{$phase} \n";
  555. }
  556. $msg['text'] = $text;
  557. $keyboard[] = [
  558. ['text' => lang("全部"), 'callback_data' => "betRecordType@@0"],
  559. ['text' => lang("盈利"), 'callback_data' => "betRecordType@@1"],
  560. ['text' => lang("亏损"), 'callback_data' => "betRecordType@@2"]
  561. ];
  562. if ($page > 1) {
  563. $keyboard[] = [
  564. ['text' => "👆" . lang("上一页"), 'callback_data' => "betRecordNextPage@@" . ($page - 1)]
  565. ];
  566. }
  567. $allPage = ceil($count / $limit);
  568. if ($allPage > $page) {
  569. if ($page > 1) {
  570. $keyboard[count($keyboard) - 1][] = ['text' => "👇" . lang("下一页"), 'callback_data' => "betRecordNextPage@@" . ($page + 1)];
  571. } else {
  572. $keyboard[] = [
  573. ['text' => "👇" . lang("下一页"), 'callback_data' => "betRecordNextPage@@" . ($page + 1)]
  574. ];
  575. }
  576. }
  577. if ($messageId) {
  578. $msg['message_id'] = $messageId;
  579. }
  580. if ($keyboard) {
  581. $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  582. }
  583. return $msg;
  584. }
  585. /**
  586. * @description: 开奖失败退回投注的
  587. * @param {*} $issue_no
  588. * @return {*}
  589. */
  590. public static function betFail($issue_no)
  591. {
  592. $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
  593. foreach ($list->toArray() as $k => $v) {
  594. $profit = $v['amount'];
  595. WalletService::updateBalance($v['member_id'], $profit);
  596. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  597. $balance = $walletInfo['available_balance'];
  598. BalanceLogService::addLog($v['member_id'], $profit, $balance, ($balance + $profit), '中奖', $v['id'], '');
  599. $text = $issue_no . "期开奖失败 \n";
  600. $text .= "投注类型:{$v['keywords']} \n";
  601. $text .= "投注金额:{$v['amount']} \n";
  602. $text .= "投注的资金已退回您的钱包 \n";
  603. self::asyncSendMessage($v['member_id'], $text);
  604. $item = [];
  605. $iem['status'] = self::model()::STATUS_SETTLED;
  606. self::model()::where('id', $v['id'])->update($item);
  607. }
  608. }
  609. /**
  610. * @description: 中奖结算
  611. * @param {*} $issue_no
  612. * @param {*} $awards
  613. * @return {*}
  614. */
  615. public static function betSettled($issue_no, $awards)
  616. {
  617. $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
  618. // 大小单双的
  619. $otherSum = self::model()::where('issue_no', $issue_no)->where('status', self::model()::STATUS_STAY)->whereIn('keywords', ['大', '小', '单', '双'])->sum('amount');
  620. $fakeOpenData = self::fakeLotteryDraw($issue_no, $awards, 0);
  621. $keywordsList = $fakeOpenData['keywordsList'];
  622. $fakeOtherSum = $fakeOpenData['sum'];
  623. $sum = $otherSum + $fakeOtherSum;
  624. $betNoticeNum = Config::where('field', 'bet_notice_num')->first()->val;
  625. $betNoticeNum = explode(',', $betNoticeNum);
  626. $betNoticeMini = $betNoticeNum[0] ?? 26;
  627. $betNoticeMax = $betNoticeNum[1] ?? 38;
  628. $noticeNum = rand($betNoticeMini, $betNoticeMax);
  629. $realNoticeNum = ceil($noticeNum / 2);
  630. $openList = [];
  631. $memberList = [];
  632. $bet_num = 0;
  633. foreach ($list->toArray() as $k => $v) {
  634. if (isset($keywordsList[$v['keywords']])) {
  635. $keywordsList[$v['keywords']] += $v['amount'];
  636. } else {
  637. $keywordsList[$v['keywords']] = $v['amount'];
  638. }
  639. // $userInfo = UserService::findAll(['member_id' => $v['member_id']]);
  640. // $lastStr = self::getLastChar($userInfo->first_name, 1);
  641. $lastStr = self::hideMiddleDigits($v['member_id'], 4);
  642. $item = [];
  643. $item['id'] = $v['id'];
  644. $item['status'] = self::model()::STATUS_SETTLED;
  645. if (in_array($v['keywords'], $awards)) {
  646. // $profit = $v['amount'] * $v['odds'];
  647. $amount = $v['amount'];
  648. // $amount = rtrim($amount, '0'); // 去掉右侧的 0
  649. // $amount = rtrim($amount, '.'); // 如果末尾是 . 就去掉
  650. $odds = $v['odds'];
  651. if (in_array('13操', $awards) || in_array('14操', $awards)) {
  652. // 13 14特殊处理倍率
  653. if (in_array($v['keywords'], self::$OTHER_BET_1)) {
  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. }