ConfigBonusForm.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Date;
  5. use common\models\Config;
  6. use yii\db\Exception;
  7. /**
  8. * Login form
  9. */
  10. class ConfigBonusForm extends Model
  11. {
  12. public $retailPoolPercent;
  13. public $retailPercent;
  14. /**
  15. * @inheritdoc
  16. */
  17. public function rules()
  18. {
  19. return [
  20. [['retailPoolPercent', 'retailPercent'], 'required'],
  21. [['retailPoolPercent', 'retailPercent'], 'number', 'max'=>100, 'min'=>0],
  22. ];
  23. }
  24. public function attributeLabels()
  25. {
  26. return [
  27. 'retailPoolPercent' => '进入零售池比例',
  28. 'retailPercent' => '零售奖比例',
  29. ];
  30. }
  31. /**
  32. * 更新配置
  33. * @return bool|null
  34. * @throws Exception
  35. */
  36. public function update(){
  37. if(!$this->validate()){
  38. return null;
  39. }
  40. $transaction = \Yii::$app->db->beginTransaction();
  41. try{
  42. if(!Config::updateAll(['VALUE'=>$this->retailPoolPercent, 'UPDATED_AT'=>Date::nowTime()], "CONFIG_NAME='retailPoolPercent'")){
  43. throw new Exception('进入零售池比例更新失败');
  44. }
  45. if(!Config::updateAll(['VALUE'=>$this->retailPercent, 'UPDATED_AT'=>Date::nowTime()], "CONFIG_NAME='retailPercent'")){
  46. throw new Exception('零售奖比例更新失败');
  47. }
  48. $transaction->commit();
  49. } catch (Exception $e){
  50. $transaction->rollBack();
  51. $this->addError('retailPoolPercent', $e->getMessage());
  52. return null;
  53. }
  54. return true;
  55. }
  56. }