BetService.php 41 KB

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