TimestampBehavior::class, ] ]; } public function beforeSave($insert) { if(parent::beforeSave($insert)){ $query = self::find()->where(['level' => $this->level, 'status' => 1, 'store_id' => $this->store_id,'is_delete' => 0]); $has = $query->andWhere([ '!=', 'id', (int)$this->id ])->one(); if($has){ $this->addError('level', '操作失败,等级已存在,等级id:' . $has->id); return false; } $al1 = self::find()->where(['store_id' => $this->store_id, 'status' => 1,'is_delete' => 0])->andWhere(['<', 'level', $this->level])->andWhere(['!=', 'level', $this->getOldAttribute('level')])->orderBy('level DESC')->limit(1)->one(); if($al1 && $this->money <= $al1->money && $this->child_num <= $al1->child_num && $this->child_level <= $al1->child_level){ $this->addError('level', '操作失败,等级升级规则错误(金额、下级数量,下级等级,不能低于上一个等级),对比等级:' . $al1->level); return false; } $al2 = self::find()->where(['store_id' => $this->store_id, 'status' => 1,'is_delete' => 0])->andWhere(['>', 'level', $this->level])->andWhere(['!=', 'level', $this->getOldAttribute('level')])->orderBy('level ASC')->limit(1)->one(); if($al2 && $this->money >= $al2->money && $this->child_num >= $al2->child_num && $this->child_level >= $al2->child_level){ $this->addError('level', '操作失败,等级升级规则错误(金额、下级数量,下级等级,不能高于下一个等级),对比等级:' . $al2->level); return false; } if(!$insert && ( ($this->getOldAttribute('level') != $this->level) || ($this->getOldAttribute('status') != $this->status && $this->status == 0) || ($this->getOldAttribute('is_delete') != $this->is_delete && $this->is_delete == 1) )){ $count = ActivityOrderRebateSelfUser::find()->where(['store_id' => $this->store_id, 'level' => $this->getOldAttribute('level')])->count(); if($count > 0) { $this->addError('level', '操作失败,当前等级下有会员,禁止修改等级,会员数量' . $count); return false; } } return true; } return false; } }