113 lines
2.7 KiB
PHP
113 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Stats\Exports;
|
|
|
|
use App\Core\AbstractExport;
|
|
use Illuminate\Support\Collection;
|
|
use Dipper\Excel\Concerns\WithRows;
|
|
use Dipper\Excel\Concerns\FromQuery;
|
|
use Dipper\Excel\Concerns\Exportable;
|
|
use App\Exceptions\NotAllowedException;
|
|
use Dipper\Excel\Concerns\WithHeadings;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use App\Domains\Stats\Services\OrderService;
|
|
use Dipper\Excel\Concerns\WithColumnFormatting;
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
|
|
|
class OrderDetailExport extends AbstractExport implements FromQuery, WithHeadings, WithRows, WithColumnFormatting
|
|
{
|
|
public $conditions;
|
|
|
|
public static $types = ['销售', '续费', '续费包', '加油包'];
|
|
|
|
public function __construct(array $conditions = [])
|
|
{
|
|
$this->conditions = $conditions;
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* @return Builder
|
|
*/
|
|
public function query()
|
|
{
|
|
if (!$class = OrderService::$classes[$this->conditions['type']]) {
|
|
throw new NotAllowedException('统计类型不存在');
|
|
}
|
|
|
|
$repository = app($class);
|
|
|
|
$builder = $repository->withConditions($this->conditions)->applyConditions();
|
|
|
|
return $builder;
|
|
}
|
|
|
|
public function headings(): array
|
|
{
|
|
return [
|
|
'SIM',
|
|
'企业名称',
|
|
'套餐名称',
|
|
'套餐周期',
|
|
'支付方式',
|
|
'价格',
|
|
'数量',
|
|
'订单时间',
|
|
];
|
|
}
|
|
|
|
|
|
/**
|
|
* @param mixed $row
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function rows($rows)
|
|
{
|
|
$rows = OrderService::detailTransformer($rows);
|
|
|
|
$rows->transform(function ($item) {
|
|
return [
|
|
$item['sim'],
|
|
$item['company_name'],
|
|
$item['package_name'],
|
|
$item['service_months'],
|
|
$item['pay_channel_name'],
|
|
$item['unit_price'],
|
|
$item['counts'],
|
|
$item['order_at'],
|
|
];
|
|
});
|
|
|
|
return $rows;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function columnFormats(): array
|
|
{
|
|
return [
|
|
'A' => NumberFormat::FORMAT_NUMBER,
|
|
'F' => NumberFormat::FORMAT_NUMBER_00,
|
|
'G' => NumberFormat::FORMAT_DATE_YYYYMMDD2,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 类型
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function tag()
|
|
{
|
|
if ((!$tag = self::$classes[get_class($this)]) || !self::$types[$this->conditions['type']]) {
|
|
throw new NotAllowedException('类型不允许');
|
|
}
|
|
|
|
$tag = self::$types[$this->conditions['type']] . $tag;
|
|
|
|
return $tag;
|
|
}
|
|
}
|