ProjectFilePDF.php 4.7 KB
<?php

namespace App\Console\Commands\GeneratePDF;

use App\Models\File\PdfFile;
use App\Models\ProjectAssociation\ProjectAssociation;
use App\Services\CosService;
use App\Services\ProjectServer;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Console\Command;
use Illuminate\Http\File;
use Illuminate\Support\Facades\DB;

class ProjectFilePDF extends Command
{
//    use CmdSignal;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'project_file_pdf';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '网站项目数据,生成PDF文件';

    protected $time;

    protected $CosService;

    // 最大支持5个进程
    public $maxRunNumber = 50;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->time       = date("Y-m");
        $this->CosService = new CosService();
        parent::__construct();
    }

    public function handle()
    {
        // 开始时间
        $startTime = microtime(true);

        $gg = $this->main();

        // 结束时间
        $endTime = microtime(true);

        // 计算执行时间
        $executionTime = ($endTime - $startTime);

        // 输出执行时间
        var_dump("程序执行时间: " . $executionTime . " 秒");

        return $gg;

    }

    /**
     * @return int
     */
    public function start(): int
    {
        return $this->main();
    }

    /**
     * @return int
     */
    protected function main()
    {
        $list = PdfFile::query()->whereIsPdf(PdfFile::GENERATE_NOT_PDF)->first();

        if (is_null($list)) {
            $this->error('没有任务,等待中');
            sleep(60);
            return 0;
        }
        $pid = $list->pid;
        if (empty($pid)) {
            $this->error('数据错误');
            return 0;
        }
        $isExists = ProjectAssociation::query()->whereId($pid)->whereStatus(ProjectAssociation::STATUS_NORMAL)->first();
        if (is_null($isExists)) {
            $this->error('数据已被禁用 —— ' . $pid);
            return 0;
        }

        $key        = 'generate_pdf_' . $list->id;
        $count      = (int)redis_get($key) ?: 0;
        $project_id = $isExists->project_id;
        $user_id    = $isExists->user_id;
        $friend_id  = $isExists->friend_id;
        // todo 根据项目查询数据
        $project_data = [];
        //获取当前数据详情
        $res = ProjectServer::useProject($project_id);
        if ($res) {
            $project_data = [];
        }
        DB::disconnect('custom_mysql');
        $html     = $this->html($project_data);
        $filename = hash('md5', $this->time . '-' . $project_id . '-' . $friend_id . '-' . $user_id);

        if ($count == 2) {
            $list->is_pdf = PdfFile::GENERATE_OTHER_PDF;
            $list->save();
            $this->error('项目文件数据保存失败! - 其他原因 - ' . $key);
            return 0;
        }

        $file_path = $this->savePDF($html, $filename);
        if (empty($file_path)) {
            redis_set($key, $count + 1);
            $this->error('pdf生成失败!');
            return 0;
        }

        // 保存文件路径
        $list->file_path = $file_path;
        $list->is_pdf    = PdfFile::GENERATE_PDF;
        $list->save();
        $this->info('项目文件数据保存成功!');
        return 0;
    }


    /**
     * @param $html
     * @param $filename
     * @return string
     */
    public function savePDF($html, $filename)
    {
        $pdf_path = public_path('PDF/');
        if (!file_exists($pdf_path)) {
            mkdir($pdf_path, 0777, true);
        }
        // 指定保存路径和文件名
        $savePath = $pdf_path . $filename . '.pdf';

        if (file_exists($savePath)) {
            echo '文件已经存在';
            return 0;
        }

        $path = '/PDF/' . $this->time;
        $pdf  = PDF::loadHTML($html);

        // 获取二进制文件
        $binary = $pdf->output(['compres' => 0]);
        // 将文件保存到本地
//        $pdf->save($savePath);
//        $binary = new File($savePath);

        return $this->CosService->uploadFile($binary, $path, $filename . '.pdf', true);
    }

    /**
     * 根据数据生成 Html
     * @param $item
     * @return string
     */
    protected function html($item)
    {
        $html = @file_get_contents(dirname(__FILE__) . '/pdf_template.html');
        if (empty($html)) {
            return '';
        }
        $rep = [
            'ENTRY_NAME' => '测试项目',
            'NUMBER'     => 1000,
        ];
        return str_replace(array_keys($rep), array_values($rep), $html);
    }
}