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.

55 lines
1.3 KiB

<?php
namespace App\Services;
use App\Models\ResearchDirection;
class ResearchDirectionResolver
{
/**
* @param array<int> $ids
* @param array<int, string> $names
* @return array<int>
*/
public function resolveIds(array $ids = [], array $names = []): array
{
$resolved = [];
foreach ($ids as $id) {
$id = (int) $id;
if ($id > 0) {
$resolved[$id] = $id;
}
}
foreach ($names as $name) {
$name = trim((string) $name);
if ($name === '') {
continue;
}
$row = ResearchDirection::withTrashed()->where('name', $name)->first();
if ($row) {
if ($row->trashed()) {
$row->restore();
}
if ((int) $row->status !== 1) {
$row->status = 1;
$row->save();
}
} else {
$row = ResearchDirection::query()->create([
'name' => $name,
'sort' => 0,
'status' => 1,
'remark' => 'user_custom',
]);
}
$resolved[(int) $row->id] = (int) $row->id;
}
return array_values($resolved);
}
}