BetService.php 43 KB

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