BetService.php 44 KB

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