BetService.php 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. <?php
  2. namespace App\Services;
  3. use App\Support\MemberAudience;
  4. use App\Constants\StepStatus;
  5. use App\Constants\Util;
  6. use App\Models\Message;
  7. use App\Models\PcIssue;
  8. use App\Models\User;
  9. use App\Models\Wallet;
  10. use App\Models\LhcOrder;
  11. use App\Models\Config;
  12. use App\Models\LhcOrderItem;
  13. use Illuminate\Support\Facades\App;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Cache;
  16. use App\Jobs\SendTelegramMessageJob;
  17. use App\Jobs\SendTelegramGroupMessageJob;
  18. use Exception;
  19. use Illuminate\Support\Facades\Log;
  20. use Telegram\Bot\Api;
  21. use Telegram\Bot\Exceptions\TelegramSDKException;
  22. use App\Services\ActivityRewardService;
  23. use App\Services\AdminRealtime\AdminRealtime;
  24. /**
  25. * 投注
  26. */
  27. class BetService extends BaseService
  28. {
  29. /**
  30. * @param Api $telegram
  31. * @param $data
  32. * @param $chatId
  33. * @param $firstName
  34. * @param $messageId
  35. * @param $callbackId
  36. * @return void
  37. * @throws TelegramSDKException
  38. */
  39. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId, $callbackId): void
  40. {
  41. switch ($data) {
  42. case "todayExchangeRate@@rate": // 今日汇率
  43. BetService::todayExchangeRate($chatId);
  44. break;
  45. case "betsAlert":// 近期注单弹窗
  46. Cache::put('message_id_bet_record_' . $chatId, 0, 600);
  47. $alertText = BetService::recentlyRecord($chatId);
  48. BetService::alertNotice($callbackId, $alertText);
  49. break;
  50. case "promotionalActivityAlert":
  51. // 删除个人缓存
  52. Util::delCache($chatId);
  53. $returnMsg = ActivityRewardService::promotionalActivity($chatId);
  54. $telegram->sendMessage($returnMsg);
  55. break;
  56. default:
  57. //选择投注记录
  58. $pattern = "/^betRecordType@@\d+$/";
  59. if (preg_match($pattern, $data)) {
  60. $type = preg_replace('/^betRecordType@@/', '', $data);
  61. Cache::put('message_id_bet_record_' . $chatId, intval($type), 600);
  62. $telegram->deleteMessage([
  63. 'chat_id' => $chatId,
  64. 'message_id' => $messageId,
  65. ]);
  66. $returnMsg = BetService::record($chatId);
  67. $telegram->sendMessage($returnMsg);
  68. }
  69. //近期注单,下一页
  70. $pattern = "/^betRecordNextPage@@\d+$/";
  71. if (preg_match($pattern, $data)) {
  72. $page = preg_replace('/^betRecordNextPage@@/', '', $data);
  73. $page = intval($page);
  74. $res = BetService::record($chatId, $messageId, $page);
  75. $telegram->editMessageText($res);
  76. }
  77. break;
  78. }
  79. }
  80. public static function onMessage($chatId, $text, $messageId, $stepStatus): ?array
  81. {
  82. switch ($stepStatus) {
  83. default:
  84. $res = null;
  85. break;
  86. }
  87. return $res;
  88. }
  89. public static $OTHER_BET_1 = [
  90. '大',
  91. '小',
  92. '单',
  93. '双',
  94. ];
  95. public static $OTHER_BET_2 = [
  96. '大单',
  97. '大双',
  98. '小单',
  99. '小双',
  100. ];
  101. /**
  102. * @description: 模型
  103. * @return {string}
  104. */
  105. public static function model(): string
  106. {
  107. return LhcOrder::class;
  108. }
  109. /**
  110. * @description: 枚举
  111. * @return {*}
  112. */
  113. public static function enum(): string
  114. {
  115. return '';
  116. }
  117. public static function getWhere(array $search = []): array
  118. {
  119. $where = [];
  120. if (isset($search['issue_no']) && !empty($search['issue_no'])) {
  121. $where[] = ['issue', '=', $search['issue_no']];
  122. }
  123. if (isset($search['issue']) && !empty($search['issue'])) {
  124. $where[] = ['issue', '=', $search['issue']];
  125. }
  126. if (isset($search['member_id']) && !empty($search['member_id'])) {
  127. $where[] = ['member_id', '=', $search['member_id']];
  128. }
  129. if (isset($search['keywords']) && !empty($search['keywords'])) {
  130. $where[] = ['number', '=', $search['keywords']];
  131. }
  132. if (isset($search['number']) && !empty($search['number'])) {
  133. $where[] = ['number', '=', $search['number']];
  134. }
  135. if (isset($search['issue_id']) && !empty($search['issue_id'])) {
  136. $where[] = ['lhc_number_id', '=', $search['issue_id']];
  137. }
  138. if (isset($search['id']) && !empty($search['id'])) {
  139. $where[] = ['id', '=', $search['id']];
  140. }
  141. if (isset($search['user_id']) && !empty($search['user_id'])) {
  142. $where[] = ['user_id', '=', $search['user_id']];
  143. }
  144. if (isset($search['start_time']) && !empty($search['start_time'])) {
  145. $where[] = ['created_at', '>=', "{$search['start_time']} 00:00:00"];
  146. $where[] = ['created_at', '<=', "{$search['end_time']} 23:59:59"];
  147. }
  148. if (isset($search['is_winner']) && $search['is_winner'] != '') {
  149. $where[] = ['lottery_status', '=', 2];
  150. if ($search['is_winner'] == 1) {
  151. $where[] = ['profit_and_loss', '>', 0];
  152. } else {
  153. $where[] = ['profit_and_loss', '<=', 0];
  154. }
  155. } else {
  156. if (isset($search['status']) && $search['status'] != '') {
  157. $where[] = ['lottery_status', '=', $search['status']];
  158. }
  159. }
  160. if (isset($search['lottery_status']) && $search['lottery_status'] !== '') {
  161. $where[] = ['lottery_status', '=', $search['lottery_status']];
  162. }
  163. $where[] = ['type', '>=', 5];
  164. $where[] = ['type', '<=', 6];
  165. return $where;
  166. }
  167. /**
  168. * @description: 查询单条数据
  169. * @param array $search
  170. * @return \App\Models\Coin|null
  171. */
  172. public static function findOne(array $search): ?LhcOrder
  173. {
  174. return self::model()::where(self::getWhere($search))->first();
  175. }
  176. /**
  177. * @description: 查询所有数据
  178. * @param array $search
  179. * @return \Illuminate\Database\Eloquent\Collection
  180. */
  181. public static function findAll(array $search = [])
  182. {
  183. return self::model()::where(self::getWhere($search))->get();
  184. }
  185. /**
  186. * @description: 分页查询
  187. * @param array $search
  188. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  189. */
  190. public static function paginate(array $search = [])
  191. {
  192. $limit = isset($search['limit']) ? $search['limit'] : 15;
  193. $query = self::model()::where(self::getWhere($search))
  194. ->with(['user']);
  195. if (isset($search['username']) && !empty($search['username'])) {
  196. $username = $search['username'];
  197. $query = $query->whereHas('user', function ($query) use ($username) {
  198. $query->where('username', $username);
  199. });
  200. }
  201. $query->orderBy('id', 'desc');
  202. $paginator = $query->paginate($limit);
  203. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  204. }
  205. /**
  206. * @description: 投注操作
  207. * @param string $memberId
  208. * @param string $input
  209. * @param int $messageId
  210. * @return array
  211. */
  212. public static function bet(string $memberId, string $input, int $messageId = 0): array
  213. {
  214. $msg = [];
  215. $msg['chat_id'] = $memberId;
  216. // 分解投注的内容
  217. $betResult = GameplayRuleService::bettingRuleVerify($input);
  218. $serviceAccount = Config::where('field', 'service_customer')->first()->val;
  219. $maintenanceSwitch = Config::where('field', 'maintenance_switch')->first()->val;
  220. if ($maintenanceSwitch != 0) {
  221. $text = lang("系统维护中") . "\n";
  222. $text .= lang("任何疑问都可以联系唯一客服") . ":@{$serviceAccount}";
  223. $msg['text'] = $text;
  224. if ($messageId) {
  225. $msg['reply_to_message_id'] = $messageId;
  226. }
  227. return $msg;
  228. }
  229. // 后台「整站与游戏维护」:整站、F1彩票大类或当前28彩种(加拿大28/极速28)维护时不接单
  230. $maintenance = (new MaintenanceService())->telegramPc28Failure(User::where('member_id', $memberId)->value('language'));
  231. if ($maintenance !== null) {
  232. $msg['text'] = $maintenance['msg'] . "\n" . lang("任何疑问都可以联系唯一客服") . ":@{$serviceAccount}";
  233. if ($messageId) {
  234. $msg['reply_to_message_id'] = $messageId;
  235. }
  236. return $msg;
  237. }
  238. //是否封号
  239. $isBanned = UserService::getIsBanned($memberId);
  240. if ($isBanned) {
  241. $text = lang("账号异常") . "\n";
  242. $text .= lang("任何疑问都可以联系唯一客服") . ":@{$serviceAccount}";
  243. $msg['text'] = $text;
  244. if ($messageId) {
  245. $msg['reply_to_message_id'] = $messageId;
  246. }
  247. return $msg;
  248. }
  249. if ($betResult == null) {
  250. $text = lang("消息格式错误!") . "\n";
  251. $text .= lang("任何疑问都可以联系唯一客服") . ":@{$serviceAccount}";
  252. $msg['text'] = $text;
  253. if ($messageId) {
  254. $msg['reply_to_message_id'] = $messageId;
  255. }
  256. return $msg;
  257. }
  258. DB::beginTransaction();
  259. try {
  260. Wallet::where('member_id', $memberId)->lockForUpdate()->first(['id']);
  261. if ((new PromotionService())->lotteryRestricted($memberId)) {
  262. DB::commit();
  263. $msg['text'] = lang('活动期间禁止投注该游戏');
  264. if ($messageId) $msg['reply_to_message_id'] = $messageId;
  265. return $msg;
  266. }
  267. $pc28Switch = (int) Config::where('field', 'pc28_switch')->value('val');
  268. $template = LotteryOdds::template($pc28Switch === 1 ? 'jisu28' : 'canada28');
  269. $text = lang('下注期数') . ":{issue_no}\n";
  270. $text .= lang("下注内容") . "\n";
  271. $text .= "--------\n";
  272. $success = false;
  273. $sumAmount = 0;
  274. $errText = $groupText = "";
  275. foreach ($betResult as $index => $item) {
  276. $keywords = $item['rule'];
  277. $amount = $item['amount'];
  278. $error = $issueNo = "";
  279. $b = static::singleNote($memberId, $keywords, $amount, $issueNo, $text, $error, $groupText, $index, $pc28Switch, $template);
  280. if ($b) {
  281. $sumAmount += $amount;
  282. $text = str_replace("{issue_no}", $issueNo, $text);
  283. $success = true;
  284. $text .= "\n";
  285. } else {
  286. $errText .= $error;
  287. $errText .= "\n";
  288. }
  289. }
  290. if (!$success) {
  291. $msg['text'] = $errText;
  292. if ($messageId) {
  293. $msg['reply_to_message_id'] = $messageId;
  294. }
  295. } else {
  296. $sumAmount = number_format($sumAmount, 2);
  297. $text .= "--------\n";
  298. $text .= lang("合计金额") . ":{$sumAmount}\n";
  299. $text .= lang("下注成功");
  300. $msg['text'] = $text;
  301. $msg['text'] .= "\n";
  302. if (!empty($errText)) {
  303. $msg['text'] .= "------------------------\n";
  304. $msg['text'] .= $errText;
  305. }
  306. $groupText .= "\n--------\n";
  307. self::asyncBettingGroupNotice($groupText, self::getOperateButton()); // 异步群通知
  308. }
  309. DB::commit();
  310. } catch (Exception $e) {
  311. DB::rollBack();
  312. Log::error('错误信息', [
  313. 'message' => $e->getMessage(),
  314. 'file' => $e->getFile(),
  315. 'line' => $e->getLine(),
  316. 'trace' => $e->getTraceAsString()
  317. ]);
  318. $msg['text'] = "投注失败,请联系管理员";
  319. if ($messageId) {
  320. $msg['reply_to_message_id'] = $messageId;
  321. }
  322. }
  323. return $msg;
  324. }
  325. private static function singleNote($memberId, $keywords, $amount, &$issueNo, &$text, &$errText, &$groupText, $index, int $pc28Switch, object $template): bool
  326. {
  327. $errText .= "【{$keywords}{$amount}】\n";
  328. //客服号
  329. $serviceAccount = Config::where('field', 'service_customer')->first()->val;
  330. $price = LotteryOdds::pc28Price($template, $keywords);
  331. if ($price === null) {
  332. $errText .= lang("玩法未配置!") . "\n";
  333. $errText .= lang("任何疑问都可以联系唯一财务") . ":@{$serviceAccount}";
  334. return false;
  335. }
  336. $gameplayRuleInfo = (array) $price;
  337. if ($gameplayRuleInfo['odds'] <= 0) {
  338. $errText .= lang("赔率为0 庄家通吃 禁止投注!") . "\n";
  339. $errText .= lang("任何疑问都可以联系唯一财务") . ":@{$serviceAccount}";
  340. return false;
  341. }
  342. // 期数验证
  343. if ($pc28Switch == 1) {
  344. $issueInfo = PcIssue::where('status', PcIssue::STATUS_BETTING)->orderBy('id', 'desc')->first();
  345. } else {
  346. $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
  347. }
  348. if (empty($issueInfo)) {
  349. if ($pc28Switch == 1) {
  350. $issueCloseInfo = PcIssue::where('status', PcIssue::STATUS_CLOSE)->orderBy('id', 'desc')->first();
  351. } else {
  352. $issueCloseInfo = IssueService::model()::where('status', IssueService::model()::STATUS_CLOSE)->orderBy('id', 'desc')->first();
  353. }
  354. if (empty($issueCloseInfo)) {
  355. $errText .= lang("暂无可下注期数,本次下注无效!");
  356. return false;
  357. } else {
  358. $errText .= lang("封盘中,本次下注无效!");
  359. return false;
  360. }
  361. }
  362. if (!is_numeric($amount) || $amount <= 0) {
  363. $errText .= lang("投注金额格式不正确!");
  364. $errText .= lang("任何疑问都可以联系唯一财务") . ":@{$serviceAccount}";
  365. return false;
  366. }
  367. $now_date = date('Y-m-d H:i:s', time() + 30); // 提前30秒
  368. if ($issueInfo['end_time'] < $now_date) {
  369. $errText .= lang("封盘中,本次下注无效!");
  370. return false;
  371. }
  372. // 投注限制校验
  373. if ($amount < $gameplayRuleInfo['mininum']) {
  374. $errText .= lang("下注失败,最小金额限制") . "{$gameplayRuleInfo['mininum']}\n";
  375. return false;
  376. }
  377. // 投注限制校验
  378. if ($amount > $gameplayRuleInfo['maxinum']) {
  379. $errText .= lang("下注失败,最大金额限制") . "{$gameplayRuleInfo['maxinum']}\n";
  380. return false;
  381. }
  382. // 获取用户余额
  383. $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  384. $balance = $walletInfo['available_balance'];
  385. // 余额计算
  386. if ($balance < $amount) {
  387. $errText .= lang("余额不足,本次下注无效!\n");
  388. return false;
  389. }
  390. $userInfo = UserService::findOne(['member_id' => $memberId]);
  391. $type = $pc28Switch == 1 ? 6 : 5;
  392. // 下注项存于 lhc_order_item;相同下注只合并到本机器人建的单项、无优惠券、未开奖的订单
  393. $betInfo = LhcOrder::where('type', $type)
  394. ->where('bet_source', LhcOrder::SOURCE_TELEGRAM)
  395. ->where('member_id', $memberId)
  396. ->where('issue', $issueInfo->issue_no)
  397. ->where('lottery_status', LhcOrder::STATUS_STAY)
  398. ->where(LotteryOdds::orderFields($template))
  399. ->where('odds', $gameplayRuleInfo['odds'])
  400. ->whereNull('coupon_id')
  401. ->whereColumn('total_amount', 'amount')
  402. ->whereIn('ordernum', LhcOrderItem::where('member_id', $memberId)->where('number', $keywords)->select('ordernum'))
  403. ->lockForUpdate()
  404. ->first(); // 相同下注
  405. if ($betInfo) {
  406. $betInfo->amount = bcadd($betInfo->amount, $amount, 2);
  407. $betInfo->total_amount = $betInfo->amount;
  408. $betInfo->save();
  409. $bet_id = $betInfo->id;
  410. } else {
  411. $ordernum = date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
  412. LhcOrderItem::create([
  413. 'ordernum' => $ordernum,
  414. 'member_id' => $memberId,
  415. 'number' => $keywords, // 玩法
  416. ]);
  417. $newBet = LhcOrder::create([
  418. 'type' => $type,
  419. 'bet_source' => LhcOrder::SOURCE_TELEGRAM,
  420. 'issue' => $issueInfo->issue_no,
  421. 'ordernum' => $ordernum,
  422. 'member_id' => $memberId,
  423. 'odds' => $gameplayRuleInfo['odds'],
  424. 'amount' => $amount, // 分数
  425. 'total_amount' => $amount,
  426. ] + LotteryOdds::orderFields($template));
  427. $bet_id = $newBet->id;
  428. }
  429. WalletService::updateBalance($memberId, -$amount);
  430. BalanceLogService::addLog($memberId, -$amount, $balance, ($balance - $amount), '投注', $bet_id, '');
  431. $odds = floatval($gameplayRuleInfo['odds']);
  432. $issueNo = $issueInfo->issue_no;
  433. $text .= "{$keywords} {$amount} ({$odds}" . lang('倍率') . ")";
  434. $lastStr = self::hideMiddleDigits($userInfo->member_id, 4);
  435. $lang = App::getLocale();
  436. $group_language = Config::where('field', 'group_language')->first()->val;
  437. App::setLocale($group_language);
  438. if ($index === 0) {
  439. $groupText .= lang("第") . "{$issueNo}" . lang("期") . "\n";
  440. $groupText .= lang("开奖时间") . ":" . date("H:i:s", strtotime($issueInfo['end_time'])) . "\n";
  441. $groupText .= lang('投注截止') . ":" . date("H:i:s", (strtotime($issueInfo['end_time'])) - 30) . "\n";;
  442. $groupText .= lang("成功") . "\n";
  443. $groupText .= lang('玩家') . ": 【{$lastStr}】 \n";
  444. }
  445. $groupText .= "{$keywords} {$amount} ({$odds}" . lang("倍率") . ") \n";
  446. App::setLocale($lang);
  447. return true;
  448. }
  449. //随机虚拟下注
  450. public static function randomVirtualBetting($maxPeople = 1): void
  451. {
  452. $maxPeople = intval($maxPeople);
  453. $maxPeople = max($maxPeople, 1);
  454. $maxPeople = min($maxPeople, 20);
  455. $num = rand(1, $maxPeople);
  456. for ($i = 0; $i < $num; $i++) {
  457. static::fakeBet(4);
  458. }
  459. }
  460. // 模拟下注
  461. private static function fakeBet($betNumber = 1): void
  462. {
  463. $noRule = ['0操', '27操'];
  464. // 防止一开就虚拟投注
  465. $cache = Cache::get('new_issue_no');
  466. if ($cache) {
  467. echo "防止一开就虚拟投注\n";
  468. return;
  469. }
  470. //系统维护
  471. $maintenanceSwitch = Config::where('field', 'maintenance_switch')->first()->val;
  472. if ($maintenanceSwitch != 0) {
  473. echo "系统维护中\n";
  474. return;
  475. }
  476. if ((new MaintenanceService())->telegramPc28Failure('zh') !== null) {
  477. echo "28彩种维护中\n";
  478. return;
  479. }
  480. //后台是否开启虚拟投注
  481. $betFake = Config::where('field', 'bet_fake')->first()->val;
  482. if ($betFake != 1) {
  483. echo "未开启虚拟投注\n";
  484. return;
  485. }
  486. // 期数验证
  487. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  488. if ($pc28Switch == 1) {
  489. $issueInfo = PcIssue::where('status', PcIssue::STATUS_BETTING)->orderBy('id', 'desc')->first();
  490. } else {
  491. $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
  492. }
  493. if ($issueInfo) {
  494. $betFakeRandAmount = Config::where('field', 'bet_fake_rand_amount')->first()->val;
  495. $betFakeRandAmount = explode(',', $betFakeRandAmount);
  496. $betMini = $betFakeRandAmount[0] ?? 10;
  497. $betMax = $betFakeRandAmount[1] ?? 10000;
  498. $now_date = date('Y-m-d H:i:s', time() + 38); // 提前45秒
  499. if ($issueInfo['end_time'] > $now_date) {
  500. $fake_bet_list = Cache::get('fake_bet_' . $issueInfo->issue_no, []);
  501. $key = $pc28Switch == 1 ? 'jisu28' : 'canada28';
  502. $gameplayRuleList = DB::transaction(fn () => LotteryOdds::pricedCatalog($key, LotteryOdds::template($key)->id)
  503. ->filter(fn ($rule) => $rule->odds > 0)->values()->map(fn ($rule) => (array) $rule)->all());
  504. if ($gameplayRuleList === []) {
  505. return;
  506. }
  507. $member_id = self::generateRandomNumber(10);
  508. $betTimes = rand(1, $betNumber); // 每次下注次数
  509. Cache::add("封盘后下注单数_{$issueInfo->issue_no}", rand(3, 5), 4);
  510. $lastStr = self::hideMiddleDigits($member_id, 4);
  511. $lang = App::getLocale();
  512. $group_language = Config::where('field', 'group_language')->first()->val;
  513. $first_name = self::generateRandomString(6);
  514. App::setLocale($group_language);
  515. $groupText = lang("第") . "{$issueInfo->issue_no}" . lang("期") . "\n";
  516. $groupText .= lang("开奖时间") . ":" . date("H:i:s", strtotime($issueInfo['end_time'])) . "\n";
  517. $groupText .= lang('投注截止') . ":" . date("H:i:s", (strtotime($issueInfo['end_time'])) - 30) . "\n";;
  518. $groupText .= lang("成功") . "\n";
  519. $groupText .= lang('玩家') . ": 【{$lastStr}】 \n";
  520. App::setLocale($lang);
  521. $haveBet = false;
  522. for ($i = 0; $i < $betTimes; $i++) {
  523. if (strtotime($issueInfo['end_time']) - time() < IssueService::COUNTDOWN_TO_CLOSING_THE_MARKET) {
  524. $fake_bet_count = Cache::get("fake_bet_count_{$issueInfo->issue_no}", 0);
  525. $cc = Cache::get("封盘后下注单数_{$issueInfo->issue_no}");
  526. if ($fake_bet_count >= $cc) return;
  527. }
  528. $randKey = array_rand($gameplayRuleList, 1);
  529. $gameplayRuleInfo = $gameplayRuleList[$randKey] ?? [];
  530. if ($gameplayRuleInfo) {
  531. if (in_array($gameplayRuleInfo['keywords'], $noRule)) {
  532. return;
  533. }
  534. if ($gameplayRuleInfo['maxinum'] < $betMax) {
  535. $betMax = $gameplayRuleInfo['maxinum'];
  536. }
  537. if ($gameplayRuleInfo['mininum'] > $betMini) {
  538. $betMini = $gameplayRuleInfo['mininum'];
  539. }
  540. $amount = rand($betMini, $betMax);
  541. $amount = floor($amount / 10) * 10;
  542. $input = $gameplayRuleInfo['keywords'] . ' ' . $amount;
  543. // $amount = number_format($amount,2);
  544. $item = [];
  545. $item['keywords'] = $gameplayRuleInfo['keywords'];
  546. $item['odds'] = floatval($gameplayRuleInfo['odds']);
  547. $item['amount'] = $amount;
  548. $item['first_name'] = $first_name;
  549. $item['member_id'] = $member_id;
  550. $item['profit'] = 0;
  551. // $input = $item['keywords'] . $item['amount'];
  552. $fake_bet_list[] = $item;
  553. $lang = App::getLocale();
  554. $group_language = Config::where('field', 'group_language')->first()->val;
  555. $first_name = self::generateRandomString(6);
  556. App::setLocale($group_language);
  557. $groupText .= "{$input} (" . $item['odds'] . lang("倍率") . ") \n";
  558. $haveBet = true;
  559. App::setLocale($lang);
  560. if (strtotime($issueInfo['end_time']) - strtotime($now_date) < 22) {
  561. $fake_bet_count = Cache::get("fake_bet_count_{$issueInfo->issue_no}", 0);
  562. Cache::put("fake_bet_count_{$issueInfo->issue_no}", $fake_bet_count + 1, 500);
  563. }
  564. }
  565. }
  566. if ($haveBet) {
  567. $groupText .= "\n-------- \n";
  568. $inlineButton = self::getOperateButton();
  569. // 群通知 暂停2秒再发送,避免同时发送多个消息而超过 Telegram 的频率限制
  570. self::asyncBettingGroupNotice($groupText, $inlineButton); // 异步群通知
  571. sleep(2);
  572. }
  573. Cache::put('fake_bet_' . $issueInfo->issue_no, $fake_bet_list, 500);
  574. }
  575. }
  576. }
  577. /**
  578. * @description: 当期下注
  579. * @param {*} $memberId
  580. * @return {*}
  581. */
  582. public static function currentBet($memberId)
  583. {
  584. $msg['chat_id'] = $memberId;
  585. // 期数验证
  586. $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
  587. $issue_no = '';
  588. if (!empty($issueInfo)) {
  589. $issue_no = $issueInfo->issue_no;
  590. } else {
  591. $issueCloseInfo = IssueService::model()::where('status', IssueService::model()::STATUS_CLOSE)->orderBy('id', 'desc')->first();
  592. if (empty($issueCloseInfo)) {
  593. $issue_no = $issueCloseInfo->issue_no;
  594. }
  595. }
  596. if ($issue_no) {
  597. $text = lang("期数") . " {$issue_no} \n";
  598. // $text .= "\n";
  599. // $text .= "----------\n";
  600. $list = self::model()::with('items')->whereIn('type', [5, 6])->where('member_id', $memberId)->where('issue', $issue_no)->get();
  601. $list = $list->map(fn ($v) => ['number' => $v->items->pluck('number')->implode(','), 'amount' => $v->amount])->toArray();
  602. if (empty($list)) {
  603. $text .= lang("本期暂未下注") . "! \n";
  604. } else {
  605. $keywords = implode(',', array_column($list, 'number'));
  606. $amounts = implode(',', array_column($list, 'amount'));
  607. $text .= lang("下注类型") . ":[" . $keywords . "] \n";
  608. $text .= lang("下注金额") . ":" . $amounts . " \n";
  609. $text .= lang("下注总额") . ":" . array_sum(array_column($list, 'amount')) . " \n";
  610. $text .= lang("开奖状态") . ":" . lang("等待开奖") . " \n";
  611. }
  612. // foreach ($list->toArray() as $k => $v) {
  613. // $text .= "{$v['keywords']}{$v['amount']} \n";
  614. // }
  615. // $text .= "\n";
  616. // $text .= "----------\n";
  617. $msg['text'] = $text;
  618. } else {
  619. $msg['text'] = lang("当前没有开放的投注期数") . "! \n";
  620. }
  621. return $msg;
  622. }
  623. /**
  624. * @description: 近期投注
  625. * @param {*} $memberId
  626. * @return {*}
  627. */
  628. public static function recentlyRecord($memberId, $page = 1, $limit = 5)
  629. {
  630. $list = self::model()::with('items')->whereIn('type', [5, 6])->where('member_id', $memberId)->orderBy('id', 'desc')->forPage($page, $limit)->get();
  631. // $text = "```\n";
  632. $text = "";
  633. $text .= "期数--内容--盈亏 \n";
  634. foreach ($list as $k => $v) {
  635. $v = $v->toArray() + ['number' => $v->items->pluck('number')->implode(',')];
  636. $profit = $v['win_amount'] - $v['amount'];
  637. // $text .= $v['issue_no']." ".$v['keywords']." ".$v['amount']." ".$v['profit']."\n";
  638. $item = $v['issue'] . "==" . $v['number'] . rtrim(rtrim(number_format($v['amount'], 2, '.', ''), '0'), '.') . "==" . rtrim(rtrim(number_format($profit, 2, '.', ''), '0'), '.') . "\n";
  639. $text .= $item;
  640. }
  641. // $text .= "```\n";
  642. return $text;
  643. }
  644. /**
  645. * @description: 投注记录
  646. * @param {*} $memberId
  647. * @param {*} $page
  648. * @param {*} $limit
  649. * @return {*}
  650. */
  651. public static function record($memberId, $messageId = null, $page = 1, $limit = 5)
  652. {
  653. $type = Cache::get('message_id_bet_record_' . $memberId, 0);
  654. if ($type == 0) {
  655. $type = '';
  656. }
  657. $msg['chat_id'] = $memberId;
  658. $list = self::model()::with('items')->whereIn('type', [5, 6])->where('member_id', $memberId)->orderBy('id', 'desc')->forPage($page, $limit)->get();
  659. $count = self::model()::whereIn('type', [5, 6])->where('member_id', $memberId)->count();
  660. $keyboard = [];
  661. $total_amount = BalanceLogService::model()::where('member_id', $memberId)->where('change_type', '中奖')->sum('amount');
  662. $total_amount = number_format($total_amount, 2);
  663. $text = lang("历史注单") . " \n";
  664. $text .= lang("中奖总派彩") . ":{$total_amount} \n";
  665. foreach ($list as $k => $v) {
  666. if ($v->lottery_status == self::model()::STATUS_WIN || $v->lottery_status == self::model()::STATUS_LOSS) {
  667. $phase = $v->win_amount - $v->amount;
  668. } else {
  669. $phase = lang('待开奖');
  670. }
  671. $text .= "-------------------------------------\n";
  672. $text .= lang("期数") . ":{$v->issue} \n";
  673. $text .= lang("内容") . ":" . $v->items->pluck('number')->implode(',') . " \n";
  674. $text .= lang("金额") . ":{$v->amount} \n";
  675. $text .= lang("盈亏") . ":{$phase} \n";
  676. }
  677. $msg['text'] = $text;
  678. $keyboard[] = [
  679. ['text' => lang("全部"), 'callback_data' => "betRecordType@@0"],
  680. ['text' => lang("盈利"), 'callback_data' => "betRecordType@@1"],
  681. ['text' => lang("亏损"), 'callback_data' => "betRecordType@@2"]
  682. ];
  683. if ($page > 1) {
  684. $keyboard[] = [
  685. ['text' => "👆" . lang("上一页"), 'callback_data' => "betRecordNextPage@@" . ($page - 1)]
  686. ];
  687. }
  688. $allPage = ceil($count / $limit);
  689. if ($allPage > $page) {
  690. if ($page > 1) {
  691. $keyboard[count($keyboard) - 1][] = ['text' => "👇" . lang("下一页"), 'callback_data' => "betRecordNextPage@@" . ($page + 1)];
  692. } else {
  693. $keyboard[] = [
  694. ['text' => "👇" . lang("下一页"), 'callback_data' => "betRecordNextPage@@" . ($page + 1)]
  695. ];
  696. }
  697. }
  698. if ($messageId) {
  699. $msg['message_id'] = $messageId;
  700. }
  701. if ($keyboard) {
  702. $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  703. }
  704. return $msg;
  705. }
  706. /**
  707. * @description: 开奖失败退回投注的
  708. * @param {*} $issue_no
  709. * @return {*}
  710. */
  711. public static function betFail($issue_no)
  712. {
  713. $list = self::findAll(['issue' => $issue_no, 'lottery_status' => self::model()::STATUS_STAY]);
  714. foreach ($list->toArray() as $k => $v) {
  715. $profit = $v['amount'];
  716. // A couponed slip refunds only what the member paid for all its numbers, and the coupon comes back.
  717. if (!empty($v['coupon_id']) || bccomp((string) ($v['coupon_discount'] ?? 0), '0', 2) > 0) {
  718. $profit = PromotionCouponService::paid($v['total_amount'], $v['coupon_discount'] ?? 0);
  719. PromotionCouponService::release('lhc_order', (int) $v['id']);
  720. }
  721. WalletService::updateBalance($v['member_id'], $profit);
  722. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  723. $balance = $walletInfo['available_balance'];
  724. BalanceLogService::addLog($v['member_id'], $profit, ($balance - $profit), $balance, '投注退分', $v['id'], '');
  725. $text = $issue_no . "期开奖失败 \n";
  726. $text .= "投注类型:" . LhcOrderItem::where('ordernum', $v['ordernum'])->pluck('number')->implode(',') . " \n";
  727. $text .= "投注金额:{$v['amount']} \n";
  728. $text .= "投注的资金已退回您的钱包 \n";
  729. if (!MemberAudience::isSimulatedMember($v['member_id'])) self::asyncSendMessage($v['member_id'], $text);
  730. $item = [];
  731. $item['lottery_status'] = self::model()::STATUS_LOSS;
  732. self::model()::where('id', $v['id'])->update($item);
  733. //下注项状态更新为未中奖
  734. LhcOrderItem::where('ordernum', $v['ordernum'])->update(['lottery_status' => self::model()::STATUS_LOSS]);
  735. \App\Services\Reporting\ReportContext::lotterySettled((int) $v['id']);
  736. AdminRealtime::record('lhc_order', $v['id']);
  737. }
  738. }
  739. /**
  740. * @description: 中奖结算
  741. * @param {*} $issue_no
  742. * @param {*} $awards
  743. * @return {*}
  744. */
  745. public static function betSettled($issue_no, $awards)
  746. {
  747. $list = self::findAll(['issue' => $issue_no, 'lottery_status' => self::model()::STATUS_STAY]);
  748. // 大小单双的
  749. // 后台模拟会员的注额不计入总注(总注决定真实玩家 13/14 的特殊赔率)
  750. $otherSum = MemberAudience::visibleMembers(self::model()::leftJoin('lhc_order_item', 'lhc_order_item.ordernum', '=', 'lhc_order.ordernum'), 'lhc_order.member_id')
  751. ->whereIn('lhc_order.type', [5, 6])
  752. ->where('lhc_order.issue', $issue_no)
  753. ->where('lhc_order.lottery_status', self::model()::STATUS_STAY)
  754. ->whereIn('lhc_order_item.number', ['大', '小', '单', '双'])
  755. ->sum('lhc_order.total_amount');
  756. $fakeOpenData = self::fakeLotteryDraw($issue_no, $awards, 0);
  757. $keywordsList = $fakeOpenData['keywordsList'];
  758. $fakeOtherSum = $fakeOpenData['sum'];
  759. $sum = $otherSum + $fakeOtherSum;
  760. $betNoticeNum = Config::where('field', 'bet_notice_num')->first()->val;
  761. $betNoticeNum = explode(',', $betNoticeNum);
  762. $betNoticeMini = max(1, (int)($betNoticeNum[0] ?? 26));
  763. $betNoticeMax = max($betNoticeMini, (int)($betNoticeNum[1] ?? 38));
  764. $noticeNum = rand($betNoticeMini, $betNoticeMax);
  765. $openList = [];
  766. $memberList = [];
  767. $simulatedMembers = [];
  768. foreach ($list as $k => $settlementOrder) {
  769. $v = $settlementOrder->toArray();
  770. // 后台模拟会员:照常结算派奖,但不进群播报、不发私聊、不累计活动投注
  771. $simulated = MemberAudience::isSimulatedMember($v['member_id']);
  772. if ($simulated) $simulatedMembers[$v['member_id']] = true;
  773. $order_item = LhcOrderItem::where('ordernum', $v['ordernum'])->get()->toArray();
  774. $total_amount = 0;
  775. foreach($order_item as $value) {
  776. $number = $value['number'];
  777. // Payout uses the full stake; net result and effective bet use what the member paid after the coupon.
  778. $paid = PromotionCouponService::paid($v['amount'], $value['coupon_discount'] ?? 0);
  779. if ($simulated) {
  780. // 不计入播报的各玩法注额
  781. } elseif (isset($keywordsList[$number])) {
  782. $keywordsList[$number] += $v['amount'];
  783. } else {
  784. $keywordsList[$number] = $v['amount'];
  785. }
  786. $lastStr = self::hideMiddleDigits($v['member_id'], 4);
  787. $item = [];
  788. $item['id'] = $value['id'];
  789. $item['lottery_status'] = self::model()::STATUS_LOSS;//未中奖
  790. $item['profit_and_loss'] = bcsub('0', $paid, 2);
  791. if (in_array($number, $awards)) {
  792. $amount = $v['amount'];
  793. $odds = $v['odds'];
  794. //玩 大单 小单 大双 小双 :如果开出13和14 总注小于10000 1.5赔率含本,大于等于10000退本金。
  795. if (in_array('13操', $awards) || in_array('14操', $awards)) {
  796. // 13 14特殊处理倍率
  797. if (in_array($number, self::$OTHER_BET_2)) {
  798. if ($sum < 10000) {
  799. $odds = 1.5;
  800. } else {
  801. $odds = 1;
  802. }
  803. }
  804. }
  805. $profit = bcmul($amount, $odds, 2); // 保留两位小数
  806. if ($profit > 1000000) {
  807. $profit = 1000000; // 单注最高奖金1000000
  808. }
  809. $item['lottery_status'] = self::model()::STATUS_WIN;//中奖
  810. $item['win_amount'] = $profit;
  811. $total_amount += $profit;
  812. $yl = bcsub($profit, $paid, 2); // 盈利
  813. $item['profit_and_loss'] = $yl;
  814. if (!$simulated && !in_array('13操', $awards) && !in_array('14操', $awards)) {
  815. ActivityUserService::updateBettingAmount($v['member_id'], $paid);
  816. }
  817. $memberList[$v['member_id']][] = [
  818. 'member_id' => $v['member_id'],
  819. 'keywords' => $number,
  820. 'amount' => $v['amount'],
  821. 'profit' => $profit,
  822. 'yl' => $yl,
  823. ];
  824. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  825. $balance = $walletInfo['available_balance'];
  826. WalletService::updateBalance($v['member_id'], $profit);
  827. BalanceLogService::addLog($v['member_id'], $profit, $balance, ($balance + $profit), '中奖', $v['id'], "盈利:{$yl}");
  828. if (isset($openList[$v['member_id']])) {
  829. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  830. $openList[$v['member_id']]['amount'] += $v['amount'];
  831. $openList[$v['member_id']]['profit'] += $profit;
  832. $openList[$v['member_id']]['lastStr'] = $lastStr;
  833. $openList[$v['member_id']]['openKeywords'][] = $number . "({$odds}" . lang("倍率") . ")";
  834. $openList[$v['member_id']]['keywords'][] = $number;
  835. $openList[$v['member_id']]['win_amount'] += $v['amount'];
  836. } else {
  837. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  838. $openList[$v['member_id']]['amount'] = $v['amount'];
  839. $openList[$v['member_id']]['profit'] = $profit;
  840. $openList[$v['member_id']]['lastStr'] = $lastStr;
  841. $openList[$v['member_id']]['openKeywords'] = [];
  842. $openList[$v['member_id']]['keywords'] = [];
  843. $openList[$v['member_id']]['openKeywords'][] = $number . "({$odds}" . lang("倍率") . ")";
  844. $openList[$v['member_id']]['keywords'][] = $number;
  845. $openList[$v['member_id']]['win_amount'] = $v['amount'];
  846. $openList[$v['member_id']]['is_send'] = true;
  847. }
  848. } else {
  849. if (!$simulated && !in_array('13操', $awards) && !in_array('14操', $awards)) {
  850. ActivityUserService::updateBettingAmount($v['member_id'], $paid);
  851. }
  852. // 未命中的组合不应用 13/14 特殊赔率。
  853. $profit = 0;
  854. if (isset($openList[$v['member_id']])) {
  855. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  856. $openList[$v['member_id']]['amount'] += $v['amount'];
  857. $openList[$v['member_id']]['profit'] += $profit;
  858. $openList[$v['member_id']]['lastStr'] = $lastStr;
  859. $openList[$v['member_id']]['keywords'][] = $number;
  860. } else {
  861. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  862. $openList[$v['member_id']]['amount'] = $v['amount'];
  863. $openList[$v['member_id']]['profit'] = $profit;
  864. $openList[$v['member_id']]['lastStr'] = $lastStr;
  865. $openList[$v['member_id']]['openKeywords'] = [];
  866. $openList[$v['member_id']]['keywords'] = [];
  867. $openList[$v['member_id']]['keywords'][] = $number;
  868. $openList[$v['member_id']]['win_amount'] = 0;
  869. $openList[$v['member_id']]['is_send'] = true;
  870. }
  871. }
  872. LhcOrderItem::where('id', $value['id'])->update($item);
  873. }
  874. if ($total_amount > 0) {
  875. LhcOrder::where("id", $v['id'])->update(['lottery_status' => '2', 'win_amount' => $total_amount]);
  876. } else {
  877. LhcOrder::where("id", $v['id'])->update(['lottery_status' => '1', 'win_amount' => 0]);
  878. }
  879. \App\Services\Reporting\ReportContext::lotterySettled((int) $v['id']);
  880. AdminRealtime::record('lhc_order', $v['id']);
  881. }
  882. $openList = array_diff_key($openList, $simulatedMembers);
  883. $openList = array_merge($openList, $fakeOpenData['list']);
  884. self::lotteryNotice($openList, $issue_no, $keywordsList);
  885. }
  886. // 虚拟开奖
  887. public static function fakeLotteryDraw($issue_no, $awards, $rand_num = 30)
  888. {
  889. $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
  890. $text = "";
  891. $keywordsList = [];
  892. $fakeOtherSum = 0;
  893. $openList = [];
  894. foreach ($fake_bet_list as $k => $v) {
  895. if (in_array($v['keywords'], ['大', '小', '单', '双'])) {
  896. $fakeOtherSum += $v['amount'];
  897. }
  898. if (isset($keywordsList[$v['keywords']])) {
  899. $keywordsList[$v['keywords']] += $v['amount'];
  900. } else {
  901. $keywordsList[$v['keywords']] = $v['amount'];
  902. }
  903. // $lastStr = self::getLastChar($v['first_name'], 1);
  904. $lastStr = self::hideMiddleDigits($v['member_id'], 4);
  905. if (in_array($v['keywords'], $awards)) {
  906. $amount = $v['amount'];
  907. $odds = $v['odds'];
  908. $profit = bcmul($amount, $odds, 2); // 保留两位小数
  909. if ($profit > 880000) {
  910. $profit = 880000; // 单注最高奖金880000
  911. }
  912. $item['profit'] = $profit;
  913. // $v['amount'] = number_format($amount,2);
  914. if (isset($openList[$v['member_id']])) {
  915. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  916. $openList[$v['member_id']]['amount'] += $v['amount'];
  917. $openList[$v['member_id']]['profit'] += $profit;
  918. $openList[$v['member_id']]['lastStr'] = $lastStr;
  919. $openList[$v['member_id']]['openKeywords'][] = $v['keywords'] . "({$odds}" . lang("倍率") . ")";
  920. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  921. $openList[$v['member_id']]['win_amount'] += $v['amount'];
  922. } else {
  923. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  924. $openList[$v['member_id']]['amount'] = $v['amount'];
  925. $openList[$v['member_id']]['profit'] = $profit;
  926. $openList[$v['member_id']]['lastStr'] = $lastStr;
  927. $openList[$v['member_id']]['openKeywords'] = [];
  928. $openList[$v['member_id']]['keywords'] = [];
  929. $openList[$v['member_id']]['openKeywords'][] = $v['keywords'] . "({$odds}" . lang("倍率") . ")";
  930. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  931. $openList[$v['member_id']]['win_amount'] = $v['amount'];
  932. $openList[$v['member_id']]['is_send'] = false;
  933. }
  934. } else {
  935. if (isset($openList[$v['member_id']])) {
  936. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  937. $openList[$v['member_id']]['amount'] += $v['amount'];
  938. $openList[$v['member_id']]['lastStr'] = $lastStr;
  939. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  940. } else {
  941. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  942. $openList[$v['member_id']]['amount'] = $v['amount'];
  943. $openList[$v['member_id']]['profit'] = 0;
  944. $openList[$v['member_id']]['lastStr'] = $lastStr;
  945. $openList[$v['member_id']]['openKeywords'] = [];
  946. $openList[$v['member_id']]['keywords'] = [];
  947. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  948. $openList[$v['member_id']]['win_amount'] = 0;
  949. $openList[$v['member_id']]['is_send'] = false;
  950. }
  951. }
  952. }
  953. return ['sum' => $fakeOtherSum, 'list' => $openList, 'keywordsList' => $keywordsList];
  954. }
  955. // 开奖通知
  956. public static function lotteryNotice($openList, $issue_no, $keywordsList = [])
  957. {
  958. $betNoticeNum = Config::where('field', 'bet_notice_num')->first()->val;
  959. $betNoticeNum = explode(',', $betNoticeNum);
  960. $betNoticeMini = max(1, (int)($betNoticeNum[0] ?? 26));
  961. $betNoticeMax = max($betNoticeMini, (int)($betNoticeNum[1] ?? 38));
  962. $noticeNum = rand($betNoticeMini, $betNoticeMax);
  963. // shuffle($openList);
  964. // Log::error('lotteryNotice openList', $openList);
  965. $lang = App::getLocale();
  966. $group_language = Config::where('field', 'group_language')->first()->val;
  967. App::setLocale($group_language);
  968. $text = "{$issue_no} " . lang("期开奖结果");
  969. $text .= "\n-----" . lang("本期开奖账单") . "----- \n";
  970. App::setLocale($lang);
  971. $noticeIndex = 0;
  972. $noticeRows = 0;
  973. foreach ($openList as $v) {
  974. $noticeIndex++;
  975. $amount = number_format($v['amount'], 2);
  976. $v['win_amount'] = number_format($v['win_amount'], 2);
  977. $profit = number_format($v['profit'], 2);
  978. $yl = bcsub($v['profit'], $v['amount'], 2); // 盈利
  979. if (empty($v['openKeywords'])) {
  980. $openKeyword = '-';
  981. } else {
  982. $openKeyword = implode(',', $v['openKeywords']);
  983. }
  984. if ($noticeIndex <= $noticeNum) {
  985. $noticeRows++;
  986. $lang = App::getLocale();
  987. $group_language = Config::where('field', 'group_language')->first()->val;
  988. App::setLocale($group_language);
  989. $text .= lang("用户ID") . ":{$v['lastStr']} \n";
  990. $text .= lang("下注类型") . ":[" . implode(',', $v['keywords']) . "] \n";
  991. $text .= lang('中奖类型') . ":[" . $openKeyword . "] \n";
  992. $text .= lang('投注金额') . ":{$amount} \n";
  993. $text .= lang('中奖金额') . ":{$v['win_amount']} \n";
  994. $text .= lang('派彩金额') . ":{$profit} \n";
  995. $text .= lang("盈亏金额") . ":{$yl} \n";
  996. $text .= "-------------------------------- \n";
  997. App::setLocale($lang);
  998. }
  999. //根据环境变量配置是否发送电报
  1000. if ($v['is_send'] && config('services.telegram.send_enabled', false)) {
  1001. $language = User::where('member_id', $v['member_id'])->first()->language;
  1002. $wallet = WalletService::findOne(['member_id' => $v['member_id']]);
  1003. App::setLocale($language);
  1004. $text2 = lang("{issue_no}期开奖结果");
  1005. $text2 = str_replace("{issue_no}", $issue_no, $text2);
  1006. $text2 .= "\n";
  1007. $text2 .= lang('下注类型') . ":[" . implode(',', $v['keywords']) . "] \n";
  1008. $text2 .= lang('中奖类型') . ":[" . $openKeyword . "] \n";
  1009. $text2 .= lang('投注金额') . ":{$amount} \n";
  1010. $text2 .= lang('中奖金额') . ":{$v['win_amount']} \n";
  1011. $text2 .= lang('派彩金额') . ":{$profit} \n";
  1012. $text2 .= lang("盈亏金额") . ":{$yl} \n";
  1013. $balance = floatval($wallet->available_balance);
  1014. $text2 .= "----------------\n";
  1015. $text2 .= "当前余额:{$balance} \n";
  1016. $keyboard = [];
  1017. $keyboard[] = [
  1018. ['text' => lang("开奖历史"), 'callback_data' => "showLotteryHistory@@" . $issue_no]
  1019. ];
  1020. // self::sendMessage($v['member_id'],$text2,$keyboard);
  1021. SendTelegramMessageJob::dispatch($v['member_id'], $text2, $keyboard)->afterCommit();
  1022. }
  1023. }
  1024. $inlineButton = self::getOperateButton();
  1025. //根据环境变量配置是否发送电报
  1026. if (config('services.telegram.send_enabled', false)) {
  1027. // 群通知
  1028. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  1029. if (($pc28Switch == 0 && is_numeric($issue_no)) || $pc28Switch == 1 && !is_numeric($issue_no)) {
  1030. Log::channel('issue')->info('结算列表群通知入队', [
  1031. 'issue_no' => $issue_no,
  1032. 'pc28_switch' => $pc28Switch,
  1033. 'text_length' => strlen($text),
  1034. 'open_list_count' => count($openList),
  1035. 'notice_num' => $noticeNum,
  1036. 'notice_rows' => $noticeRows,
  1037. ]);
  1038. SendTelegramGroupMessageJob::dispatch($text, $inlineButton, '', false, '--------------------------------', [
  1039. 'message_type' => 'settlement_list',
  1040. 'issue_no' => $issue_no,
  1041. 'open_list_count' => count($openList),
  1042. 'notice_rows' => $noticeRows,
  1043. ])->afterCommit();
  1044. } else {
  1045. Log::channel('issue')->info('结算列表群通知跳过', [
  1046. 'issue_no' => $issue_no,
  1047. 'pc28_switch' => $pc28Switch,
  1048. 'reason' => 'pc28_switch_not_match_issue_type',
  1049. ]);
  1050. }
  1051. } else {
  1052. Log::channel('issue')->info('结算列表群通知跳过', [
  1053. 'issue_no' => $issue_no,
  1054. 'reason' => 'send_telegram_disabled',
  1055. ]);
  1056. }
  1057. }
  1058. /**
  1059. * @description: 统计投注情况通知
  1060. * @param {*} $issue_no
  1061. * @return {*}
  1062. */
  1063. public static function statNotice($issue_no)
  1064. {
  1065. $keywordsList = [];
  1066. // 虚拟投注情况
  1067. $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
  1068. foreach ($fake_bet_list as $k => $v) {
  1069. if (isset($keywordsList[$v['keywords']])) {
  1070. $keywordsList[$v['keywords']] += $v['amount'];
  1071. } else {
  1072. $keywordsList[$v['keywords']] = $v['amount'];
  1073. }
  1074. }
  1075. // 真实投注
  1076. $list = self::findAll(['issue_no' => $issue_no]);
  1077. foreach ($list->toArray() as $k => $v) {
  1078. if (isset($keywordsList[$v['keywords']])) {
  1079. $keywordsList[$v['keywords']] += $v['amount'];
  1080. } else {
  1081. $keywordsList[$v['keywords']] = $v['amount'];
  1082. }
  1083. }
  1084. $lang = App::getLocale();
  1085. $group_language = Config::where('field', 'group_language')->first()->val;
  1086. App::setLocale($group_language);
  1087. $text3 = "📝 {$issue_no}" . lang('期投注统计') . " \n";
  1088. $text3 .= lang("玩法") . " " . lang("总投") . " \n";
  1089. App::setLocale($lang);
  1090. if ($keywordsList) {
  1091. uksort($keywordsList, 'custom_sort');
  1092. // ksort($keywordsList);
  1093. foreach ($keywordsList as $k => $v) {
  1094. $text3 .= "{$k} {$v} \n";
  1095. }
  1096. }
  1097. $inlineButton = self::getOperateButton();
  1098. // 投注统计的消息不发了
  1099. // SendTelegramGroupMessageJob::dispatch($text3, $inlineButton, '');
  1100. }
  1101. public static function todayExchangeRate($chatId)
  1102. {
  1103. $exchangeRate = Config::where('field', 'exchange_rate_rmb')->first()->val;
  1104. $text = lang("今日汇率") . ":1USDT = {$exchangeRate} RMB \n";
  1105. // $botMsg = [
  1106. // 'chat_id' => "@{$chatId}",
  1107. // 'text' => $text
  1108. // ];
  1109. self::sendMessage($chatId, $text);
  1110. // return $botMsg;
  1111. }
  1112. }