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.
374 lines
12 KiB
PHP
374 lines
12 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 ExclusionController extends Controller
|
|
{
|
|
/**
|
|
* 获取互斥类型列表
|
|
*/
|
|
public function getExclusionTypes()
|
|
{
|
|
$types = config('exclusion.types');
|
|
|
|
$typeList = [];
|
|
foreach ($types as $key => $config) {
|
|
$typeList[] = [
|
|
'key' => $key,
|
|
'name' => $config['name'],
|
|
'description' => $config['description'] ?? '',
|
|
'require_param' => $config['require_param'] ?? false,
|
|
'fields' => $config['fields'] ?? [],
|
|
];
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'types' => $typeList
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 查询互斥配置列表
|
|
*/
|
|
public function list(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'source_item_id' => 'required|integer|min:1'
|
|
]);
|
|
|
|
$sourceItemId = $data['source_item_id'];
|
|
|
|
$exclusions = DB::table('exam_item_exclusion as eie')
|
|
->where('eie.source_item_id', $sourceItemId)
|
|
->where('eie.deleted', 0)
|
|
->leftJoin('exam_item as ei', function ($join) {
|
|
$join->on('eie.target_id', '=', 'ei.id')
|
|
->where('eie.target_type', 'ITEM');
|
|
})
|
|
->leftJoin('exam_group as eg', function ($join) {
|
|
$join->on('eie.target_id', '=', 'eg.id')
|
|
->where('eie.target_type', 'GROUP');
|
|
})
|
|
->select(
|
|
'eie.id',
|
|
'eie.source_item_id',
|
|
'eie.target_type',
|
|
'eie.target_id',
|
|
DB::raw('IF(eie.target_type = "ITEM", ei.name, eg.name) as target_name'),
|
|
'eie.exclusion_type',
|
|
'eie.exclusion_value',
|
|
'eie.remark',
|
|
'eie.status'
|
|
)
|
|
->orderBy('eie.id')
|
|
->get();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'list' => $exclusions
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 新增互斥配置
|
|
*/
|
|
public function add(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'source_item_id' => 'required|integer|min:1',
|
|
'target_type' => 'required|in:ITEM,GROUP',
|
|
'target_id' => 'required|integer|min:1',
|
|
'exclusion_type' => 'required|string|max:50',
|
|
'exclusion_value' => 'nullable|string',
|
|
'remark' => 'nullable|string|max:255',
|
|
'status' => 'nullable|integer|in:0,1'
|
|
]);
|
|
|
|
// 验证互斥类型是否存在
|
|
$types = config('exclusion.types');
|
|
if (!isset($types[$data['exclusion_type']])) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '无效的互斥类型'
|
|
]);
|
|
}
|
|
|
|
$typeConfig = $types[$data['exclusion_type']];
|
|
|
|
// 如果需要参数,验证参数值
|
|
if ($typeConfig['require_param'] ?? false) {
|
|
$exclusionValue = $data['exclusion_value'] ?? null;
|
|
|
|
// 验证是否为有效的 JSON
|
|
if ($exclusionValue) {
|
|
json_decode($exclusionValue);
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '参数值必须是有效的 JSON 格式'
|
|
]);
|
|
}
|
|
}
|
|
|
|
// 验证各个字段
|
|
$fields = $typeConfig['fields'] ?? [];
|
|
foreach ($fields as $field) {
|
|
$validationRule = $field['validation'] ?? 'nullable';
|
|
$fieldValue = null;
|
|
|
|
if ($exclusionValue) {
|
|
$decoded = json_decode($exclusionValue, true);
|
|
$fieldValue = $decoded[$field['key']] ?? null;
|
|
}
|
|
|
|
$validator = Validator::make([
|
|
$field['key'] => $fieldValue
|
|
], [
|
|
$field['key'] => $validationRule
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => "字段【{$field['label']}】验证失败: " . $validator->errors()->first()
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 检查是否已存在相同的互斥配置
|
|
$exists = DB::table('exam_item_exclusion')
|
|
->where('source_item_id', $data['source_item_id'])
|
|
->where('target_type', $data['target_type'])
|
|
->where('target_id', $data['target_id'])
|
|
->where('deleted', 0)
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '该互斥配置已存在'
|
|
]);
|
|
}
|
|
|
|
// 验证目标是否存在
|
|
if ($data['target_type'] === 'ITEM') {
|
|
$exists = DB::table('exam_item')
|
|
->where('id', $data['target_id'])
|
|
->where('deleted', 0)
|
|
->exists();
|
|
} else {
|
|
$exists = DB::table('exam_group')
|
|
->where('id', $data['target_id'])
|
|
->exists();
|
|
}
|
|
|
|
if (!$exists) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '互斥目标不存在'
|
|
]);
|
|
}
|
|
|
|
// 插入数据
|
|
DB::table('exam_item_exclusion')->insert([
|
|
'source_item_id' => $data['source_item_id'],
|
|
'target_type' => $data['target_type'],
|
|
'target_id' => $data['target_id'],
|
|
'exclusion_type' => $data['exclusion_type'],
|
|
'exclusion_value' => $data['exclusion_value'] ?? null,
|
|
'remark' => $data['remark'] ?? null,
|
|
'status' => $data['status'] ?? 1,
|
|
'deleted' => 0,
|
|
'created_at' => now(),
|
|
'updated_at' => now()
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '新增成功'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 更新互斥配置
|
|
*/
|
|
public function update(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'id' => 'required|integer|min:1',
|
|
'target_type' => 'nullable|in:ITEM,GROUP',
|
|
'target_id' => 'nullable|integer|min:1',
|
|
'exclusion_type' => 'nullable|string|max:50',
|
|
'exclusion_value' => 'nullable|string',
|
|
'remark' => 'nullable|string|max:255',
|
|
'status' => 'nullable|integer|in:0,1'
|
|
]);
|
|
|
|
$id = $data['id'];
|
|
|
|
// 检查记录是否存在
|
|
$exclusion = DB::table('exam_item_exclusion')
|
|
->where('id', $id)
|
|
->where('deleted', 0)
|
|
->first();
|
|
|
|
if (!$exclusion) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '互斥配置不存在'
|
|
]);
|
|
}
|
|
|
|
// 如果更新了互斥类型,验证类型是否有效
|
|
if (isset($data['exclusion_type'])) {
|
|
$types = config('exclusion.types');
|
|
if (!isset($types[$data['exclusion_type']])) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '无效的互斥类型'
|
|
]);
|
|
}
|
|
|
|
$typeConfig = $types[$data['exclusion_type']];
|
|
|
|
// 如果需要参数,验证参数值
|
|
if ($typeConfig['require_param'] ?? false) {
|
|
$exclusionValue = isset($data['exclusion_value']) ? $data['exclusion_value'] : $exclusion->exclusion_value;
|
|
|
|
// 验证是否为有效的 JSON
|
|
if ($exclusionValue) {
|
|
json_decode($exclusionValue);
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '参数值必须是有效的 JSON 格式'
|
|
]);
|
|
}
|
|
}
|
|
|
|
// 验证各个字段
|
|
$fields = $typeConfig['fields'] ?? [];
|
|
foreach ($fields as $field) {
|
|
$validationRule = $field['validation'] ?? 'nullable';
|
|
$fieldValue = null;
|
|
|
|
if ($exclusionValue) {
|
|
$decoded = json_decode($exclusionValue, true);
|
|
$fieldValue = $decoded[$field['key']] ?? null;
|
|
}
|
|
|
|
$validator = Validator::make([
|
|
$field['key'] => $fieldValue
|
|
], [
|
|
$field['key'] => $validationRule
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => "字段【{$field['label']}】验证失败: " . $validator->errors()->first()
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$id = $data['id'];
|
|
|
|
// 检查记录是否存在
|
|
$exclusion = DB::table('exam_item_exclusion')
|
|
->where('id', $id)
|
|
->where('deleted', 0)
|
|
->first();
|
|
|
|
if (!$exclusion) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '互斥配置不存在'
|
|
]);
|
|
}
|
|
|
|
// 构建更新数据
|
|
$updateData = [
|
|
'updated_at' => now()
|
|
];
|
|
|
|
if (isset($data['target_type'])) {
|
|
$updateData['target_type'] = $data['target_type'];
|
|
}
|
|
if (isset($data['target_id'])) {
|
|
$updateData['target_id'] = $data['target_id'];
|
|
}
|
|
if (isset($data['exclusion_type'])) {
|
|
$updateData['exclusion_type'] = $data['exclusion_type'];
|
|
}
|
|
if (array_key_exists('exclusion_value', $data)) {
|
|
$updateData['exclusion_value'] = $data['exclusion_value'];
|
|
}
|
|
if (array_key_exists('remark', $data)) {
|
|
$updateData['remark'] = $data['remark'];
|
|
}
|
|
if (isset($data['status'])) {
|
|
$updateData['status'] = $data['status'];
|
|
}
|
|
|
|
DB::table('exam_item_exclusion')
|
|
->where('id', $id)
|
|
->update($updateData);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '更新成功'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 删除互斥配置
|
|
*/
|
|
public function delete(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'id' => 'required|integer|min:1'
|
|
]);
|
|
|
|
$id = $data['id'];
|
|
|
|
// 检查记录是否存在
|
|
$exclusion = DB::table('exam_item_exclusion')
|
|
->where('id', $id)
|
|
->where('deleted', 0)
|
|
->first();
|
|
|
|
if (!$exclusion) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '互斥配置不存在'
|
|
]);
|
|
}
|
|
|
|
// 逻辑删除
|
|
DB::table('exam_item_exclusion')
|
|
->where('id', $id)
|
|
->update([
|
|
'deleted' => 1,
|
|
'updated_at' => now()
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '删除成功'
|
|
]);
|
|
}
|
|
}
|