BetService.php 43 KB

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