BetService.php 38 KB

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