海工商新版后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

83 lines
2.9 KiB

<?php
namespace App\Admin\Actions\Form\Follow;
use App\Models\AdminFollowShow;
use App\Models\SecondaryCollege;
use App\Models\Speciality;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
class FollowAdminShowForm extends Form
{
use LazyWidget;
public function handle(array $input)
{
$user_id = $this->payload['user_id'];
$data = array_filter($input);
$data['updated_at'] = date('Y-m-d H:i:s', time());
if (AdminFollowShow::query()->where('user_id', $user_id)->exists()) {
if ($data['type'] == '1') {
$data['speciality_id'] = [];
} else {
$data['secondary_college_id'] = [];
}
$sql = AdminFollowShow::query()->where('user_id', $user_id)->update($data);
} else {
$data['user_id'] = $user_id;
$data['created_at'] = date('Y-m-d H:i:s', time());
$sql = AdminFollowShow::query()->insert($data);
}
if (!$sql) {
return $this->response()->error('绑定数据失败');
}
return $this->response()->success('绑定数据成功')->refresh();
}
public function form(): void
{
$user_id = $this->payload['user_id'];
$info = AdminFollowShow::query()->where('user_id', $user_id)->first();
$this->select("type", "类型")
->options([1 => '二级学院', 2 => '专业'])
->default($info['type'] ?? '')
->when('=', 1, function (Form $form) use ($info) {
$this->multipleSelect("secondary_college_id", "二级学院")
->options(function () {
return SecondaryCollege::query()->pluck("name", "id");
})
->default($info['secondary_college_id'] ?? '')
->saving(function ($value) {
return json_encode($value);
});
})
->when('=', 2, function (Form $form) use ($info) {
$this->multipleSelect("speciality_id", "专业")
->options(function () {
$list = Speciality::query()->where("status", Speciality::STATUS_YES)->get()->toArray();
if (empty($list)) {
return [];
}
$select = [];
foreach ($list as $item) {
$college = SecondaryCollege::query()->where("id", $item["secondary_college_id"])->first();
$select[$item["id"]] = $item["speciality_name"] . "{$college->name}";
}
return $select;
})
->default($info['speciality_id'] ?? '')
->saving(function ($value) {
return json_encode($value);
});
})
->required();
}
}