'ID', 'mall_id' => 'Mall ID', 'template_id' => 'Template ID', 'goods_id' => 'Goods ID', 'status' => 'Status', 'created_at' => 'Created At', 'updated_at' => 'Updated At', ]; } // 关联模型 public function getTemplate() { return $this->hasOne(AddonsSmartFormTemplate::class, ['id' => 'template_id']); } /** * * @param array $data * @return int * @throws \Exception */ public static function setData(array $data) { if(isset($data['id']) && $data['id'] > 0 ){ $model = self::findOne([ 'id' => $data['id']]); if(!$model){ throw new \Exception('记录不存在'); } }else{ $model = self::findOne(['mall_id' => $data['mall_id'], 'goods_id' => $data['goods_id']]); if(!$model){ $model = new self(); } } $model->attributes = $data; if (!$model->save()) { throw new \Exception($model->getErrorMessage()); } return $model->id; } /** * 获取商品所关联的智能表单 * @param $params * @return array */ public function getGoodsTemplate($params) { $ret = []; $info = self::findOne(['mall_id' => $params['mall_id'] , 'goods_id' => $params['goods_id'],'status' => StatusEnum::ENABLED]); if(!$info){ return $ret; } $template_id = $info->template_id; if($params['user_id']){ // 判断是否可以重复填写 - 这里死循环了 - 需要更改条件 if (AddonsSmartFormTemplate::find()->where(['id' => $info['template_id']])->one()){ $user_form = AddonsSmartFormTemplateUserForm::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'smart_form_template_id' => $info['template_id'],'status' => StatusEnum::ENABLED, 'is_ignore' => StatusEnum::DISABLED]); if (!empty($user_form)) { $ret['user_form_id'] = $user_form->id; } } $template_id = empty($user_form) ? $template_id : 0; } $ret['smart_form_template_id'] = $template_id; return $ret; } public static function postGoodSmartForm($params) { // 后台页面组件展示 if ($params['type'] == 'show_component') { return [ 'component' => 'com-smartform-goods', 'path' => Yii::$app->basePath . '/../addons/SmartForm/backend/views/components', ]; } else { try { if (empty($params['setting'][SmartFormEnum::ADDONS_NAME])) return true; $post = $params['setting'][SmartFormEnum::ADDONS_NAME]; $post['mall_id'] = $params['mall_id']; $res = self::setData($post); if($res==false){ throw new \Exception(self::getStaticError()); } return true; } catch (\Exception $e) { self::setStaticError($e->getMessage()); return false; } } } }