255], [['desc', 'rule'], 'string', 'max' => 2000], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'mall ID', 'name' => '优惠券名称', 'type' => '优惠券类型:1=折扣,2=满减', 'discount' => '折扣率', 'discount_limit' => '优惠上限', 'pic_url' => '未用', 'desc' => '未用', 'min_price' => '最低消费金额', 'sub_price' => '优惠金额', 'total_count' => '发放总数量', 'can_receive_count' => '可领取数量', 'sort' => '排序按升序排列', 'expire_type' => '到期类型', 'expire_day' => '有效天数', 'appoint_type' => '指定方式', 'begin_time' => '有效期开始时间', 'end_time' => '有效期结束时间', 'rule' => '使用说明', 'is_member' => '是否指定会员等级领取', 'is_delete' => '删除', 'deleted_at' => 'Deleted At', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'app_share_title' => 'App Share Title', 'app_share_pic' => 'App Share Pic', 'use_obtain' => '领取后赠送', ]; } public function getCat() { return $this->hasMany(GoodsCats::className(), ['id' => 'cat_id'])->where(['is_delete' => 0]) ->via('couponCat'); } public function getGoodsWarehouse() { return $this->hasMany(GoodsWarehouse::className(), ['id' => 'goods_warehouse_id']) ->via('couponGoods'); } public function getGoods() { return $this->hasMany(GoodsWarehouse::className(), ['id' => 'goods_warehouse_id']) ->via('couponGoods'); } public function getCouponCat() { return $this->hasMany(CouponCatRelation::className(), ['coupon_id' => 'id'])->where(['is_delete' => 0]); } public function getCouponGoods() { return $this->hasMany(CouponGoodsRelation::className(), ['coupon_id' => 'id'])->where(['is_delete' => 0]); } public function getCouponMember() { return $this->hasMany(CouponMemberRelation::className(), ['coupon_id' => 'id']); } public function getCouponCenter() { return $this->hasOne(CouponCenter::className(), ['coupon_id' => 'id']); } public function getBeginTime() { return date('Y-m-d', strtotime($this->begin_time)); } public function getEndTime() { return date('Y-m-d', strtotime($this->end_time)); } /** * @param $num integer 修改的数量 * @param $type string 增加add|减少sub * @param null|integer $id 优惠券ID * @return Coupon|null * @throws Exception */ public function updateCount($num, $type, $id = null) { if ($id) { $coupon = self::findOne(['id' => $id, 'is_delete' => 0]); } else { $coupon = $this; } if (!$coupon || !$coupon->id) { throw new Exception('错误的优惠券信息'); } if ($coupon->total_count == -1) { return $coupon; } if ($type === 'add') { $coupon->total_count += $num; } elseif ($type === 'sub') { if ($coupon->total_count < $num) { throw new Exception('优惠券库存不足'); } $coupon->total_count -= $num; } else { throw new Exception('错误的$type'); } if ($coupon->save()) { return $coupon; } else { throw new Exception($coupon->errors[0]); } } public function getAppointTypeText() { switch ($this->appoint_type) { case 1: $text = '指定商品'; break; case 2: $text = '指定商品类别'; break; case 3: $text = '全场通用'; break; case 4: $text = '仅限当面付'; break; default: $text = ''; } return $text; } }