request->input('limit', 10); $this->getWhere(); if (empty($this->where)) { $data = $this->kitchenModel->orderBy('id','desc')->paginate($limit)->toArray(); }else{ $data = $this->kitchenModel->orderBy('id','desc')->where($this->where)->paginate($limit)->toArray(); } $cityId = array_unique(array_column($data['data'],'city_id')); $cityName = $this->systemCityModel->getCityNameByIds($cityId); foreach ($data['data'] as &$one) { $one['city_name'] = $cityName[$one['city_id']] ?? ''; } return $this->return->success(); } /** * @return void */ private function getWhere(): void { $this->where[] = ['is_del','=',SiteCode::KITCHEN_NO_DEL]; if ($this->request->input('city_id',0) > 0) { $this->where[] = ['city_id', '=', $this->request->input('city_id')]; } } /** * @return array */ public function add(): array { $name = $this->request->input('name'); $info = $this->kitchenModel->getInfoByName($name); if (!empty($info)) throw new ErrException('数据已存在'); $model = new Kitchen(); $model->name = $this->request->input('name'); $model->city_id = $this->request->input('city_id'); $model->address = $this->request->input('address'); $model->lng = $this->request->input('lng'); $model->lat = $this->request->input('lat'); $model->status = $this->request->input('status'); if (!$model->save()) throw new ErrException('添加失败'); // if ($model->status == SiteCode::KITCHEN_ENABLE) { // $this->setSiteCache(SiteCode::KITCHEN_REDIS_PREFIX.$model->id,$model->lng,$model->lat); // } return $this->return->success(); } /** * @return array */ public function edit(): array { $id = (int)$this->request->input('id'); $name = $this->request->input('name'); $status = (int)$this->request->input('status'); $info = $this->kitchenModel->getInfoById($id); if (empty($info)) throw new ErrException('数据不存在'); $name = $this->kitchenModel->getInfoByName($name); if ($name->id != $info->id) throw new ErrException('数据已存在'); if ($info->status == SiteCode::KITCHEN_ENABLE && $status == SiteCode::KITCHEN_DISABLE) { $this->siteModel->disableStatusByKitchenId($info->id); } $info->name = $this->request->input('name'); $info->address = $this->request->input('address'); $info->lng = $this->request->input('lng'); $info->lat = $this->request->input('lat'); $info->status = $status; if (!$info->save()) throw new ErrException('修改失败'); return $this->return->success(); } /** * @return array * @throws Exception */ public function del(): array { $id = (int)$this->request->input('id'); $info = $this->kitchenModel->getInfoById($id); if (empty($info)) throw new ErrException('数据不存在'); $this->siteModel->disableStatusByKitchenId($info->id); $info->is_del = SiteCode::SITE_DEL; if (!$info->save()) throw new ErrException('删除失败'); return $this->return->success(); } /** * @return array */ public function info(): array { $data = $this->kitchenModel ->getInfoById((int)$this->request->input('id')); if (empty($data)) throw new ErrException('数据不存在'); $res = [ 'id' => $data->id, 'city_id' => $data->city_id, 'name' => $data->name, 'address' => $data->address, 'lng' => $data->lng, 'lat' => $data->lat, 'status' => $data->status, 'city_name' => $this->systemCityModel->where('id',$data->city_id)->value('title') ]; return $this->return->success('success', $res); } }