BetService.php 16 KB

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