request->input('parent_id',0); if ($pid > 0){ // $pidInfo = $this->MaterialCategoryModel->getInfoById($pid); $model->parent_id = $pid; } $model->name = $this->request->input('name'); $model->city_id = (int)$this->request->input('city_id',0); $model->kitchen_id = (int)$this->request->input('kitchen_id',0); if (!$model->save()) throw new ErrException('添加失败'); return $this->return->success(); } /** * @return array */ public function edit(): array { $id = (int)$this->request->input('id'); $info = $this->MaterialCategoryModel->getInfoById($id); if (empty($info)) throw new ErrException('数据不存在'); $info->name = $this->request->input('name'); $pid = (int)$this->request->input('parent_id',0); if ($pid == $id) throw new ErrException('上级数据不能为自身'); $pidInfo = $this->MaterialCategoryModel->getInfoById($pid); if (empty($pidInfo)) throw new ErrException('该上级数据不存在'); $info->parent_id = $pid; if (!$info->save()) throw new ErrException('修改失败'); return $this->return->success(); } public function delete(): array { $id = (int)$this->request->input('id'); $info = $this->MaterialCategoryModel->getInfoById($id); if (empty($info)) throw new ErrException('数据不存在'); $children = $this->MaterialCategoryModel->getInfoByPId($id); if (!empty($children)) throw new ErrException('请先删除下级数据'); $info->is_del = MaterialCode::IS_DEL; if (!$info->save()) throw new ErrException('删除失败'); return $this->return->success(); } /** * @return array */ public function findById():array { $id = (int)$this->request->input('query_id'); $info = $this->MaterialCategoryModel->getInfoById($id)->toArray(); if (empty($info)) throw new ErrException('数据不存在'); return $this->return->success('success',$info); } /** * @return array */ public function list(): array { $cityId = (int)$this->request->input('query_city_id'); $list = $this->MaterialCategoryModel ->when($cityId > 0,function($query) use($cityId){ $query->where('city_id',$cityId); }) ->where('is_del',MaterialCode::IS_NO_DEL)->get(); return $this->return->success('success',['list' => $list->toArray()]); } }