BetService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. $groupText = "";
  239. $groupText .= "私聊下注 【xxxxxx】 \n";
  240. $groupText .= "下注期数:{$issueInfo->issue_no} \n";
  241. $groupText .= "下注内容: \n";
  242. $groupText .= "----------- \n";
  243. $groupText .= "{$input} \n";
  244. $groupText .= "----------- \n";
  245. $inlineButton = self::getOperateButton();
  246. // 群通知
  247. self::bettingGroupNotice($groupText,$inlineButton); // 群通知
  248. return $msg;
  249. }
  250. /**
  251. * @description: 当期下注
  252. * @param {*} $memberId
  253. * @return {*}
  254. */
  255. public static function currentBet($memberId)
  256. {
  257. $msg['chat_id'] = $memberId;
  258. // 期数验证
  259. $issueInfo = IssueService::model()::where('status',IssueService::model()::STATUS_BETTING)->orderBy('id','desc')->first();
  260. $issue_no = '';
  261. if(!empty($issueInfo)){
  262. $issue_no = $issueInfo->issue_no;
  263. }else{
  264. $issueCloseInfo = IssueService::model()::where('status',IssueService::model()::STATUS_CLOSE)->orderBy('id','desc')->first();
  265. if(empty($issueCloseInfo)){
  266. $issue_no = $issueCloseInfo->issue_no;
  267. }
  268. }
  269. if($issue_no){
  270. $text = "当前期号:{$issue_no} \n";
  271. $text .= "\n";
  272. $text .= "----------\n";
  273. $list = self::findAll(['member_id' => $memberId ,'issue_no' => $issue_no]);
  274. foreach($list->toArray() as $k => $v){
  275. $text .= "{$v['keywords']}{$v['amount']} \n";
  276. }
  277. $text .= "\n";
  278. $text .= "----------\n";
  279. $msg['text'] = $text;
  280. }else{
  281. $msg['text'] = "当前没有开放的投注期数! \n";
  282. }
  283. return $msg;
  284. }
  285. /**
  286. * @description: 近期投注
  287. * @param {*} $memberId
  288. * @return {*}
  289. */
  290. public static function recentlyRecord($memberId ,$page = 1 ,$limit = 5)
  291. {
  292. $list = self::model()::where('member_id',$memberId)->whereIn('status',[self::model()::STATUS_STAY,self::model()::STATUS_SETTLED])->orderBy('id','desc')->forPage($page, $limit)->get();
  293. // $text = "```\n";
  294. $text = "";
  295. $text .= "期数--内容--盈亏 \n";
  296. foreach($list->toArray() as $k => $v){
  297. $profit = $v['profit'] - $v['amount'];
  298. // $text .= $v['issue_no']." ".$v['keywords']." ".$v['amount']." ".$v['profit']."\n";
  299. $item = $v['issue_no']."==".$v['keywords'].rtrim(rtrim(number_format($v['amount'], 2, '.', ''), '0'), '.')."==".rtrim(rtrim(number_format($profit, 2, '.', ''), '0'), '.')."\n";
  300. $text .= $item;
  301. }
  302. // $text .= "```\n";
  303. return $text;
  304. }
  305. /**
  306. * @description: 投注记录
  307. * @param {*} $memberId
  308. * @param {*} $page
  309. * @param {*} $limit
  310. * @return {*}
  311. */
  312. public static function record($memberId ,$messageId = null ,$page = 1 ,$limit = 5)
  313. {
  314. $msg['chat_id'] = $memberId;
  315. $list = self::model()::where('member_id',$memberId)->whereIn('status',[self::model()::STATUS_STAY,self::model()::STATUS_SETTLED])->orderBy('id','desc')->forPage($page, $limit)->get();
  316. $count = self::model()::where('member_id',$memberId)->whereIn('status',[self::model()::STATUS_STAY,self::model()::STATUS_SETTLED])->count();
  317. $keyboard = [];
  318. $text = "历史注单 \n";
  319. foreach($list as $k => $v){
  320. $phase = $v->profit - $v->amount;
  321. $text .= "-------------------------------------\n";
  322. $text .= "期数:{$v->issue_no} \n";
  323. $text .= "内容:{$v->keywords} \n";
  324. $text .= "金额:{$v->amount} \n";
  325. $text .= "盈亏:{$phase} \n";
  326. }
  327. $msg['text'] = $text;
  328. if ($page > 1) {
  329. $keyboard[] = [
  330. ['text' => "👆上一页", 'callback_data' => "betRecordNextPage@@" . ($page - 1)]
  331. ];
  332. }
  333. $allPage = ceil($count / $limit);
  334. if ($allPage > $page) {
  335. if ($page > 1) {
  336. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)];
  337. } else {
  338. $keyboard[] = [
  339. ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)]
  340. ];
  341. }
  342. }
  343. if($messageId){
  344. $msg['message_id'] = $messageId;
  345. }
  346. if($keyboard){
  347. $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  348. }
  349. return $msg;
  350. }
  351. /**
  352. * @description: 中奖结算
  353. * @param {*} $issue_no
  354. * @param {*} $awards
  355. * @return {*}
  356. */
  357. public static function betSettled($issue_no,$awards)
  358. {
  359. $list = self::findAll(['issue_no' => $issue_no ,'status' => self::model()::STATUS_STAY]);
  360. $data = [];
  361. $text = $issue_no."期开奖结果 \n";
  362. $text .= "-----本期开奖账单----- \n";
  363. $bet_num = 0;
  364. foreach($list->toArray() as $k => $v){
  365. $item = [];
  366. $item['id'] = $v['id'];
  367. $item['status'] = self::model()::STATUS_SETTLED;
  368. if(in_array($v['keywords'],$awards)){
  369. $profit = $v['amount'] * $v['odds'];
  370. if($profit > 880000){
  371. $profit = 880000; // 单注最高奖金880000
  372. }
  373. $item['profit'] = $profit;
  374. $yl = $profit - $v['amount'];
  375. if($k+1 <= 15){
  376. $text .= "私聊下注 【******】 {$yl}\n";
  377. $bet_num++;
  378. }
  379. // 结算
  380. WalletService::updateBalance($v['member_id'],$profit);
  381. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  382. $balance = $walletInfo['available_balance'];
  383. BalanceLogService::addLog($v['member_id'],$profit,$balance,($balance+$profit),'中奖',$v['id'],'');
  384. }else{
  385. if($k+1 <= 15){
  386. $text .= "私聊下注 【******】 -{$v['amount']}\n";
  387. $bet_num++;
  388. }
  389. }
  390. self::model()::where('id',$v['id'])->update($item);
  391. }
  392. $inlineButton = self::getOperateButton();
  393. $rand_num = 30 - $bet_num;
  394. for($i=0;$i<$rand_num;$i++){
  395. // 生成 -100000 到 100000 的随机数,但排除 -10 到 10 的范围
  396. $randomNumber = random_int(-1000000, 1000000) / 100;
  397. if ($randomNumber >= -10 && $randomNumber <= 10) {
  398. // 如果落在 -10 到 10 之间,重新生成或调整
  399. $randomNumber = $randomNumber < 0 ? -random_int(10, 100000) : random_int(10, 100000);
  400. }
  401. $text .= "私聊下注 【******】 {$randomNumber}\n";
  402. }
  403. // 群通知
  404. self::bettingGroupNotice($text, $inlineButton, '');
  405. }
  406. }