192 lines
4.7 KiB
PHP
192 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Stats\Exports;
|
|
|
|
use App\Models\Stats\Export;
|
|
use Illuminate\Support\Carbon;
|
|
use Dipper\Excel\Events\AfterStore;
|
|
use Dipper\Excel\Concerns\WithTitle;
|
|
use Dipper\Excel\Events\AfterExport;
|
|
use Dipper\Excel\Concerns\WithEvents;
|
|
use Dipper\Excel\Events\BeforeExport;
|
|
use Illuminate\Filesystem\Filesystem;
|
|
use Dipper\Excel\Events\BeforeWriting;
|
|
use Illuminate\Support\Facades\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Dipper\Excel\Concerns\ShouldAutoSize;
|
|
use App\Domains\Stats\Repositories\ExportRepository;
|
|
|
|
abstract class AbstractExport implements WithEvents, WithTitle, ShouldAutoSize
|
|
{
|
|
public static $classes = [
|
|
\App\Domains\Stats\Exports\CompanyCountExport::class => '企业统计',
|
|
];
|
|
|
|
public $sn;
|
|
|
|
public $tag;
|
|
|
|
public $filename;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->sn = $this->sn();
|
|
$this->tag = $this->tag();
|
|
$this->filename = $this->filename();
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function registerEvents(): array
|
|
{
|
|
return [
|
|
BeforeExport::class => [self::class, 'beforeExport'],
|
|
BeforeWriting::class => [self::class, 'beforeWriting'],
|
|
AfterExport::class => [self::class, 'afterExport'],
|
|
AfterStore::class => [self::class, 'afterStore'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 开始导出
|
|
*
|
|
* @param BeforeExport $event
|
|
* @return void
|
|
*/
|
|
public static function beforeExport(BeforeExport $event)
|
|
{
|
|
$data = [
|
|
'sn' => $event->getConcernable()->sn,
|
|
'tag' => $event->getConcernable()->tag,
|
|
'filename' => $event->getConcernable()->filename,
|
|
'filesize' => 0,
|
|
'conditions' => $event->getConcernable()->conditions ?: null,
|
|
'status' => 0,
|
|
'progress' => 0,
|
|
];
|
|
|
|
Export::create($data);
|
|
|
|
app(ExportRepository::class)->forgetCached();
|
|
}
|
|
|
|
/**
|
|
* 写入数据
|
|
*
|
|
* @param BeforeWriting $event
|
|
* @return void
|
|
*/
|
|
public static function beforeWriting(BeforeWriting $event)
|
|
{
|
|
Export::where(['sn' => $event->getConcernable()->sn])->update([
|
|
'status' => 1,
|
|
'progress' => 50,
|
|
]);
|
|
|
|
app(ExportRepository::class)->forgetCached();
|
|
}
|
|
|
|
/**
|
|
* 写入结束
|
|
*
|
|
* @param AfterExport $event
|
|
* @return void
|
|
*/
|
|
public static function afterExport(AfterExport $event)
|
|
{
|
|
Export::where(['sn' => $event->getConcernable()->sn])->update([
|
|
'status' => 2,
|
|
'progress' => 90,
|
|
]);
|
|
|
|
app(ExportRepository::class)->forgetCached();
|
|
}
|
|
|
|
/**
|
|
* 保存结束
|
|
*
|
|
* @param AfterStore $event
|
|
* @return void
|
|
*/
|
|
public static function afterStore(AfterStore $event)
|
|
{
|
|
$disk = Storage::disk($event->getDisk());
|
|
|
|
if ($disk->exists($event->getFilePath())) {
|
|
Export::where(['sn' => $event->getConcernable()->sn])->update([
|
|
'filesize' => $disk->size($event->getFilePath()),
|
|
'status' => 3,
|
|
'progress' => 100,
|
|
'disk' => $event->getDisk(),
|
|
]);
|
|
} else {
|
|
Export::where(['sn' => $event->getConcernable()->sn])->update([
|
|
'status' => 4,
|
|
'progress' => 100,
|
|
]);
|
|
}
|
|
|
|
app(ExportRepository::class)->forgetCached();
|
|
}
|
|
|
|
/**
|
|
* 表格标题
|
|
*
|
|
* @return string
|
|
*/
|
|
public function title(): string
|
|
{
|
|
$title = self::$classes[get_class($this)];
|
|
|
|
if (($conditions = $this->conditions) && $conditions['starttime'] && $conditions['endtime']) {
|
|
$title .= Carbon::parse($conditions['starttime'])->format('Ymd') . '-' . Carbon::parse($conditions['endtime'])->format('Ymd');
|
|
}
|
|
|
|
return $title ?? '列表';
|
|
}
|
|
|
|
/**
|
|
* 序列号
|
|
*
|
|
* @return string
|
|
*/
|
|
private function sn(): string
|
|
{
|
|
return date('YmdHis') .sprintf('%04d', explode('.', microtime(true))[1]) . sprintf('%02d', rand(0, 99));
|
|
}
|
|
|
|
/**
|
|
* 文件名称
|
|
*
|
|
* @return string
|
|
*/
|
|
private function filename(): string
|
|
{
|
|
$title = self::$classes[get_class($this)];
|
|
|
|
$filename = $title . date('YmdHis');
|
|
|
|
return "export/{$filename}.xlsx";
|
|
}
|
|
|
|
/**
|
|
* 类型
|
|
*
|
|
* @return void
|
|
*/
|
|
private function tag()
|
|
{
|
|
$className = get_class($this);
|
|
|
|
return self::transformerClassName($className);
|
|
}
|
|
|
|
public static function transformerClassName($className)
|
|
{
|
|
$baseName = array_last(explode('\\', $className));
|
|
|
|
return str_replace('Export', '', $baseName);
|
|
}
|
|
}
|