BetService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. <?php
  2. namespace App\Services;
  3. use App\Http\Controllers\admin\GameplayRule;
  4. use App\Models\Rebate;
  5. use App\Services\BaseService;
  6. use App\Models\Bet;
  7. use App\Models\Config;
  8. use Carbon\Carbon;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Collection;
  11. use Illuminate\Support\Facades\Cache;
  12. use Illuminate\Support\Facades\Log;
  13. use App\Services\GameplayRuleService;
  14. use App\Services\WalletService;
  15. use App\Services\IssueService;
  16. use App\Services\UserService;
  17. use App\Services\BalanceLogService;
  18. use App\Jobs\SendTelegramMessageJob;
  19. /**
  20. * 投注
  21. */
  22. class BetService extends BaseService
  23. {
  24. /**
  25. * @description: 模型
  26. * @return {string}
  27. */
  28. public static function model(): string
  29. {
  30. return Bet::class;
  31. }
  32. /**
  33. * @description: 枚举
  34. * @return {*}
  35. */
  36. public static function enum(): string
  37. {
  38. return '';
  39. }
  40. /**
  41. * @description: 获取查询条件
  42. * @param {array} $search 查询内容
  43. * @return {array}
  44. */
  45. public static function getWhere(array $search = []): array
  46. {
  47. $where = [];
  48. if (isset($search['issue_no']) && !empty($search['issue_no'])) {
  49. $where[] = ['issue_no', '=', $search['issue_no']];
  50. }
  51. if (isset($search['member_id']) && !empty($search['member_id'])) {
  52. $where[] = ['member_id', '=', $search['member_id']];
  53. }
  54. if (isset($search['keywords']) && !empty($search['keywords'])) {
  55. $where[] = ['keywords', '=', $search['keywords']];
  56. }
  57. if (isset($search['issue_id']) && !empty($search['issue_id'])) {
  58. $where[] = ['issue_id', '=', $search['issue_id']];
  59. }
  60. if (isset($search['id']) && !empty($search['id'])) {
  61. $where[] = ['id', '=', $search['id']];
  62. }
  63. if (isset($search['user_id']) && !empty($search['user_id'])) {
  64. $where[] = ['user_id', '=', $search['user_id']];
  65. }
  66. if (isset($search['start_time']) && !empty($search['status'])) {
  67. $where[] = ['created_at', '>=', "{$search['start_time']} 00:00:00"];
  68. $where[] = ['created_at', '<=', "{$search['end_time']} 23:59:59"];
  69. }
  70. if (isset($search['is_winner']) && !empty($search['is_winner'])) {
  71. $where[] = ['status', '=', 2];
  72. if ($search['is_winner'] == 1) {
  73. $where[] = ['profit', '>', 0];
  74. } else {
  75. $where[] = ['profit', '<=', 0];
  76. }
  77. } else {
  78. if (isset($search['status']) && !empty($search['status'])) {
  79. $where[] = ['status', '=', $search['status']];
  80. }
  81. }
  82. return $where;
  83. }
  84. /**
  85. * @description: 查询单条数据
  86. * @param array $search
  87. * @return \App\Models\Coin|null
  88. */
  89. public static function findOne(array $search): ?Bet
  90. {
  91. return self::model()::where(self::getWhere($search))->first();
  92. }
  93. /**
  94. * @description: 查询所有数据
  95. * @param array $search
  96. * @return \Illuminate\Database\Eloquent\Collection
  97. */
  98. public static function findAll(array $search = [])
  99. {
  100. return self::model()::where(self::getWhere($search))->get();
  101. }
  102. /**
  103. * @description: 分页查询
  104. * @param array $search
  105. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  106. */
  107. public static function paginate(array $search = [])
  108. {
  109. $limit = isset($search['limit']) ? $search['limit'] : 15;
  110. $query = self::model()::where(self::getWhere($search))
  111. ->with(['user']);
  112. if (isset($search['username']) && !empty($search['username'])) {
  113. $username = $search['username'];
  114. $query = $query->whereHas('user', function ($query) use ($username) {
  115. $query->where('username', $username);
  116. });
  117. }
  118. $paginator = $query->paginate($limit);
  119. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  120. }
  121. /**
  122. * @description: 投注操作
  123. * @param {string} $memberId
  124. * @param {string} $input
  125. * @return {*}
  126. */
  127. public static function bet(string $memberId, string $input, $messageId = 0)
  128. {
  129. $msg = [];
  130. $msg['chat_id'] = $memberId;
  131. // 钱包生成
  132. // $walletInfo = WalletService::getUserWallet($memberId);
  133. // 分解投注的内容
  134. $betResult = GameplayRuleService::bettingRuleVerify($input);
  135. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  136. if ($betResult == null) {
  137. $text = "消息格式错误!\n";
  138. $text .= "任何疑问都可以联系唯一客服:@{$serviceAccount}";
  139. $msg['text'] = $text;
  140. if ($messageId) {
  141. $msg['reply_to_message_id'] = $messageId;
  142. }
  143. return $msg;
  144. }
  145. $keywords = $betResult['rule']; // 玩法
  146. $amount = $betResult['amount']; // 投注金额
  147. $gameplayRuleInfo = GameplayRuleService::getGameplayRules($keywords);
  148. if ($gameplayRuleInfo == null) {
  149. $text = "玩法未配置!\n";
  150. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  151. $msg['text'] = $text;
  152. if ($messageId) {
  153. $msg['reply_to_message_id'] = $messageId;
  154. }
  155. return $msg;
  156. }
  157. if ($gameplayRuleInfo['odds'] <= 0) {
  158. $text = "赔率为0 庄家通吃 禁止投注!\n";
  159. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  160. $msg['text'] = $text;
  161. if ($messageId) {
  162. $msg['reply_to_message_id'] = $messageId;
  163. }
  164. return $msg;
  165. }
  166. // 期数验证
  167. $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
  168. if (empty($issueInfo)) {
  169. $issueCloseInfo = IssueService::model()::where('status', IssueService::model()::STATUS_CLOSE)->orderBy('id', 'desc')->first();
  170. if (empty($issueCloseInfo)) {
  171. $text = "暂无可下注期数,本次下注无效!\n";
  172. $msg['text'] = $text;
  173. if ($messageId) {
  174. $msg['reply_to_message_id'] = $messageId;
  175. }
  176. return $msg;
  177. } else {
  178. $text = "封盘中,本次下注无效!\n";
  179. $msg['text'] = $text;
  180. if ($messageId) {
  181. $msg['reply_to_message_id'] = $messageId;
  182. }
  183. return $msg;
  184. }
  185. }
  186. if (!is_numeric($amount) || $amount <= 0) {
  187. $text = "投注金额格式不正确!\n";
  188. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  189. $msg['text'] = $text;
  190. if ($messageId) {
  191. $msg['reply_to_message_id'] = $messageId;
  192. }
  193. return $msg;
  194. }
  195. $now_date = date('Y-m-d H:i:s',time() + 30); // 提前30秒
  196. if($issueInfo['end_time'] < $now_date){
  197. $text = "封盘中,本次下注无效!\n";
  198. $msg['text'] = $text;
  199. if ($messageId) {
  200. $msg['reply_to_message_id'] = $messageId;
  201. }
  202. return $msg;
  203. }
  204. // 投注限制校验
  205. if ($amount < $gameplayRuleInfo['mininum']) {
  206. $text = "下注失败,最小金额限制{$gameplayRuleInfo['mininum']}\n";
  207. $msg['text'] = $text;
  208. if ($messageId) {
  209. $msg['reply_to_message_id'] = $messageId;
  210. }
  211. return $msg;
  212. }
  213. // 投注限制校验
  214. if ($amount > $gameplayRuleInfo['maxinum']) {
  215. $text = "下注失败,最大金额限制{$gameplayRuleInfo['maxinum']}\n";
  216. $msg['text'] = $text;
  217. if ($messageId) {
  218. $msg['reply_to_message_id'] = $messageId;
  219. }
  220. return $msg;
  221. }
  222. // 获取用户余额
  223. $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  224. $balance = $walletInfo['available_balance'];
  225. // 余额计算
  226. if ($balance < $amount) {
  227. $text = "余额不足,本次下注无效!\n";
  228. $msg['text'] = $text;
  229. if ($messageId) {
  230. $msg['reply_to_message_id'] = $messageId;
  231. }
  232. return $msg;
  233. }
  234. $userInfo = UserService::findOne(['member_id' => $memberId]);
  235. $betInfo = self::findOne(['member_id' => $memberId, 'issue_no' => $issueInfo->issue_no, 'keywords' => $keywords]); // 相同下注
  236. if ($betInfo) {
  237. $betInfo->amount = $betInfo->amount + $amount;
  238. $bet_id = $betInfo->id;
  239. $betInfo->save();
  240. } else {
  241. $data = [];
  242. $data['amount'] = $amount; // 分数
  243. $data['keywords'] = $keywords; // 玩法
  244. $data['member_id'] = $memberId;
  245. $data['user_id'] = $userInfo->id;
  246. $data['issue_no'] = $issueInfo->issue_no;
  247. $data['issue_id'] = $issueInfo->id;
  248. $data['odds'] = $gameplayRuleInfo['odds'];
  249. $newBet = self::model()::create($data);
  250. $bet_id = $newBet->id;
  251. }
  252. WalletService::updateBalance($memberId, -$amount);
  253. BalanceLogService::addLog($memberId, -$amount, $balance, ($balance - $amount), '投注', $bet_id, '');
  254. $now = Carbon::now('America/New_York')->format('Y-m-d');
  255. $rebate = Config::where('field', 'rebate')->first()->val;
  256. Rebate::addOrUpdate([
  257. 'date' => $now,
  258. 'member_id' => $memberId,
  259. 'betting_amount' => $amount,
  260. 'rebate_ratio' => $rebate,
  261. 'first_name' => $userInfo->first_name,
  262. 'username' => $userInfo->username,
  263. ]);
  264. // // 返利
  265. // $rebate = Config::where('field', 'rebate')->first()->val;
  266. // if($rebate > 0){
  267. // $rebateAmount = bcmul($amount, $rebate, 2); // 返利金额
  268. // if($rebateAmount > 0){
  269. // WalletService::updateBalance($memberId,$rebateAmount);
  270. // $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  271. // $balance = $walletInfo['available_balance'];
  272. // BalanceLogService::addLog($memberId,$rebateAmount,$balance,($balance+$rebateAmount),'返水',$bet_id,'');
  273. // }
  274. // }
  275. $text = "下注期数:{$issueInfo->issue_no}\n";
  276. $text .= "下注内容\n";
  277. $text .= "--------\n";
  278. $text .= "{$input}\n";
  279. $text .= "--------\n";
  280. $text .= "下注成功\n";
  281. $msg['text'] = $text;
  282. // $lastStr = self::getLastChar($userInfo->first_name, 1);
  283. $lastStr = self::hideMiddleDigits($userInfo->member_id, 4);
  284. $groupText = "";
  285. $groupText .= "私聊下注 【" . $lastStr . "】 \n";
  286. $groupText .= "下注期数:{$issueInfo->issue_no} \n";
  287. $groupText .= "下注内容: \n";
  288. $groupText .= "----------- \n";
  289. $groupText .= "{$input} \n";
  290. $groupText .= "----------- \n";
  291. $inlineButton = self::getOperateButton();
  292. // 群通知
  293. self::bettingGroupNotice($groupText, $inlineButton); // 群通知
  294. return $msg;
  295. }
  296. // 模拟下注
  297. public static function fakeBet()
  298. {
  299. // 防止一开就虚拟投注
  300. $cache = Cache::get('new_issue_no');
  301. if($cache){
  302. return;
  303. }
  304. $betFake = Config::where('field', 'bet_fake')->first()->val;
  305. if ($betFake) {
  306. // 期数验证
  307. $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
  308. if ($issueInfo) {
  309. $betFakeRandAmount = Config::where('field', 'bet_fake_rand_amount')->first()->val;
  310. $betFakeRandAmount = explode(',', $betFakeRandAmount);
  311. $betMini = $betFakeRandAmount[0]??10;
  312. $betMax = $betFakeRandAmount[1]??10000;
  313. $now_date = date('Y-m-d H:i:s',time() + 30); // 提前30秒
  314. if($issueInfo['end_time'] > $now_date){
  315. $fake_bet_list = Cache::get('fake_bet_' . $issueInfo->issue_no, []);
  316. $gameplayRuleList = GameplayRuleService::model()::where('odds', '>', 0)->get();
  317. $gameplayRuleList = $gameplayRuleList->toArray();
  318. $randKey = array_rand($gameplayRuleList, 1);
  319. $gameplayRuleInfo = $gameplayRuleList[$randKey] ?? [];
  320. if ($gameplayRuleInfo) {
  321. if($gameplayRuleInfo['maxinum'] < $betMax){
  322. $betMax = $gameplayRuleInfo['maxinum'];
  323. }
  324. if($gameplayRuleInfo['mininum'] > $betMini){
  325. $betMini = $gameplayRuleInfo['mininum'];
  326. }
  327. $amount = rand($betMini, $betMax);
  328. $amount = floor($amount / 10) * 10;
  329. $input = $gameplayRuleInfo['keywords'] . $amount;
  330. // $amount = number_format($amount,2);
  331. $item = [];
  332. $item['keywords'] = $gameplayRuleInfo['keywords'];
  333. $item['odds'] = $gameplayRuleInfo['odds'];
  334. $item['amount'] = $amount;
  335. $item['first_name'] = self::generateRandomString(6);
  336. $item['member_id'] = self::generateRandomNumber(10);
  337. $item['profit'] = 0;
  338. // $input = $item['keywords'] . $item['amount'];
  339. $fake_bet_list[] = $item;
  340. $lastStr = self::hideMiddleDigits($item['member_id'], 4);
  341. $groupText = "";
  342. $groupText .= "私聊下注 【" . $lastStr . "】 \n";
  343. $groupText .= "下注期数:{$issueInfo->issue_no} \n";
  344. $groupText .= "下注内容: \n";
  345. $groupText .= "----------- \n";
  346. $groupText .= "{$input} \n";
  347. $groupText .= "----------- \n";
  348. $inlineButton = self::getOperateButton();
  349. // 群通知
  350. self::bettingGroupNotice($groupText, $inlineButton); // 群通知
  351. }
  352. Cache::put('fake_bet_' . $issueInfo->issue_no, $fake_bet_list, 500);
  353. }
  354. }
  355. }
  356. }
  357. /**
  358. * @description: 当期下注
  359. * @param {*} $memberId
  360. * @return {*}
  361. */
  362. public static function currentBet($memberId)
  363. {
  364. $msg['chat_id'] = $memberId;
  365. // 期数验证
  366. $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
  367. $issue_no = '';
  368. if (!empty($issueInfo)) {
  369. $issue_no = $issueInfo->issue_no;
  370. } else {
  371. $issueCloseInfo = IssueService::model()::where('status', IssueService::model()::STATUS_CLOSE)->orderBy('id', 'desc')->first();
  372. if (empty($issueCloseInfo)) {
  373. $issue_no = $issueCloseInfo->issue_no;
  374. }
  375. }
  376. if ($issue_no) {
  377. $text = "当前期号:{$issue_no} \n";
  378. $text .= "\n";
  379. $text .= "----------\n";
  380. $list = self::findAll(['member_id' => $memberId, 'issue_no' => $issue_no]);
  381. foreach ($list->toArray() as $k => $v) {
  382. $text .= "{$v['keywords']}{$v['amount']} \n";
  383. }
  384. $text .= "\n";
  385. $text .= "----------\n";
  386. $msg['text'] = $text;
  387. } else {
  388. $msg['text'] = "当前没有开放的投注期数! \n";
  389. }
  390. return $msg;
  391. }
  392. /**
  393. * @description: 近期投注
  394. * @param {*} $memberId
  395. * @return {*}
  396. */
  397. public static function recentlyRecord($memberId, $page = 1, $limit = 5)
  398. {
  399. $list = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->orderBy('id', 'desc')->forPage($page, $limit)->get();
  400. // $text = "```\n";
  401. $text = "";
  402. $text .= "期数--内容--盈亏 \n";
  403. foreach ($list->toArray() as $k => $v) {
  404. $profit = $v['profit'] - $v['amount'];
  405. // $text .= $v['issue_no']." ".$v['keywords']." ".$v['amount']." ".$v['profit']."\n";
  406. $item = $v['issue_no'] . "==" . $v['keywords'] . rtrim(rtrim(number_format($v['amount'], 2, '.', ''), '0'), '.') . "==" . rtrim(rtrim(number_format($profit, 2, '.', ''), '0'), '.') . "\n";
  407. $text .= $item;
  408. }
  409. // $text .= "```\n";
  410. return $text;
  411. }
  412. /**
  413. * @description: 投注记录
  414. * @param {*} $memberId
  415. * @param {*} $page
  416. * @param {*} $limit
  417. * @return {*}
  418. */
  419. public static function record($memberId, $messageId = null, $page = 1, $limit = 5)
  420. {
  421. $type = Cache::get('message_id_bet_record_' . $memberId, 0);
  422. $msg['chat_id'] = $memberId;
  423. $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();
  424. $count = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->where(self::getWhere(['is_winner' => $type]))->count();
  425. $keyboard = [];
  426. $total_amount = BalanceLogService::model()::where('member_id', $memberId)->where('change_type', '中奖')->sum('amount');
  427. $total_amount = number_format($total_amount, 2);
  428. $text = "历史注单 \n";
  429. $text .= "中奖总派彩:{$total_amount} \n";
  430. foreach ($list as $k => $v) {
  431. if ($v->status == self::model()::STATUS_SETTLED) {
  432. $phase = $v->profit - $v->amount;
  433. } else {
  434. $phase = '待开奖';
  435. }
  436. $text .= "-------------------------------------\n";
  437. $text .= "期数:{$v->issue_no} \n";
  438. $text .= "内容:{$v->keywords} \n";
  439. $text .= "金额:{$v->amount} \n";
  440. $text .= "盈亏:{$phase} \n";
  441. }
  442. $msg['text'] = $text;
  443. $keyboard[] = [
  444. ['text' => "全部", 'callback_data' => "betRecordType@@0"],
  445. ['text' => "盈利", 'callback_data' => "betRecordType@@1"],
  446. ['text' => "亏损", 'callback_data' => "betRecordType@@2"]
  447. ];
  448. if ($page > 1) {
  449. $keyboard[] = [
  450. ['text' => "👆上一页", 'callback_data' => "betRecordNextPage@@" . ($page - 1)]
  451. ];
  452. }
  453. $allPage = ceil($count / $limit);
  454. if ($allPage > $page) {
  455. if ($page > 1) {
  456. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)];
  457. } else {
  458. $keyboard[] = [
  459. ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)]
  460. ];
  461. }
  462. }
  463. if ($messageId) {
  464. $msg['message_id'] = $messageId;
  465. }
  466. if ($keyboard) {
  467. $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  468. }
  469. return $msg;
  470. }
  471. /**
  472. * @description: 中奖结算
  473. * @param {*} $issue_no
  474. * @param {*} $awards
  475. * @return {*}
  476. */
  477. public static function betSettled($issue_no, $awards)
  478. {
  479. $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
  480. $data = [];
  481. $text = $issue_no . "期开奖结果 \n";
  482. $text .= "-----本期开奖账单----- \n";
  483. $text .=" 中奖类型 用户 投注金额 中奖金额 盈亏 \n";
  484. $betNoticeNum = Config::where('field', 'bet_notice_num')->first()->val;
  485. $betNoticeNum = explode(',', $betNoticeNum);
  486. $betNoticeMini = $betNoticeNum[0] ?? 26;
  487. $betNoticeMax = $betNoticeNum[1] ?? 38;
  488. $noticeNum = rand($betNoticeMini, $betNoticeMax);
  489. $realNoticeNum = ceil($noticeNum / 2);
  490. $bet_num = 0;
  491. foreach ($list->toArray() as $k => $v) {
  492. // $userInfo = UserService::findAll(['member_id' => $v['member_id']]);
  493. // $lastStr = self::getLastChar($userInfo->first_name, 1);
  494. $lastStr = self::hideMiddleDigits($v['member_id'], 4);
  495. $item = [];
  496. $item['id'] = $v['id'];
  497. $item['status'] = self::model()::STATUS_SETTLED;
  498. if (in_array($v['keywords'], $awards)) {
  499. // $profit = $v['amount'] * $v['odds'];
  500. $amount = $v['amount'];
  501. // $amount = rtrim($amount, '0'); // 去掉右侧的 0
  502. // $amount = rtrim($amount, '.'); // 如果末尾是 . 就去掉
  503. $odds = $v['odds'];
  504. $profit = bcmul($amount, $odds, 2); // 保留两位小数
  505. if ($profit > 880000) {
  506. $profit = 880000; // 单注最高奖金880000
  507. }
  508. $item['profit'] = $profit;
  509. // $yl = $profit - $amount;
  510. $yl = bcsub($profit, $amount, 2); // 盈利
  511. if ($k + 1 <= $realNoticeNum) {
  512. $text .= "{$v['keywords']} 【" . $lastStr . "】{$v['amount']} {$profit} {$yl}\n";
  513. $bet_num++;
  514. }
  515. // 结算
  516. WalletService::updateBalance($v['member_id'], $profit);
  517. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  518. $balance = $walletInfo['available_balance'];
  519. BalanceLogService::addLog($v['member_id'], $profit, $balance, ($balance + $profit), '中奖', $v['id'], '');
  520. } else {
  521. if ($k + 1 <= $realNoticeNum) {
  522. $text .= "---- 【" . $lastStr . "】{$v['amount']} {$v['profit']} -{$v['amount']}\n";
  523. $bet_num++;
  524. }
  525. }
  526. self::model()::where('id', $v['id'])->update($item);
  527. }
  528. $inlineButton = self::getOperateButton();
  529. $rand_num = $noticeNum - $bet_num;
  530. $text .= self::fakeLotteryDraw($issue_no, $awards, $rand_num);
  531. // for ($i = 0; $i < $rand_num; $i++) {
  532. // // 生成 -100000 到 100000 的随机数,但排除 -10 到 10 的范围
  533. // $randomNumber = random_int(-1000000, 1000000) / 100;
  534. // if ($randomNumber >= -10 && $randomNumber <= 10) {
  535. // // 如果落在 -10 到 10 之间,重新生成或调整
  536. // $randomNumber = $randomNumber < 0 ? -random_int(10, 100000) : random_int(10, 100000);
  537. // }
  538. // $text .= "私聊下注 【******】 {$randomNumber}\n";
  539. // }
  540. // 群通知
  541. self::bettingGroupNotice($text, $inlineButton, '');
  542. }
  543. // 虚拟开奖
  544. public static function fakeLotteryDraw($issue_no, $awards, $rand_num = 30)
  545. {
  546. $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
  547. $text = "";
  548. foreach ($fake_bet_list as $k => $v) {
  549. // $lastStr = self::getLastChar($v['first_name'], 1);
  550. $lastStr = self::hideMiddleDigits($v['member_id'], 4);
  551. if (in_array($v['keywords'], $awards)) {
  552. $amount = $v['amount'];
  553. // $amount = rtrim($amount, '0'); // 去掉右侧的 0
  554. // $amount = rtrim($amount, '.'); // 如果末尾是 . 就去掉
  555. $odds = $v['odds'];
  556. $profit = bcmul($amount, $odds, 2); // 保留两位小数
  557. if ($profit > 880000) {
  558. $profit = 880000; // 单注最高奖金880000
  559. }
  560. $item['profit'] = $profit;
  561. $v['amount'] = number_format($amount,2);
  562. // $yl = $profit - $amount;
  563. $yl = bcsub($profit, $amount, 2); // 盈利
  564. if ($k + 1 <= $rand_num) {
  565. $text .= "{$v['keywords']} 【" . $lastStr . "】 {$v['amount']} {$profit} {$yl}\n";
  566. }
  567. } else {
  568. $v['amount'] = number_format($v['amount'],2);
  569. if ($k + 1 <= $rand_num) {
  570. $text .= "---- 【" . $lastStr . "】 {$v['amount']} {$v['profit']} -{$v['amount']}\n";
  571. }
  572. }
  573. }
  574. return $text;
  575. }
  576. public static function todayExchangeRate($chatId)
  577. {
  578. $exchangeRate = Config::where('field', 'exchange_rate_rmb')->first()->val;
  579. $text = "今日汇率:1USDT = {$exchangeRate} RMB \n";
  580. // $botMsg = [
  581. // 'chat_id' => "@{$chatId}",
  582. // 'text' => $text
  583. // ];
  584. self::sendMessage($chatId, $text);
  585. // return $botMsg;
  586. }
  587. }