BetService.php 43 KB

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