84 lines
1.9 KiB
PHP
84 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Merchant\Exporters;
|
|
|
|
use App\Models\ActivityRule;
|
|
use App\Models\Coupon;
|
|
use Maatwebsite\Excel\Concerns\Exportable;
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
|
use Maatwebsite\Excel\Concerns\WithMapping;
|
|
use Illuminate\Support\Arr;
|
|
|
|
class CensusExport implements FromCollection, WithMapping, WithHeadings
|
|
{
|
|
|
|
use Exportable;
|
|
|
|
public $year = '';
|
|
|
|
public $month = '';
|
|
|
|
public $user = '';
|
|
|
|
public $rules;
|
|
|
|
public function __construct($date, $user)
|
|
{
|
|
$this->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;
|
|
}
|
|
|
|
}
|