$this->mall_id, 'status' => StatusEnum::ENABLED]; $where = [$baseWhere]; if (isset($params['name']) && $params['name'] !== '') { $where[] = ['like', 'name', $params['name']]; } return GoodsDescriptionTemplate::listPage([ 'where' => $where, 'order' => 'id desc' ]); } /** * @param int $id * * @return bool */ public function del(int $id): bool { $goodsIds = GoodsExtra::find() ->where([ 'description_template_id' => $id, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id ]) ->select('goods_id') ->column(); if ( !empty($goodsIds) && Goods::find() ->where([ 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $goodsIds ]) ->exists() ) { $this->error('当前模板已有商品关联,不允许删除'); return false; } if (!$this->edit(['id' => $id, 'status' => StatusEnum::DELETE])) { return false; } return true; } /** * @param array $data * * @return bool */ public function edit(array $data): bool { $data['mall_id'] = $this->mall_id; if (GoodsDescriptionTemplate::setData($data) === false) { $this->error(GoodsDescriptionTemplate::getStaticError()); return false; } return true; } /** * @param int $id * * @return array|\yii\db\ActiveRecord|null */ public function detail(int $id) { return GoodsDescriptionTemplate::find() ->select('id, name, frontend_name, fields') ->where(['id' => $id, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]) ->one(); } /** * @return array */ public function selectList(): array { $list = GoodsDescriptionTemplate::find() ->select('id, name, fields') ->where(['status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]) ->indexBy('id') ->asArray() ->all(); foreach ($list as &$item) { $item['fields'] = json_decode($item['fields'], true) ?: []; } return $list; } }