1.将部门修改为渠道,并且所有dice_表关联渠道表
2.将所有配置表,记录表设置关联渠道 3.优化后台页面设置
This commit is contained in:
@@ -6,8 +6,10 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\model\reward_config;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\helper\ConfigScopeEditHelper;
|
||||
use app\dice\model\reward\DiceReward;
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
use app\dice\model\DiceModel;
|
||||
use support\think\Cache;
|
||||
|
||||
/**
|
||||
@@ -27,7 +29,7 @@ use support\think\Cache;
|
||||
* @property $create_time 创建时间
|
||||
* @property $update_time 修改时间
|
||||
*/
|
||||
class DiceRewardConfig extends BaseModel
|
||||
class DiceRewardConfig extends DiceModel
|
||||
{
|
||||
/** 缓存键:彩金池奖励列表实例 */
|
||||
private const CACHE_KEY_INSTANCE = 'dice:reward_config:instance';
|
||||
@@ -45,31 +47,35 @@ class DiceRewardConfig extends BaseModel
|
||||
* 优先从共享缓存读取,保证多进程(如一键测试 worker)能拿到最新配置,与数据库一致
|
||||
* @return array{list: array, by_tier: array, by_tier_grid: array, min_real_ev: float}
|
||||
*/
|
||||
public static function getCachedInstance(): array
|
||||
public static function getCachedInstance(?int $deptId = null): array
|
||||
{
|
||||
$instance = Cache::get(self::CACHE_KEY_INSTANCE);
|
||||
if ($deptId === null) {
|
||||
$deptId = AdminScopeHelper::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
$cacheKey = self::cacheKeyForDept($deptId);
|
||||
$instance = Cache::get($cacheKey);
|
||||
if ($instance !== null && is_array($instance)) {
|
||||
self::$instance = $instance;
|
||||
return $instance;
|
||||
}
|
||||
if (self::$instance !== null) {
|
||||
return self::$instance;
|
||||
}
|
||||
self::refreshCache();
|
||||
$instance = Cache::get(self::CACHE_KEY_INSTANCE);
|
||||
self::$instance = is_array($instance) ? $instance : self::buildEmptyInstance();
|
||||
return self::$instance;
|
||||
self::refreshCache($deptId);
|
||||
$instance = Cache::get($cacheKey);
|
||||
return is_array($instance) ? $instance : self::buildEmptyInstance();
|
||||
}
|
||||
|
||||
public static function getCachedList(): array
|
||||
private static function cacheKeyForDept(int $deptId): string
|
||||
{
|
||||
$inst = self::getCachedInstance();
|
||||
return self::CACHE_KEY_INSTANCE . ':' . $deptId;
|
||||
}
|
||||
|
||||
public static function getCachedList(?int $deptId = null): array
|
||||
{
|
||||
$inst = self::getCachedInstance($deptId);
|
||||
return $inst['list'] ?? [];
|
||||
}
|
||||
|
||||
public static function getCachedById(int $id): ?array
|
||||
public static function getCachedById(int $id, ?int $deptId = null): ?array
|
||||
{
|
||||
$list = self::getCachedList();
|
||||
$list = self::getCachedList($deptId);
|
||||
foreach ($list as $row) {
|
||||
if (isset($row['id']) && (int) $row['id'] === $id) {
|
||||
return $row;
|
||||
@@ -79,11 +85,16 @@ class DiceRewardConfig extends BaseModel
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新从数据库加载并写入缓存(按档位+权重抽 grid_number,含 by_tier、by_tier_grid)
|
||||
* 按渠道从数据库加载并写入缓存(避免多渠道配置混在同一缓存键)
|
||||
*/
|
||||
public static function refreshCache(): void
|
||||
public static function refreshCache(?int $deptId = null): void
|
||||
{
|
||||
$list = (new self())->order('id', 'asc')->select()->toArray();
|
||||
if ($deptId === null) {
|
||||
$deptId = AdminScopeHelper::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
$query = (new self())->order('id', 'asc');
|
||||
ConfigScopeEditHelper::applyDeptIdWhere($query, $deptId);
|
||||
$list = $query->select()->toArray();
|
||||
$byTier = [];
|
||||
$byTierGrid = [];
|
||||
foreach ($list as $row) {
|
||||
@@ -103,13 +114,16 @@ class DiceRewardConfig extends BaseModel
|
||||
}
|
||||
}
|
||||
$minRealEv = empty($list) ? 0.0 : (float) min(array_column($list, 'real_ev'));
|
||||
self::$instance = [
|
||||
$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);
|
||||
Cache::set(self::cacheKeyForDept($deptId), $instance, self::CACHE_TTL);
|
||||
if ($deptId === AdminScopeHelper::DEFAULT_TEMPLATE_DEPT) {
|
||||
self::$instance = $instance;
|
||||
}
|
||||
}
|
||||
|
||||
private static function buildEmptyInstance(): array
|
||||
@@ -125,9 +139,9 @@ class DiceRewardConfig extends BaseModel
|
||||
/**
|
||||
* 按档位+色子点数取一条(用于 BIGWIN)
|
||||
*/
|
||||
public static function getCachedByTierAndGridNumber(string $tier, int $gridNumber): ?array
|
||||
public static function getCachedByTierAndGridNumber(string $tier, int $gridNumber, ?int $deptId = null): ?array
|
||||
{
|
||||
$inst = self::getCachedInstance();
|
||||
$inst = self::getCachedInstance($deptId);
|
||||
$byTierGrid = $inst['by_tier_grid'] ?? [];
|
||||
$tierData = $byTierGrid[$tier] ?? [];
|
||||
$row = $tierData[$gridNumber] ?? null;
|
||||
@@ -143,9 +157,9 @@ class DiceRewardConfig extends BaseModel
|
||||
/**
|
||||
* 从缓存按档位取奖励列表(不含权重,仅配置)
|
||||
*/
|
||||
public static function getCachedByTier(string $tier): array
|
||||
public static function getCachedByTier(string $tier, ?int $deptId = null): array
|
||||
{
|
||||
$inst = self::getCachedInstance();
|
||||
$inst = self::getCachedInstance($deptId);
|
||||
$byTier = $inst['by_tier'] ?? [];
|
||||
return $byTier[$tier] ?? [];
|
||||
}
|
||||
@@ -155,10 +169,10 @@ class DiceRewardConfig extends BaseModel
|
||||
* @param int $direction 0=顺时针, 1=逆时针
|
||||
* @return array 每行含 id, grid_number, real_ev, tier, weight 等
|
||||
*/
|
||||
public static function getCachedByTierForDirection(string $tier, int $direction): array
|
||||
public static function getCachedByTierForDirection(string $tier, int $direction, ?int $deptId = null): array
|
||||
{
|
||||
$list = self::getCachedByTier($tier);
|
||||
$weightMap = DiceReward::getCachedByTierAndDirection($tier, $direction);
|
||||
$list = self::getCachedByTier($tier, $deptId);
|
||||
$weightMap = DiceReward::getCachedByTierAndDirection($tier, $direction, $deptId);
|
||||
foreach ($list as $i => $row) {
|
||||
$id = isset($row['id']) ? (int) $row['id'] : 0;
|
||||
$list[$i]['weight'] = $weightMap[$id] ?? 1;
|
||||
@@ -171,19 +185,27 @@ class DiceRewardConfig extends BaseModel
|
||||
self::$instance = null;
|
||||
}
|
||||
|
||||
private static function refreshCacheForModel($model): void
|
||||
{
|
||||
$deptId = AdminScopeHelper::normalizeRecordDeptId(
|
||||
is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null)
|
||||
);
|
||||
self::refreshCache($deptId);
|
||||
}
|
||||
|
||||
public static function onAfterInsert($model): void
|
||||
{
|
||||
self::refreshCache();
|
||||
self::refreshCacheForModel($model);
|
||||
}
|
||||
|
||||
public static function onAfterUpdate($model): void
|
||||
{
|
||||
self::refreshCache();
|
||||
self::refreshCacheForModel($model);
|
||||
}
|
||||
|
||||
public static function onAfterDelete($model): void
|
||||
{
|
||||
self::refreshCache();
|
||||
self::refreshCacheForModel($model);
|
||||
}
|
||||
|
||||
public function searchGridNumberMinAttr($query, $value)
|
||||
|
||||
Reference in New Issue
Block a user