BetService.php 44 KB

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