['user_id'], ]; } public function queryWhere(){ $where = []; if(isset($this->params['voucher_status']) && is_array($this->params['voucher_status'])){ $where[] = ['voucher_status','IN',$this->params['voucher_status']]; } $where[] = ['user_id','=',$this->userId]; return $where; } public function lists(): array { $data = UserCoupon::with(['couponRules'=>function(Query $query){ $query->withoutField(['voucher_status','voucher_count']); $query->with(['couponWithCategory'=>function(Query $query){ $query->field('id,name'); }]); }]) ->where($this->searchWhere) ->where($this->queryWhere()) ->field(['id', 'user_id', 'coupon_id', 'voucher_status','voucher_count']) ->limit($this->limitOffset, $this->limitLength) ->order(['id' => 'desc']) ->select() ->toArray(); $couponIds = []; foreach($data as $k => $v){ if($v['couponRules']){ if($v['voucher_status'] !=2 &&($v['couponRules']['begin_use'] time())){ $couponIds[] = $v['coupon_id']; $v['voucher_status'] = 2; } $v['couponRules']['begin_use'] = date("Y-m-d H:i:s",$v['couponRules']['begin_use'] ); $v['couponRules']['expire_time'] = date("Y-m-d H:i:s",$v['couponRules']['expire_time'] ); $v['couponRules']['goods_category_ids'] = array_column($v['couponRules']['couponWithCategory'],'id'); $data[$k] = $v; } } if(!empty($couponIds)){ UserCoupon::whereIn('coupon_id',$couponIds)->update(['voucher_status'=>2]); } $this->count = count($data); return $data; } public function count(): int { return $this->count; } }