DownloadProject.php 3.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 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)];
    }
}