RechargeForm.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace addons\Member\merchant\forms;
  3. use Yii;
  4. use yii\base\Model;
  5. use yii\web\NotFoundHttpException;
  6. use common\forms\CreditsLogForm;
  7. /**
  8. * Class RechargeForm
  9. * @package addons\Member\merchant\forms
  10. * @author jianyan74 <751393839@qq.com>
  11. */
  12. class RechargeForm extends Model
  13. {
  14. const TYPE_MONEY = 'Money'; // 余额
  15. const TYPE_INT = 'Int'; // 积分
  16. const TYPE_GROWTH = 'Growth'; // 成长值
  17. const CHANGE_INCR = 1;
  18. const CHANGE_DECR = 2;
  19. public $old_num;
  20. public $change = self::CHANGE_INCR;
  21. public $money;
  22. public $int;
  23. public $growth;
  24. public $remark;
  25. public $type;
  26. protected $sercive;
  27. /**
  28. * @var array
  29. */
  30. public static $changeExplain = [
  31. self::CHANGE_INCR => '增加',
  32. self::CHANGE_DECR => '减少',
  33. ];
  34. public function rules()
  35. {
  36. return [
  37. [['change'], 'integer'],
  38. [['money'], 'number', 'min' => 0.01, 'max' => 999999.99],
  39. [['int', 'growth'], 'integer', 'min' => 1, 'max' => 999999],
  40. [['remark', 'type'], 'string'],
  41. [['type'], 'verifyEmpty'],
  42. ];
  43. }
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'old_num' => '当前',
  48. 'change' => '变更',
  49. 'money' => '数量',
  50. 'int' => '数量',
  51. 'growth' => '数量',
  52. 'remark' => '备注',
  53. ];
  54. }
  55. public function verifyEmpty()
  56. {
  57. if ($this->type == self::TYPE_MONEY && !$this->money) {
  58. $this->addError('money', '数量不能为空');
  59. }
  60. if ($this->type == self::TYPE_INT && !$this->int) {
  61. $this->addError('int', '数量不能为空');
  62. }
  63. if ($this->type == self::TYPE_GROWTH && !$this->growth) {
  64. $this->addError('growth', '数量不能为空');
  65. }
  66. }
  67. /**
  68. * @param $member
  69. * @return bool
  70. * @throws \yii\base\InvalidConfigException
  71. * @throws \yii\db\Exception
  72. */
  73. public function save($member)
  74. {
  75. $action = 'decr' . $this->type;
  76. if ($this->change == self::CHANGE_INCR) {
  77. $action = 'incr' . $this->type;
  78. }
  79. $num = $this->money;
  80. if ($this->type == self::TYPE_INT) {
  81. $num = $this->int;
  82. }
  83. if ($this->type == self::TYPE_GROWTH) {
  84. $num = $this->growth;
  85. }
  86. // 写入当前会员
  87. $transaction = Yii::$app->db->beginTransaction();
  88. try {
  89. Yii::$app->services->member->set($member);
  90. // 变动积分/余额
  91. Yii::$app->services->memberCreditsLog->$action(new CreditsLogForm([
  92. 'member' => Yii::$app->services->member->get($member->id),
  93. 'num' => $num,
  94. 'group' => 'manager',
  95. 'remark' => !empty($this->remark) ? $this->remark : '管理员操作',
  96. ]));
  97. $transaction->commit();
  98. } catch (NotFoundHttpException $e) {
  99. $transaction->rollBack();
  100. $this->addError('remark', $e->getMessage());
  101. return false;
  102. }
  103. return true;
  104. }
  105. }
粤ICP备19079148号