| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- namespace addons\SmartForm\common\models;
- use addons\SmartForm\common\enums\SmartFormEnum;
- use common\enums\StatusEnum;
- use Yii;
- /**
- * This is the model class for table "{{%addons_smart_form_template_goods}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $template_id 表单ID
- * @property int $goods_id 商品ID
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class SmartFormTemplateGoods extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_smart_form_template_goods}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id','goods_id'], 'required'],
- [['mall_id', 'template_id', 'goods_id', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '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;
- }
- }
- }
- }
|