<?php
/**
 * Created by PhpStorm.
 * User: zhl
 * Date: 2023/2/7
 * Time: 17:58
 */
namespace App\Console\Commands\Test;

use App\Models\Devops\ServerConfig;
use App\Services\ProjectServer;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class Demo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'demo';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'demo';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @return bool
     */
    public function handle()
    {
        //切换数据库配置
        $project = ProjectServer::useProject(1);
        //创建数据库
        ProjectServer::createDatabase($project);
        //创建表
        ProjectServer::initTable($project);

        dd(1);

        $sql = 'CREATE DATABASE database_name;';
        $results = DB::select($sql);
        dd($results);
        return true;
    }

    public function printMessage()
    {
        $client = new Client();
        $headers = [
            'Accept-Language' => 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
            'Cache-Control' => 'no-cache',
            'Content-Type' => 'application/json',
            'DNT' => '1',
            'Origin' => 'http://openai.waimaoq.com',
            'Pragma' => 'no-cache',
            'Proxy-Connection' => 'keep-alive',
            'Referer' => 'http://openai.waimaoq.com/docs',
            'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
            'accept' => 'application/json',
            'Access-Control-Allow-Origin' => '*'
        ];
        $body = '{
                "prompt": "Human: 我需要一篇100字的英文原创博客并包含标题,内容结合:“cnc machine”。AI:"
            }';
        $response = $client->post('http://openai.waimaoq.com/v1/openai_chat_stream', [
            'stream' => true,
            'headers' => $headers,
            'body' => $body
        ]);
        // 获取响应流对象
        $stream = $response->getBody();

        // 设置输出缓冲区
        ob_start();

        // 读取流中的数据并输出到页面
        while (!$stream->eof()) {
            echo $stream->read(4);
            ob_flush();
            flush();
        }
        dd(1);
    }
}