BetService.php 43 KB

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