104 lines
2.9 KiB
PHP
104 lines
2.9 KiB
PHP
<?php
|
|
namespace App\Domains\Stats\Services;
|
|
|
|
use App\Dicts;
|
|
use App\Core\Service;
|
|
use Dipper\Excel\Facades\Excel;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use App\Domains\Stats\Exports\AbstractExport;
|
|
use App\Domains\Stats\Repositories\ExportRepository;
|
|
|
|
class ExportService extends Service
|
|
{
|
|
protected $exportRepository;
|
|
|
|
protected static $status = ['数据准备中', '开始写入', '写入结束', '保存成功', '任务失败'];
|
|
|
|
protected static $maxRow = 30000;
|
|
|
|
/**
|
|
* 构造函数
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(ExportRepository $exportRepository)
|
|
{
|
|
$this->exportRepository = $exportRepository;
|
|
}
|
|
|
|
/**
|
|
* 导出记录
|
|
*
|
|
* @param array $conditions
|
|
* @return void
|
|
*/
|
|
public function index(array $conditions = [])
|
|
{
|
|
$limit = $conditions['limit'] ?? 20;
|
|
|
|
$exports = $this->exportRepository->withConditions($conditions)->applyConditions()->paginate($limit);
|
|
|
|
$classes = AbstractExport::$classes;
|
|
|
|
foreach ($classes as $key => $value) {
|
|
$new = AbstractExport::transformerClassName($key);
|
|
$classes[$new] = $value;
|
|
unset($classes[$key]);
|
|
}
|
|
|
|
$status = app(Dicts::class)->get('export_status');
|
|
|
|
$exports->transform(function ($item) use ($classes, $status) {
|
|
$exists = Storage::disk($item->disk)->exists($item->filename);
|
|
|
|
if ($item->status === 3) {
|
|
if (!$exists = Storage::disk($item->disk)->exists($item->filename)) {
|
|
$item->status = 5;
|
|
}
|
|
}
|
|
|
|
return [
|
|
'id' => $item->id,
|
|
'tag_name' => $classes[$item['tag']] ?? '未知',
|
|
'filesize' => human_filesize($item->filesize),
|
|
'dateline' => !$item->conditions['starttime'] ? '所有' : $item->conditions['starttime'] . ' 至 ' . $item->conditions['endtime'],
|
|
'status' => $item->status,
|
|
'status_name' => $status[$item->status],
|
|
'url' => $status[$item->status] === 3 ? Storage::disk($item->disk)->url($item->filename) : '',
|
|
'created_at' => (string)$item->created_at,
|
|
'updated_at' => (string)$item->updated_at,
|
|
];
|
|
});
|
|
|
|
return $exports;
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function destroy($ids)
|
|
{
|
|
$ids = is_array($ids) ? $ids : [$ids];
|
|
|
|
$this->exportRepository->destroy($ids);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 导出
|
|
*/
|
|
public static function store($export, $disk, $total = 0)
|
|
{
|
|
if ($total < self::$maxRow) {
|
|
Excel::store($export, $export->filename, $disk);
|
|
$url = Storage::disk($disk)->url($export->filename);
|
|
return $url;
|
|
}
|
|
|
|
Excel::queue($export, $export->filename, $disk);
|
|
}
|
|
}
|