BetService.php 52 KB

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