DecLevelForm.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\helpers\Tool;
  7. use common\models\DeclarationLevel;
  8. /**
  9. * Login form
  10. */
  11. class DecLevelForm extends Model
  12. {
  13. public $id;
  14. public $levelName;
  15. public $perf;
  16. public $isAdjustGift;
  17. public $isDec;
  18. /**
  19. * @inheritdoc
  20. */
  21. public function rules()
  22. {
  23. return [
  24. [['id', 'levelName', 'perf', 'isAdjustGift', 'isDec'], 'trim', 'on'=>['add', 'edit']],
  25. [['id'], 'required', 'on'=>'edit'],
  26. [['id'], 'exist', 'targetClass'=>DeclarationLevel::class, 'targetAttribute'=>'ID'],
  27. [['levelName', 'perf', 'isAdjustGift', 'isDec'], 'required'],
  28. [['perf'], 'price'],
  29. ];
  30. }
  31. /**
  32. * 编辑
  33. * @return DeclarationLevel|null|static
  34. */
  35. public function edit(){
  36. if(!$this->validate()){
  37. return null;
  38. }
  39. if($this->scenario == 'add'){
  40. $model = new DeclarationLevel();
  41. $model->CREATE_ADMIN = Admin::getAdminNameById(\Yii::$app->user->id);
  42. $model->CREATED_AT = Date::nowTime();
  43. } elseif($this->scenario == 'edit') {
  44. $model = DeclarationLevel::findOne(['ID'=>$this->id]);
  45. $model->UPDATE_ADMIN = Admin::getAdminNameById(\Yii::$app->user->id);
  46. $model->UPDATED_AT = Date::nowTime();
  47. } else {
  48. $this->addError('id', '提交场景不存在');
  49. return null;
  50. }
  51. $model->LEVEL_NAME = $this->levelName;
  52. $model->PERF = $this->perf;
  53. $model->IS_ADJUST_GIFT = $this->isAdjustGift;
  54. $model->IS_DEC = $this->isDec;
  55. if($model->save()){
  56. return $model;
  57. } else {
  58. $this->addErrors($model->getErrors());
  59. return null;
  60. }
  61. }
  62. }