DecRoleForm.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace common\models\forms;
  3. use backendApi\modules\v1\models\Admin;
  4. use common\components\Model;
  5. use common\helpers\Date;
  6. use common\models\DecRole;
  7. /**
  8. * Login form
  9. */
  10. class DecRoleForm extends Model
  11. {
  12. public $id;
  13. public $roleName;
  14. public $fwBonusPercent;
  15. /**
  16. * @inheritdoc
  17. */
  18. public function rules()
  19. {
  20. return [
  21. [['id', 'roleName', 'fwBonusPercent'], 'trim', 'on'=>['add', 'edit']],
  22. [['id'], 'required', 'on'=>'edit'],
  23. [['id'], 'exist', 'targetClass'=>DecRole::class, 'targetAttribute'=>'ID'],
  24. [['roleName', 'fwBonusPercent'], 'required'],
  25. [['fwBonusPercent'], 'price'],
  26. ];
  27. }
  28. /**
  29. * 编辑
  30. */
  31. public function edit(){
  32. if(!$this->validate()){
  33. return null;
  34. }
  35. if($this->scenario == 'edit') {
  36. $model = DecRole::findOne(['ID'=>$this->id]);
  37. $model->UPDATER = Admin::getAdminNameById(\Yii::$app->user->id);
  38. $model->UPDATED_AT = Date::nowTime();
  39. } else {
  40. $this->addError('id', '提交场景不存在');
  41. return null;
  42. }
  43. $model->ROLE_NAME = $this->roleName;
  44. $model->FW_BONUS_PERCENT = $this->fwBonusPercent;
  45. if($model->save()){
  46. return $model;
  47. } else {
  48. $this->addErrors($model->getErrors());
  49. return null;
  50. }
  51. }
  52. }