Files
dafuweng-saiadmin6.x/server/app/dice/model/reward_config/DiceRewardConfig.php

167 lines
4.4 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\dice\model\reward_config;
use plugin\saiadmin\basic\think\BaseModel;
use support\think\Cache;
/**
* 奖励配置模型
*
* dice_reward_config 奖励配置
* 奖励列表为全玩家通用,保存时刷新缓存,游戏时优先读缓存。
*
* @property $id ID
* @property $grid_number 色子点数
* @property $ui_text 前端显示文本
* @property $real_ev 真实资金结算
* @property $tier 所属档位
* @property $remark 备注
* @property $create_time 创建时间
* @property $update_time 修改时间
*/
class DiceRewardConfig extends BaseModel
{
/** 缓存键:全玩家通用的奖励配置列表 */
private const CACHE_KEY_LIST = 'dice:reward_config:list';
/** 缓存过期时间(秒),保存时会主动刷新故设较长 */
private const CACHE_TTL = 86400 * 30;
/**
* 数据表主键
* @var string
*/
protected $pk = 'id';
/**
* 数据库表名称
* @var string
*/
protected $table = 'dice_reward_config';
/**
* 获取缓存的奖励列表(无则从库加载并写入缓存)
* @return array<int, array>
*/
public static function getCachedList(): array
{
$list = Cache::get(self::CACHE_KEY_LIST);
if ($list !== null && is_array($list)) {
return $list;
}
self::refreshCache();
$list = Cache::get(self::CACHE_KEY_LIST);
return is_array($list) ? $list : [];
}
/**
* 重新从数据库加载并写入缓存(保存时调用)
*/
public static function refreshCache(): void
{
$list = (new self())->order('id', 'asc')->select()->toArray();
Cache::set(self::CACHE_KEY_LIST, $list, self::CACHE_TTL);
}
/**
* 从缓存取最小 real_ev
*/
public static function getCachedMinRealEv(): float
{
$list = self::getCachedList();
if (empty($list)) {
return 0.0;
}
$vals = array_column($list, 'real_ev');
$min = min($vals);
return (float) $min;
}
/**
* 从缓存按档位取奖励列表
* @return array<int, array>
*/
public static function getCachedByTier(string $tier): array
{
$list = self::getCachedList();
$rows = [];
foreach ($list as $row) {
if (isset($row['tier']) && (string) $row['tier'] === $tier) {
$rows[] = $row;
}
}
return $rows;
}
/** 保存后刷新缓存 */
public static function onAfterInsert($model): void
{
self::refreshCache();
}
/** 更新后刷新缓存 */
public static function onAfterUpdate($model): void
{
self::refreshCache();
}
/** 删除后刷新缓存 */
public static function onAfterDelete($model): void
{
self::refreshCache();
}
/** 色子点数下限 */
public function searchGridNumberMinAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('grid_number', '>=', $value);
}
}
/** 色子点数上限 */
public function searchGridNumberMaxAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('grid_number', '<=', $value);
}
}
/** 前端显示文本模糊 */
public function searchUiTextAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('ui_text', 'like', '%' . $value . '%');
}
}
/** 真实资金结算下限 */
public function searchRealEvMinAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('real_ev', '>=', $value);
}
}
/** 真实资金结算上限 */
public function searchRealEvMaxAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('real_ev', '<=', $value);
}
}
/** 所属档位 */
public function searchTierAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('tier', '=', $value);
}
}
}