<?php

namespace App\Domains\Stats\Exports;

use App\Core\AbstractExport;
use Dipper\Excel\Concerns\WithHeadings;
use Dipper\Excel\Concerns\FromCollection;
use App\Domains\Stats\Services\CompanyCountService;

class CompanyCountExport extends AbstractExport implements FromCollection, WithHeadings
{
    public $conditions;

    public function __construct(array $conditions = [])
    {
        $this->conditions = $conditions;
        parent::__construct();
    }

    public function collection()
    {
        $companies = app(CompanyCountService::class)->index($this->conditions)->toArray();

        $companies = collect($companies);

        $companies->push([
            '',
            '总计',
            array_sum($companies->pluck('total')->toArray()) ?: 0,
            array_sum($companies->pluck('counts')->toArray()) ?: 0,
            array_sum($companies->pluck('renewed_counts')->toArray()) ?: 0,
            array_sum($companies->pluck('valid_counts')->toArray()) ?: 0,
        ]);

        return $companies;
    }

    public function headings(): array
    {
        return [
            '企业ID',
            '企业名称',
            '总用户数',
            '新增用户数',
            '续费用户数',
            '服务期内用户数',
        ];
    }
}
