BetService.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. $list = $list->toArray();
  386. if(empty($list)){
  387. $text .= "本期暂未下注! \n";
  388. }else{
  389. $keywords = implode(',',array_column($list, 'keywords'));
  390. $amounts = implode(',',array_column($list, 'amount'));
  391. $text .= "下注类型:[".$keywords."] \n";
  392. $text .= "下注金额:".$amounts." \n";
  393. $text .= "下注总额:".array_sum(array_column($list, 'amount'))." \n";
  394. $text .= "开奖状态:等待开奖 \n";
  395. }
  396. // foreach ($list->toArray() as $k => $v) {
  397. // $text .= "{$v['keywords']}{$v['amount']} \n";
  398. // }
  399. // $text .= "\n";
  400. // $text .= "----------\n";
  401. $msg['text'] = $text;
  402. } else {
  403. $msg['text'] = "当前没有开放的投注期数! \n";
  404. }
  405. return $msg;
  406. }
  407. /**
  408. * @description: 近期投注
  409. * @param {*} $memberId
  410. * @return {*}
  411. */
  412. public static function recentlyRecord($memberId, $page = 1, $limit = 5)
  413. {
  414. $list = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->orderBy('id', 'desc')->forPage($page, $limit)->get();
  415. // $text = "```\n";
  416. $text = "";
  417. $text .= "期数--内容--盈亏 \n";
  418. foreach ($list->toArray() as $k => $v) {
  419. $profit = $v['profit'] - $v['amount'];
  420. // $text .= $v['issue_no']." ".$v['keywords']." ".$v['amount']." ".$v['profit']."\n";
  421. $item = $v['issue_no'] . "==" . $v['keywords'] . rtrim(rtrim(number_format($v['amount'], 2, '.', ''), '0'), '.') . "==" . rtrim(rtrim(number_format($profit, 2, '.', ''), '0'), '.') . "\n";
  422. $text .= $item;
  423. }
  424. // $text .= "```\n";
  425. return $text;
  426. }
  427. /**
  428. * @description: 投注记录
  429. * @param {*} $memberId
  430. * @param {*} $page
  431. * @param {*} $limit
  432. * @return {*}
  433. */
  434. public static function record($memberId, $messageId = null, $page = 1, $limit = 5)
  435. {
  436. $type = Cache::get('message_id_bet_record_' . $memberId, 0);
  437. $msg['chat_id'] = $memberId;
  438. $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();
  439. $count = self::model()::where('member_id', $memberId)->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_SETTLED])->where(self::getWhere(['is_winner' => $type]))->count();
  440. $keyboard = [];
  441. $total_amount = BalanceLogService::model()::where('member_id', $memberId)->where('change_type', '中奖')->sum('amount');
  442. $total_amount = number_format($total_amount, 2);
  443. $text = "历史注单 \n";
  444. $text .= "中奖总派彩:{$total_amount} \n";
  445. foreach ($list as $k => $v) {
  446. if ($v->status == self::model()::STATUS_SETTLED) {
  447. $phase = $v->profit - $v->amount;
  448. } else {
  449. $phase = '待开奖';
  450. }
  451. $text .= "-------------------------------------\n";
  452. $text .= "期数:{$v->issue_no} \n";
  453. $text .= "内容:{$v->keywords} \n";
  454. $text .= "金额:{$v->amount} \n";
  455. $text .= "盈亏:{$phase} \n";
  456. }
  457. $msg['text'] = $text;
  458. $keyboard[] = [
  459. ['text' => "全部", 'callback_data' => "betRecordType@@0"],
  460. ['text' => "盈利", 'callback_data' => "betRecordType@@1"],
  461. ['text' => "亏损", 'callback_data' => "betRecordType@@2"]
  462. ];
  463. if ($page > 1) {
  464. $keyboard[] = [
  465. ['text' => "👆上一页", 'callback_data' => "betRecordNextPage@@" . ($page - 1)]
  466. ];
  467. }
  468. $allPage = ceil($count / $limit);
  469. if ($allPage > $page) {
  470. if ($page > 1) {
  471. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)];
  472. } else {
  473. $keyboard[] = [
  474. ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)]
  475. ];
  476. }
  477. }
  478. if ($messageId) {
  479. $msg['message_id'] = $messageId;
  480. }
  481. if ($keyboard) {
  482. $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  483. }
  484. return $msg;
  485. }
  486. /**
  487. * @description: 中奖结算
  488. * @param {*} $issue_no
  489. * @param {*} $awards
  490. * @return {*}
  491. */
  492. public static function betSettled($issue_no, $awards)
  493. {
  494. $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
  495. $data = [];
  496. $text = $issue_no . "期开奖结果 \n";
  497. $text .= "-----本期开奖账单----- \n";
  498. $text .= "\n";
  499. // $text .=" 中奖类型 用户 投注金额 中奖金额 盈亏 \n";
  500. $betNoticeNum = Config::where('field', 'bet_notice_num')->first()->val;
  501. $betNoticeNum = explode(',', $betNoticeNum);
  502. $betNoticeMini = $betNoticeNum[0] ?? 26;
  503. $betNoticeMax = $betNoticeNum[1] ?? 38;
  504. $noticeNum = rand($betNoticeMini, $betNoticeMax);
  505. $realNoticeNum = ceil($noticeNum / 2);
  506. $openList = [];
  507. $memberList = [];
  508. $bet_num = 0;
  509. foreach ($list->toArray() as $k => $v) {
  510. // $userInfo = UserService::findAll(['member_id' => $v['member_id']]);
  511. // $lastStr = self::getLastChar($userInfo->first_name, 1);
  512. $lastStr = self::hideMiddleDigits($v['member_id'], 4);
  513. $item = [];
  514. $item['id'] = $v['id'];
  515. $item['status'] = self::model()::STATUS_SETTLED;
  516. if (in_array($v['keywords'], $awards)) {
  517. // $profit = $v['amount'] * $v['odds'];
  518. $amount = $v['amount'];
  519. // $amount = rtrim($amount, '0'); // 去掉右侧的 0
  520. // $amount = rtrim($amount, '.'); // 如果末尾是 . 就去掉
  521. $odds = $v['odds'];
  522. $profit = bcmul($amount, $odds, 2); // 保留两位小数
  523. if ($profit > 880000) {
  524. $profit = 880000; // 单注最高奖金880000
  525. }
  526. $item['profit'] = $profit;
  527. // $yl = $profit - $amount;
  528. $yl = bcsub($profit, $amount, 2); // 盈利
  529. $memberList[$v['member_id']][] = [
  530. 'member_id' => $v['member_id'],
  531. 'keywords' => $v['keywords'],
  532. 'amount' => $v['amount'],
  533. 'profit' => $profit,
  534. 'yl' => $yl,
  535. ];
  536. // if ($k + 1 <= $realNoticeNum) {
  537. // // $text .= "会员下注 【" . $lastStr . "】{$v['amount']} {$profit} {$yl}\n";
  538. // $bet_num++;
  539. // }
  540. // 结算
  541. WalletService::updateBalance($v['member_id'], $profit);
  542. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  543. $balance = $walletInfo['available_balance'];
  544. BalanceLogService::addLog($v['member_id'], $profit, $balance, ($balance + $profit), '中奖', $v['id'], '');
  545. if(isset($openList[$v['member_id']])){
  546. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  547. $openList[$v['member_id']]['amount'] += $v['amount'];
  548. $openList[$v['member_id']]['profit'] += $profit;
  549. $openList[$v['member_id']]['lastStr'] = $lastStr;
  550. $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
  551. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  552. $openList[$v['member_id']]['win_amount'] += $v['amount'];
  553. }else{
  554. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  555. $openList[$v['member_id']]['amount'] = $v['amount'];
  556. $openList[$v['member_id']]['profit'] = $profit;
  557. $openList[$v['member_id']]['lastStr'] = $lastStr;
  558. $openList[$v['member_id']]['openKeywords'] = [];
  559. $openList[$v['member_id']]['keywords'] = [];
  560. $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
  561. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  562. $openList[$v['member_id']]['win_amount'] = $v['amount'];
  563. }
  564. } else {
  565. if(isset($openList[$v['member_id']])){
  566. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  567. $openList[$v['member_id']]['amount'] += $v['amount'];
  568. $openList[$v['member_id']]['lastStr'] = $lastStr;
  569. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  570. }else{
  571. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  572. $openList[$v['member_id']]['amount'] = $v['amount'];
  573. $openList[$v['member_id']]['profit'] = 0;
  574. $openList[$v['member_id']]['lastStr'] = $lastStr;
  575. $openList[$v['member_id']]['openKeywords'] = [];
  576. $openList[$v['member_id']]['keywords'] = [];
  577. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  578. $openList[$v['member_id']]['win_amount'] = 0;
  579. }
  580. // if ($k + 1 <= $realNoticeNum) {
  581. // // $text .= "会员下注 【" . $lastStr . "】{$v['amount']} {$v['profit']} -{$v['amount']}\n";
  582. // $bet_num++;
  583. // }
  584. // $memberList[$v['member_id']][] = [
  585. // 'member_id' => $v['member_id'],
  586. // 'amount' => $v['amount'],
  587. // 'keywords' => $v['keywords'],
  588. // 'profit' => 0,
  589. // 'yl' => '-'.$v['amount'],
  590. // ];
  591. }
  592. self::model()::where('id', $v['id'])->update($item);
  593. }
  594. foreach($openList as $k => $v){
  595. $amount = $v['amount'];
  596. // if($v['profit'] >= 0){
  597. $profit = number_format($v['profit'],2);
  598. $yl = bcsub($v['profit'], $v['amount'], 2); // 盈利
  599. // $text .= "会员下注 【" . $v['lastStr'] . "】 {$amount} {$profit} {$yl}\n";
  600. if(++$bet_num <= $realNoticeNum){
  601. if(empty($v['openKeywords'])){
  602. $openKeyword = '-';
  603. }else{
  604. $openKeyword = implode(',', $v['openKeywords']);
  605. }
  606. $text .= "用户ID:{$v['lastStr']} \n";
  607. $text .= "下注类型:[".implode(',', $v['keywords'])."] \n";
  608. $text .= "中奖类型:[".$openKeyword."] \n";
  609. $text .= "投注金额:{$amount} \n";
  610. $text .= "中奖金额:{$v['win_amount']} \n";
  611. $text .= "派彩金额:{$profit} \n";
  612. $text .= "盈亏金额:{$yl} \n";
  613. $text .= "-------------------------------- \n";
  614. }
  615. $text2 = "{$issue_no}期开奖结果 \n";
  616. $text2 .= "下注类型:[".implode(',', $v['keywords'])."] \n";
  617. $text2 .= "中奖类型:[".$openKeyword."] \n";
  618. $text2 .= "投注金额:{$amount} \n";
  619. $text2 .= "中奖金额:{$v['win_amount']} \n";
  620. $text2 .= "派彩金额:{$profit} \n";
  621. $text2 .= "盈亏金额:{$yl} \n";
  622. self::sendMessage($v['member_id'],$text2);
  623. // }else{
  624. // $text .= "会员下注 【" . $v['lastStr'] . "】 {$amount} 0 -{$amount}\n";
  625. // }
  626. }
  627. $inlineButton = self::getOperateButton();
  628. $rand_num = $noticeNum - $bet_num;
  629. $text .= self::fakeLotteryDraw($issue_no, $awards, $rand_num);
  630. // for ($i = 0; $i < $rand_num; $i++) {
  631. // // 生成 -100000 到 100000 的随机数,但排除 -10 到 10 的范围
  632. // $randomNumber = random_int(-1000000, 1000000) / 100;
  633. // if ($randomNumber >= -10 && $randomNumber <= 10) {
  634. // // 如果落在 -10 到 10 之间,重新生成或调整
  635. // $randomNumber = $randomNumber < 0 ? -random_int(10, 100000) : random_int(10, 100000);
  636. // }
  637. // $text .= "私聊下注 【******】 {$randomNumber}\n";
  638. // }
  639. // 群通知
  640. self::bettingGroupNotice($text, $inlineButton, '');
  641. }
  642. /**
  643. * @description: 中奖结算
  644. * @param {*} $issue_no
  645. * @param {*} $awards
  646. * @return {*}
  647. */
  648. public static function betSettled2($issue_no, $awards)
  649. {
  650. $list = self::findAll(['issue_no' => $issue_no, 'status' => self::model()::STATUS_STAY]);
  651. $data = [];
  652. $text = $issue_no . "期开奖结果 \n";
  653. $text .= "-----本期开奖账单----- \n";
  654. // $text .=" 中奖类型 用户 投注金额 中奖金额 盈亏 \n";
  655. $text .= "\n";
  656. $betNoticeNum = Config::where('field', 'bet_notice_num')->first()->val;
  657. $betNoticeNum = explode(',', $betNoticeNum);
  658. $betNoticeMini = $betNoticeNum[0] ?? 26;
  659. $betNoticeMax = $betNoticeNum[1] ?? 38;
  660. $noticeNum = rand($betNoticeMini, $betNoticeMax);
  661. $realNoticeNum = ceil($noticeNum / 2);
  662. $openList = [];
  663. $bet_num = 0;
  664. foreach ($list->toArray() as $k => $v) {
  665. // $userInfo = UserService::findAll(['member_id' => $v['member_id']]);
  666. // $lastStr = self::getLastChar($userInfo->first_name, 1);
  667. $lastStr = self::hideMiddleDigits($v['member_id'], 4);
  668. $item = [];
  669. $item['id'] = $v['id'];
  670. $item['status'] = self::model()::STATUS_SETTLED;
  671. if (in_array($v['keywords'], $awards)) {
  672. // $profit = $v['amount'] * $v['odds'];
  673. $amount = $v['amount'];
  674. // $amount = rtrim($amount, '0'); // 去掉右侧的 0
  675. // $amount = rtrim($amount, '.'); // 如果末尾是 . 就去掉
  676. $odds = $v['odds'];
  677. $profit = bcmul($amount, $odds, 2); // 保留两位小数
  678. if ($profit > 880000) {
  679. $profit = 880000; // 单注最高奖金880000
  680. }
  681. $item['profit'] = $profit;
  682. // $yl = $profit - $amount;
  683. $yl = bcsub($profit, $amount, 2); // 盈利
  684. // if ($k + 1 <= $realNoticeNum) {
  685. // // $text .= "会员下注 【" . $lastStr . "】{$v['amount']} {$profit} {$yl}\n";
  686. // $bet_num++;
  687. // }
  688. // 结算
  689. // WalletService::updateBalance($v['member_id'], $profit);
  690. // $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  691. // $balance = $walletInfo['available_balance'];
  692. // BalanceLogService::addLog($v['member_id'], $profit, $balance, ($balance + $profit), '中奖', $v['id'], '');
  693. if(isset($openList[$v['member_id']])){
  694. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  695. $openList[$v['member_id']]['amount'] += $v['amount'];
  696. $openList[$v['member_id']]['profit'] += $profit;
  697. $openList[$v['member_id']]['lastStr'] = $lastStr;
  698. $openList[$v['member_id']]['openKeywords'][] = $v['keywords'];
  699. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  700. $openList[$v['member_id']]['win_amount'] += $v['amount'];
  701. }else{
  702. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  703. $openList[$v['member_id']]['amount'] = $v['amount'];
  704. $openList[$v['member_id']]['profit'] = $profit;
  705. $openList[$v['member_id']]['lastStr'] = $lastStr;
  706. $openList[$v['member_id']]['openKeywords'] = [];
  707. $openList[$v['member_id']]['keywords'] = [];
  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. }
  712. } else {
  713. if(isset($openList[$v['member_id']])){
  714. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  715. $openList[$v['member_id']]['amount'] += $v['amount'];
  716. $openList[$v['member_id']]['lastStr'] = $lastStr;
  717. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  718. }else{
  719. $openList[$v['member_id']]['member_id'] = $v['member_id'];
  720. $openList[$v['member_id']]['amount'] = $v['amount'];
  721. $openList[$v['member_id']]['profit'] = 0;
  722. $openList[$v['member_id']]['lastStr'] = $lastStr;
  723. $openList[$v['member_id']]['openKeywords'] = [];
  724. $openList[$v['member_id']]['keywords'] = [];
  725. $openList[$v['member_id']]['keywords'][] = $v['keywords'];
  726. $openList[$v['member_id']]['win_amount'] = 0;
  727. }
  728. // if ($k + 1 <= $realNoticeNum) {
  729. // // $text .= "会员下注 【" . $lastStr . "】{$v['amount']} {$v['profit']} -{$v['amount']}\n";
  730. // $bet_num++;
  731. // }
  732. }
  733. // self::model()::where('id', $v['id'])->update($item);
  734. }
  735. foreach($openList as $k => $v){
  736. $amount = $v['amount'];
  737. // if($v['profit'] >= 0){
  738. $profit = number_format($v['profit'],2);
  739. $yl = bcsub($v['profit'], $v['amount'], 2); // 盈利
  740. // $text .= "会员下注 【" . $v['lastStr'] . "】 {$amount} {$profit} {$yl}\n";
  741. if(++$bet_num <= $realNoticeNum){
  742. $text .= "用户ID:{$v['lastStr']} \n";
  743. $text .= "下注类型:[".implode(',', $v['keywords'])."] \n";
  744. $text .= "中奖类型:[".implode(',', $v['openKeywords'])."] \n";
  745. $text .= "投注金额:{$amount} \n";
  746. $text .= "中奖金额:{$v['win_amount']} \n";
  747. $text .= "派彩金额:{$profit} \n";
  748. $text .= "盈亏金额:{$yl} \n";
  749. $text .= "-------------------------------- \n";
  750. }
  751. $text2 = "{$issue_no}期开奖结果 \n";
  752. $text2 .= "下注类型:[".implode(',', $v['keywords'])."] \n";
  753. $text2 .= "中奖类型:[".implode(',', $v['openKeywords'])."] \n";
  754. $text2 .= "投注金额:{$amount} \n";
  755. $text2 .= "中奖金额:{$v['win_amount']} \n";
  756. $text2 .= "派彩金额:{$profit} \n";
  757. $text2 .= "盈亏金额:{$yl} \n";
  758. SendTelegramMessageJob::dispatch($v['member_id'],$text2);
  759. // self::sendMessage($v['member_id'],$text2);
  760. // }else{
  761. // $text .= "会员下注 【" . $v['lastStr'] . "】 {$amount} 0 -{$amount}\n";
  762. // }
  763. }
  764. $inlineButton = self::getOperateButton();
  765. $rand_num = $noticeNum - $bet_num;
  766. $text .= self::fakeLotteryDraw($issue_no, $awards, $rand_num);
  767. // for ($i = 0; $i < $rand_num; $i++) {
  768. // // 生成 -100000 到 100000 的随机数,但排除 -10 到 10 的范围
  769. // $randomNumber = random_int(-1000000, 1000000) / 100;
  770. // if ($randomNumber >= -10 && $randomNumber <= 10) {
  771. // // 如果落在 -10 到 10 之间,重新生成或调整
  772. // $randomNumber = $randomNumber < 0 ? -random_int(10, 100000) : random_int(10, 100000);
  773. // }
  774. // $text .= "私聊下注 【******】 {$randomNumber}\n";
  775. // }
  776. // 群通知
  777. self::bettingGroupNotice($text, $inlineButton, '');
  778. }
  779. // 虚拟开奖
  780. public static function fakeLotteryDraw($issue_no, $awards, $rand_num = 30)
  781. {
  782. $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
  783. $text = "";
  784. foreach ($fake_bet_list as $k => $v) {
  785. // $lastStr = self::getLastChar($v['first_name'], 1);
  786. $lastStr = self::hideMiddleDigits($v['member_id'], 4);
  787. if (in_array($v['keywords'], $awards)) {
  788. $amount = $v['amount'];
  789. // $amount = rtrim($amount, '0'); // 去掉右侧的 0
  790. // $amount = rtrim($amount, '.'); // 如果末尾是 . 就去掉
  791. $odds = $v['odds'];
  792. $profit = bcmul($amount, $odds, 2); // 保留两位小数
  793. if ($profit > 880000) {
  794. $profit = 880000; // 单注最高奖金880000
  795. }
  796. $item['profit'] = $profit;
  797. $v['amount'] = number_format($amount,2);
  798. // $yl = $profit - $amount;
  799. $yl = bcsub($profit, $amount, 2); // 盈利
  800. // if ($k + 1 <= $rand_num) {
  801. // $text .= "会员下注 【" . $lastStr . "】 {$v['amount']} {$profit} {$yl}\n";
  802. // }
  803. if ($k + 1 <= $rand_num) {
  804. $text .= "用户ID:{$lastStr} \n";
  805. $text .= "下注类型:[".$v['keywords']."] \n";
  806. $text .= "中奖类型:[".$v['keywords']."] \n";
  807. $text .= "投注金额:{$v['amount']} \n";
  808. $text .= "中奖金额:{$v['amount']} \n";
  809. $text .= "派彩金额:{$profit} \n";
  810. $text .= "盈亏金额:{$yl} \n";
  811. $text .= "-------------------------------- \n";
  812. }
  813. } else {
  814. $amount = number_format($v['amount'],2);
  815. $v['amount'] = $v['amount'];
  816. // if ($k + 1 <= $rand_num) {
  817. // $text .= "会员下注 【" . $lastStr . "】 {$v['amount']} {$v['profit']} -{$v['amount']}\n";
  818. // }
  819. $profit = number_format($v['profit'],2);
  820. $yl = bcsub($v['profit'], $v['amount'], 2); // 盈利
  821. // $text .= "会员下注 【" . $v['lastStr'] . "】 {$amount} {$profit} {$yl}\n";
  822. if ($k + 1 <= $rand_num) {
  823. $text .= "用户ID:{$lastStr} \n";
  824. $text .= "下注类型:[".$v['keywords']."] \n";
  825. $text .= "中奖类型:[-] \n";
  826. $text .= "投注金额:{$amount} \n";
  827. $text .= "中奖金额:0 \n";
  828. $text .= "派彩金额:0 \n";
  829. $text .= "盈亏金额:-{$amount} \n";
  830. $text .= "-------------------------------- \n";
  831. }
  832. }
  833. }
  834. return $text;
  835. }
  836. public static function todayExchangeRate($chatId)
  837. {
  838. $exchangeRate = Config::where('field', 'exchange_rate_rmb')->first()->val;
  839. $text = "今日汇率:1USDT = {$exchangeRate} RMB \n";
  840. // $botMsg = [
  841. // 'chat_id' => "@{$chatId}",
  842. // 'text' => $text
  843. // ];
  844. self::sendMessage($chatId, $text);
  845. // return $botMsg;
  846. }
  847. }