generated from liutk/owl-admin-base
86 lines
2.8 KiB
PHP
86 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin;
|
|
|
|
use App\Models\Employee;
|
|
use App\Models\Filters\EmployeeFilter;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Slowlyo\OwlAdmin\Services\AdminUserService;
|
|
use Slowlyo\OwlAdmin\Models\AdminUser;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
class EmployeeService extends BaseService
|
|
{
|
|
protected array $withRelationships = ['jobs', 'adminUser'];
|
|
|
|
protected string $modelName = Employee::class;
|
|
|
|
protected string $modelFilterName = EmployeeFilter::class;
|
|
|
|
public function getEditData($id): Model|\Illuminate\Database\Eloquent\Collection|Builder|array|null
|
|
{
|
|
$model = $this->getModel();
|
|
|
|
$hidden = collect([$model->getCreatedAtColumn(), $model->getUpdatedAtColumn()])
|
|
->filter(fn($item) => $item !== null)
|
|
->toArray();
|
|
|
|
return $this->query()->with(['adminUser'])->find($id)->makeHidden($hidden);
|
|
}
|
|
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
// 管理员信息
|
|
$adminUserService = AdminUserService::make();
|
|
if ($model) {
|
|
// 修改管理员信息
|
|
if (Arr::hasAny($data, ['username', 'password', 'confirm_password'])) {
|
|
if (!$adminUserService->update($model->admin_user_id, Arr::only($data, ['username', 'password', 'confirm_password']))) {
|
|
$this->setError($adminUserService->getError());
|
|
return false;
|
|
}
|
|
}
|
|
} else {
|
|
// 添加管理员信息
|
|
if (!$adminUserService->store(Arr::only($data, ['username', 'password', 'confirm_password']))) {
|
|
$this->setError($adminUserService->getError());
|
|
return false;
|
|
}
|
|
$adminUser = AdminUser::where('username', $data['username'])->first();
|
|
$data['admin_user_id'] = $adminUser->id;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public function preDelete(array $ids)
|
|
{
|
|
// 删除管理员
|
|
$adminUserIds = Employee::whereIn('id', $ids)->pluck('admin_user_id')->implode(',');
|
|
$adminUserService = AdminUserService::make();
|
|
if (!$adminUserService->delete($adminUserIds)) {
|
|
$this->setError($adminUserService->getError());
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function validate($data, $model = null)
|
|
{
|
|
$createRules = [
|
|
'name' => ['required'],
|
|
'phone' => ['required'],
|
|
];
|
|
$updateRules = [];
|
|
$validator = Validator::make($data, $model ? $updateRules : $createRules, [
|
|
'name.required' => __('employee.name') . '必填',
|
|
'phone.required' => __('employee.phone') . '必填',
|
|
]);
|
|
if ($validator->fails()) {
|
|
return $validator->errors()->first();
|
|
}
|
|
return true;
|
|
}
|
|
}
|