1.将部门修改为渠道,并且所有dice_表关联渠道表

2.将所有配置表,记录表设置关联渠道
3.优化后台页面设置
This commit is contained in:
2026-05-19 09:49:02 +08:00
parent 085454fb78
commit dd264b1e97
143 changed files with 4741 additions and 1254 deletions

View File

@@ -4,7 +4,9 @@
// +----------------------------------------------------------------------
namespace app\dice\model\reward;
use plugin\saiadmin\basic\think\BaseModel;
use app\dice\helper\AdminScopeHelper;
use app\dice\helper\ConfigScopeEditHelper;
use app\dice\model\DiceModel;
use support\think\Cache;
/**
@@ -25,42 +27,70 @@ use support\think\Cache;
* @property $remark 备注(来自config)
* @property $type 奖励类型(来自config)
*/
class DiceReward extends BaseModel
class DiceReward extends DiceModel
{
/** 方向:顺时针 */
public const DIRECTION_CLOCKWISE = 0;
/** 方向:逆时针 */
public const DIRECTION_COUNTERCLOCKWISE = 1;
/** 缓存键:奖励对照实例 */
/** 缓存键前缀:奖励对照实例(按渠道分键) */
private const CACHE_KEY_INSTANCE = 'dice:reward:instance';
private const CACHE_TTL = 86400 * 30;
private static ?array $instance = null;
private static ?int $requestDeptId = null;
protected $table = 'dice_reward';
/** 主键 id 自增,唯一约束 (direction, grid_number) */
protected $pk = 'id';
private static function cacheKeyForDept(int $deptId): string
{
return self::CACHE_KEY_INSTANCE . ':' . $deptId;
}
private static function resolveDeptId(?int $deptId): int
{
if ($deptId !== null) {
return AdminScopeHelper::normalizeRecordDeptId($deptId);
}
if (self::$requestDeptId !== null) {
return self::$requestDeptId;
}
return AdminScopeHelper::DEFAULT_TEMPLATE_DEPT;
}
/**
* 请求级设置当前渠道(一键测试 worker 内调用 simulateOnePlay 前设置)
*/
public static function setRequestDeptId(?int $deptId): void
{
self::$requestDeptId = $deptId !== null
? AdminScopeHelper::normalizeRecordDeptId($deptId)
: null;
}
/**
* 获取奖励对照实例(按档位+方向索引,用于抽奖与权重配比)
* 优先从共享缓存读取,保证多进程(如一键测试 worker与数据库一致
* @return array{list: array, by_tier_direction: array}
*/
public static function getCachedInstance(): array
public static function getCachedInstance(?int $deptId = null): array
{
$instance = Cache::get(self::CACHE_KEY_INSTANCE);
$deptId = self::resolveDeptId($deptId);
$cacheKey = self::cacheKeyForDept($deptId);
$instance = Cache::get($cacheKey);
if ($instance !== null && is_array($instance)) {
self::$instance = $instance;
return $instance;
}
if (self::$instance !== null) {
if (self::$instance !== null && self::$requestDeptId === $deptId) {
return self::$instance;
}
self::refreshCache();
$instance = Cache::get(self::CACHE_KEY_INSTANCE);
self::refreshCache($deptId);
$instance = Cache::get($cacheKey);
self::$instance = is_array($instance) ? $instance : self::buildEmptyInstance();
return self::$instance;
}
@@ -69,9 +99,9 @@ class DiceReward extends BaseModel
* 按档位+方向取权重列表(用于抽奖:该档位该方向下 end_index => weight
* @return array<int, int> end_index => weight
*/
public static function getCachedByTierAndDirection(string $tier, int $direction): array
public static function getCachedByTierAndDirection(string $tier, int $direction, ?int $deptId = null): array
{
$inst = self::getCachedInstance();
$inst = self::getCachedInstance($deptId);
$byTierDirection = $inst['by_tier_direction'] ?? [];
$list = $byTierDirection[$tier][$direction] ?? [];
$result = [];
@@ -84,11 +114,14 @@ class DiceReward extends BaseModel
}
/**
* 重新从数据库加载并写入缓存;修改/新增/删除后需调用以实例化
* 按渠道从数据库加载并写入缓存
*/
public static function refreshCache(): void
public static function refreshCache(?int $deptId = null): void
{
$list = (new self())->order('tier')->order('direction')->order('end_index')->select()->toArray();
$deptId = self::resolveDeptId($deptId);
$query = (new self())->order('tier')->order('direction')->order('end_index');
ConfigScopeEditHelper::applyDeptIdWhere($query, $deptId);
$list = $query->select()->toArray();
$byTierDirection = [];
foreach ($list as $row) {
$tier = isset($row['tier']) ? (string) $row['tier'] : '';
@@ -103,11 +136,12 @@ class DiceReward extends BaseModel
$byTierDirection[$tier][$direction][] = $row;
}
}
self::$instance = [
$instance = [
'list' => $list,
'by_tier_direction' => $byTierDirection,
];
Cache::set(self::CACHE_KEY_INSTANCE, self::$instance, self::CACHE_TTL);
self::$instance = $instance;
Cache::set(self::cacheKeyForDept($deptId), $instance, self::CACHE_TTL);
}
private static function buildEmptyInstance(): array
@@ -121,20 +155,29 @@ class DiceReward extends BaseModel
public static function clearRequestInstance(): void
{
self::$instance = null;
self::$requestDeptId = 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);
}
}