BetService.php 44 KB

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