'电子卡', 2=>'实体卡']; const COUPON_CATEGORY = [1=>'兑换卡', 2=>'储值卡']; // const activation_state const ACTIVATION_STATE = [0=>'未激活',1=>'已激活']; const FROZEN_STATE = [0=>'未冻结',1=>'已冻结']; protected $name = 'user_coupons'; protected $append = ['coupon_type_text','coupon_category_text','state_text']; public function getStateTextAttr(){ if($this->activation_state==0){ return ''; } $now = Date("Y-m-d H:i:s",time()); if($this->avaiable_good_num>0&&$this->effect_state==1&&$now<$this->deadline_at){ return '可使用'; } if($this->avaiable_good_num==0){ $good_num = Usercoupondetail::where("user_coupon_id",$this->id)->sum('good_num'); if($this->good_num==$good_num){ return '已使用'; } } $coupon = Usercoupon::where("parent_id",$this->id)->where("isall_trans",1)->where('user_id','>',0)->find(); if($coupon){ return '已转赠'; } if($now>$this->deadline_at){ return '已过期'; } return ''; } public function getCouponTypeTextAttr(){ return self::COUPON_TYPE[$this->coupon_type]??''; } public function getCouponCategoryTextAttr(){ return self::COUPON_CATEGORY[$this->coupon_category]??''; } /** * 米卡列表 * @param string $dataType 订单类型 * @param array $param * @return mixed */ public function getAllList(array $param = []) { // $goodsku = GoodsSkuModel::where('goods_sku_id',10001)->where('goods_id',10001)->find(); // return $goodsku->goods_props[0]['value']['name']??''; // 检索查询条件 $filter = $this->getQueryFilter($param); // 获取数据列表 $query = $this->with([])->where($filter) ->leftJoin('user', 'user.user_id = coupon.user_id') ->alias('coupon') ->field('coupon.*,user.mobile,user.nick_name'); $list = $query->order(['coupon.id' => 'desc'])->paginate(15); foreach($list as $row){ // $row->user = User::field('mobile,nick_name')->where("user_id",$row->user_id)->find(); $detail = Usercoupondetail::where('user_coupon_id',$row->id)->select(); $row->exchange_cnt = count($detail); $row->late_user = null; if($row->exchange_cnt>0){ $user_id = $detail[0]['user_id']; $row->late_user = User::field('mobile,nick_name')->where("user_id",$user_id)->find(); } $goodsku = GoodsSkuModel::where('goods_sku_id',$row->good_sku_id)->where('goods_id',$row->good_id)->find(); $goods = GoodsModel::field('goods_name')->where("goods_id",$row->good_id)->find(); $row->sku_name = $goodsku->goods_props[0]['value']['name']??''; $row->good_name = $goods->goods_name??''; } return $list; } //查询 public function getQueryFilter($param){ return ['coupon.is_delete'=>0]; } /** * 获取优惠券列表 * @param int|null $limit 获取的数量 * @param bool $onlyReceive 只显示可领取 * @return \think\Collection * @throws BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getList(int $user_id, int $type = 0,int $limit = 15){ // 查询构造器 $query = $this->getNewQuery(); // 只显示可领取(未过期,未发完)的优惠券 $query->where('user_id',$user_id); $query->where("coupon.is_delete",0); $now = Date("Y-m-d H:i:s",time()); if($type==1){ // $query->where("deadline_at",">",$now); $query->where('effect_state','in',[0,1])->where("deadline_at",">",$now); } if($type==2){ $query->where(function($query)use($now){ $query->whereOr('effect_state',2)->whereOr("deadline_at","<",$now); ; }); } //查询数量 $limit > 0 && $query->limit($limit); // 优惠券列表 $couponList = $query->alias('coupon')->where('activation_state', '=', 1)->where("frozen_state",0) ->order(['get_at' => 'desc']) ->select(); if($type==0){ $newarr = []; foreach($couponList as $row){ if($row->state_text=='已转赠'){ // unset($row); } else{ $newarr[] = $row; } } return $newarr; } return $couponList; } //用户 public function user(){ return $this->hasOne(User::class,'user_id','user_id'); } }