first(); } /** * @description: 查询所有数据 * @param array $search * @return \Illuminate\Database\Eloquent\Collection */ public static function findAll(array $search = []) { return self::model()::where(self::getWhere($search))->get(); } /** * @description: 分页查询 * @param array $search * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator */ public static function paginate(array $search = []) { $limit = isset($search['limit']) ? $search['limit'] : 15; $paginator = self::model()::where(self::getWhere($search))->paginate($limit); return ['total' => $paginator->total(), 'data' => $paginator->items()]; } /** * @description: * @param {*} $params * @return {*} */ public static function submit($params = []) { $result = DB::transaction(function () use ($params) { $templates = LotteryOdds::lockPc28Templates(); if (empty($params['id'])) { $info = self::model()::create($params); LotteryOdds::pc28CatalogChanged($templates, $info->id, $info->only(['odds', 'mininum', 'maxinum'])); return ['code' => self::YES, 'msg' => '设置成功']; } $info = self::model()::whereKey($params['id'])->lockForUpdate()->first(); if (!$info) { return ['code' => self::NOT, 'msg' => '规则不存在!']; } foreach (['odds', 'mininum', 'maxinum'] as $field) { if (isset($params[$field]) && $params[$field] != $info->$field) { return ['code' => self::NOT, 'msg' => '请前往赔率管理选择彩种和模板']; } } $oldKeywords = $info->keywords; $info->fill(array_intersect_key($params, ['keywords' => true, 'groups' => true])); if ($info->isDirty()) { LotteryOdds::pc28CatalogChanged($templates, $info->id); $info->save(); } return ['code' => self::YES, 'msg' => '设置成功', 'old_keywords' => $oldKeywords]; }, 3); if ($result['code'] === self::YES) { self::clearCache($params['keywords']); if (isset($result['old_keywords'])) { self::clearCache($result['old_keywords']); } } return $result; } /** * @description: 获取玩法规则 * @param string $keywords * @return * */ public static function getGameplayRules($keywords) { $cacheKey = self::CACHE_KEY . $keywords; if (Cache::has($cacheKey)) { return Cache::get($cacheKey); } $rules = self::model()::where('keywords', $keywords)->first(); if (!$rules) { return null; } $rulesArray = $rules->toArray(); Cache::put($cacheKey, $rulesArray, 3600); // 缓存1小时 return $rulesArray; } /** * @description: 清除缓存 * @param string $keywords * @return void */ public static function clearCache($keywords) { $cacheKey = self::CACHE_KEY . $keywords; Cache::forget($cacheKey); } /** * @description: 校验输入的内容 * @param {*} $input * @return {*} */ public static function bettingRuleVerify($input) { $input = explode(' ', $input); $result = []; foreach ($input as $value) { $res = self::validateInput($value); if ($res !== null) { $result[] = $res; } } if (empty($result)) return null; return $result; } /** * @description: 校验输入的内容 * @param {*} $input * @return {*} */ public static function validateInput($input) { // 获取所有玩法 $allRules = self::model()::pluck('keywords')->toArray(); // 玩法正则(防止玩法里有特殊符号出错) $rulesPattern = implode('|', array_map('preg_quote', $allRules)); // 匹配玩法 + 金额 if (preg_match('/^(' . $rulesPattern . ')(\d+)$/u', $input, $matches)) { return [ 'rule' => $matches[1], 'amount' => (int)$matches[2] ]; } return null; } }