121 lines
3.0 KiB
PHP
121 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Stats\Exports;
|
|
|
|
use App\Dicts;
|
|
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 Dipper\Excel\Concerns\WithColumnFormatting;
|
|
use App\Domains\Virtual\Services\CompanyService;
|
|
use App\Domains\Virtual\Services\PackageService;
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
|
use App\Domains\Stats\Services\CompanyReportService;
|
|
use App\Domains\Virtual\Repositories\OrderCardPartitionRepository;
|
|
|
|
class CompanyReportDetailExport 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()
|
|
{
|
|
$repository = app(OrderCardPartitionRepository::class);
|
|
|
|
$select = [
|
|
'sim',
|
|
'company_id',
|
|
'package_id',
|
|
'order_id',
|
|
'type',
|
|
];
|
|
|
|
$builder = $repository->select($select)
|
|
->withConditions($this->conditions)
|
|
->orderBy('id', 'desc');
|
|
|
|
return $builder;
|
|
}
|
|
|
|
public function headings(): array
|
|
{
|
|
return [
|
|
'SIM',
|
|
'运营商',
|
|
'企业名称',
|
|
'套餐名称',
|
|
'套餐单价(元/月)',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param mixed $row
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function rows($rows)
|
|
{
|
|
$carrierOperators = app(Dicts::class)->get('carrier_operator');
|
|
|
|
$rows->load('order:id,unit_price');
|
|
|
|
$rows->transform(function ($item) use ($carrierOperators) {
|
|
$company = app(CompanyService::class)->load($item->company_id);
|
|
$package = app(PackageService::class)->load($item->package_id);
|
|
$month_price = $item->order['unit_price'] / $package['service_months'];
|
|
|
|
return [
|
|
$item['sim'],
|
|
$carrierOperators[$package['carrier_operator']],
|
|
$company['name'],
|
|
$package['name'],
|
|
$month_price,
|
|
];
|
|
});
|
|
|
|
return $rows;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function columnFormats(): array
|
|
{
|
|
return [
|
|
'A' => NumberFormat::FORMAT_NUMBER,
|
|
'E' => NumberFormat::FORMAT_NUMBER_00,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 类型
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function tag()
|
|
{
|
|
if ((!$tag = self::$classes[get_class($this)]) || !self::$types[$this->conditions['type'][0]]) {
|
|
throw new NotAllowedException('类型不允许');
|
|
}
|
|
|
|
$tag = self::$types[$this->conditions['type'][0]] . $tag;
|
|
|
|
return $tag;
|
|
}
|
|
}
|