model = new DiceRewardConfig(); } /** * 新增:weight 限制 1-10000;保存后刷新缓存 */ public function add(array $data): mixed { $data = $this->normalizeWeight($data); $result = parent::add($data); DiceRewardConfig::refreshCache(); return $result; } /** * 修改:weight 限制 1-10000;保存后刷新缓存 */ public function edit($id, array $data): mixed { $data = $this->normalizeWeight($data); $result = parent::edit($id, $data); DiceRewardConfig::refreshCache(); return $result; } /** * 删除后刷新缓存 */ public function destroy($ids): bool { $result = parent::destroy($ids); if ($result) { DiceRewardConfig::refreshCache(); } return $result; } /** * weight 限制 1-10000 */ private function normalizeWeight(array $data): array { $w = isset($data['weight']) ? (int) $data['weight'] : self::WEIGHT_MIN; $data['weight'] = max(self::WEIGHT_MIN, min(self::WEIGHT_MAX, $w)); return $data; } /** * 按档位分组返回奖励配置列表(用于 T1-T5、BIGWIN 权重配比) * @return array 键为 T1|T2|T3|T4|T5|BIGWIN,值为该档位下的配置行数组 */ public function getListGroupedByTier(): array { $tiers = ['T1', 'T2', 'T3', 'T4', 'T5', 'BIGWIN']; $list = $this->model->whereIn('tier', $tiers)->order('tier')->order('id')->select()->toArray(); $grouped = []; foreach ($tiers as $t) { $grouped[$t] = []; } foreach ($list as $row) { $tier = isset($row['tier']) ? (string) $row['tier'] : ''; if ($tier !== '' && isset($grouped[$tier])) { $grouped[$tier][] = $row; } } return $grouped; } /** * 批量更新权重:单条 weight 1-10000,各档位权重和不限制 * @param array $items 元素为 [ id => 配置ID, weight => 1-10000 ] * @throws ApiException 当单条 weight 非法时 */ public function batchUpdateWeights(array $items): void { if (empty($items)) { return; } $items = array_values($items); $ids = []; $weightById = []; foreach ($items as $item) { if (!is_array($item)) { continue; } $id = isset($item['id']) ? (int) $item['id'] : 0; $w = isset($item['weight']) ? (int) $item['weight'] : self::WEIGHT_MIN; if ($id < 0) { throw new ApiException('存在无效的配置ID'); } if ($w < self::WEIGHT_MIN || $w > self::WEIGHT_MAX) { throw new ApiException('权重必须在 ' . self::WEIGHT_MIN . '-' . self::WEIGHT_MAX . ' 之间'); } $ids[] = $id; $weightById[$id] = $w; } $list = $this->model->whereIn('id', array_unique($ids))->field('id,tier,grid_number')->select()->toArray(); $idToTier = []; foreach ($list as $r) { $id = isset($r['id']) ? (int) $r['id'] : 0; $idToTier[$id] = isset($r['tier']) ? (string) $r['tier'] : ''; } foreach ($weightById as $id => $w) { $tier = $idToTier[$id] ?? ''; if ($tier === '') { throw new ApiException('配置ID ' . $id . ' 不存在或档位为空'); } DiceRewardConfig::where('id', $id)->update(['weight' => $w]); } DiceRewardConfig::refreshCache(); } }