You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
208 lines
5.8 KiB
PHP
208 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
/**
|
|
* 检查项目规则控制器
|
|
*/
|
|
class ItemRuleController extends Controller
|
|
{
|
|
/**
|
|
* 获取规则类型定义
|
|
*/
|
|
public function getTypes(Request $request)
|
|
{
|
|
$types = config('item_rule');
|
|
|
|
$formattedTypes = [];
|
|
foreach ($types as $key => $type) {
|
|
$formattedTypes[] = [
|
|
'key' => $key,
|
|
'name' => $type['name'],
|
|
'description' => $type['description'],
|
|
'fields' => $type['fields'],
|
|
];
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'types' => $formattedTypes,
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取已绑定的规则列表
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'target_type' => 'required|in:ITEM,GROUP',
|
|
'target_id' => 'required|integer|min:1',
|
|
]);
|
|
|
|
$targetType = $validated['target_type'];
|
|
$targetId = $validated['target_id'];
|
|
|
|
// 获取规则列表
|
|
$rules = DB::table('exam_item_rule as eir')
|
|
->where('eir.target_type', $targetType)
|
|
->where('eir.target_id', $targetId)
|
|
->where('eir.deleted', 0)
|
|
->orderBy('eir.id')
|
|
->get()
|
|
->toArray();
|
|
|
|
// 获取规则类型定义
|
|
$typeDefinitions = config('item_rule');
|
|
|
|
$formattedRules = [];
|
|
foreach ($rules as $rule) {
|
|
$typeDef = $typeDefinitions[$rule->rule_type] ?? null;
|
|
|
|
$formattedRules[] = [
|
|
'id' => $rule->id,
|
|
'target_type' => $rule->target_type,
|
|
'target_id' => $rule->target_id,
|
|
'rule_type' => $rule->rule_type,
|
|
'rule_name' => $typeDef['name'] ?? $rule->rule_type,
|
|
'rule_value' => json_decode($rule->rule_value, true),
|
|
'status' => $rule->status,
|
|
'created_at' => $rule->created_at,
|
|
];
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'rules' => $formattedRules,
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 新增/更新规则绑定
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'id' => 'nullable|integer|min:1',
|
|
'target_type' => 'required|in:ITEM,GROUP',
|
|
'target_id' => 'required|integer|min:1',
|
|
'rule_type' => 'required|string',
|
|
'rule_value' => 'required|array',
|
|
'status' => 'nullable|integer|in:0,1',
|
|
]);
|
|
|
|
$typeDefinitions = config('item_rule');
|
|
|
|
// 验证规则类型是否存在
|
|
if (!isset($typeDefinitions[$validated['rule_type']])) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '规则类型不存在',
|
|
], 400);
|
|
}
|
|
|
|
// 验证规则值字段
|
|
$fields = $typeDefinitions[$validated['rule_type']]['fields'] ?? [];
|
|
foreach ($fields as $field) {
|
|
$key = $field['key'];
|
|
$validation = $field['validation'] ?? null;
|
|
|
|
if ($validation && !isset($validated['rule_value'][$key])) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => "规则值缺少必填字段:{$field['label']}",
|
|
], 400);
|
|
}
|
|
}
|
|
|
|
$data = [
|
|
'target_type' => $validated['target_type'],
|
|
'target_id' => $validated['target_id'],
|
|
'rule_type' => $validated['rule_type'],
|
|
'rule_value' => json_encode($validated['rule_value']),
|
|
'status' => $validated['status'] ?? 1,
|
|
'updated_at' => now(),
|
|
];
|
|
|
|
// 判断是新增还是更新
|
|
if (isset($validated['id'])) {
|
|
// 更新
|
|
$id = $validated['id'];
|
|
$exists = DB::table('exam_item_rule')
|
|
->where('id', $id)
|
|
->where('deleted', 0)
|
|
->exists();
|
|
|
|
if (!$exists) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '规则不存在',
|
|
], 404);
|
|
}
|
|
|
|
DB::table('exam_item_rule')->where('id', $id)->update($data);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'id' => $id,
|
|
],
|
|
]);
|
|
} else {
|
|
// 新增
|
|
$data['deleted'] = 0;
|
|
$data['created_at'] = now();
|
|
$ruleId = DB::table('exam_item_rule')->insertGetId($data);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'id' => $ruleId,
|
|
],
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除规则绑定
|
|
*/
|
|
public function destroy(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'id' => 'required|integer|min:1',
|
|
]);
|
|
|
|
$id = $validated['id'];
|
|
|
|
$rule = DB::table('exam_item_rule')->where('id', $id)->first();
|
|
|
|
if (!$rule) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '规则不存在',
|
|
], 404);
|
|
}
|
|
|
|
// 逻辑删除
|
|
DB::table('exam_item_rule')->where('id', $id)->update([
|
|
'deleted' => 1,
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => null,
|
|
]);
|
|
}
|
|
|
|
}
|