BetService.php 45 KB

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