BetService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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['issue_id']) && !empty($search['issue_id'])){
  51. $where[] = ['issue_id', '=', $search['issue_id']];
  52. }
  53. if(isset($search['id']) && !empty($search['id'])){
  54. $where[] = ['id', '=', $search['id']];
  55. }
  56. if(isset($search['user_id']) && !empty($search['user_id'])){
  57. $where[] = ['user_id', '=', $search['user_id']];
  58. }
  59. if(isset($search['status']) && !empty($search['status'])){
  60. $where[] = ['status', '=', $search['status']];
  61. }
  62. return $where;
  63. }
  64. /**
  65. * @description: 查询单条数据
  66. * @param array $search
  67. * @return \App\Models\Coin|null
  68. */
  69. public static function findOne(array $search): ?Bet
  70. {
  71. return self::model()::where(self::getWhere($search))->first();
  72. }
  73. /**
  74. * @description: 查询所有数据
  75. * @param array $search
  76. * @return \Illuminate\Database\Eloquent\Collection
  77. */
  78. public static function findAll(array $search = [])
  79. {
  80. return self::model()::where(self::getWhere($search))->get();
  81. }
  82. /**
  83. * @description: 分页查询
  84. * @param array $search
  85. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  86. */
  87. public static function paginate(array $search = [])
  88. {
  89. $limit = isset($search['limit'])?$search['limit']:15;
  90. $paginator = self::model()::where(self::getWhere($search))->paginate($limit);
  91. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  92. }
  93. /**
  94. * @description: 投注操作
  95. * @param {string} $memberId
  96. * @param {string} $input
  97. * @return {*}
  98. */
  99. public static function bet(string $memberId,string $input ,$messageId = 0)
  100. {
  101. $msg = [];
  102. $msg['chat_id'] = $memberId;
  103. // 钱包生成
  104. // $walletInfo = WalletService::getUserWallet($memberId);
  105. // 分解投注的内容
  106. $betResult = GameplayRuleService::bettingRuleVerify($input);
  107. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  108. if($betResult == null){
  109. $text = "消息格式错误!\n";
  110. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  111. $msg['text'] = $text;
  112. if($messageId){
  113. $msg['reply_to_message_id'] = $messageId;
  114. }
  115. return $msg;
  116. }
  117. $keywords = $betResult['rule']; // 玩法
  118. $amount = $betResult['amount']; // 投注金额
  119. $gameplayRuleInfo = GameplayRuleService::getGameplayRules($keywords);
  120. if($gameplayRuleInfo == null){
  121. $text = "玩法未配置!\n";
  122. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  123. $msg['text'] = $text;
  124. if($messageId){
  125. $msg['reply_to_message_id'] = $messageId;
  126. }
  127. return $msg;
  128. }
  129. // 期数验证
  130. $issueInfo = IssueService::model()::where('status',IssueService::model()::STATUS_BETTING)->orderBy('id','desc')->first();
  131. if(empty($issueInfo)){
  132. $issueCloseInfo = IssueService::model()::where('status',IssueService::model()::STATUS_CLOSE)->orderBy('id','desc')->first();
  133. if(empty($issueCloseInfo)){
  134. $text = "暂无可下注期数,本次下注无效!\n";
  135. $msg['text'] = $text;
  136. if($messageId){
  137. $msg['reply_to_message_id'] = $messageId;
  138. }
  139. return $msg;
  140. }else{
  141. $text = "封盘中,本次下注无效!\n";
  142. $msg['text'] = $text;
  143. if($messageId){
  144. $msg['reply_to_message_id'] = $messageId;
  145. }
  146. return $msg;
  147. }
  148. }
  149. if(!is_numeric($amount) || $amount <= 0){
  150. $text = "投注金额格式不正确!\n";
  151. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  152. $msg['text'] = $text;
  153. if($messageId){
  154. $msg['reply_to_message_id'] = $messageId;
  155. }
  156. return $msg;
  157. }
  158. // 投注限制校验
  159. if($amount < $gameplayRuleInfo['mininum']){
  160. $text = "下注失败,最小金额限制{$gameplayRuleInfo['mininum']}\n";
  161. $msg['text'] = $text;
  162. if($messageId){
  163. $msg['reply_to_message_id'] = $messageId;
  164. }
  165. return $msg;
  166. }
  167. // 投注限制校验
  168. if($amount > $gameplayRuleInfo['maxinum']){
  169. $text = "下注失败,最大金额限制{$gameplayRuleInfo['maxinum']}\n";
  170. $msg['text'] = $text;
  171. if($messageId){
  172. $msg['reply_to_message_id'] = $messageId;
  173. }
  174. return $msg;
  175. }
  176. // 获取用户余额
  177. $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  178. $balance = $walletInfo['available_balance'];
  179. // 余额计算
  180. if($balance < $amount){
  181. $text = "余额不足,本次下注无效!\n";
  182. $msg['text'] = $text;
  183. if($messageId){
  184. $msg['reply_to_message_id'] = $messageId;
  185. }
  186. return $msg;
  187. }
  188. $userInfo = UserService::findOne(['member_id' => $memberId]);
  189. $data = [];
  190. $data['amount'] = $amount; // 分数
  191. $data['keywords'] = $keywords; // 玩法
  192. $data['member_id'] = $memberId;
  193. $data['user_id'] = $userInfo->id;
  194. $data['issue_no'] = $issueInfo->issue_no;
  195. $data['issue_id'] = $issueInfo->id;
  196. $data['odds'] = $gameplayRuleInfo['odds'];
  197. $newBet = self::model()::create($data);
  198. WalletService::updateBalance($memberId,-$amount);
  199. BalanceLogService::addLog($memberId,-$amount,$balance,($balance-$amount),'投注',$newBet->id,'');
  200. $text = "下注期数:{$issueInfo->issue_no}\n";
  201. $text .= "下注内容\n";
  202. $text .= "--------\n";
  203. $text .= "{$input}\n";
  204. $text .= "--------\n";
  205. $text .= "下注成功\n";
  206. $msg['text'] = $text;
  207. $groupText = "";
  208. $groupText .= "私聊下注 【xxxxxx】 \n";
  209. $groupText .= "下注期数:{$issueInfo->issue_no} \n";
  210. $groupText .= "下注内容: \n";
  211. $groupText .= "----------- \n";
  212. $groupText .= "{$input} \n";
  213. $groupText .= "----------- \n";
  214. $username = config('services.telegram.username');
  215. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  216. $inlineButton = [];
  217. $inlineButton[] = [
  218. ['text' => "查看余额", 'callback_data' => 'balanceAlert'],
  219. ['text' => "✅唯一财务", 'url' => "https://t.me/{$serviceAccount}"]
  220. ];
  221. $inlineButton[] = [
  222. ['text' => "近期注单", 'callback_data' => 'betsAlert'],
  223. ['text' => "今日流水", 'callback_data' => 'todayFlowAlert']
  224. ];
  225. $inlineButton[] = [
  226. ['text' => "私聊下注", 'url' => "https://t.me/{$username}"]
  227. ];
  228. $inlineButton[] = [
  229. ['text' => "官方频道", 'url' => 'https://t.me/xxx']
  230. ];
  231. // 群通知
  232. self::bettingGroupNotice($groupText,$inlineButton); // 群通知
  233. return $msg;
  234. }
  235. /**
  236. * @description: 当期下注
  237. * @param {*} $memberId
  238. * @return {*}
  239. */
  240. public static function currentBet($memberId)
  241. {
  242. $msg['chat_id'] = $memberId;
  243. // 期数验证
  244. $issueInfo = IssueService::model()::where('status',IssueService::model()::STATUS_BETTING)->orderBy('id','desc')->first();
  245. if(!empty($issueInfo)){
  246. $issue_no = $issueInfo->issue_no;
  247. }else{
  248. $issueCloseInfo = IssueService::model()::where('status',IssueService::model()::STATUS_CLOSE)->orderBy('id','desc')->first();
  249. if(empty($issueCloseInfo)){
  250. $issue_no = $issueCloseInfo->issue_no;
  251. }
  252. }
  253. if($issue_no){
  254. $text = "当前期号:{$issue_no} \n";
  255. $text .= "\n";
  256. $text .= "----------\n";
  257. $list = self::findAll(['member_id' => $memberId ,'issue_no' => $issue_no]);
  258. foreach($list->toArray() as $k => $v){
  259. $text .= "{$v['keywords']}{$v['amount']} \n";
  260. }
  261. $text .= "\n";
  262. $text .= "----------\n";
  263. $msg['text'] = $text;
  264. }else{
  265. $msg['text'] = "当前没有开放的投注期数! \n";
  266. }
  267. return $msg;
  268. }
  269. /**
  270. * @description: 近期投注
  271. * @param {*} $memberId
  272. * @return {*}
  273. */
  274. public static function recentlyRecord($memberId ,$page = 1 ,$limit = 5)
  275. {
  276. $list = self::model()::where('member_id',$memberId)->whereIn('status',[self::model()::STATUS_STAY,self::model()::STATUS_SETTLED])->orderBy('id','desc')->forPage($page, $limit)->get();
  277. // $text = "```\n";
  278. $text = "";
  279. $text .= "内容--盈亏 \n";
  280. foreach($list->toArray() as $k => $v){
  281. // $text .= $v['issue_no']." ".$v['keywords']." ".$v['amount']." ".$v['profit']."\n";
  282. $item = $v['keywords'].rtrim(rtrim(number_format($v['amount'], 2, '.', ''), '0'), '.')."--".rtrim(rtrim(number_format($v['profit'], 2, '.', ''), '0'), '.')."\n";
  283. $text .= $item;
  284. }
  285. // $text .= "```\n";
  286. return $text;
  287. }
  288. /**
  289. * @description: 投注记录
  290. * @param {*} $memberId
  291. * @param {*} $page
  292. * @param {*} $limit
  293. * @return {*}
  294. */
  295. public static function record($memberId ,$messageId = null ,$page = 1 ,$limit = 5)
  296. {
  297. $msg['chat_id'] = $memberId;
  298. $list = self::model()::where('member_id',$memberId)->whereIn('status',[self::model()::STATUS_STAY,self::model()::STATUS_SETTLED])->orderBy('id','desc')->forPage($page, $limit)->get();
  299. $count = self::model()::where('member_id',$memberId)->whereIn('status',[self::model()::STATUS_STAY,self::model()::STATUS_SETTLED])->count();
  300. $keyboard = [];
  301. $text = "历史注单 \n";
  302. foreach($list as $k => $v){
  303. $text .= "-------------------------------------\n";
  304. $text .= "期数:{$v->issue_no} \n";
  305. $text .= "内容:{$v->keywords} \n";
  306. $text .= "金额:{$v->amount} \n";
  307. $text .= "盈亏:{$v->profit} \n";
  308. }
  309. $msg['text'] = $text;
  310. if ($page > 1) {
  311. $keyboard[] = [
  312. ['text' => "👆上一页", 'callback_data' => "betRecordNextPage@@" . ($page - 1)]
  313. ];
  314. }
  315. $allPage = ceil($count / $limit);
  316. if ($allPage > $page) {
  317. if ($page > 1) {
  318. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)];
  319. } else {
  320. $keyboard[] = [
  321. ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)]
  322. ];
  323. }
  324. }
  325. if($messageId){
  326. $msg['message_id'] = $messageId;
  327. }
  328. if($keyboard){
  329. $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  330. }
  331. return $msg;
  332. }
  333. /**
  334. * @description: 中奖结算
  335. * @param {*} $issue_no
  336. * @param {*} $awards
  337. * @return {*}
  338. */
  339. public static function betSettled($issue_no,$awards)
  340. {
  341. $list = self::findAll(['issue_no' => $issue_no ,'status' => self::model()::STATUS_STAY]);
  342. $data = [];
  343. foreach($list->toArray() as $k => $v){
  344. $item = [];
  345. $item['id'] = $v['id'];
  346. $item['status'] = self::model()::STATUS_SETTLED;
  347. if(in_array($v['keywords'],$awards)){
  348. $profit = $v['amount'] * $v['odds'];
  349. $item['profit'] = $profit;
  350. // 结算
  351. WalletService::updateBalance($v['member_id'],$profit);
  352. $walletInfo = WalletService::findOne(['member_id' => $v['member_id']]);
  353. $balance = $walletInfo['available_balance'];
  354. BalanceLogService::addLog($v['member_id'],$profit,$balance,($balance+$profit),'中奖',$v['id'],'');
  355. }
  356. self::model()::where('id',$v['id'])->update($item);
  357. }
  358. }
  359. }