'json', 'category_ids' => 'json', 'goods_names' => 'json', 'categorynames' => 'json', 'supplierids' => 'json', 'storeids' => 'json', 'storenames' => 'json', 'member_tags_ids' => 'json', 'member_tags_names' => 'json', ]; /** * @var array */ protected $appends = ['coupon_type_name']; public function getCouponTypeNameAttribute() { switch ($this->attributes['use_type']) { case Coupon::COUPON_SHOP_USE: $name = '平台商品'; break; case Coupon::COUPON_STORE_USE: $name = '门店商品'; break; case Coupon::COUPON_SINGLE_STORE_USE: $name = '门店商品'; break; case Coupon::COUPON_ONE_HOTEL_USE: $name = '酒店商品'; break; case Coupon::COUPON_MORE_HOTEL_USE: $name = '酒店商品'; break; case Coupon::COUPON_GOODS_USE: $name = '指定商品'; break; case Coupon::COUPON_CATEGORY_USE: $name = '分类商品'; break; case Coupon::COUPON_EXCHANGE_USE: $name = '兑换券'; break; case Coupon::COUPON_GOODS_AND_STORE_USE: $name = '商品和门店'; break; default: $name = '未知类型'; } return $name; } // protected $hidden = ['storeids']; // protected $hidden = ['uniacid', 'cat_id', 'get_type', 'level_limit', 'use_type', 'return_type', 'coupon_type' // , 'coupon_method','back_type','supplier_uid','cashiersids','cashiersnames','category_ids','goods_ids', // 'storeids','supplierids','is_complex','getcashier','getstore','getsupplier','back_money','back_credit', // 'back_redpack','back_when','descnoset','deleted_at']; public function hasManyMemberCoupon() { return $this->hasMany('app\common\models\MemberCoupon','coupon_id','id'); } public static function getValidCoupon($MemberModel) { return MemberCoupon::getMemberCoupon($MemberModel); } public static function getUsageCount($couponId) { return static::uniacid() ->select(['id']) ->where('id', '=', $couponId) ->withCount(['hasManyMemberCoupon' => function ($query) { return $query->where('used', '=', 0); }]); } public static function getCouponById($couponId) { return static::uniacid() ->where('id', '=', $couponId) // ->with(['hasManyMemberCoupon'])(这个查询导致数据量太大崩了) ->first(); } //获取优惠券优惠方式 public static function getPromotionMethod($couponId) { $useType = static::uniacid()->where('id', '=', $couponId)->value('coupon_method'); switch ($useType) { case self::COUPON_MONEY_OFF: return [ 'type' => self::COUPON_MONEY_OFF, 'mode' => static::uniacid()->where('id', '=', $couponId)->value('deduct'), ]; break; case self::COUPON_DISCOUNT: return [ 'type' => self::COUPON_DISCOUNT, 'mode' => static::uniacid()->where('id', '=', $couponId)->value('discount'), ]; break; default: return [ 'type' => self::COUPON_SHOP_USE, ]; break; } } //获取优惠券适用期限 public static function getTimeLimit($couponId) { $time_limit = static::uniacid()->where('id', '=', $couponId)->value('time_limit'); switch ($time_limit) { case self::COUPON_SINCE_RECEIVE: return [ 'type' => self::COUPON_SINCE_RECEIVE, 'time_end' => static::uniacid()->where('id', '=', $couponId)->value('time_days'), ]; break; case self::COUPON_DATE_TIME_RANGE: return [ 'type' => self::COUPON_DATE_TIME_RANGE, 'time_end' => static::uniacid()->where('id', '=', $couponId)->value('time_end'), ]; break; default: return [ 'type' => self::COUPON_SHOP_USE, ]; break; } } //获取优惠券的适用范围 public static function getApplicableScope($couponId) { $useType = static::uniacid() ->where('id', '=', $couponId) ->value('use_type'); switch ($useType) { case 8: $goodIds = self::getApplicalbeGoodIds($couponId); return [ 'type' => self::COUPON_GOODS_USE, 'scope' => $goodIds, ]; break; case self::COUPON_GOODS_USE: $goodIds = self::getApplicalbeGoodIds($couponId); return [ 'type' => self::COUPON_GOODS_USE, 'scope' => $goodIds, ]; break; case self::COUPON_CATEGORY_USE: $categoryIds = self::getApplicalbeCategoryIds($couponId); return [ 'type' => self::COUPON_CATEGORY_USE, 'scope' => $categoryIds, ]; break; case self::COUPON_STORE_USE: $categoryIds = self::getApplicalbeCategoryIds($couponId); return [ 'type' => self::COUPON_STORE_USE, 'scope' => $categoryIds, ]; break; case self::COUPON_SINGLE_STORE_USE: $categoryIds = self::getApplicalbeCategoryIds($couponId); return [ 'type' => self::COUPON_SINGLE_STORE_USE, 'scope' => $categoryIds, ]; break; case self::COUPON_GOODS_AND_STORE_USE: return [ 'type' => self::COUPON_GOODS_AND_STORE_USE, ]; break; default: return [ 'type' => self::COUPON_SHOP_USE, ]; break; } } //获取优惠券的适用商品ID public static function getApplicalbeGoodIds($couponId) { return static::uniacid() ->where('id', '=', $couponId) ->value('goods_ids'); } //获取优惠券的适用商品分类ID public static function getApplicalbeCategoryIds($couponId) { return static::uniacid() ->where('id', '=', $couponId) ->value('category_ids'); } /** * 筛选某会员等级可领 * @param Builder $query * @param $memberLevel * @return Builder */ public function scopeMemberLevel(Builder $query, $memberLevel) { return $query->where(function ($query) use ($memberLevel) { $query->where('level_limit', '<=', $memberLevel) ->orWhere(function ($query) { $query->where('level_limit', -1); }); }); } /** * 筛选未过期 * @param Builder $query * @param null $time * @return Builder */ public function scopeUnexpired(Builder $query, $time = null) { if (!isset($time)) { $time = time(); } return $query->where(function ($query) use ($time) { // 不限时间 $query->where('time_limit', 1)->where('time_end', '>', $time-86400) ->orWhere(function ($query) { // 未结束的优惠券 $query->where('time_limit', 0); }); }); } /** * todo 为什么会出现负数 * 可领取张数 * @param $receivedCount * @return int */ public function availableCount($receivedCount) { if ($this->get_max == self::NO_LIMIT) { return 999; } return max($this->get_max - $receivedCount,0); } /** * todo 应在优惠券表添加这个字段 * 获取已领取数量 * @return int */ public function getReceiveCount() { return $this->hasManyMemberCoupon()->count(); } /** * 是否可领取 * @return bool */ public function available($special_type = 0) { $res = $this->status == 1 && ($this->total == -1 || $this->total > 0); if ($special_type != 1){ $res = $res && $this->get_type == 1; } return $res; // return $this->status == 1 && $this->get_type == 1 && ($this->total == -1 || $this->total > 0); } }