model = new Notebook(); } /** * @return array */ public function lists(): array { $query = $this->model ->leftJoin('administrators as a1', 'a1.id', '=', 'notebook.administrators_id') ->leftJoin('administrators as a2', 'a2.id', '=', 'notebook.latest_administrators_id') ->with(['user']) ->select(["notebook.id","notebook.title","notebook.content","notebook.mode","notebook.share_key","a1.name as create_name","a2.name as latest_name","notebook.create_time","notebook.update_time"]); return EasySearch::create($query)->getData(); } public function beforeStore(&$data): void { $data['administrators_id'] = AdminAuth::getInfo()->id; } public function beforeWrite(&$data, $id = null): void { $data['latest_administrators_id'] = AdminAuth::getInfo()->id; } public function beforeUpdate(&$data, $id): void { if (strtotime($data['update_time']) < strtotime($this->model->update_time)){ throw new ApiMessageException("文档内容不是最新版,请更新后编辑"); } } /** * @param int $id * @param string $update_time * @return array|null */ public function getLatest(int $id, string $update_time): ?array { $notebook = $this->model->find($id); if (strtotime($update_time) < strtotime($notebook->update_time)) { $data = $notebook->toArray(); $data['latest_name'] = $notebook->latestAdmin->name; }else{ $data = null; } return $data; } /** * @param int $notebook_id * @param array $userIds * * @return void * @throws ApiMessageException */ public function addUser(int $notebook_id, array $userIds): void { $data = []; foreach ($userIds as $user_id) { $data[] = [ "notebook_id" => $notebook_id, "administrators_id" => $user_id, 'create_time' => date('Y-m-d H:i:s'), 'update_time' => date('Y-m-d H:i:s'), ]; } if (!NotebookUser::insert($data)){ throw new ApiMessageException('添加失败'); } } }