resloveData($data); $validate = $this->validate($data); if ($validate !== true) { $this->setError($validate); return false; } // 添加管理员信息 $adminUserService = AdminUserService::make(); if (! $adminUserService->store(Arr::only($data, ['username', 'avatar', 'password', 'confirm_password', 'name']))) { $this->setError($adminUserService->getError()); return false; } $adminUser = AdminUser::where('username', $data['username'])->first(); $data['admin_user_id'] = $adminUser->id; $model = $this->modelName::create($data); if (isset($data['jobs']) && is_string($data['jobs'])) { $this->resloveJob($model, explode(',', $data['jobs'])); } return true; } public function update($primaryKey, $data): bool { $model = $this->query()->whereKey($primaryKey)->firstOrFail(); $data = $this->resloveData($data, $model); $validate = $this->validate($data, $model); if ($validate !== true) { $this->setError($validate); return false; } // 修改管理员 $adminUserService = AdminUserService::make(); if (!$adminUserService->update($model->admin_user_id, Arr::only($data, ['password', 'confirm_password', 'name', 'avatar']))) { return $this->setError($adminUserService->getError()); } if (isset($data['jobs']) && is_string($data['jobs'])) { $this->resloveJob($model, explode(',', $data['jobs'])); } return $model->update($data); } /** * 员工离职/还原 * * @param Employee $user * @param array $options {leave_at: 离职时间} * * @return boolean */ public function leave(Employee $user, $options = []) { if ($user->employee_status == EmployeeStatus::Online) { $user->update([ 'leave_at' => data_get($options, 'leave_at', now()), 'employee_status' => EmployeeStatus::Offline, ]); } else { $user->update([ 'leave_at' => null, 'employee_status' => EmployeeStatus::Online, ]); } return true; } /** * 处理职位关联 * * @param Employee $model * @param array $jobs(字典表 key 组成的数组) */ public function resloveJob($model, $jobs) { $model->jobs()->sync($jobs); } public function preDelete(array $ids): void { if (in_array(1, $ids)) { admin_abort('超级管理员不能删除'); } // 无打卡记录 if (EmployeeSignLog::whereIn('employee_id', $ids)->exists()) { admin_abort('请先删除员工打卡记录'); } // 无请假申请 if (HolidayApply::whereIn('employee_id', $ids)->exists()) { admin_abort('请先删除员工打卡记录'); } // 无出差申请 if (OfficalBusiness::whereIn('employee_id', $ids)->exists()) { admin_abort('请先删除员工打卡记录'); } // 加班申请 if (OvertimeApply::whereIn('employee_id', $ids)->exists()) { admin_abort('请先删除员工打卡记录'); } // 店员关联 if (DB::table('store_employees')->whereIn('employee_id', $ids)->exists()) { admin_abort('员工已关联门店, 请先从门店中删除'); } // 删除管理员 $adminUserIds = Employee::whereIn('id', $ids)->pluck('admin_user_id')->implode(','); $adminUserService = AdminUserService::make(); $adminUserService->delete($adminUserIds); // 删除职位关联 DB::table('employee_jobs')->whereIn('employee_id', $ids)->delete(); } public function validate($data, $model = null) { $createRules = [ 'name' => ['required'], 'phone' => ['required'], 'username' => ['required'], 'password' => ['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; } }