WithdrawLevel::class, 'targetAttribute'=>'ID'], [['minAmount', 'maxAmount', 'taxPercent'], 'price'], [['minAmount', 'maxAmount'], 'isRightAmount'], ]; } /** * 指定校验场景 * @return array */ public function scenarios() { $parentScenarios = parent::scenarios(); $customScenarios = [ 'add' => ['minAmount', 'maxAmount', 'taxPercent'], 'edit' => ['id', 'minAmount', 'maxAmount', 'taxPercent'], ]; return array_merge($parentScenarios, $customScenarios); } public function attributeLabels() { return [ 'minAmount' => '最小提现金额', 'maxAmount' => '最高提现金额', 'taxPercent' => '扣税比率', ]; } /** * 校验最小金额 * @param $attribute */ public function isRightAmount($attribute){ if($this->maxAmount){ if($this->minAmount >= $this->maxAmount){ $this->addError($attribute, '最小提现金额比最高提现金额大'); } } $where = 'MIN_AMOUNT<=:AMOUNT AND MAX_AMOUNT>=:AMOUNT'; $params = [ ':AMOUNT'=>$this->$attribute ]; if($this->scenario == 'edit'){ $where .= ' AND ID<>:ID'; $params[':ID'] = $this->id; } if(WithdrawLevel::find()->where($where, $params)->exists()){ $this->addError($attribute, $this->attributeLabels()[$attribute].'和其他设置有冲突'); } $whereT = 'MIN_AMOUNT<=:AMOUNT AND MAX_AMOUNT IS NULL'; $paramsT = [ ':AMOUNT'=>floatval($this->$attribute) ]; if($this->scenario == 'edit'){ $whereT .= ' AND ID<>:ID'; $paramsT[':ID'] = $this->id; } if(WithdrawLevel::find()->where($whereT, $paramsT)->exists()){ $this->addError($attribute, $this->attributeLabels()[$attribute].'和其他设置有冲突'); } } /** * 编辑 * @return WithdrawLevel|null|static */ public function edit(){ if(!$this->validate()){ return null; } if($this->scenario == 'add'){ $model = new WithdrawLevel(); $model->CREATED_AT = Date::nowTime(); } elseif($this->scenario == 'edit') { $model = WithdrawLevel::findOne(['ID'=>$this->id]); } else { $this->addError('id', '提交场景不存在'); return null; } $model->MIN_AMOUNT = $this->minAmount; $model->MAX_AMOUNT = $this->maxAmount; $model->TAX_PERCENT = $this->taxPercent; if($model->save()){ return $model; } else { $this->addErrors($model->getErrors()); return null; } } }