BetService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. // 期数验证
  133. $issueInfo = IssueService::model()::where('status',IssueService::model()::STATUS_BETTING)->orderBy('id','desc')->first();
  134. if(empty($issueInfo)){
  135. $issueCloseInfo = IssueService::model()::where('status',IssueService::model()::STATUS_CLOSE)->orderBy('id','desc')->first();
  136. if(empty($issueCloseInfo)){
  137. $text = "暂无可下注期数,本次下注无效!\n";
  138. $msg['text'] = $text;
  139. if($messageId){
  140. $msg['reply_to_message_id'] = $messageId;
  141. }
  142. return $msg;
  143. }else{
  144. $text = "封盘中,本次下注无效!\n";
  145. $msg['text'] = $text;
  146. if($messageId){
  147. $msg['reply_to_message_id'] = $messageId;
  148. }
  149. return $msg;
  150. }
  151. }
  152. if(!is_numeric($amount) || $amount <= 0){
  153. $text = "投注金额格式不正确!\n";
  154. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  155. $msg['text'] = $text;
  156. if($messageId){
  157. $msg['reply_to_message_id'] = $messageId;
  158. }
  159. return $msg;
  160. }
  161. // 投注限制校验
  162. if($amount < $gameplayRuleInfo['mininum']){
  163. $text = "下注失败,最小金额限制{$gameplayRuleInfo['mininum']}\n";
  164. $msg['text'] = $text;
  165. if($messageId){
  166. $msg['reply_to_message_id'] = $messageId;
  167. }
  168. return $msg;
  169. }
  170. // 投注限制校验
  171. if($amount > $gameplayRuleInfo['maxinum']){
  172. $text = "下注失败,最大金额限制{$gameplayRuleInfo['maxinum']}\n";
  173. $msg['text'] = $text;
  174. if($messageId){
  175. $msg['reply_to_message_id'] = $messageId;
  176. }
  177. return $msg;
  178. }
  179. // 获取用户余额
  180. $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  181. $balance = $walletInfo['available_balance'];
  182. // 余额计算
  183. if($balance < $amount){
  184. $text = "余额不足,本次下注无效!\n";
  185. $msg['text'] = $text;
  186. if($messageId){
  187. $msg['reply_to_message_id'] = $messageId;
  188. }
  189. return $msg;
  190. }
  191. $userInfo = UserService::findOne(['member_id' => $memberId]);
  192. $betInfo = self::findOne(['member_id' => $memberId,'issue_no' => $issueInfo->issue_no,'keywords' => $keywords]); // 相同下注
  193. if($betInfo){
  194. $betInfo->amount = $betInfo->amount + $amount;
  195. $bet_id = $betInfo->id;
  196. $betInfo->save();
  197. }else{
  198. $data = [];
  199. $data['amount'] = $amount; // 分数
  200. $data['keywords'] = $keywords; // 玩法
  201. $data['member_id'] = $memberId;
  202. $data['user_id'] = $userInfo->id;
  203. $data['issue_no'] = $issueInfo->issue_no;
  204. $data['issue_id'] = $issueInfo->id;
  205. $data['odds'] = $gameplayRuleInfo['odds'];
  206. $newBet = self::model()::create($data);
  207. $bet_id = $newBet->id;
  208. }
  209. WalletService::updateBalance($memberId,-$amount);
  210. BalanceLogService::addLog($memberId,-$amount,$balance,($balance-$amount),'投注',$bet_id,'');
  211. // 返利
  212. $rebate = Config::where('field', 'rebate')->first()->val;
  213. if($rebate > 0){
  214. $rebateAmount = bcmul($amount, $rebate, 2); // 返利金额
  215. if($rebateAmount > 0){
  216. WalletService::updateBalance($memberId,$rebateAmount);
  217. $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  218. $balance = $walletInfo['available_balance'];
  219. BalanceLogService::addLog($memberId,$rebateAmount,$balance,($balance+$rebateAmount),'返水',$bet_id,'');
  220. }
  221. }
  222. $text = "下注期数:{$issueInfo->issue_no}\n";
  223. $text .= "下注内容\n";
  224. $text .= "--------\n";
  225. $text .= "{$input}\n";
  226. $text .= "--------\n";
  227. $text .= "下注成功\n";
  228. $msg['text'] = $text;
  229. $groupText = "";
  230. $groupText .= "私聊下注 【xxxxxx】 \n";
  231. $groupText .= "下注期数:{$issueInfo->issue_no} \n";
  232. $groupText .= "下注内容: \n";
  233. $groupText .= "----------- \n";
  234. $groupText .= "{$input} \n";
  235. $groupText .= "----------- \n";
  236. $inlineButton = self::getOperateButton();
  237. // 群通知
  238. self::bettingGroupNotice($groupText,$inlineButton); // 群通知
  239. return $msg;
  240. }
  241. /**
  242. * @description: 当期下注
  243. * @param {*} $memberId
  244. * @return {*}
  245. */
  246. public static function currentBet($memberId)
  247. {
  248. $msg['chat_id'] = $memberId;
  249. // 期数验证
  250. $issueInfo = IssueService::model()::where('status',IssueService::model()::STATUS_BETTING)->orderBy('id','desc')->first();
  251. $issue_no = '';
  252. if(!empty($issueInfo)){
  253. $issue_no = $issueInfo->issue_no;
  254. }else{
  255. $issueCloseInfo = IssueService::model()::where('status',IssueService::model()::STATUS_CLOSE)->orderBy('id','desc')->first();
  256. if(empty($issueCloseInfo)){
  257. $issue_no = $issueCloseInfo->issue_no;
  258. }
  259. }
  260. if($issue_no){
  261. $text = "当前期号:{$issue_no} \n";
  262. $text .= "\n";
  263. $text .= "----------\n";
  264. $list = self::findAll(['member_id' => $memberId ,'issue_no' => $issue_no]);
  265. foreach($list->toArray() as $k => $v){
  266. $text .= "{$v['keywords']}{$v['amount']} \n";
  267. }
  268. $text .= "\n";
  269. $text .= "----------\n";
  270. $msg['text'] = $text;
  271. }else{
  272. $msg['text'] = "当前没有开放的投注期数! \n";
  273. }
  274. return $msg;
  275. }
  276. /**
  277. * @description: 近期投注
  278. * @param {*} $memberId
  279. * @return {*}
  280. */
  281. public static function recentlyRecord($memberId ,$page = 1 ,$limit = 5)
  282. {
  283. $list = self::model()::where('member_id',$memberId)->whereIn('status',[self::model()::STATUS_STAY,self::model()::STATUS_SETTLED])->orderBy('id','desc')->forPage($page, $limit)->get();
  284. // $text = "```\n";
  285. $text = "";
  286. $text .= "期数--内容--盈亏 \n";
  287. foreach($list->toArray() as $k => $v){
  288. // $text .= $v['issue_no']." ".$v['keywords']." ".$v['amount']." ".$v['profit']."\n";
  289. $item = $v['issue_no']."--".$v['keywords'].rtrim(rtrim(number_format($v['amount'], 2, '.', ''), '0'), '.')."--".rtrim(rtrim(number_format($v['profit'], 2, '.', ''), '0'), '.')."\n";
  290. $text .= $item;
  291. }
  292. // $text .= "```\n";
  293. return $text;
  294. }
  295. /**
  296. * @description: 投注记录
  297. * @param {*} $memberId
  298. * @param {*} $page
  299. * @param {*} $limit
  300. * @return {*}
  301. */
  302. public static function record($memberId ,$messageId = null ,$page = 1 ,$limit = 5)
  303. {
  304. $msg['chat_id'] = $memberId;
  305. $list = self::model()::where('member_id',$memberId)->whereIn('status',[self::model()::STATUS_STAY,self::model()::STATUS_SETTLED])->orderBy('id','desc')->forPage($page, $limit)->get();
  306. $count = self::model()::where('member_id',$memberId)->whereIn('status',[self::model()::STATUS_STAY,self::model()::STATUS_SETTLED])->count();
  307. $keyboard = [];
  308. $text = "历史注单 \n";
  309. foreach($list as $k => $v){
  310. $text .= "-------------------------------------\n";
  311. $text .= "期数:{$v->issue_no} \n";
  312. $text .= "内容:{$v->keywords} \n";
  313. $text .= "金额:{$v->amount} \n";
  314. $text .= "盈亏:{$v->profit} \n";
  315. }
  316. $msg['text'] = $text;
  317. if ($page > 1) {
  318. $keyboard[] = [
  319. ['text' => "👆上一页", 'callback_data' => "betRecordNextPage@@" . ($page - 1)]
  320. ];
  321. }
  322. $allPage = ceil($count / $limit);
  323. if ($allPage > $page) {
  324. if ($page > 1) {
  325. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)];
  326. } else {
  327. $keyboard[] = [
  328. ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)]
  329. ];
  330. }
  331. }
  332. if($messageId){
  333. $msg['message_id'] = $messageId;
  334. }
  335. if($keyboard){
  336. $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  337. }
  338. return $msg;
  339. }
  340. /**
  341. * @description: 中奖结算
  342. * @param {*} $issue_no
  343. * @param {*} $awards
  344. * @return {*}
  345. */
  346. public static function betSettled($issue_no,$awards)
  347. {
  348. $list = self::findAll(['issue_no' => $issue_no ,'status' => self::model()::STATUS_STAY]);
  349. $data = [];
  350. $text = $issue_no."期开奖结果 \n";
  351. $text .= "-----本期开奖账单----- \n";
  352. foreach($list->toArray() as $k => $v){
  353. $item = [];
  354. $item['id'] = $v['id'];
  355. $item['status'] = self::model()::STATUS_SETTLED;
  356. if(in_array($v['keywords'],$awards)){
  357. $profit = $v['amount'] * $v['odds'];
  358. if($profit > 880000){
  359. $profit = 880000; // 单注最高奖金880000
  360. }
  361. $item['profit'] = $profit;
  362. $text .= "私聊下注 【******】 {$profit}\n";
  363. // 结算
  364. WalletService::updateBalance($v['member_id'],$profit);
  365. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  366. $balance = $walletInfo['available_balance'];
  367. BalanceLogService::addLog($v['member_id'],$profit,$balance,($balance+$profit),'中奖',$v['id'],'');
  368. }else{
  369. $text .= "私聊下注 【******】 -{$v['amount']}\n";
  370. }
  371. self::model()::where('id',$v['id'])->update($item);
  372. }
  373. $inlineButton = self::getOperateButton();
  374. self::bettingGroupNotice($text, $inlineButton, '');
  375. }
  376. }