GameplayRuleService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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['group']) && !empty($search['group'])){
  42. $where[] = ['group', '=', $search['group']];
  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. $msg['code'] = self::YES;
  108. $msg['msg'] = '设置成功';
  109. }else{
  110. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  111. }
  112. return $msg;
  113. }
  114. /**
  115. * @description: 获取玩法规则
  116. * @param string $keywords
  117. * @return *
  118. */
  119. public static function getGameplayRules($keywords)
  120. {
  121. $cacheKey = self::CACHE_KEY . $keywords;
  122. if (Cache::has($cacheKey)) {
  123. return Cache::get($cacheKey);
  124. }
  125. $rules = self::model()::where('keywords', $keywords)->first();
  126. if (!$rules) {
  127. return null;
  128. }
  129. $rulesArray = $rules->toArray();
  130. Cache::put($cacheKey, $rulesArray, 3600); // 缓存1小时
  131. return $rulesArray;
  132. }
  133. }