217 lines
6.4 KiB
PHP
217 lines
6.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 奖励配置
|
||
* 按档位 T1-T5 直接权重抽取 grid_number,weight 1-10000;起始索引 s_start_index / n_start_index
|
||
*
|
||
* @property $id ID
|
||
* @property $grid_number 色子点数
|
||
* @property $ui_text 前端显示文本
|
||
* @property $real_ev 真实资金结算
|
||
* @property $tier 所属档位
|
||
* @property $weight 权重 1-10000,档位内按权重比抽取
|
||
* @property $n_start_index 逆时针起始索引
|
||
* @property $s_start_index 顺时针起始索引
|
||
* @property $remark 备注
|
||
* @property $create_time 创建时间
|
||
* @property $update_time 修改时间
|
||
*/
|
||
class DiceRewardConfig extends BaseModel
|
||
{
|
||
/** 缓存键:彩金池奖励列表实例 */
|
||
private const CACHE_KEY_INSTANCE = 'dice:reward_config:instance';
|
||
|
||
private const CACHE_TTL = 86400 * 30;
|
||
|
||
private static ?array $instance = null;
|
||
|
||
protected $pk = 'id';
|
||
|
||
protected $table = 'dice_reward_config';
|
||
|
||
/**
|
||
* 获取彩金池实例(含 list / by_tier / by_tier_grid),无则从库加载并写入缓存
|
||
* @return array{list: array, by_tier: array, by_tier_grid: array, min_real_ev: float}
|
||
*/
|
||
public static function getCachedInstance(): array
|
||
{
|
||
if (self::$instance !== null) {
|
||
return self::$instance;
|
||
}
|
||
$instance = Cache::get(self::CACHE_KEY_INSTANCE);
|
||
if ($instance !== null && is_array($instance)) {
|
||
self::$instance = $instance;
|
||
return $instance;
|
||
}
|
||
self::refreshCache();
|
||
$instance = Cache::get(self::CACHE_KEY_INSTANCE);
|
||
self::$instance = is_array($instance) ? $instance : self::buildEmptyInstance();
|
||
return self::$instance;
|
||
}
|
||
|
||
public static function getCachedList(): array
|
||
{
|
||
$inst = self::getCachedInstance();
|
||
return $inst['list'] ?? [];
|
||
}
|
||
|
||
/**
|
||
* 重新从数据库加载并写入缓存(按档位+权重抽 grid_number,含 by_tier、by_tier_grid)
|
||
*/
|
||
public static function refreshCache(): void
|
||
{
|
||
$list = (new self())->order('id', 'asc')->select()->toArray();
|
||
$byTier = [];
|
||
$byTierGrid = [];
|
||
foreach ($list as $row) {
|
||
$tier = isset($row['tier']) ? (string) $row['tier'] : '';
|
||
if ($tier !== '') {
|
||
if (!isset($byTier[$tier])) {
|
||
$byTier[$tier] = [];
|
||
}
|
||
$byTier[$tier][] = $row;
|
||
$gridNum = isset($row['grid_number']) ? (int) $row['grid_number'] : 0;
|
||
if (!isset($byTierGrid[$tier])) {
|
||
$byTierGrid[$tier] = [];
|
||
}
|
||
if (!isset($byTierGrid[$tier][$gridNum])) {
|
||
$byTierGrid[$tier][$gridNum] = $row;
|
||
}
|
||
}
|
||
}
|
||
$minRealEv = empty($list) ? 0.0 : (float) min(array_column($list, 'real_ev'));
|
||
self::$instance = [
|
||
'list' => $list,
|
||
'by_tier' => $byTier,
|
||
'by_tier_grid' => $byTierGrid,
|
||
'min_real_ev' => $minRealEv,
|
||
];
|
||
Cache::set(self::CACHE_KEY_INSTANCE, self::$instance, self::CACHE_TTL);
|
||
}
|
||
|
||
private static function buildEmptyInstance(): array
|
||
{
|
||
return [
|
||
'list' => [],
|
||
'by_tier' => [],
|
||
'by_tier_grid' => [],
|
||
'min_real_ev' => 0.0,
|
||
];
|
||
}
|
||
|
||
/**
|
||
* 按档位+色子点数取一条(用于 BIGWIN)
|
||
*/
|
||
public static function getCachedByTierAndGridNumber(string $tier, int $gridNumber): ?array
|
||
{
|
||
$inst = self::getCachedInstance();
|
||
$byTierGrid = $inst['by_tier_grid'] ?? [];
|
||
$tierData = $byTierGrid[$tier] ?? [];
|
||
$row = $tierData[$gridNumber] ?? null;
|
||
return is_array($row) ? $row : null;
|
||
}
|
||
|
||
public static function getCachedMinRealEv(): float
|
||
{
|
||
$inst = self::getCachedInstance();
|
||
return (float) ($inst['min_real_ev'] ?? 0.0);
|
||
}
|
||
|
||
/**
|
||
* 从缓存按档位取奖励列表(含 weight,用于按权重抽 grid_number)
|
||
*/
|
||
public static function getCachedByTier(string $tier): array
|
||
{
|
||
$inst = self::getCachedInstance();
|
||
$byTier = $inst['by_tier'] ?? [];
|
||
return $byTier[$tier] ?? [];
|
||
}
|
||
|
||
public static function clearRequestInstance(): void
|
||
{
|
||
self::$instance = null;
|
||
}
|
||
|
||
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);
|
||
}
|
||
}
|
||
|
||
public function searchWeightMinAttr($query, $value)
|
||
{
|
||
if ($value !== '' && $value !== null) {
|
||
$query->where('weight', '>=', $value);
|
||
}
|
||
}
|
||
|
||
public function searchWeightMaxAttr($query, $value)
|
||
{
|
||
if ($value !== '' && $value !== null) {
|
||
$query->where('weight', '<=', $value);
|
||
}
|
||
}
|
||
}
|