139 lines
4.8 KiB
PHP
139 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class AITaskRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$allowedTasks = [
|
|
'data_analysis',
|
|
'report_generation',
|
|
'prediction',
|
|
'recommendation',
|
|
'document_summary',
|
|
];
|
|
|
|
return [
|
|
'task' => 'required|string|in:' . implode(',', $allowedTasks),
|
|
'parameters' => 'required|array',
|
|
'parameters.type' => 'nullable|string|max:50',
|
|
'parameters.period' => 'nullable|string|max:50',
|
|
'parameters.report_type' => 'nullable|string|max:50',
|
|
'parameters.start_date' => 'nullable|date',
|
|
'parameters.end_date' => 'nullable|date',
|
|
'parameters.target' => 'nullable|string|max:50',
|
|
'parameters.periods' => 'nullable|integer|min:1|max:12',
|
|
'parameters.area' => 'nullable|string|max:50',
|
|
'parameters.content' => 'nullable|string|max:10000',
|
|
'parameters.max_length' => 'nullable|integer|min:50|max:1000',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom messages for validator errors.
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'task.required' => '任务类型不能为空',
|
|
'task.in' => '不支持的任务类型',
|
|
'parameters.required' => '任务参数不能为空',
|
|
'parameters.array' => '任务参数必须是数组格式',
|
|
'parameters.type.max' => '分析类型最多50个字符',
|
|
'parameters.period.max' => '分析周期最多50个字符',
|
|
'parameters.report_type.max' => '报告类型最多50个字符',
|
|
'parameters.start_date.date' => '开始日期格式不正确',
|
|
'parameters.end_date.date' => '结束日期格式不正确',
|
|
'parameters.target.max' => '预测目标最多50个字符',
|
|
'parameters.periods.min' => '预测期数至少1期',
|
|
'parameters.periods.max' => '预测期数最多12期',
|
|
'parameters.area.max' => '建议领域最多50个字符',
|
|
'parameters.content.max' => '文档内容最多10000个字符',
|
|
'parameters.max_length.min' => '摘要长度至少50个字符',
|
|
'parameters.max_length.max' => '摘要长度最多1000个字符',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*/
|
|
protected function prepareForValidation()
|
|
{
|
|
// 确保parameters是数组
|
|
if ($this->has('parameters') && !is_array($this->parameters)) {
|
|
$this->merge([
|
|
'parameters' => [],
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get validated data with defaults.
|
|
*/
|
|
public function validatedData(): array
|
|
{
|
|
$validated = parent::validated();
|
|
|
|
// 设置默认值
|
|
if (!isset($validated['parameters'])) {
|
|
$validated['parameters'] = [];
|
|
}
|
|
|
|
// 根据任务类型设置默认参数
|
|
switch ($validated['task']) {
|
|
case 'data_analysis':
|
|
if (!isset($validated['parameters']['type'])) {
|
|
$validated['parameters']['type'] = 'sales';
|
|
}
|
|
if (!isset($validated['parameters']['period'])) {
|
|
$validated['parameters']['period'] = 'month';
|
|
}
|
|
break;
|
|
|
|
case 'report_generation':
|
|
if (!isset($validated['parameters']['report_type'])) {
|
|
$validated['parameters']['report_type'] = 'sales';
|
|
}
|
|
break;
|
|
|
|
case 'prediction':
|
|
if (!isset($validated['parameters']['target'])) {
|
|
$validated['parameters']['target'] = 'sales';
|
|
}
|
|
if (!isset($validated['parameters']['periods'])) {
|
|
$validated['parameters']['periods'] = 3;
|
|
}
|
|
break;
|
|
|
|
case 'recommendation':
|
|
if (!isset($validated['parameters']['area'])) {
|
|
$validated['parameters']['area'] = 'inventory';
|
|
}
|
|
break;
|
|
|
|
case 'document_summary':
|
|
if (!isset($validated['parameters']['max_length'])) {
|
|
$validated['parameters']['max_length'] = 200;
|
|
}
|
|
break;
|
|
}
|
|
|
|
return $validated;
|
|
}
|
|
} |