DownloadProject.php
3.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?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 App\Models\WebSetting\WebSettingSeo;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
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 handle(){
$data = [];
$projectModel = new Project();
$projectList = $projectModel->formatQuery(['delete_status'=>0,'type'=>['in',[2,3]]])->with(['deploy_optimize'])->select(['id','status','type','title','remain_day'])->get()->toArray();;
foreach ($projectList as $v){
ProjectServer::useProject($v['id']);
$seoModel = new WebSettingSeo();
$seoInfo = $seoModel->read(['project_id'=>$v['id']]);
if($seoInfo === false){
$data[] = $v;
}else{
if(empty($seoInfo['single_page_suffix'])){
$data[] = $v;
}
}
DB::disconnect('custom_mysql');
}
return $this->exportData($data);
}
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);
echo date('Y-m-d H:i:s') . 'file_link:'.url('upload/excel/'.$filename) . PHP_EOL;
// 返回导出文件的响应
return ['file_link'=>url('upload/excel/'.$filename)];
}
}