Files
pingan_new/app/Merchant/Exporters/CensusExport.php
2020-08-06 16:42:18 +08:00

71 lines
2.0 KiB
PHP

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