BetService.php 43 KB

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