作者 lyh

gx项目暂停

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