year = $date[0]; $this->month = $date[1]; $this->user = $user; $this->fileName = '核销统计' . $this->year . '-' . $this->month . '.xlsx'; $this->rules = ActivityRule::get(); } public function headings(): array { $titles = ['日期']; foreach ($this->rules as $rule) { $titles[] = $rule->title; } return $titles; } 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, ]; foreach ($this->rules as $rule) { $data[$rule->code] = $items->where('thirdPartyGoodsId', $rule->code)->count(); } return collect($data); }); $coupons = $coupons->sortByDesc('day'); return $coupons; } public function map($info): array { $data = []; foreach ($info as $value) { $data[] = '' . $value; } return $data; } }