'timestamp', 'end_time' => 'timestamp', 'valid_start_time' => 'timestamp', 'valid_end_time' => 'timestamp', ]; // 设置json类型字段 protected $json = [ 'join_site_ids' ]; // 设置JSON数据返回数组 protected $jsonAssoc = true; /** * 优惠券商品项 * @return HasMany */ public function goods() { return $this->hasMany(CouponGoods::class, 'coupon_id', 'id'); } /** * 关联站点表 * @return hasOne */ public function site() { return $this->hasOne(Site::class, 'site_id', 'site_id'); } /** * 关联商品项 * @param $value * @param $data * @return int|string * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ // public function getGoodsDataAttr($value, $data) // { // $coupon_id = $data['id'] ?? 0; // $list = []; // if($coupon_id > 0) { // $coupon_id = $data['id']; // $list = (new CouponGoods())->where([['coupon_id', '=', $coupon_id]])->select(); // } // return $list; // } public function getCouponPriceAttr($value, $data) { if (empty($data['price'])) { return 0; } return rtrim(rtrim($data['price'], '0'), '.'); } public function getCouponMinPriceAttr($value, $data) { if (empty($data['min_condition_money'])) { return 0; } return rtrim(rtrim($data['min_condition_money'], '0'), '.'); } /** * 搜索器:标题 * @param $value * @param $data */ public function searchTitleAttr($query, $value, $data) { if ($value) { $query->where('title', 'like', '%'.$value.'%'); } } /** * 搜索器:状态 * @param $value * @param $data */ public function searchStatusAttr($query, $value, $data) { if ($value != '') { $query->where('status', '=', $value); } } /** * 搜索器:优惠券类型 * @param $value * @param $data */ public function searchTypeAttr($query, $value, $data) { if ($value) { $query->where('type', '=', $value); } } /** * 搜索器:领取方式 * @param $value * @param $data */ public function searchReceiveTypeAttr($query, $value, $data) { if ($value) { $query->where('receive_type', '=', $value); } } /** * 搜索器:站点id * @param $value * @param $data */ public function searchSiteIdAttr($query, $value, $data) { if ($value != '') { if (is_array($value)) { $query->where('site_id', 'in', $value); } else { $query->where('site_id', '=', $value); } } } /** * 类型 * @param $value * @param $data * @return string */ public function getTypeNameAttr($value, $data) { if (empty($data['type'])) return ''; return CouponDict::getType()[$data['type']] ?? ''; } /** * 领取类型 * @param $value * @param $data * @return string */ public function getReceiveTypeNameAttr($value, $data) { if (empty($data['receive_type'])) return ''; return CouponDict::getReceiveType()[$data['receive_type']] ?? ''; } /** * 优惠券状态 * @param $value * @param $data * @return string */ public function getStatusNameAttr($value, $data) { return CouponDict::getStatus()[$data['status']] ?? ''; } /** * 活动开始时间搜索器 * @param Query $query * @param $value * @param $data */ public function searchStartTimeAttr(Query $query, $value, $data) { $start_time = empty($value[0]) ? 0 : strtotime($value[0]); $end_time = empty($value[1]) ? 0 : strtotime($value[1]); if ($start_time > 0 && $end_time > 0) { $query->whereBetweenTime('start_time', $start_time, $end_time); } elseif ($start_time > 0 && $end_time == 0) { $query->where([['start_time', '>=', $start_time]]); } elseif ($start_time == 0 && $end_time > 0) { $query->where([['start_time', '<=', $end_time]]); } } /** * 活动结束时间搜索器 * @param Query $query * @param $value * @param $data */ public function searchEndTimeAttr(Query $query, $value, $data) { $start_time = empty($value[0]) ? 0 : strtotime($value[0]); $end_time = empty($value[1]) ? 0 : strtotime($value[1]); if ($start_time > 0 && $end_time > 0) { $query->whereBetweenTime('end_time', $start_time, $end_time); } elseif ($start_time > 0 && $end_time == 0) { $query->where([['end_time', '>=', $start_time]]); } elseif ($start_time == 0 && $end_time > 0) { $query->where([['end_time', '<=', $end_time]]); } } /** * 领取生效时间范围搜索器(查询生效时间范围内的优惠券) * @param Query $query * @param $value * @param $data */ public function searchReceiveTimeAttr(Query $query, $value, $data) { $now_time = time(); $time_where = [ [ [ 'start_time', '<=', $now_time ], [ 'end_time', '>=', $now_time ], ], [ [ 'start_time', '=', 0 ], [ 'end_time', '=', 0 ], ] ]; $query->where(function ($query) use ($time_where){ $query->whereOr($time_where); }); } }