GameplayRuleService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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))->paginate($limit);
  79. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  80. }
  81. /**
  82. * @description:
  83. * @param {*} $params
  84. * @return {*}
  85. */
  86. public static function submit($params = [])
  87. {
  88. $result = false;
  89. $msg['code'] = self::NOT;
  90. $msg['msg'] = '';
  91. // 2. 判断是否是更新
  92. if (!empty($params['id'])) {
  93. // 更新
  94. $info = self::findOne(['id'=>$params['id']] );
  95. if (!$info) {
  96. $msg['msg'] = '规则不存在!';
  97. }else{
  98. $result = $info->update($params);
  99. $id = $params['id'];
  100. }
  101. } else {
  102. // 创建
  103. $result = $info = self::model()::create($params);
  104. $id = $result->id;
  105. }
  106. if($result){
  107. self::clearCache($params['keywords']);
  108. $msg['code'] = self::YES;
  109. $msg['msg'] = '设置成功';
  110. }else{
  111. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  112. }
  113. return $msg;
  114. }
  115. /**
  116. * @description: 获取玩法规则
  117. * @param string $keywords
  118. * @return *
  119. */
  120. public static function getGameplayRules($keywords)
  121. {
  122. $cacheKey = self::CACHE_KEY . $keywords;
  123. if (Cache::has($cacheKey)) {
  124. return Cache::get($cacheKey);
  125. }
  126. $rules = self::model()::where('keywords', $keywords)->first();
  127. if (!$rules) {
  128. return null;
  129. }
  130. $rulesArray = $rules->toArray();
  131. Cache::put($cacheKey, $rulesArray, 3600); // 缓存1小时
  132. return $rulesArray;
  133. }
  134. /**
  135. * @description: 清除缓存
  136. * @param string $keywords
  137. * @return void
  138. */
  139. public static function clearCache($keywords)
  140. {
  141. $cacheKey = self::CACHE_KEY . $keywords;
  142. Cache::forget($cacheKey);
  143. }
  144. }