PushAiccData.php 2.9 KB
<?php

namespace App\Console\Commands\GeneratePDF;

use App\Models\File\PdfFile;
use App\Models\ProjectAssociation\ProjectAssociation;
use Illuminate\Console\Command;

class PushAiccData extends Command
{
//    use CmdSignal;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '向AICC推送数据';

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

    protected $time;

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


    public function handle()
    {
        return $this->main();
    }

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

    protected function main()
    {
        $list = PdfFile::query()->whereIsPdf(PdfFile::GENERATE_PUSH)->whereIsPush(PdfFile::GENERATE_NOT_PUSH)->first();
        if (is_null($list)) {
            $this->error('向AICC推送 - 没有任务,等待中');
            sleep(60);
            return 0;
        }

        $pid = $list->pid;
        if (empty($pid)) {
            $this->error('向AICC推送数据错误');
            return 0;
        }
        $isExists = ProjectAssociation::query()->whereId($pid)->whereStatus(ProjectAssociation::STATUS_NORMAL)->first();
        if (is_null($isExists)) {
            $this->error('向AICC推送数据已被禁用 —— ' . $pid);
            return 0;
        }

        $key   = 'push_data_to_aicc_' . $list->id;
        $count = (int)redis_get($key) ?: 0;

        $data               = $list->toArray();
        $data['project_id'] = $isExists->project_id;
        $data['friend_id']  = $isExists->friend_id;
        $data['user_id']    = $isExists->user_id;

        $url         = env('AICC_URL') . env('AICC_PUSH_API_URL');
        $msg         = http_post($url, json_encode(compact('data')));
        $status_code = 0;
        if ($msg) {
            $status_code = (int)$msg['status'];
        }
        if ($count == 2) {
            $list->is_push = PdfFile::GENERATE_OTHER_PUSH;
            $list->save();
            $this->debug_echo('项目文件数据保存失败! - 其他原因 - ' . $key);
            return 0;
        }

        if ($status_code != 200) {
            redis_set($key, $count + 1);
            $this->debug_echo('项目文件数据保存失败! - ' . $key);
            return 0;
        }
        $list->is_push = PdfFile::GENERATE_PUSH;
        $list->save();
        $this->info('项目文件数据保存成功!');
        return 0;
    }

    public function post_data($data)
    {
        $url = env('AICC_URL') . env('AICC_PUSH_API_URL');
        $msg = http_post($url, json_encode(compact('data')));
        print_r($msg);
    }

}