name; $is_show = $this->is_show; $store_id = $this->store_id; $query = WorkerCat::find()->where(['store_id' => $store_id, 'is_delete' => 0]); if (!is_null($is_show) && in_array($is_show, [0, 1])) { $query->andWhere(['is_show' => $is_show]); } if ($name) { $query->andWhere(['LIKE', 'name', $name]); } $query->select('id, name, pic_url, is_show, created_at, updated_at, sort')->orderBy('sort desc'); $list = pagination_make($query); foreach ($list['list'] as &$item) { $item['is_show'] = (int)$item['is_show']; $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); $item['updated_at'] = $item['updated_at'] > 0 ? date('Y-m-d H:i:s', $item['updated_at']) : '-'; } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $list['list'], 'pageNo' => $list['pageNo'], 'totalCount' => $list['totalCount'], ], ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /* * 服务分类设置 */ public function workerCateSave() { $t = \Yii::$app->db->beginTransaction(); try { $id = $this->id; $name = $this->name; $pic_url = $this->pic_url; $sort = $this->sort; $is_show = $this->is_show; $store_id = $this->store_id; //名称或类型错误 if (!$name || !in_array($is_show, [0, 1])) { throw new \Exception('参数错误'); } $work_cat = WorkerCat::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => $store_id]); if (!$work_cat) { $work_cat = new WorkerCat(); $work_cat->store_id = $store_id; } $work_cat->name = $name; $work_cat->pic_url = $pic_url; $work_cat->sort = $sort; $work_cat->is_show = $is_show; if (!$work_cat->save()) { throw new \Exception(json_encode($work_cat->errors)); } $t->commit(); return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /* * 服务分类状态修改 */ public function workerCateStatus() { try { $id = \explode(',', $this->id); $store_id = $this->store_id; $is_show = (int)$this->is_show; $work_cat = WorkerCat::find()->where(['id' => $id, 'is_delete' => 0, 'store_id' => $store_id])->all(); if (count($work_cat) == 0) { throw new \Exception('数据不存在'); } foreach ($work_cat as $item) { if ($is_show === 2) { //删除 $worker = WorkerCatExt::findOne(['cat_id' => $item->id, 'is_delete' => 0]); if ($worker) { throw new \Exception('该分类下有用户,不可删除'); } $item->is_delete = 1; } else { //修改状态 if (!in_array($is_show, [0, 1])) { throw new \Exception('状态错误'); } $item->is_show = $is_show; } if (!$item->save()) { throw new \Exception(json_encode($item->errors)); } } return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }