GameplayRuleService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. ->orderBy("groups")
  80. ->orderBy("keywords")->paginate($limit);
  81. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  82. }
  83. /**
  84. * @description:
  85. * @param {*} $params
  86. * @return {*}
  87. */
  88. public static function submit($params = [])
  89. {
  90. $result = false;
  91. $msg['code'] = self::NOT;
  92. $msg['msg'] = '';
  93. // 2. 判断是否是更新
  94. if (!empty($params['id'])) {
  95. // 更新
  96. $info = self::findOne(['id'=>$params['id']] );
  97. if (!$info) {
  98. $msg['msg'] = '规则不存在!';
  99. }else{
  100. $result = $info->update($params);
  101. $id = $params['id'];
  102. }
  103. } else {
  104. // 创建
  105. $result = $info = self::model()::create($params);
  106. $id = $result->id;
  107. }
  108. if($result){
  109. self::clearCache($params['keywords']);
  110. $msg['code'] = self::YES;
  111. $msg['msg'] = '设置成功';
  112. }else{
  113. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  114. }
  115. return $msg;
  116. }
  117. /**
  118. * @description: 获取玩法规则
  119. * @param string $keywords
  120. * @return *
  121. */
  122. public static function getGameplayRules($keywords)
  123. {
  124. $cacheKey = self::CACHE_KEY . $keywords;
  125. if (Cache::has($cacheKey)) {
  126. return Cache::get($cacheKey);
  127. }
  128. $rules = self::model()::where('keywords', $keywords)->first();
  129. if (!$rules) {
  130. return null;
  131. }
  132. $rulesArray = $rules->toArray();
  133. Cache::put($cacheKey, $rulesArray, 3600); // 缓存1小时
  134. return $rulesArray;
  135. }
  136. /**
  137. * @description: 清除缓存
  138. * @param string $keywords
  139. * @return void
  140. */
  141. public static function clearCache($keywords)
  142. {
  143. $cacheKey = self::CACHE_KEY . $keywords;
  144. Cache::forget($cacheKey);
  145. }
  146. /**
  147. * @description: 校验输入的内容
  148. * @param {*} $input
  149. * @return {*}
  150. */
  151. public static function bettingRuleVerify($input)
  152. {
  153. $result = self::validateInput($input);
  154. return $result;
  155. }
  156. /**
  157. * @description: 校验输入的内容
  158. * @param {*} $input
  159. * @return {*}
  160. */
  161. public static function validateInput($input)
  162. {
  163. // 获取所有玩法
  164. $allRules = self::model()::pluck('keywords')->toArray();
  165. // 玩法正则(防止玩法里有特殊符号出错)
  166. $rulesPattern = implode('|', array_map('preg_quote', $allRules));
  167. // 匹配玩法 + 金额
  168. if (preg_match('/^(' . $rulesPattern . ')(\d+)$/u', $input, $matches)) {
  169. return [
  170. 'rule' => $matches[1],
  171. 'amount' => (int)$matches[2]
  172. ];
  173. }
  174. return null;
  175. }
  176. }