where(['is_delete' => 0, 'store_id' => $this->store_id]); if (!empty($this->id)) { $query->andWhere(['id' => $this->id]); } if (!empty($this->name)) { $query->andWhere(['like', 'name', $this->name]); } $query->orderBy('sort DESC'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['warehouseZoneNum'] = WarehouseZone::find()->where(['is_delete' => 0, 'warehouse_id' => $item['id']])->count(); $item['goods_num'] = Goods::find()->where(['is_delete' => 0, 'warehouse_id' => $item['id']])->count(); } return [ 'code' => 0, 'msg' => 'success', 'data' => $pagination, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function selectList () { try { $query = Warehouse::find()->where(['is_delete' => 0, 'store_id' => $this->store_id]); $query->orderBy('id DESC'); $query->select('id, name'); $data = $query->asArray()->all(); return [ 'code' => 0, 'msg' => 'success', 'data' => $data, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function save () { $t = \Yii::$app->db->beginTransaction(); try { $model = Warehouse::findOne(['id' => $this->id, 'store_id' => $this->store_id]); if (empty($model)) { $model = new Warehouse(); $model->store_id = $this->store_id; } $model->name = $this->name; $model->sort = $this->sort; if (!$model->save()) { \Yii::error([__METHOD__, $model->attributes]); throw new \Exception(array_shift($model->getFirstErrors())); } $t->commit(); return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { \Yii::error($e); $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function del () { try { if ($this->id) { Warehouse::updateAll(['is_delete' => 1], ['id' => $this->id]); } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }