DownloadProject.php
2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* @remark :
* @name :CountProject.php
* @author :lyh
* @method :post
* @time :2024/9/26 14:19
*/
namespace App\Console\Commands\Project;
use App\Models\Domain\DomainInfo;
use App\Models\Project\Project;
use Illuminate\Console\Command;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class DownloadProject extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'downloads_project';
/**
* The console command description.
*
* @var string
*/
protected $description = '导出项目数据';
public function handle(){
$projectModel = new Project();
$data = $projectModel->formatQuery(['channel'=>['like','%"channel_id": "57"%'],'delete_status'=>0])->with(['deploy_optimize'])->get()->toArray();
if(!empty($data)){
$result = $this->exportData($data);
}
echo date('Y-m-d H:i:s') . ' ' . json_encode($result) . PHP_EOL;
return $result;
}
public function exportData($data){
// 创建一个新的 Excel 电子表格实例
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// 添加表头
$sheet->setCellValue('A1', '项目ID');
$sheet->setCellValue('B1', '项目名称');
$sheet->setCellValue('C1', '域名');
$sheet->setCellValue('D1', '状态');
$sheet->setCellValue('E1', '剩余服务时间');
$rowCount = 2;
// $allData = $this->countAll();
foreach ($data as $v) {
$domain = (new DomainInfo())->getDomain($v['deploy_optimize']['domain'] ?? 0);
if($v['type'] == 1){
$status = '建站中';
}elseif ($v['type'] == 2){
$status = '建站后';
}elseif ($v['type'] == 3){
$status = '优化中';
}else{
$status = '';
}
$sheet->setCellValue('A' . $rowCount, $v['id']);
$sheet->setCellValue('B' . $rowCount, $v['title']);
$sheet->setCellValue('C' . $rowCount, $domain);
$sheet->setCellValue('D' . $rowCount, $status);
$sheet->setCellValue('E' . $rowCount, $v['remain_day']);
$rowCount++;
}
// 创建一个新的 Excel Writer 对象
$writer = new Xlsx($spreadsheet);
$filename = time().'.xlsx';
// 设置导出文件的保存路径和文件名
$filePath = public_path('upload/excel/'.$filename);
// 导出 Excel 文件
$writer->save($filePath);
// 返回导出文件的响应
return ['file_link'=>url('upload/excel/'.$filename)];
}
}