130 lines
3.3 KiB
PHP
130 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Stats\Exports;
|
|
|
|
use App\Core\AbstractExport;
|
|
use Dipper\Excel\Concerns\WithRows;
|
|
use Dipper\Excel\Concerns\FromQuery;
|
|
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;
|
|
use App\Domains\Virtual\Repositories\OrderCardPartitionRepository;
|
|
use App\Domains\Virtual\Services\PackageService;
|
|
|
|
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 (!isset(self::$types[$this->conditions['type']])) {
|
|
throw new NotAllowedException('统计类型不存在');
|
|
}
|
|
|
|
$builder = app(OrderCardPartitionRepository::class)->forceNoReset()->withConditions($this->conditions)->applyConditions()->orderBy('sim');
|
|
|
|
return $builder;
|
|
}
|
|
|
|
public function headings(): array
|
|
{
|
|
$headings = [
|
|
'SIM',
|
|
'企业名称',
|
|
'套餐名称',
|
|
'套餐周期',
|
|
'支付方式',
|
|
'价格',
|
|
'数量',
|
|
'订单时间',
|
|
];
|
|
|
|
if (in_array($this->conditions['type'], [2, 3])) {
|
|
array_splice($headings, 2, 0, '基础套餐');
|
|
}
|
|
|
|
return $headings;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param mixed $row
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function rows($rows)
|
|
{
|
|
$rows = OrderService::detailTransformer(collect($rows));
|
|
|
|
$array = [];
|
|
|
|
if (in_array($this->conditions['type'], [2, 3])) {
|
|
$basePackages = app(OrderCardPartitionRepository::class)->getCardBasePackages(collect($rows)->pluck('sim')->toArray())->keyBy('sim')->toArray();
|
|
}
|
|
|
|
foreach ($rows as $item) {
|
|
$data = [
|
|
$item['sim'],
|
|
$item['company_name'],
|
|
$item['package_name'],
|
|
$item['service_months'],
|
|
$item['pay_channel_name'],
|
|
$item['unit_price'],
|
|
$item['counts'],
|
|
$item['order_at'],
|
|
];
|
|
|
|
if (in_array($this->conditions['type'], [2, 3])) {
|
|
array_splice($data, 2, 0, PackageService::load($basePackages[$item['sim']]['package_id'])['name']);
|
|
}
|
|
|
|
$array[] = $data;
|
|
}
|
|
|
|
return $array;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function columnFormats(): array
|
|
{
|
|
return [
|
|
'A' => NumberFormat::FORMAT_NUMBER,
|
|
'F' => NumberFormat::FORMAT_NUMBER_00,
|
|
'G' => NumberFormat::FORMAT_NUMBER,
|
|
'H' => NumberFormat::FORMAT_DATE_YYYYMMDD2,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 类型
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function tag()
|
|
{
|
|
if (!isset(self::$types[$this->conditions['type']])) {
|
|
throw new NotAllowedException('类型不允许');
|
|
}
|
|
|
|
$tag = self::$types[$this->conditions['type']] . $tag;
|
|
|
|
return $tag;
|
|
}
|
|
}
|