FetchTicketProjects.php 4.8 KB
<?php

namespace App\Console\Commands\WorkOrder;

use App\Models\Manage\Manage;
use App\Models\Project\Project;
use App\Models\WorkOrder\TicketProject;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

class FetchTicketProjects extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'workorder:fetch-ticket-projects {version}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '同步V5,V6的项目到 gl_ticket_projects 表';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $version = $this->argument('version');
        if ($version == 'v5') {
            $this->fetch_v5();
        } elseif ($version == 'v6') {
            $this->fetch_v6();
        } else {
            $this->error('Invalid action. Use "v5" or "v6".');
            return 1;
        }
        return 0;
    }


    /**
     * @return void
     * 请求:https://www.quanqiusou.cn/extend_api/webs/globalso_all.php
     */
    public function fetch_v5()
    {
        # pm 项目经理  assm 售后服务经理
        $response = Http::get('https://www.quanqiusou.cn/extend_api/webs/globalso_all.php');
        if ($response->status() == 200) {
            $items = $response->json();
            foreach ($items as $item) {
                # V5: 版本号+postid
                $uuid = md5("V5{$item['postid']}");
                $project = TicketProject::where('uuid', $uuid)->first();

                $item['pm'] = $item['pm'] == '未安排' ? '杨长远' : $item['pm'];
                $item['assm'] = $item['assm'] == '未安排' ? '杨长远' : $item['assm'];

                // 如果 $item['cate'] 包含”推广“字符,则$engineer_name = $item['assm']
                $engineer_name = (strpos($item['cate'], '推广') !== false) ? $item['assm'] : $item['pm'];

                $fields = [
                    'post_id'        => $item['postid'],
                    'company_name'   => $item['company'],
                    'title'          => $item['title'],
                    'engineer_id'   => Manage::where('name', $engineer_name)->value('id') ?? 0,
                    'website'        => $item['main_url'] ?? '',
                ];
                if (!$project) {
                    $new = new TicketProject();
                    $new->uuid = $uuid;
                    $new->version = 5;
                    $new->table_id = 0;
                    foreach ($fields as $k => $v) {
                        $new->$k = $v;
                    }
                    $new->save();
                } else {
                    $changed = false;
                    foreach ($fields as $k => $v) {
                        if ($project->$k != $v) {
                            $project->$k = $v;
                            $changed = true;
                        }
                    }
                    if ($changed) {
                        $project->save();
                    }
                }
                echo "V5: {$item['postid']} - {$item['title']} - {$item['company']} - {$item['main_url']}\n";
            }
        }
    }

    /**
     * @return void
     * 1. 按照ID升序查询 gl_project 表 limit 10
     * 2。同步到 TicketProject 后,redis 缓存 ID
     */
    public function  fetch_v6()
    {
        while (true) {
            try {
                $lastid = Cache::store('redis')->get('fetch_v6_lastid', 0);
                $items = Project::where('id', '>', intval($lastid))
                    ->orderBy('id', 'asc')
                    ->limit(10)
                    ->get();
                if ($items->isEmpty()) {
                    echo "not found items \n";
                    break;
                }
                foreach ($items as $item) {
                    $uuid = md5("V5{$item->id}");
                    $project = TicketProject::where('uuid', $uuid)->first();
                    if (!$project) {
                        $project = new TicketProject();
                        $project->uuid = $uuid;
                        $project->post_id = $item->post_id;
                        $project->version = 6;
                        $project->table_id = $item->id;
                        $project->save();
                        Cache::store('redis')->put('fetch_v6_lastid', $item->id);
                    }
                    echo "V6: {$item->company}\n";
                }
            }catch (\Exception $exception) {
                echo $exception;
                break;
            }
        }

    }
}