BetService.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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}\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. Cache::add("封盘后下注单数_{$issueInfo->issue_no}", rand(3, 5),4);
  374. if (strtotime($issueInfo['end_time']) - time() < IssueService::COUNTDOWN_TO_CLOSING_THE_MARKET) {
  375. $fake_bet_count = Cache::get("fake_bet_count_{$issueInfo->issue_no}", 0);
  376. $cc = Cache::get("封盘后下注单数_{$issueInfo->issue_no}");
  377. if ($fake_bet_count >= $cc) return;
  378. }
  379. $randKey = array_rand($gameplayRuleList, 1);
  380. $gameplayRuleInfo = $gameplayRuleList[$randKey] ?? [];
  381. if ($gameplayRuleInfo) {
  382. if (in_array($gameplayRuleInfo['keywords'], $noRule)) {
  383. return;
  384. }
  385. if ($gameplayRuleInfo['maxinum'] < $betMax) {
  386. $betMax = $gameplayRuleInfo['maxinum'];
  387. }
  388. if ($gameplayRuleInfo['mininum'] > $betMini) {
  389. $betMini = $gameplayRuleInfo['mininum'];
  390. }
  391. $amount = rand($betMini, $betMax);
  392. $amount = floor($amount / 10) * 10;
  393. $input = $gameplayRuleInfo['keywords'] . ' ' . $amount;
  394. // $amount = number_format($amount,2);
  395. $item = [];
  396. $item['keywords'] = $gameplayRuleInfo['keywords'];
  397. $item['odds'] = $gameplayRuleInfo['odds'];
  398. $item['amount'] = $amount;
  399. $item['first_name'] = self::generateRandomString(6);
  400. $item['member_id'] = $member_id;
  401. $item['profit'] = 0;
  402. // $input = $item['keywords'] . $item['amount'];
  403. $fake_bet_list[] = $item;
  404. $lastStr = self::hideMiddleDigits($item['member_id'], 4);
  405. $lang = App::getLocale();
  406. $group_language = Config::where('field', 'group_language')->first()->val;
  407. App::setLocale($group_language);
  408. $groupText = lang('会员下注') . " 【" . $lastStr . "】 \n";
  409. $groupText .= lang('下注期数') . ":{$issueInfo->issue_no} \n";
  410. $groupText .= lang('下注内容') . ": \n";
  411. $groupText .= "----------- \n";
  412. $groupText .= "{$input} \n";
  413. $groupText .= "----------- \n";
  414. App::setLocale($lang);
  415. if (strtotime($issueInfo['end_time']) - strtotime($now_date) < 22) {
  416. $fake_bet_count = Cache::get("fake_bet_count_{$issueInfo->issue_no}", 0);
  417. Cache::put("fake_bet_count_{$issueInfo->issue_no}", $fake_bet_count + 1, 500);
  418. }
  419. $inlineButton = self::getOperateButton();
  420. // 群通知
  421. self::asyncBettingGroupNotice($groupText, $inlineButton); // 异步群通知
  422. }
  423. }
  424. Cache::put('fake_bet_' . $issueInfo->issue_no, $fake_bet_list, 500);
  425. }
  426. }
  427. }
  428. }
  429. /**
  430. * @description: 当期下注
  431. * @param {*} $memberId
  432. * @return {*}
  433. */
  434. public static function currentBet($memberId)
  435. {
  436. $msg['chat_id'] = $memberId;
  437. // 期数验证
  438. $issueInfo = IssueService::model()::where('status', IssueService::model()::STATUS_BETTING)->orderBy('id', 'desc')->first();
  439. $issue_no = '';
  440. if (!empty($issueInfo)) {
  441. $issue_no = $issueInfo->issue_no;
  442. } else {
  443. $issueCloseInfo = IssueService::model()::where('status', IssueService::model()::STATUS_CLOSE)->orderBy('id', 'desc')->first();
  444. if (empty($issueCloseInfo)) {
  445. $issue_no = $issueCloseInfo->issue_no;
  446. }
  447. }
  448. if ($issue_no) {
  449. $text = lang("期数") . " {$issue_no} \n";
  450. // $text .= "\n";
  451. // $text .= "----------\n";
  452. $list = self::findAll(['member_id' => $memberId, 'issue_no' => $issue_no]);
  453. $list = $list->toArray();
  454. if (empty($list)) {
  455. $text .= lang("本期暂未下注") . "! \n";
  456. } else {
  457. $keywords = implode(',', array_column($list, 'keywords'));
  458. $amounts = implode(',', array_column($list, 'amount'));
  459. $text .= lang("下注类型") . ":[" . $keywords . "] \n";
  460. $text .= lang("下注金额") . ":" . $amounts . " \n";
  461. $text .= lang("下注总额") . ":" . array_sum(array_column($list, 'amount')) . " \n";
  462. $text .= lang("开奖状态") . ":" . lang("等待开奖") . " \n";
  463. }
  464. // foreach ($list->toArray() as $k => $v) {
  465. // $text .= "{$v['keywords']}{$v['amount']} \n";
  466. // }
  467. // $text .= "\n";
  468. // $text .= "----------\n";
  469. $msg['text'] = $text;
  470. } else {
  471. $msg['text'] = lang("当前没有开放的投注期数") . "! \n";
  472. }
  473. return $msg;
  474. }
  475. /**
  476. * @description: 近期投注
  477. * @param {*} $memberId
  478. * @return {*}
  479. */
  480. public static function recentlyRecord($memberId, $page = 1, $limit = 5)
  481. {
  482. $list = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->orderBy('id', 'desc')->forPage($page, $limit)->get();
  483. // $text = "```\n";
  484. $text = "";
  485. $text .= "期数--内容--盈亏 \n";
  486. foreach ($list->toArray() as $k => $v) {
  487. $profit = $v['profit'] - $v['amount'];
  488. // $text .= $v['issue_no']." ".$v['keywords']." ".$v['amount']." ".$v['profit']."\n";
  489. $item = $v['issue_no'] . "==" . $v['keywords'] . rtrim(rtrim(number_format($v['amount'], 2, '.', ''), '0'), '.') . "==" . rtrim(rtrim(number_format($profit, 2, '.', ''), '0'), '.') . "\n";
  490. $text .= $item;
  491. }
  492. // $text .= "```\n";
  493. return $text;
  494. }
  495. /**
  496. * @description: 投注记录
  497. * @param {*} $memberId
  498. * @param {*} $page
  499. * @param {*} $limit
  500. * @return {*}
  501. */
  502. public static function record($memberId, $messageId = null, $page = 1, $limit = 5)
  503. {
  504. $type = Cache::get('message_id_bet_record_' . $memberId, 0);
  505. if ($type == 0) {
  506. $type = '';
  507. }
  508. $msg['chat_id'] = $memberId;
  509. $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();
  510. $count = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->where(self::getWhere(['is_winner' => $type]))->count();
  511. $keyboard = [];
  512. $total_amount = BalanceLogService::model()::where('member_id', $memberId)->where('change_type', '中奖')->sum('amount');
  513. $total_amount = number_format($total_amount, 2);
  514. $text = lang("历史注单") . " \n";
  515. $text .= lang("中奖总派彩") . ":{$total_amount} \n";
  516. foreach ($list as $k => $v) {
  517. if ($v->status == self::model()::STATUS_SETTLED) {
  518. $phase = $v->profit - $v->amount;
  519. } else {
  520. $phase = lang('待开奖');
  521. }
  522. $text .= "-------------------------------------\n";
  523. $text .= lang("期数") . ":{$v->issue_no} \n";
  524. $text .= lang("内容") . ":{$v->keywords} \n";
  525. $text .= lang("金额") . ":{$v->amount} \n";
  526. $text .= lang("盈亏") . ":{$phase} \n";
  527. }
  528. $msg['text'] = $text;
  529. $keyboard[] = [
  530. ['text' => lang("全部"), 'callback_data' => "betRecordType@@0"],
  531. ['text' => lang("盈利"), 'callback_data' => "betRecordType@@1"],
  532. ['text' => lang("亏损"), 'callback_data' => "betRecordType@@2"]
  533. ];
  534. if ($page > 1) {
  535. $keyboard[] = [
  536. ['text' => "👆" . lang("上一页"), 'callback_data' => "betRecordNextPage@@" . ($page - 1)]
  537. ];
  538. }
  539. $allPage = ceil($count / $limit);
  540. if ($allPage > $page) {
  541. if ($page > 1) {
  542. $keyboard[count($keyboard) - 1][] = ['text' => "👇" . lang("下一页"), 'callback_data' => "betRecordNextPage@@" . ($page + 1)];
  543. } else {
  544. $keyboard[] = [
  545. ['text' => "👇" . lang("下一页"), 'callback_data' => "betRecordNextPage@@" . ($page + 1)]
  546. ];
  547. }
  548. }
  549. if ($messageId) {
  550. $msg['message_id'] = $messageId;
  551. }
  552. if ($keyboard) {
  553. $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  554. }
  555. return $msg;
  556. }
  557. /**
  558. * @description: 开奖失败退回投注的
  559. * @param {*} $issue_no
  560. * @return {*}
  561. */
  562. public static function betFail($issue_no)
  563. {
  564. $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
  565. foreach ($list->toArray() as $k => $v) {
  566. $profit = $v['amount'];
  567. WalletService::updateBalance($v['member_id'], $profit);
  568. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  569. $balance = $walletInfo['available_balance'];
  570. BalanceLogService::addLog($v['member_id'], $profit, $balance, ($balance + $profit), '中奖', $v['id'], '');
  571. $text = $issue_no . "期开奖失败 \n";
  572. $text .= "投注类型:{$v['keywords']} \n";
  573. $text .= "投注金额:{$v['amount']} \n";
  574. $text .= "投注的资金已退回您的钱包 \n";
  575. self::asyncSendMessage($v['member_id'], $text);
  576. $item = [];
  577. $iem['status'] = self::model()::STATUS_SETTLED;
  578. self::model()::where('id', $v['id'])->update($item);
  579. }
  580. }
  581. /**
  582. * @description: 中奖结算
  583. * @param {*} $issue_no
  584. * @param {*} $awards
  585. * @return {*}
  586. */
  587. public static function betSettled($issue_no, $awards)
  588. {
  589. $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
  590. // 大小单双的
  591. $otherSum = self::model()::where('issue_no', $issue_no)->where('status', self::model()::STATUS_STAY)->whereIn('keywords', ['大', '小', '单', '双'])->sum('amount');
  592. $fakeOpenData = self::fakeLotteryDraw($issue_no, $awards, 0);
  593. $keywordsList = $fakeOpenData['keywordsList'];
  594. $fakeOtherSum = $fakeOpenData['sum'];
  595. $sum = $otherSum + $fakeOtherSum;
  596. $betNoticeNum = Config::where('field', 'bet_notice_num')->first()->val;
  597. $betNoticeNum = explode(',', $betNoticeNum);
  598. $betNoticeMini = $betNoticeNum[0] ?? 26;
  599. $betNoticeMax = $betNoticeNum[1] ?? 38;
  600. $noticeNum = rand($betNoticeMini, $betNoticeMax);
  601. $realNoticeNum = ceil($noticeNum / 2);
  602. $openList = [];
  603. $memberList = [];
  604. $bet_num = 0;
  605. foreach ($list->toArray() as $k => $v) {
  606. if (isset($keywordsList[$v['keywords']])) {
  607. $keywordsList[$v['keywords']] += $v['amount'];
  608. } else {
  609. $keywordsList[$v['keywords']] = $v['amount'];
  610. }
  611. // $userInfo = UserService::findAll(['member_id' => $v['member_id']]);
  612. // $lastStr = self::getLastChar($userInfo->first_name, 1);
  613. $lastStr = self::hideMiddleDigits($v['member_id'], 4);
  614. $item = [];
  615. $item['id'] = $v['id'];
  616. $item['status'] = self::model()::STATUS_SETTLED;
  617. if (in_array($v['keywords'], $awards)) {
  618. // $profit = $v['amount'] * $v['odds'];
  619. $amount = $v['amount'];
  620. // $amount = rtrim($amount, '0'); // 去掉右侧的 0
  621. // $amount = rtrim($amount, '.'); // 如果末尾是 . 就去掉
  622. $odds = $v['odds'];
  623. if (in_array('13操', $awards) || in_array('14操', $awards)) {
  624. // 13 14特殊处理倍率
  625. if (in_array($v['keywords'], self::$OTHER_BET_1)) {
  626. if ($sum < 10000) {
  627. $odds = 1.5;
  628. } else {
  629. $odds = 1;
  630. }
  631. }
  632. if (in_array($v['keywords'], self::$OTHER_BET_2)) {
  633. $odds = 1;
  634. }
  635. }
  636. $profit = bcmul($amount, $odds, 2); // 保留两位小数
  637. if ($profit > 1000000) {
  638. $profit = 1000000; // 单注最高奖金1000000
  639. }
  640. $item['profit'] = $profit;
  641. // $yl = $profit - $amount;
  642. $yl = bcsub($profit, $amount, 2); // 盈利
  643. if (!in_array('13操', $awards) && !in_array('14操', $awards)) {
  644. Rebate::updateProfit([
  645. 'member_id' => $v['member_id'],
  646. 'profit' => $yl,
  647. ]);
  648. }
  649. $memberList[$v['member_id']][] = [
  650. 'member_id' => $v['member_id'],
  651. 'keywords' => $v['keywords'],
  652. 'amount' => $v['amount'],
  653. 'profit' => $profit,
  654. 'yl' => $yl,
  655. ];
  656. // 结算
  657. WalletService::updateBalance($v['member_id'], $profit);
  658. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  659. $balance = $walletInfo['available_balance'];
  660. BalanceLogService::addLog($v['member_id'], $profit, $balance, ($balance + $profit), '中奖', $v['id'], '');
  661. if (isset($openList[$v['member_id']])) {
  662. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  663. $openList[$v['member_id']]['amount'] += $v['amount'];
  664. $openList[$v['member_id']]['profit'] += $profit;
  665. $openList[$v['member_id']]['lastStr'] = $lastStr;
  666. $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
  667. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  668. $openList[$v['member_id']]['win_amount'] += $v['amount'];
  669. } else {
  670. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  671. $openList[$v['member_id']]['amount'] = $v['amount'];
  672. $openList[$v['member_id']]['profit'] = $profit;
  673. $openList[$v['member_id']]['lastStr'] = $lastStr;
  674. $openList[$v['member_id']]['openKeywords'] = [];
  675. $openList[$v['member_id']]['keywords'] = [];
  676. $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
  677. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  678. $openList[$v['member_id']]['win_amount'] = $v['amount'];
  679. $openList[$v['member_id']]['is_send'] = true;
  680. }
  681. } else {
  682. if (!in_array('13操', $awards) && !in_array('14操', $awards)) {
  683. Rebate::updateProfit([
  684. 'member_id' => $v['member_id'],
  685. 'profit' => ($v['amount'] * -1),
  686. ]);
  687. }
  688. $profit = 0;
  689. if (in_array('13操', $awards) || in_array('14操', $awards)) {
  690. $amount = $v['amount'];
  691. $odds = 0;
  692. // 13 14特殊处理倍率
  693. if (in_array($v['keywords'], self::$OTHER_BET_1)) {
  694. if ($sum < 10000) {
  695. $odds = 1.5;
  696. } else {
  697. $odds = 1;
  698. }
  699. }
  700. if (in_array($v['keywords'], self::$OTHER_BET_2)) {
  701. $odds = 1;
  702. }
  703. $profit = bcmul($amount, $odds, 2); // 保留两位小数
  704. if ($profit > 1000000) {
  705. $profit = 1000000; // 单注最高奖金1000000
  706. }
  707. $item['profit'] = $profit;
  708. $yl = bcsub($profit, $amount, 2); // 盈利
  709. WalletService::updateBalance($v['member_id'], $profit);
  710. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  711. $balance = $walletInfo['available_balance'];
  712. BalanceLogService::addLog($v['member_id'], $profit, $balance, ($balance + $profit), '中奖', $v['id'], '');
  713. }
  714. if (isset($openList[$v['member_id']])) {
  715. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  716. $openList[$v['member_id']]['amount'] += $v['amount'];
  717. $openList[$v['member_id']]['profit'] += $profit;
  718. $openList[$v['member_id']]['lastStr'] = $lastStr;
  719. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  720. } else {
  721. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  722. $openList[$v['member_id']]['amount'] = $v['amount'];
  723. $openList[$v['member_id']]['profit'] = $profit;
  724. $openList[$v['member_id']]['lastStr'] = $lastStr;
  725. $openList[$v['member_id']]['openKeywords'] = [];
  726. $openList[$v['member_id']]['keywords'] = [];
  727. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  728. $openList[$v['member_id']]['win_amount'] = 0;
  729. $openList[$v['member_id']]['is_send'] = true;
  730. }
  731. }
  732. self::model()::where('id', $v['id'])->update($item);
  733. }
  734. $openList = array_merge($openList, $fakeOpenData['list']);
  735. self::lotteryNotice($openList, $issue_no, $keywordsList);
  736. }
  737. // 虚拟开奖
  738. public static function fakeLotteryDraw($issue_no, $awards, $rand_num = 30)
  739. {
  740. $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
  741. $text = "";
  742. $keywordsList = [];
  743. $fakeOtherSum = 0;
  744. $openList = [];
  745. foreach ($fake_bet_list as $k => $v) {
  746. if (in_array($v['keywords'], ['大', '小', '单', '双'])) {
  747. $fakeOtherSum += $v['amount'];
  748. }
  749. if (isset($keywordsList[$v['keywords']])) {
  750. $keywordsList[$v['keywords']] += $v['amount'];
  751. } else {
  752. $keywordsList[$v['keywords']] = $v['amount'];
  753. }
  754. // $lastStr = self::getLastChar($v['first_name'], 1);
  755. $lastStr = self::hideMiddleDigits($v['member_id'], 4);
  756. if (in_array($v['keywords'], $awards)) {
  757. $amount = $v['amount'];
  758. $odds = $v['odds'];
  759. $profit = bcmul($amount, $odds, 2); // 保留两位小数
  760. if ($profit > 880000) {
  761. $profit = 880000; // 单注最高奖金880000
  762. }
  763. $item['profit'] = $profit;
  764. // $v['amount'] = number_format($amount,2);
  765. if (isset($openList[$v['member_id']])) {
  766. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  767. $openList[$v['member_id']]['amount'] += $v['amount'];
  768. $openList[$v['member_id']]['profit'] += $profit;
  769. $openList[$v['member_id']]['lastStr'] = $lastStr;
  770. $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
  771. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  772. $openList[$v['member_id']]['win_amount'] += $v['amount'];
  773. } else {
  774. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  775. $openList[$v['member_id']]['amount'] = $v['amount'];
  776. $openList[$v['member_id']]['profit'] = $profit;
  777. $openList[$v['member_id']]['lastStr'] = $lastStr;
  778. $openList[$v['member_id']]['openKeywords'] = [];
  779. $openList[$v['member_id']]['keywords'] = [];
  780. $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
  781. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  782. $openList[$v['member_id']]['win_amount'] = $v['amount'];
  783. $openList[$v['member_id']]['is_send'] = false;
  784. }
  785. } else {
  786. if (isset($openList[$v['member_id']])) {
  787. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  788. $openList[$v['member_id']]['amount'] += $v['amount'];
  789. $openList[$v['member_id']]['lastStr'] = $lastStr;
  790. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  791. } else {
  792. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  793. $openList[$v['member_id']]['amount'] = $v['amount'];
  794. $openList[$v['member_id']]['profit'] = 0;
  795. $openList[$v['member_id']]['lastStr'] = $lastStr;
  796. $openList[$v['member_id']]['openKeywords'] = [];
  797. $openList[$v['member_id']]['keywords'] = [];
  798. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  799. $openList[$v['member_id']]['win_amount'] = 0;
  800. $openList[$v['member_id']]['is_send'] = false;
  801. }
  802. }
  803. }
  804. return ['sum' => $fakeOtherSum, 'list' => $openList, 'keywordsList' => $keywordsList];
  805. }
  806. // 开奖通知
  807. public static function lotteryNotice($openList, $issue_no, $keywordsList = [])
  808. {
  809. $betNoticeNum = Config::where('field', 'bet_notice_num')->first()->val;
  810. $betNoticeNum = explode(',', $betNoticeNum);
  811. $betNoticeMini = $betNoticeNum[0] ?? 26;
  812. $betNoticeMax = $betNoticeNum[1] ?? 38;
  813. $noticeNum = rand($betNoticeMini, $betNoticeMax);
  814. // shuffle($openList);
  815. // Log::error('lotteryNotice openList', $openList);
  816. $lang = App::getLocale();
  817. $group_language = Config::where('field', 'group_language')->first()->val;
  818. App::setLocale($group_language);
  819. $text = "{$issue_no} " . lang("期开奖结果");
  820. $text .= "\n-----" . lang("本期开奖账单") . "----- \n";
  821. App::setLocale($lang);
  822. foreach ($openList as $k => $v) {
  823. $amount = number_format($v['amount'], 2);
  824. $v['win_amount'] = number_format($v['win_amount'], 2);
  825. $profit = number_format($v['profit'], 2);
  826. $yl = bcsub($v['profit'], $v['amount'], 2); // 盈利
  827. if (empty($v['openKeywords'])) {
  828. $openKeyword = '-';
  829. } else {
  830. $openKeyword = implode(',', $v['openKeywords']);
  831. }
  832. if (($k + 1) <= $noticeNum) {
  833. $lang = App::getLocale();
  834. $group_language = Config::where('field', 'group_language')->first()->val;
  835. App::setLocale($group_language);
  836. $text .= lang("用户ID") . ":{$v['lastStr']} \n";
  837. $text .= lang("下注类型") . ":[" . implode(',', $v['keywords']) . "] \n";
  838. $text .= lang('中奖类型') . ":[" . $openKeyword . "] \n";
  839. $text .= lang('投注金额') . ":{$amount} \n";
  840. $text .= lang('中奖金额') . ":{$v['win_amount']} \n";
  841. $text .= lang('派彩金额') . ":{$profit} \n";
  842. $text .= lang("盈亏金额") . ":{$yl} \n";
  843. $text .= "-------------------------------- \n";
  844. App::setLocale($lang);
  845. }
  846. if ($v['is_send']) {
  847. $language = User::where('member_id', $v['member_id'])->first()->language;
  848. App::setLocale($language);
  849. $text2 = lang("{issue_no}期开奖结果");
  850. $text2 = str_replace("{issue_no}", $issue_no, $text2);
  851. $text2 .= "\n";
  852. $text2 .= lang('下注类型') . ":[" . implode(',', $v['keywords']) . "] \n";
  853. $text2 .= lang('中奖类型') . ":[" . $openKeyword . "] \n";
  854. $text2 .= lang('投注金额') . ":{$amount} \n";
  855. $text2 .= lang('中奖金额') . ":{$v['win_amount']} \n";
  856. $text2 .= lang('派彩金额') . ":{$profit} \n";
  857. $text2 .= lang("盈亏金额") . ":{$yl} \n";
  858. $keyboard = [];
  859. $keyboard[] = [
  860. ['text' => lang("开奖历史"), 'callback_data' => "showLotteryHistory@@" . $issue_no]
  861. ];
  862. // self::sendMessage($v['member_id'],$text2,$keyboard);
  863. SendTelegramMessageJob::dispatch($v['member_id'], $text2, $keyboard);
  864. }
  865. }
  866. $inlineButton = self::getOperateButton();
  867. // 群通知
  868. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  869. if (($pc28Switch == 0 && is_numeric($issue_no)) || $pc28Switch == 1 && !is_numeric($issue_no)) {
  870. SendTelegramGroupMessageJob::dispatch($text, $inlineButton, '', false, '--------------------------------');
  871. }
  872. }
  873. /**
  874. * @description: 统计投注情况通知
  875. * @param {*} $issue_no
  876. * @return {*}
  877. */
  878. public static function statNotice($issue_no)
  879. {
  880. $keywordsList = [];
  881. // 虚拟投注情况
  882. $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
  883. foreach ($fake_bet_list as $k => $v) {
  884. if (isset($keywordsList[$v['keywords']])) {
  885. $keywordsList[$v['keywords']] += $v['amount'];
  886. } else {
  887. $keywordsList[$v['keywords']] = $v['amount'];
  888. }
  889. }
  890. // 真实投注
  891. $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
  892. foreach ($list->toArray() as $k => $v) {
  893. if (isset($keywordsList[$v['keywords']])) {
  894. $keywordsList[$v['keywords']] += $v['amount'];
  895. } else {
  896. $keywordsList[$v['keywords']] = $v['amount'];
  897. }
  898. }
  899. $lang = App::getLocale();
  900. $group_language = Config::where('field', 'group_language')->first()->val;
  901. App::setLocale($group_language);
  902. $text3 = "📝 {$issue_no}" . lang('期投注统计') . " \n";
  903. $text3 .= lang("玩法") . " " . lang("总投") . " \n";
  904. App::setLocale($lang);
  905. if ($keywordsList) {
  906. uksort($keywordsList, 'custom_sort');
  907. // ksort($keywordsList);
  908. foreach ($keywordsList as $k => $v) {
  909. $text3 .= "{$k} {$v} \n";
  910. }
  911. }
  912. $inlineButton = self::getOperateButton();
  913. // 投注统计的消息不发了
  914. // SendTelegramGroupMessageJob::dispatch($text3, $inlineButton, '');
  915. }
  916. public static function todayExchangeRate($chatId)
  917. {
  918. $exchangeRate = Config::where('field', 'exchange_rate_rmb')->first()->val;
  919. $text = lang("今日汇率") . ":1USDT = {$exchangeRate} RMB \n";
  920. // $botMsg = [
  921. // 'chat_id' => "@{$chatId}",
  922. // 'text' => $text
  923. // ];
  924. self::sendMessage($chatId, $text);
  925. // return $botMsg;
  926. }
  927. }