year = $date[0]; $this->month = $date[1]; $this->user = $user; $this->fileName = '核销统计' . $this->year . '-' . $this->month . '.xlsx'; } public function headings(): array { return ['日期', '100元减10元优惠券', '100元减25元优惠券', '100元减50元优惠券', '200元减100元优惠券']; } public function collection() { $coupons = Coupon::where('user_id', $this->user->id) ->whereYear('created_at', $this->year) ->whereMonth('created_at', $this->month) ->where('status', 2) ->get(['id', 'thirdPartyGoodsId', 'created_at']); $coupons = $coupons->groupBy('create_day')->map(function ($items, $key) { $data = [ 'day' => $key, 'ysd10' => $items->where('thirdPartyGoodsId', 'YSD-full100-10')->count() ?? 0, 'ysd25' => $items->where('thirdPartyGoodsId', 'YSD-full100-25')->count() ?? 0, 'ysd50' => $items->where('thirdPartyGoodsId', 'YSD-full100-50')->count() ?? 0, 'ysd100' => $items->where('thirdPartyGoodsId', 'YSD-full200-100')->count() ?? 0, ]; return collect($data); }); $coupons = $coupons->sortByDesc('day'); return $coupons; } public function map($info): array { $data = [ $info['day'], ' ' . $info['ysd10'], ' ' . $info['ysd25'], ' ' . $info['ysd50'], ' ' . $info['ysd100'], ]; return $data; } }