GameplayRuleService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\GameplayRule;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\Log;
  9. use App\Services\BalanceLogService;
  10. use App\Constants\GameplayRuleEnum;
  11. /**
  12. * 玩法规则服务类
  13. */
  14. class GameplayRuleService extends BaseService
  15. {
  16. const CACHE_KEY = 'gameplay_rules_';
  17. /**
  18. * @description: 模型
  19. * @return {string}
  20. */
  21. public static function model() :string
  22. {
  23. return GameplayRule::class;
  24. }
  25. /**
  26. * @description: 枚举
  27. * @return {*}
  28. */
  29. public static function enum() :string
  30. {
  31. return GameplayRuleEnum::class;
  32. }
  33. /**
  34. * @description: 获取查询条件
  35. * @param {array} $search 查询内容
  36. * @return {array}
  37. */
  38. public static function getWhere(array $search = []) :array
  39. {
  40. $where = [];
  41. if(isset($search['groups']) && !empty($search['groups'])){
  42. $where[] = ['groups', '=', $search['groups']];
  43. }
  44. if(isset($search['keywords']) && !empty($search['keywords'])){
  45. $where[] = ['keywords', '=', $search['keywords']];
  46. }
  47. if(isset($search['id']) && !empty($search['id'])){
  48. $where[] = ['id', '=', $search['id']];
  49. }
  50. return $where;
  51. }
  52. /**
  53. * @description: 查询单条数据
  54. * @param array $search
  55. * @return \App\Models\Coin|null
  56. */
  57. public static function findOne(array $search): ?GameplayRule
  58. {
  59. return self::model()::where(self::getWhere($search))->first();
  60. }
  61. /**
  62. * @description: 查询所有数据
  63. * @param array $search
  64. * @return \Illuminate\Database\Eloquent\Collection
  65. */
  66. public static function findAll(array $search = [])
  67. {
  68. return self::model()::where(self::getWhere($search))->get();
  69. }
  70. /**
  71. * @description: 分页查询
  72. * @param array $search
  73. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  74. */
  75. public static function paginate(array $search = [])
  76. {
  77. $limit = isset($search['limit'])?$search['limit']:15;
  78. $paginator = self::model()::where(self::getWhere($search))
  79. ->orderByDesc("odds")->paginate($limit);
  80. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  81. }
  82. /**
  83. * @description:
  84. * @param {*} $params
  85. * @return {*}
  86. */
  87. public static function submit($params = [])
  88. {
  89. $result = false;
  90. $msg['code'] = self::NOT;
  91. $msg['msg'] = '';
  92. // 2. 判断是否是更新
  93. if (!empty($params['id'])) {
  94. // 更新
  95. $info = self::findOne(['id'=>$params['id']] );
  96. if (!$info) {
  97. $msg['msg'] = '规则不存在!';
  98. }else{
  99. $result = $info->update($params);
  100. $id = $params['id'];
  101. }
  102. } else {
  103. // 创建
  104. $result = $info = self::model()::create($params);
  105. $id = $result->id;
  106. }
  107. if($result){
  108. self::clearCache($params['keywords']);
  109. $msg['code'] = self::YES;
  110. $msg['msg'] = '设置成功';
  111. }else{
  112. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  113. }
  114. return $msg;
  115. }
  116. /**
  117. * @description: 获取玩法规则
  118. * @param string $keywords
  119. * @return *
  120. */
  121. public static function getGameplayRules($keywords)
  122. {
  123. $cacheKey = self::CACHE_KEY . $keywords;
  124. if (Cache::has($cacheKey)) {
  125. return Cache::get($cacheKey);
  126. }
  127. $rules = self::model()::where('keywords', $keywords)->first();
  128. if (!$rules) {
  129. return null;
  130. }
  131. $rulesArray = $rules->toArray();
  132. Cache::put($cacheKey, $rulesArray, 3600); // 缓存1小时
  133. return $rulesArray;
  134. }
  135. /**
  136. * @description: 清除缓存
  137. * @param string $keywords
  138. * @return void
  139. */
  140. public static function clearCache($keywords)
  141. {
  142. $cacheKey = self::CACHE_KEY . $keywords;
  143. Cache::forget($cacheKey);
  144. }
  145. /**
  146. * @description: 校验输入的内容
  147. * @param {*} $input
  148. * @return {*}
  149. */
  150. public static function bettingRuleVerify($input)
  151. {
  152. $result = self::validateInput($input);
  153. return $result;
  154. }
  155. /**
  156. * @description: 校验输入的内容
  157. * @param {*} $input
  158. * @return {*}
  159. */
  160. public static function validateInput($input)
  161. {
  162. // 获取所有玩法
  163. $allRules = self::model()::pluck('keywords')->toArray();
  164. // 玩法正则(防止玩法里有特殊符号出错)
  165. $rulesPattern = implode('|', array_map('preg_quote', $allRules));
  166. // 匹配玩法 + 金额
  167. if (preg_match('/^(' . $rulesPattern . ')(\d+)$/u', $input, $matches)) {
  168. return [
  169. 'rule' => $matches[1],
  170. 'amount' => (int)$matches[2]
  171. ];
  172. }
  173. return null;
  174. }
  175. }