BetService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /**
  14. * 投注
  15. */
  16. class BetService extends BaseService
  17. {
  18. /**
  19. * @description: 模型
  20. * @return {string}
  21. */
  22. public static function model() :string
  23. {
  24. return Bet::class;
  25. }
  26. /**
  27. * @description: 枚举
  28. * @return {*}
  29. */
  30. public static function enum() :string
  31. {
  32. return '';
  33. }
  34. /**
  35. * @description: 获取查询条件
  36. * @param {array} $search 查询内容
  37. * @return {array}
  38. */
  39. public static function getWhere(array $search = []) :array
  40. {
  41. $where = [];
  42. if(isset($search['issue_no']) && !empty($search['issue_no'])){
  43. $where[] = ['issue_no', '=', $search['issue_no']];
  44. }
  45. if(isset($search['member_id']) && !empty($search['member_id'])){
  46. $where[] = ['member_id', '=', $search['member_id']];
  47. }
  48. if(isset($search['issue_id']) && !empty($search['issue_id'])){
  49. $where[] = ['issue_id', '=', $search['issue_id']];
  50. }
  51. if(isset($search['id']) && !empty($search['id'])){
  52. $where[] = ['id', '=', $search['id']];
  53. }
  54. if(isset($search['user_id']) && !empty($search['user_id'])){
  55. $where[] = ['user_id', '=', $search['user_id']];
  56. }
  57. if(isset($search['status']) && !empty($search['status'])){
  58. $where[] = ['status', '=', $search['status']];
  59. }
  60. return $where;
  61. }
  62. /**
  63. * @description: 查询单条数据
  64. * @param array $search
  65. * @return \App\Models\Coin|null
  66. */
  67. public static function findOne(array $search): ?Bet
  68. {
  69. return self::model()::where(self::getWhere($search))->first();
  70. }
  71. /**
  72. * @description: 查询所有数据
  73. * @param array $search
  74. * @return \Illuminate\Database\Eloquent\Collection
  75. */
  76. public static function findAll(array $search = [])
  77. {
  78. return self::model()::where(self::getWhere($search))->get();
  79. }
  80. /**
  81. * @description: 分页查询
  82. * @param array $search
  83. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  84. */
  85. public static function paginate(array $search = [])
  86. {
  87. $limit = isset($search['limit'])?$search['limit']:15;
  88. $paginator = self::model()::where(self::getWhere($search))->paginate($limit);
  89. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  90. }
  91. /**
  92. * @description: 投注操作
  93. * @param {string} $memberId
  94. * @param {string} $input
  95. * @return {*}
  96. */
  97. public static function bet(string $memberId,string $input ,$messageId = 0)
  98. {
  99. $msg = [];
  100. $msg['chat_id'] = $memberId;
  101. // 钱包生成
  102. // $walletInfo = WalletService::getUserWallet($memberId);
  103. // 分解投注的内容
  104. $betResult = GameplayRuleService::bettingRuleVerify($input);
  105. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  106. if($betResult == null){
  107. $text = "消息格式错误!\n";
  108. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  109. $msg['text'] = $text;
  110. if($memberId){
  111. $msg['message_id'] = $messageId;
  112. }
  113. return $msg;
  114. }
  115. $keywords = $betResult['rule']; // 玩法
  116. $amount = $betResult['amount']; // 投注金额
  117. $GameplayRuleInfo = GameplayRuleService::getGameplayRules($keywords);
  118. if($GameplayRuleInfo == null){
  119. $text = "玩法未配置!\n";
  120. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  121. $msg['text'] = $text;
  122. if($memberId){
  123. $msg['message_id'] = $messageId;
  124. }
  125. return $msg;
  126. }
  127. if(!is_numeric($amount) || $amount <= 0){
  128. $text = "投注金额格式不正确!\n";
  129. $text .= "任何疑问都可以联系唯一财务:@{$serviceAccount}";
  130. $msg['text'] = $text;
  131. if($memberId){
  132. $msg['message_id'] = $messageId;
  133. }
  134. return $msg;
  135. }
  136. // 获取用户余额
  137. $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  138. $balance = $walletInfo['available_balance'];
  139. // 余额计算
  140. if($balance < $amount){
  141. $text = "余额不足,本次下注无效!\n";
  142. $msg['text'] = $text;
  143. if($memberId){
  144. $msg['message_id'] = $messageId;
  145. }
  146. return $msg;
  147. }
  148. }
  149. }