CreateHbForm.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace hd\models\hb;
  3. use hd\models\BaseForm;
  4. /**
  5. * 创建红包表单
  6. * 兼容旧版单客户单红包,以及新版多客户 + 多红包规则 + 适用范围。
  7. */
  8. class CreateHbForm extends BaseForm
  9. {
  10. /** @var int 旧版单用户 */
  11. public $userId;
  12. /** @var int 旧版单客户 */
  13. public $customId;
  14. /** @var array 新版多客户 [{userId,customId,customName?}] 或 customId 数组 */
  15. public $customs;
  16. /** @var array 新版多红包规则 */
  17. public $rules;
  18. /** @var float 旧版金额 */
  19. public $amount;
  20. /** @var float 旧版最低消费 */
  21. public $minConsume;
  22. /** @var int 旧版有效天数 */
  23. public $days;
  24. /** @var int 旧版发放张数 */
  25. public $count;
  26. /** @var string 备注 */
  27. public $remark;
  28. /** @var string 生效时间 */
  29. public $effectiveDate;
  30. /** @var string 红包名称 */
  31. public $name;
  32. /** @var int 适用范围类型 */
  33. public $scopeType;
  34. /** @var string 适用范围值 */
  35. public $scopeValue;
  36. /** @var int 特价活动是否适用 */
  37. public $specialApplicable;
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['userId', 'customId', 'days', 'count', 'scopeType', 'specialApplicable'], 'integer'],
  45. [['amount', 'minConsume'], 'number'],
  46. [['remark', 'effectiveDate', 'name', 'scopeValue'], 'string'],
  47. [['customs', 'rules'], 'safe'],
  48. [['remark', 'effectiveDate', 'name', 'scopeValue'], 'filter', 'filter' => 'trim'],
  49. [['days'], 'default', 'value' => 0],
  50. [['count'], 'default', 'value' => 1],
  51. [['remark', 'effectiveDate', 'name', 'scopeValue'], 'default', 'value' => ''],
  52. [['scopeType'], 'default', 'value' => 1],
  53. [['specialApplicable'], 'default', 'value' => 1],
  54. ];
  55. }
  56. /**
  57. * 业务校验:新旧两种入参至少满足一种
  58. */
  59. public function validateForm()
  60. {
  61. if (!parent::validateForm()) {
  62. return false;
  63. }
  64. $hasNew = !empty($this->customs) || !empty($this->rules);
  65. if ($hasNew) {
  66. if (empty($this->customs) || !is_array($this->customs)) {
  67. $this->addError('customs', '请选择客户');
  68. return $this->failFirstError();
  69. }
  70. if (empty($this->rules) || !is_array($this->rules)) {
  71. $this->addError('rules', '请至少配置一个红包');
  72. return $this->failFirstError();
  73. }
  74. return true;
  75. }
  76. // 旧版必填
  77. if (empty($this->userId)) {
  78. $this->addError('userId', '请选择用户');
  79. return $this->failFirstError();
  80. }
  81. if (empty($this->customId)) {
  82. $this->addError('customId', '请选择客户');
  83. return $this->failFirstError();
  84. }
  85. if ($this->amount === null || $this->amount === '' || floatval($this->amount) < 0.01) {
  86. $this->addError('amount', '请填写红包金额');
  87. return $this->failFirstError();
  88. }
  89. if ($this->minConsume === null || $this->minConsume === '') {
  90. $this->addError('minConsume', '请填写最低消费金额');
  91. return $this->failFirstError();
  92. }
  93. return true;
  94. }
  95. protected function failFirstError()
  96. {
  97. $errors = $this->getFirstErrors();
  98. $msg = $errors ? reset($errors) : '参数错误';
  99. \common\components\util::fail($msg);
  100. return false;
  101. }
  102. public function attributeLabels()
  103. {
  104. return [
  105. 'userId' => '用户ID',
  106. 'customId' => '客户ID',
  107. 'customs' => '客户列表',
  108. 'rules' => '红包规则',
  109. 'amount' => '红包金额',
  110. 'minConsume' => '最低消费',
  111. 'days' => '有效时长',
  112. 'count' => '红包个数',
  113. 'effectiveDate' => '生效时间',
  114. 'remark' => '备注信息',
  115. 'name' => '红包名称',
  116. 'scopeType' => '适用范围',
  117. 'scopeValue' => '适用范围内容',
  118. 'specialApplicable' => '特价活动是否适用',
  119. ];
  120. }
  121. }