BetService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. namespace App\Services;
  3. use App\Http\Controllers\admin\GameplayRule;
  4. use App\Services\BaseService;
  5. use App\Models\Bet;
  6. use App\Models\Config;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Collection;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Facades\Log;
  11. use App\Services\GameplayRuleService;
  12. use App\Services\WalletService;
  13. use App\Services\IssueService;
  14. use App\Services\UserService;
  15. use App\Services\BalanceLogService;
  16. /**
  17. * 投注
  18. */
  19. class BetService extends BaseService
  20. {
  21. /**
  22. * @description: 模型
  23. * @return {string}
  24. */
  25. public static function model() :string
  26. {
  27. return Bet::class;
  28. }
  29. /**
  30. * @description: 枚举
  31. * @return {*}
  32. */
  33. public static function enum() :string
  34. {
  35. return '';
  36. }
  37. /**
  38. * @description: 获取查询条件
  39. * @param {array} $search 查询内容
  40. * @return {array}
  41. */
  42. public static function getWhere(array $search = []) :array
  43. {
  44. $where = [];
  45. if(isset($search['issue_no']) && !empty($search['issue_no'])){
  46. $where[] = ['issue_no', '=', $search['issue_no']];
  47. }
  48. if(isset($search['member_id']) && !empty($search['member_id'])){
  49. $where[] = ['member_id', '=', $search['member_id']];
  50. }
  51. if(isset($search['keywords']) && !empty($search['keywords'])){
  52. $where[] = ['keywords', '=', $search['keywords']];
  53. }
  54. if(isset($search['issue_id']) && !empty($search['issue_id'])){
  55. $where[] = ['issue_id', '=', $search['issue_id']];
  56. }
  57. if(isset($search['id']) && !empty($search['id'])){
  58. $where[] = ['id', '=', $search['id']];
  59. }
  60. if(isset($search['user_id']) && !empty($search['user_id'])){
  61. $where[] = ['user_id', '=', $search['user_id']];
  62. }
  63. if(isset($search['status']) && !empty($search['status'])){
  64. $where[] = ['status', '=', $search['status']];
  65. }
  66. return $where;
  67. }
  68. /**
  69. * @description: 查询单条数据
  70. * @param array $search
  71. * @return \App\Models\Coin|null
  72. */
  73. public static function findOne(array $search): ?Bet
  74. {
  75. return self::model()::where(self::getWhere($search))->first();
  76. }
  77. /**
  78. * @description: 查询所有数据
  79. * @param array $search
  80. * @return \Illuminate\Database\Eloquent\Collection
  81. */
  82. public static function findAll(array $search = [])
  83. {
  84. return self::model()::where(self::getWhere($search))->get();
  85. }
  86. /**
  87. * @description: 分页查询
  88. * @param array $search
  89. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  90. */
  91. public static function paginate(array $search = [])
  92. {
  93. $limit = isset($search['limit'])?$search['limit']:15;
  94. $paginator = self::model()::where(self::getWhere($search))->paginate($limit);
  95. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  96. }
  97. /**
  98. * @description: 投注操作
  99. * @param {string} $memberId
  100. * @param {string} $input
  101. * @return {*}
  102. */
  103. public static function bet(string $memberId,string $input ,$messageId = 0)
  104. {
  105. $msg = [];
  106. $msg['chat_id'] = $memberId;
  107. // 钱包生成
  108. // $walletInfo = WalletService::getUserWallet($memberId);
  109. // 分解投注的内容
  110. $betResult = GameplayRuleService::bettingRuleVerify($input);
  111. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  112. if($betResult == null){
  113. $text = "消息格式错误!\n";
  114. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  115. $msg['text'] = $text;
  116. if($messageId){
  117. $msg['reply_to_message_id'] = $messageId;
  118. }
  119. return $msg;
  120. }
  121. $keywords = $betResult['rule']; // 玩法
  122. $amount = $betResult['amount']; // 投注金额
  123. $gameplayRuleInfo = GameplayRuleService::getGameplayRules($keywords);
  124. if($gameplayRuleInfo == null){
  125. $text = "玩法未配置!\n";
  126. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  127. $msg['text'] = $text;
  128. if($messageId){
  129. $msg['reply_to_message_id'] = $messageId;
  130. }
  131. return $msg;
  132. }
  133. if($gameplayRuleInfo['odds'] <= 0){
  134. $text = "赔率为0 庄家通吃 禁止投注!\n";
  135. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  136. $msg['text'] = $text;
  137. if($messageId){
  138. $msg['reply_to_message_id'] = $messageId;
  139. }
  140. return $msg;
  141. }
  142. // 期数验证
  143. $issueInfo = IssueService::model()::where('status',IssueService::model()::STATUS_BETTING)->orderBy('id','desc')->first();
  144. if(empty($issueInfo)){
  145. $issueCloseInfo = IssueService::model()::where('status',IssueService::model()::STATUS_CLOSE)->orderBy('id','desc')->first();
  146. if(empty($issueCloseInfo)){
  147. $text = "暂无可下注期数,本次下注无效!\n";
  148. $msg['text'] = $text;
  149. if($messageId){
  150. $msg['reply_to_message_id'] = $messageId;
  151. }
  152. return $msg;
  153. }else{
  154. $text = "封盘中,本次下注无效!\n";
  155. $msg['text'] = $text;
  156. if($messageId){
  157. $msg['reply_to_message_id'] = $messageId;
  158. }
  159. return $msg;
  160. }
  161. }
  162. if(!is_numeric($amount) || $amount <= 0){
  163. $text = "投注金额格式不正确!\n";
  164. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  165. $msg['text'] = $text;
  166. if($messageId){
  167. $msg['reply_to_message_id'] = $messageId;
  168. }
  169. return $msg;
  170. }
  171. // 投注限制校验
  172. if($amount < $gameplayRuleInfo['mininum']){
  173. $text = "下注失败,最小金额限制{$gameplayRuleInfo['mininum']}\n";
  174. $msg['text'] = $text;
  175. if($messageId){
  176. $msg['reply_to_message_id'] = $messageId;
  177. }
  178. return $msg;
  179. }
  180. // 投注限制校验
  181. if($amount > $gameplayRuleInfo['maxinum']){
  182. $text = "下注失败,最大金额限制{$gameplayRuleInfo['maxinum']}\n";
  183. $msg['text'] = $text;
  184. if($messageId){
  185. $msg['reply_to_message_id'] = $messageId;
  186. }
  187. return $msg;
  188. }
  189. // 获取用户余额
  190. $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  191. $balance = $walletInfo['available_balance'];
  192. // 余额计算
  193. if($balance < $amount){
  194. $text = "余额不足,本次下注无效!\n";
  195. $msg['text'] = $text;
  196. if($messageId){
  197. $msg['reply_to_message_id'] = $messageId;
  198. }
  199. return $msg;
  200. }
  201. $userInfo = UserService::findOne(['member_id' => $memberId]);
  202. $betInfo = self::findOne(['member_id' => $memberId,'issue_no' => $issueInfo->issue_no,'keywords' => $keywords]); // 相同下注
  203. if($betInfo){
  204. $betInfo->amount = $betInfo->amount + $amount;
  205. $bet_id = $betInfo->id;
  206. $betInfo->save();
  207. }else{
  208. $data = [];
  209. $data['amount'] = $amount; // 分数
  210. $data['keywords'] = $keywords; // 玩法
  211. $data['member_id'] = $memberId;
  212. $data['user_id'] = $userInfo->id;
  213. $data['issue_no'] = $issueInfo->issue_no;
  214. $data['issue_id'] = $issueInfo->id;
  215. $data['odds'] = $gameplayRuleInfo['odds'];
  216. $newBet = self::model()::create($data);
  217. $bet_id = $newBet->id;
  218. }
  219. WalletService::updateBalance($memberId,-$amount);
  220. BalanceLogService::addLog($memberId,-$amount,$balance,($balance-$amount),'投注',$bet_id,'');
  221. // // 返利
  222. // $rebate = Config::where('field', 'rebate')->first()->val;
  223. // if($rebate > 0){
  224. // $rebateAmount = bcmul($amount, $rebate, 2); // 返利金额
  225. // if($rebateAmount > 0){
  226. // WalletService::updateBalance($memberId,$rebateAmount);
  227. // $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  228. // $balance = $walletInfo['available_balance'];
  229. // BalanceLogService::addLog($memberId,$rebateAmount,$balance,($balance+$rebateAmount),'返水',$bet_id,'');
  230. // }
  231. // }
  232. $text = "下注期数:{$issueInfo->issue_no}\n";
  233. $text .= "下注内容\n";
  234. $text .= "--------\n";
  235. $text .= "{$input}\n";
  236. $text .= "--------\n";
  237. $text .= "下注成功\n";
  238. $msg['text'] = $text;
  239. $lastStr = self::getLastChar($userInfo->first_name,1);
  240. $groupText = "";
  241. $groupText .= "私聊下注 【xxxxxx".$lastStr."】 \n";
  242. $groupText .= "下注期数:{$issueInfo->issue_no} \n";
  243. $groupText .= "下注内容: \n";
  244. $groupText .= "----------- \n";
  245. $groupText .= "{$input} \n";
  246. $groupText .= "----------- \n";
  247. $inlineButton = self::getOperateButton();
  248. // 群通知
  249. self::bettingGroupNotice($groupText,$inlineButton); // 群通知
  250. return $msg;
  251. }
  252. // 模拟下注
  253. public static function fakeBet()
  254. {
  255. // 期数验证
  256. $issueInfo = IssueService::model()::where('status',IssueService::model()::STATUS_BETTING)->orderBy('id','desc')->first();
  257. if($issueInfo){
  258. $fake_bet_list = Cache::get('fake_bet_'.$issueInfo->issue_no,[]);
  259. $gameplayRuleList = GameplayRuleService::model()::where('odds','>',0)->get();
  260. $gameplayRuleList = $gameplayRuleList->toArray();
  261. $randKey = array_rand($gameplayRuleList,1);
  262. $gameplayRuleInfo = $gameplayRuleList[$randKey]??[];
  263. if($gameplayRuleInfo){
  264. $item = [];
  265. $item['keywords'] = $gameplayRuleInfo['keywords'];
  266. $item['odds'] = $gameplayRuleInfo['odds'];
  267. $item['amount'] = rand($gameplayRuleInfo['mininum'],$gameplayRuleInfo['maxinum']);
  268. $item['first_name'] = self::generateRandomString(6);
  269. $item['profit'] = 0;
  270. $input = $item['keywords'].$item['amount'];
  271. $fake_bet_list[] = $item;
  272. $lastStr = self::getLastChar($item['first_name'],1);
  273. $groupText = "";
  274. $groupText .= "私聊下注 【xxxxxx".$lastStr."】 \n";
  275. $groupText .= "下注期数:{$issueInfo->issue_no} \n";
  276. $groupText .= "下注内容: \n";
  277. $groupText .= "----------- \n";
  278. $groupText .= "{$input} \n";
  279. $groupText .= "----------- \n";
  280. $inlineButton = self::getOperateButton();
  281. // 群通知
  282. self::bettingGroupNotice($groupText,$inlineButton); // 群通知
  283. }
  284. Cache::put('fake_bet_'.$issueInfo->issue_no,$fake_bet_list,500);
  285. }
  286. }
  287. /**
  288. * @description: 当期下注
  289. * @param {*} $memberId
  290. * @return {*}
  291. */
  292. public static function currentBet($memberId)
  293. {
  294. $msg['chat_id'] = $memberId;
  295. // 期数验证
  296. $issueInfo = IssueService::model()::where('status',IssueService::model()::STATUS_BETTING)->orderBy('id','desc')->first();
  297. $issue_no = '';
  298. if(!empty($issueInfo)){
  299. $issue_no = $issueInfo->issue_no;
  300. }else{
  301. $issueCloseInfo = IssueService::model()::where('status',IssueService::model()::STATUS_CLOSE)->orderBy('id','desc')->first();
  302. if(empty($issueCloseInfo)){
  303. $issue_no = $issueCloseInfo->issue_no;
  304. }
  305. }
  306. if($issue_no){
  307. $text = "当前期号:{$issue_no} \n";
  308. $text .= "\n";
  309. $text .= "----------\n";
  310. $list = self::findAll(['member_id' => $memberId ,'issue_no' => $issue_no]);
  311. foreach($list->toArray() as $k => $v){
  312. $text .= "{$v['keywords']}{$v['amount']} \n";
  313. }
  314. $text .= "\n";
  315. $text .= "----------\n";
  316. $msg['text'] = $text;
  317. }else{
  318. $msg['text'] = "当前没有开放的投注期数! \n";
  319. }
  320. return $msg;
  321. }
  322. /**
  323. * @description: 近期投注
  324. * @param {*} $memberId
  325. * @return {*}
  326. */
  327. public static function recentlyRecord($memberId ,$page = 1 ,$limit = 5)
  328. {
  329. $list = self::model()::where('member_id',$memberId)->whereIn('status',[self::model()::STATUS_STAY,self::model()::STATUS_SETTLED])->orderBy('id','desc')->forPage($page, $limit)->get();
  330. // $text = "```\n";
  331. $text = "";
  332. $text .= "期数--内容--盈亏 \n";
  333. foreach($list->toArray() as $k => $v){
  334. $profit = $v['profit'] - $v['amount'];
  335. // $text .= $v['issue_no']." ".$v['keywords']." ".$v['amount']." ".$v['profit']."\n";
  336. $item = $v['issue_no']."==".$v['keywords'].rtrim(rtrim(number_format($v['amount'], 2, '.', ''), '0'), '.')."==".rtrim(rtrim(number_format($profit, 2, '.', ''), '0'), '.')."\n";
  337. $text .= $item;
  338. }
  339. // $text .= "```\n";
  340. return $text;
  341. }
  342. /**
  343. * @description: 投注记录
  344. * @param {*} $memberId
  345. * @param {*} $page
  346. * @param {*} $limit
  347. * @return {*}
  348. */
  349. public static function record($memberId ,$messageId = null ,$page = 1 ,$limit = 5)
  350. {
  351. $msg['chat_id'] = $memberId;
  352. $list = self::model()::where('member_id',$memberId)->whereIn('status',[self::model()::STATUS_STAY,self::model()::STATUS_SETTLED])->orderBy('id','desc')->forPage($page, $limit)->get();
  353. $count = self::model()::where('member_id',$memberId)->whereIn('status',[self::model()::STATUS_STAY,self::model()::STATUS_SETTLED])->count();
  354. $keyboard = [];
  355. $text = "历史注单 \n";
  356. foreach($list as $k => $v){
  357. $phase = $v->profit - $v->amount;
  358. $text .= "-------------------------------------\n";
  359. $text .= "期数:{$v->issue_no} \n";
  360. $text .= "内容:{$v->keywords} \n";
  361. $text .= "金额:{$v->amount} \n";
  362. $text .= "盈亏:{$phase} \n";
  363. }
  364. $msg['text'] = $text;
  365. if ($page > 1) {
  366. $keyboard[] = [
  367. ['text' => "👆上一页", 'callback_data' => "betRecordNextPage@@" . ($page - 1)]
  368. ];
  369. }
  370. $allPage = ceil($count / $limit);
  371. if ($allPage > $page) {
  372. if ($page > 1) {
  373. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)];
  374. } else {
  375. $keyboard[] = [
  376. ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)]
  377. ];
  378. }
  379. }
  380. if($messageId){
  381. $msg['message_id'] = $messageId;
  382. }
  383. if($keyboard){
  384. $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  385. }
  386. return $msg;
  387. }
  388. /**
  389. * @description: 中奖结算
  390. * @param {*} $issue_no
  391. * @param {*} $awards
  392. * @return {*}
  393. */
  394. public static function betSettled($issue_no,$awards)
  395. {
  396. $list = self::findAll(['issue_no' => $issue_no ,'status' => self::model()::STATUS_STAY]);
  397. $data = [];
  398. $text = $issue_no."期开奖结果 \n";
  399. $text .= "-----本期开奖账单----- \n";
  400. $bet_num = 0;
  401. foreach($list->toArray() as $k => $v){
  402. $userInfo = UserService::findAll(['member_id' => $v['member_id']]);
  403. $lastStr = self::getLastChar($userInfo->first_name,1);
  404. $item = [];
  405. $item['id'] = $v['id'];
  406. $item['status'] = self::model()::STATUS_SETTLED;
  407. if(in_array($v['keywords'],$awards)){
  408. $profit = $v['amount'] * $v['odds'];
  409. if($profit > 880000){
  410. $profit = 880000; // 单注最高奖金880000
  411. }
  412. $item['profit'] = $profit;
  413. $yl = $profit - $v['amount'];
  414. if($k+1 <= 15){
  415. $text .= "私聊下注 【******".$lastStr."】 {$yl}\n";
  416. $bet_num++;
  417. }
  418. // 结算
  419. WalletService::updateBalance($v['member_id'],$profit);
  420. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  421. $balance = $walletInfo['available_balance'];
  422. BalanceLogService::addLog($v['member_id'],$profit,$balance,($balance+$profit),'中奖',$v['id'],'');
  423. }else{
  424. if($k+1 <= 15){
  425. $text .= "私聊下注 【******".$lastStr."】 -{$v['amount']}\n";
  426. $bet_num++;
  427. }
  428. }
  429. self::model()::where('id',$v['id'])->update($item);
  430. }
  431. $inlineButton = self::getOperateButton();
  432. $rand_num = 30 - $bet_num;
  433. for($i=0;$i<$rand_num;$i++){
  434. // 生成 -100000 到 100000 的随机数,但排除 -10 到 10 的范围
  435. $randomNumber = random_int(-1000000, 1000000) / 100;
  436. if ($randomNumber >= -10 && $randomNumber <= 10) {
  437. // 如果落在 -10 到 10 之间,重新生成或调整
  438. $randomNumber = $randomNumber < 0 ? -random_int(10, 100000) : random_int(10, 100000);
  439. }
  440. $text .= "私聊下注 【******】 {$randomNumber}\n";
  441. }
  442. // 群通知
  443. self::bettingGroupNotice($text, $inlineButton, '');
  444. }
  445. }