BetService.php 45 KB

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