BetService.php 40 KB

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