BetService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. return $msg;
  208. }
  209. /**
  210. * @description: 当期下注
  211. * @param {*} $memberId
  212. * @return {*}
  213. */
  214. public static function currentBet($memberId)
  215. {
  216. $msg['chat_id'] = $memberId;
  217. // 期数验证
  218. $issueInfo = IssueService::model()::where('status',IssueService::model()::STATUS_BETTING)->orderBy('id','desc')->first();
  219. if(!empty($issueInfo)){
  220. $issue_no = $issueInfo->issue_no;
  221. }else{
  222. $issueCloseInfo = IssueService::model()::where('status',IssueService::model()::STATUS_CLOSE)->orderBy('id','desc')->first();
  223. if(empty($issueCloseInfo)){
  224. $issue_no = $issueCloseInfo->issue_no;
  225. }
  226. }
  227. if($issue_no){
  228. $text = "当前期号:{$issue_no} \n";
  229. $text .= "\n";
  230. $text .= "----------\n";
  231. $list = self::findAll(['member_id' => $memberId ,'issue_no' => $issue_no]);
  232. foreach($list->toArray() as $k => $v){
  233. $text .= "{$v['keywords']}{$v['amount']} \n";
  234. }
  235. $text .= "\n";
  236. $text .= "----------\n";
  237. $msg['text'] = $text;
  238. }else{
  239. $msg['text'] = "当前没有开放的投注期数! \n";
  240. }
  241. return $msg;
  242. }
  243. /**
  244. * @description: 投注记录
  245. * @param {*} $memberId
  246. * @param {*} $page
  247. * @param {*} $limit
  248. * @return {*}
  249. */
  250. public static function record($memberId ,$messageId = null ,$page = 1 ,$limit = 5)
  251. {
  252. $msg['chat_id'] = $memberId;
  253. $list = self::model()::where('member_id',$memberId)->whereIn('status',[self::model()::STATUS_STAY,self::model()::STATUS_SETTLED])->orderBy('id','desc')->forPage($page, $limit)->get();
  254. $count = self::model()::where('member_id',$memberId)->whereIn('status',[self::model()::STATUS_STAY,self::model()::STATUS_SETTLED])->count();
  255. $keyboard = [];
  256. $text = "历史注单 \n";
  257. foreach($list as $k => $v){
  258. $text .= "-------------------------------------\n";
  259. $text .= "期数:{$v->issue_no} \n";
  260. $text .= "内容:{$v->keywords} \n";
  261. $text .= "金额:{$v->amount} \n";
  262. $text .= "盈亏:{$v->profit} \n";
  263. }
  264. $msg['text'] = $text;
  265. if ($page > 1) {
  266. $keyboard[] = [
  267. ['text' => "👆上一页", 'callback_data' => "betRecordNextPage@@" . ($page - 1)]
  268. ];
  269. }
  270. $allPage = ceil($count / $limit);
  271. if ($allPage > $page) {
  272. if ($page > 1) {
  273. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)];
  274. } else {
  275. $keyboard[] = [
  276. ['text' => "👇下一页", 'callback_data' => "betRecordNextPage@@" . ($page + 1)]
  277. ];
  278. }
  279. }
  280. if($messageId){
  281. $msg['message_id'] = $messageId;
  282. }
  283. if($keyboard){
  284. $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  285. }
  286. return $msg;
  287. }
  288. }