BetService.php 42 KB

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