| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace hd\models\hb;
- use hd\models\BaseForm;
- /**
- * 创建红包表单
- * 兼容旧版单客户单红包,以及新版多客户 + 多红包规则 + 适用范围。
- */
- class CreateHbForm extends BaseForm
- {
- /** @var int 旧版单用户 */
- public $userId;
- /** @var int 旧版单客户 */
- public $customId;
- /** @var array 新版多客户 [{userId,customId,customName?}] 或 customId 数组 */
- public $customs;
- /** @var array 新版多红包规则 */
- public $rules;
- /** @var float 旧版金额 */
- public $amount;
- /** @var float 旧版最低消费 */
- public $minConsume;
- /** @var int 旧版有效天数 */
- public $days;
- /** @var int 旧版发放张数 */
- public $count;
- /** @var string 备注 */
- public $remark;
- /** @var string 生效时间 */
- public $effectiveDate;
- /** @var string 红包名称 */
- public $name;
- /** @var int 适用范围类型 */
- public $scopeType;
- /** @var string 适用范围值 */
- public $scopeValue;
- /** @var int 特价活动是否适用 */
- public $specialApplicable;
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['userId', 'customId', 'days', 'count', 'scopeType', 'specialApplicable'], 'integer'],
- [['amount', 'minConsume'], 'number'],
- [['remark', 'effectiveDate', 'name', 'scopeValue'], 'string'],
- [['customs', 'rules'], 'safe'],
- [['remark', 'effectiveDate', 'name', 'scopeValue'], 'filter', 'filter' => 'trim'],
- [['days'], 'default', 'value' => 0],
- [['count'], 'default', 'value' => 1],
- [['remark', 'effectiveDate', 'name', 'scopeValue'], 'default', 'value' => ''],
- [['scopeType'], 'default', 'value' => 1],
- [['specialApplicable'], 'default', 'value' => 1],
- ];
- }
- /**
- * 业务校验:新旧两种入参至少满足一种
- */
- public function validateForm()
- {
- if (!parent::validateForm()) {
- return false;
- }
- $hasNew = !empty($this->customs) || !empty($this->rules);
- if ($hasNew) {
- if (empty($this->customs) || !is_array($this->customs)) {
- $this->addError('customs', '请选择客户');
- return $this->failFirstError();
- }
- if (empty($this->rules) || !is_array($this->rules)) {
- $this->addError('rules', '请至少配置一个红包');
- return $this->failFirstError();
- }
- return true;
- }
- // 旧版必填
- if (empty($this->userId)) {
- $this->addError('userId', '请选择用户');
- return $this->failFirstError();
- }
- if (empty($this->customId)) {
- $this->addError('customId', '请选择客户');
- return $this->failFirstError();
- }
- if ($this->amount === null || $this->amount === '' || floatval($this->amount) < 0.01) {
- $this->addError('amount', '请填写红包金额');
- return $this->failFirstError();
- }
- if ($this->minConsume === null || $this->minConsume === '') {
- $this->addError('minConsume', '请填写最低消费金额');
- return $this->failFirstError();
- }
- return true;
- }
- protected function failFirstError()
- {
- $errors = $this->getFirstErrors();
- $msg = $errors ? reset($errors) : '参数错误';
- \common\components\util::fail($msg);
- return false;
- }
- public function attributeLabels()
- {
- return [
- 'userId' => '用户ID',
- 'customId' => '客户ID',
- 'customs' => '客户列表',
- 'rules' => '红包规则',
- 'amount' => '红包金额',
- 'minConsume' => '最低消费',
- 'days' => '有效时长',
- 'count' => '红包个数',
- 'effectiveDate' => '生效时间',
- 'remark' => '备注信息',
- 'name' => '红包名称',
- 'scopeType' => '适用范围',
- 'scopeValue' => '适用范围内容',
- 'specialApplicable' => '特价活动是否适用',
- ];
- }
- }
|