BetService.php 42 KB

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