新增全局 sysparam 配置 reward_recipient_daily_max: - 控制每位用户单日内从所有职务持有者处累计接收奖励的最高次数 - 0 = 不限制 后端变更: - PositionController::saveRewardConfig() 保存配置 - POST admin/positions/reward-config 路由 - AdminCommandController::reward() 新增第④层校验: 全局次数上限(优先级低于职务级别的 recipient_daily_limit) 视图变更: - 职务管理页顶部加橙色配置卡片(行内表单,即改即存) - 显示当前全局配置值
149 lines
5.3 KiB
PHP
149 lines
5.3 KiB
PHP
<?php
|
||
|
||
/**
|
||
* 文件功能:后台职务管理控制器
|
||
* 提供职务的 CRUD 功能,包含任命权限白名单(多选 position_appoint_limits)的同步
|
||
* 职务属于部门,包含等级、图标、人数上限、奖励上限等配置
|
||
*
|
||
* @author ChatRoom Laravel
|
||
*
|
||
* @version 1.0.0
|
||
*/
|
||
|
||
namespace App\Http\Controllers\Admin;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Models\Department;
|
||
use App\Models\Position;
|
||
use App\Models\Sysparam;
|
||
use Illuminate\Http\RedirectResponse;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\View\View;
|
||
|
||
class PositionController extends Controller
|
||
{
|
||
/**
|
||
* 职务列表页
|
||
*/
|
||
public function index(): View
|
||
{
|
||
// 按部门分组展示
|
||
$departments = Department::with([
|
||
'positions' => fn ($q) => $q->withCount(['activeUserPositions'])->ordered(),
|
||
])->ordered()->get();
|
||
|
||
// 全部职务(供任命白名单多选框使用)
|
||
$allPositions = Position::with('department')->orderByDesc('rank')->get();
|
||
|
||
// 全局奖励接收次数上限(0 = 不限)
|
||
$globalRecipientDailyMax = (int) Sysparam::getValue('reward_recipient_daily_max', '0');
|
||
|
||
return view('admin.positions.index', compact('departments', 'allPositions', 'globalRecipientDailyMax'));
|
||
}
|
||
|
||
/**
|
||
* 创建职务(同时同步任命白名单)
|
||
*/
|
||
public function store(Request $request): RedirectResponse
|
||
{
|
||
$data = $request->validate([
|
||
'department_id' => 'required|exists:departments,id',
|
||
'name' => 'required|string|max:50',
|
||
'icon' => 'nullable|string|max:10',
|
||
'rank' => 'required|integer|min:0|max:99',
|
||
'level' => 'required|integer|min:1|max:100',
|
||
'max_persons' => 'nullable|integer|min:1',
|
||
'max_reward' => 'nullable|integer|min:0',
|
||
'daily_reward_limit' => 'nullable|integer|min:0',
|
||
'recipient_daily_limit' => 'nullable|integer|min:0',
|
||
'sort_order' => 'required|integer|min:0',
|
||
'appointable_ids' => 'nullable|array',
|
||
'appointable_ids.*' => 'exists:positions,id',
|
||
]);
|
||
|
||
$appointableIds = $data['appointable_ids'] ?? [];
|
||
unset($data['appointable_ids']);
|
||
|
||
$position = Position::create($data);
|
||
|
||
// 同步任命白名单(有选则写,没选则清空=无任命权)
|
||
$position->appointablePositions()->sync($appointableIds);
|
||
|
||
return redirect()->route('admin.positions.index')->with('success', "职务【{$data['name']}】创建成功!");
|
||
}
|
||
|
||
/**
|
||
* 保存全局奖励金币接收次数上限
|
||
*
|
||
* 控制每位用户单日内可从所有职务持有者处累计接收奖励的最高次数。
|
||
* 0 表示不限制,保存到 sysparam 表中(key: reward_recipient_daily_max)。
|
||
*/
|
||
public function saveRewardConfig(Request $request): RedirectResponse
|
||
{
|
||
$request->validate([
|
||
'reward_recipient_daily_max' => 'required|integer|min:0|max:9999',
|
||
]);
|
||
|
||
$value = (string) $request->integer('reward_recipient_daily_max');
|
||
|
||
Sysparam::updateOrCreate(
|
||
['alias' => 'reward_recipient_daily_max'],
|
||
[
|
||
'body' => $value,
|
||
'guidetxt' => '用户单日最多接收奖励金币次数(0=不限,统计所有职务持有者的发放总次数)',
|
||
]
|
||
);
|
||
|
||
Sysparam::clearCache('reward_recipient_daily_max');
|
||
|
||
$label = $value === '0' ? '不限' : "{$value} 次";
|
||
|
||
return redirect()->route('admin.positions.index')
|
||
->with('success', "全局接收次数上限已更新为:{$label}");
|
||
}
|
||
|
||
/**
|
||
* 更新职务(含任命白名单同步)
|
||
*/
|
||
public function update(Request $request, Position $position): RedirectResponse
|
||
{
|
||
$data = $request->validate([
|
||
'department_id' => 'required|exists:departments,id',
|
||
'name' => 'required|string|max:50',
|
||
'icon' => 'nullable|string|max:10',
|
||
'rank' => 'required|integer|min:0|max:99',
|
||
'level' => 'required|integer|min:1|max:100',
|
||
'max_persons' => 'nullable|integer|min:1',
|
||
'max_reward' => 'nullable|integer|min:0',
|
||
'daily_reward_limit' => 'nullable|integer|min:0',
|
||
'recipient_daily_limit' => 'nullable|integer|min:0',
|
||
'sort_order' => 'required|integer|min:0',
|
||
'appointable_ids' => 'nullable|array',
|
||
'appointable_ids.*' => 'exists:positions,id',
|
||
]);
|
||
|
||
$appointableIds = $data['appointable_ids'] ?? [];
|
||
unset($data['appointable_ids']);
|
||
|
||
$position->update($data);
|
||
$position->appointablePositions()->sync($appointableIds);
|
||
|
||
return redirect()->route('admin.positions.index')->with('success', "职务【{$data['name']}】更新成功!");
|
||
}
|
||
|
||
/**
|
||
* 删除职务(有在职人员时拒绝)
|
||
*/
|
||
public function destroy(Position $position): RedirectResponse
|
||
{
|
||
if ($position->isFull() || $position->activeUserPositions()->exists()) {
|
||
return redirect()->route('admin.positions.index')
|
||
->with('error', "职务【{$position->name}】尚有在职人员,请先撤销后再删除。");
|
||
}
|
||
|
||
$position->delete();
|
||
|
||
return redirect()->route('admin.positions.index')->with('success', "职务【{$position->name}】已删除!");
|
||
}
|
||
}
|