diff --git a/server/app/api/service/LotteryService.php b/server/app/api/service/LotteryService.php index fd659a7..d167ba5 100644 --- a/server/app/api/service/LotteryService.php +++ b/server/app/api/service/LotteryService.php @@ -83,6 +83,47 @@ class LotteryService Cache::set($key, json_encode($data), self::EXPIRE); } + /** + * 使玩家彩金池 Redis 快照失效(下次 getOrCreate 从库重建) + */ + public static function invalidatePlayerLotteryCache(int $playerId): void + { + if ($playerId <= 0) { + return; + } + Cache::delete(self::getRedisKey($playerId)); + } + + /** + * 若 Redis 中已有该玩家彩金池快照,则仅更新 player_weights(避免全量重建) + * + * @param array{t1_weight?:int,t2_weight?:int,t3_weight?:int,t4_weight?:int,t5_weight?:int} $weights + */ + public static function patchPlayerWeightsCache(int $playerId, array $weights): void + { + if ($playerId <= 0) { + return; + } + $key = self::getRedisKey($playerId); + $cached = Cache::get($key); + if (!$cached || !is_string($cached)) { + return; + } + $data = json_decode($cached, true); + if (!is_array($data)) { + Cache::delete($key); + return; + } + $data['player_weights'] = [ + 't1_weight' => (int) ($weights['t1_weight'] ?? 0), + 't2_weight' => (int) ($weights['t2_weight'] ?? 0), + 't3_weight' => (int) ($weights['t3_weight'] ?? 0), + 't4_weight' => (int) ($weights['t4_weight'] ?? 0), + 't5_weight' => (int) ($weights['t5_weight'] ?? 0), + ]; + Cache::set($key, json_encode($data), self::EXPIRE); + } + /** 根据奖池配置的 t1_weight..t5_weight 权重随机抽取档位 T1-T5 */ public static function drawTierByWeights(DiceLotteryPoolConfig $config): string { diff --git a/server/app/dice/logic/lottery_pool_config/DiceLotteryPoolConfigLogic.php b/server/app/dice/logic/lottery_pool_config/DiceLotteryPoolConfigLogic.php index a9212b5..2ad4413 100644 --- a/server/app/dice/logic/lottery_pool_config/DiceLotteryPoolConfigLogic.php +++ b/server/app/dice/logic/lottery_pool_config/DiceLotteryPoolConfigLogic.php @@ -6,9 +6,11 @@ // +---------------------------------------------------------------------- namespace app\dice\logic\lottery_pool_config; +use app\api\service\LotteryService; use app\dice\helper\AdminScopeHelper; use app\dice\helper\ConfigScopeEditHelper; use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig; +use app\dice\model\player\DicePlayer; use app\dice\basic\DiceBaseLogic; use plugin\saiadmin\exception\ApiException; use plugin\saiadmin\utils\Helper; @@ -39,7 +41,7 @@ class DiceLotteryPoolConfigLogic extends DiceBaseLogic { $pickedDeptId = AdminScopeHelper::pickRequestDeptId($requestDeptId, $data); $deptId = AdminScopeHelper::resolveConfigDeptId($adminInfo, $pickedDeptId); - return ConfigScopeEditHelper::updateByPkAndDept( + $result = ConfigScopeEditHelper::updateByPkAndDept( $this->model, $id, $deptId, @@ -48,6 +50,58 @@ class DiceLotteryPoolConfigLogic extends DiceBaseLogic $adminInfo, $pickedDeptId ); + if ($result) { + $pool = DiceLotteryPoolConfig::find($id); + if ($pool && $pool->isPlayerDefaultTemplate()) { + $this->syncPlayersBoundToPlayerDefaultPool($pool); + } + } + return $result; + } + + /** + * 修改 playerDefault 后:同步同渠道所有 lottery_config_id 指向该池的玩家 T1–T5 权重,并刷新 Redis 彩金池快照 + * + * @return int 已同步玩家数量 + */ + public function syncPlayersBoundToPlayerDefaultPool(DiceLotteryPoolConfig $pool): int + { + $poolId = (int) ($pool->id ?? 0); + if ($poolId <= 0) { + return 0; + } + $weights = [ + 't1_weight' => (int) ($pool->t1_weight ?? 0), + 't2_weight' => (int) ($pool->t2_weight ?? 0), + 't3_weight' => (int) ($pool->t3_weight ?? 0), + 't4_weight' => (int) ($pool->t4_weight ?? 0), + 't5_weight' => (int) ($pool->t5_weight ?? 0), + ]; + $poolDeptId = AdminScopeHelper::normalizeRecordDeptId($pool->dept_id ?? null); + + $query = DicePlayer::where('lottery_config_id', $poolId); + if (AdminScopeHelper::isTemplateDeptId($poolDeptId)) { + $query->where(function ($q) { + $q->where('dept_id', AdminScopeHelper::DEFAULT_TEMPLATE_DEPT) + ->whereOr('dept_id', null); + }); + } else { + $query->where('dept_id', $poolDeptId); + } + + $playerIds = $query->column('id'); + if ($playerIds === [] || $playerIds === null) { + return 0; + } + $ids = array_map('intval', is_array($playerIds) ? $playerIds : [$playerIds]); + + DicePlayer::whereIn('id', $ids)->update($weights); + + foreach ($ids as $playerId) { + LotteryService::patchPlayerWeightsCache($playerId, $weights); + } + + return count($ids); } /**