WithdrawLevelForm.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Date;
  5. use common\models\WithdrawLevel;
  6. /**
  7. * Login form
  8. */
  9. class WithdrawLevelForm extends Model
  10. {
  11. public $id;
  12. public $minAmount;
  13. public $maxAmount;
  14. public $taxPercent;
  15. /**
  16. * @inheritdoc
  17. */
  18. public function rules()
  19. {
  20. return [
  21. [['minAmount', 'maxAmount', 'taxPercent'], 'trim'],
  22. [['minAmount', 'taxPercent'], 'required'],
  23. [['id'], 'exist', 'targetClass'=>WithdrawLevel::class, 'targetAttribute'=>'ID'],
  24. [['minAmount', 'maxAmount', 'taxPercent'], 'price'],
  25. [['minAmount', 'maxAmount'], 'isRightAmount'],
  26. ];
  27. }
  28. /**
  29. * 指定校验场景
  30. * @return array
  31. */
  32. public function scenarios()
  33. {
  34. $parentScenarios = parent::scenarios();
  35. $customScenarios = [
  36. 'add' => ['minAmount', 'maxAmount', 'taxPercent'],
  37. 'edit' => ['id', 'minAmount', 'maxAmount', 'taxPercent'],
  38. ];
  39. return array_merge($parentScenarios, $customScenarios);
  40. }
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'minAmount' => '最小提现金额',
  45. 'maxAmount' => '最高提现金额',
  46. 'taxPercent' => '扣税比率',
  47. ];
  48. }
  49. /**
  50. * 校验最小金额
  51. * @param $attribute
  52. */
  53. public function isRightAmount($attribute){
  54. if($this->maxAmount){
  55. if($this->minAmount >= $this->maxAmount){
  56. $this->addError($attribute, '最小提现金额比最高提现金额大');
  57. }
  58. }
  59. $where = 'MIN_AMOUNT<=:AMOUNT AND MAX_AMOUNT>=:AMOUNT';
  60. $params = [
  61. ':AMOUNT'=>$this->$attribute
  62. ];
  63. if($this->scenario == 'edit'){
  64. $where .= ' AND ID<>:ID';
  65. $params[':ID'] = $this->id;
  66. }
  67. if(WithdrawLevel::find()->where($where, $params)->exists()){
  68. $this->addError($attribute, $this->attributeLabels()[$attribute].'和其他设置有冲突');
  69. }
  70. $whereT = 'MIN_AMOUNT<=:AMOUNT AND MAX_AMOUNT IS NULL';
  71. $paramsT = [
  72. ':AMOUNT'=>floatval($this->$attribute)
  73. ];
  74. if($this->scenario == 'edit'){
  75. $whereT .= ' AND ID<>:ID';
  76. $paramsT[':ID'] = $this->id;
  77. }
  78. if(WithdrawLevel::find()->where($whereT, $paramsT)->exists()){
  79. $this->addError($attribute, $this->attributeLabels()[$attribute].'和其他设置有冲突');
  80. }
  81. }
  82. /**
  83. * 编辑
  84. * @return WithdrawLevel|null|static
  85. */
  86. public function edit(){
  87. if(!$this->validate()){
  88. return null;
  89. }
  90. if($this->scenario == 'add'){
  91. $model = new WithdrawLevel();
  92. $model->CREATED_AT = Date::nowTime();
  93. } elseif($this->scenario == 'edit') {
  94. $model = WithdrawLevel::findOne(['ID'=>$this->id]);
  95. } else {
  96. $this->addError('id', '提交场景不存在');
  97. return null;
  98. }
  99. $model->MIN_AMOUNT = $this->minAmount;
  100. $model->MAX_AMOUNT = $this->maxAmount;
  101. $model->TAX_PERCENT = $this->taxPercent;
  102. if($model->save()){
  103. return $model;
  104. } else {
  105. $this->addErrors($model->getErrors());
  106. return null;
  107. }
  108. }
  109. }