BetService.php 45 KB

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