143 lines
4.8 KiB
PHP
143 lines
4.8 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | saiadmin [ saiadmin快速开发框架 ]
|
||
// +----------------------------------------------------------------------
|
||
// | Author: your name
|
||
// +----------------------------------------------------------------------
|
||
namespace app\dice\logic\reward_config;
|
||
|
||
use plugin\saiadmin\basic\think\BaseLogic;
|
||
use plugin\saiadmin\exception\ApiException;
|
||
use plugin\saiadmin\utils\Helper;
|
||
use app\dice\model\reward_config\DiceRewardConfig;
|
||
use support\Log;
|
||
|
||
/**
|
||
* 奖励配置逻辑层
|
||
* weight 仅 tier=BIGWIN 时可设定,保存时非 BIGWIN 强制 weight=0
|
||
*/
|
||
class DiceRewardConfigLogic extends BaseLogic
|
||
{
|
||
/**
|
||
* 构造函数
|
||
*/
|
||
public function __construct()
|
||
{
|
||
$this->model = new DiceRewardConfig();
|
||
}
|
||
|
||
/**
|
||
* 新增前:非 BIGWIN 时强制 weight=0
|
||
*/
|
||
public function add(array $data): mixed
|
||
{
|
||
$data = $this->normalizeWeightByTier($data);
|
||
return parent::add($data);
|
||
}
|
||
|
||
/**
|
||
* 修改前:非 BIGWIN 时强制 weight=0
|
||
*/
|
||
public function edit($id, array $data): mixed
|
||
{
|
||
$data = $this->normalizeWeightByTier($data);
|
||
return parent::edit($id, $data);
|
||
}
|
||
|
||
/**
|
||
* 仅 tier=BIGWIN 时保留 weight(且限制 0-100),否则强制为 0
|
||
*/
|
||
private function normalizeWeightByTier(array $data): array
|
||
{
|
||
$tier = isset($data['tier']) ? (string) $data['tier'] : '';
|
||
if ($tier !== 'BIGWIN') {
|
||
$data['weight'] = 0;
|
||
return $data;
|
||
}
|
||
$w = isset($data['weight']) ? (float) $data['weight'] : 0;
|
||
$data['weight'] = max(0, min(100, $w));
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 按档位分组返回奖励配置列表(用于 T1-T5、BIGWIN 权重配比)
|
||
* @return array<string, 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;
|
||
}
|
||
|
||
/**
|
||
* 批量更新权重:T1-T5 同档位权重之和必须等于 100;BIGWIN 为豹子权重单独设定,不校验合计
|
||
* @param array<int, array{id: int, weight: float}> $items 元素为 [ id => 配置ID, weight => 0-100 ]
|
||
* @throws ApiException 当单条 weight 非法或 T1-T5 某档位权重和≠100 时
|
||
*/
|
||
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']) ? (float) $item['weight'] : 0;
|
||
if ($id < 0) {
|
||
throw new ApiException('存在无效的配置ID');
|
||
}
|
||
if ($w < 0 || $w > 100) {
|
||
throw new ApiException('权重必须在 0-100 之间');
|
||
}
|
||
$ids[] = $id;
|
||
$w = max(0, min(100, $w));
|
||
$weightById[$id] = isset($weightById[$id]) ? min(100, $weightById[$id] + $w) : $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'] : '';
|
||
}
|
||
$sumByTier = [];
|
||
foreach ($weightById as $id => $w) {
|
||
$tier = $idToTier[$id] ?? '';
|
||
if ($tier === '') {
|
||
throw new ApiException('配置ID ' . $id . ' 不存在或档位为空');
|
||
}
|
||
if ($tier === 'BIGWIN') {
|
||
continue;
|
||
}
|
||
if (!isset($sumByTier[$tier])) {
|
||
$sumByTier[$tier] = 0;
|
||
}
|
||
$sumByTier[$tier] += $w;
|
||
}
|
||
foreach ($sumByTier as $tier => $sum) {
|
||
if (abs($sum - 100)) {
|
||
throw new ApiException('档位 ' . $tier . ' 的权重之和必须等于 100%,当前为 ' . round($sum, 2));
|
||
}
|
||
}
|
||
foreach ($weightById as $id => $w) {
|
||
DiceRewardConfig::where('id', $id)->update(['weight' => $w]);
|
||
}
|
||
DiceRewardConfig::refreshCache();
|
||
}
|
||
}
|