DownloadProject.php 2.7 KB
<?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)];
    }
}