RecommendedSuppliers.php 3.1 KB
<?php
/**
 * @remark :
 * @name   :RecommendedSuppliers.php
 * @author :lyh
 * @method :post
 * @time   :2024/3/5 11:27
 */

namespace App\Console\Commands\Suppliers;

use App\Models\Com\Purchaser;
use App\Models\Product\Keyword;
use App\Models\Project\DeployBuild;
use App\Models\Project\Project;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '推荐供应商';

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


    /**
     * @return bool
     */
    public function handle()
    {
        $projectModel = new DeployBuild();
        $project_list = $projectModel->list(['is_supplier'=>1]);//TODO::已开启推荐供应商
        foreach ($project_list as $k => $v){
            echo date('Y-m-d H:i:s') . 'project_id:'.$v['project_id'] . PHP_EOL;
            ProjectServer::useProject($v['project_id']);
            $info = Keyword::inRandomOrder()->first();
            if(empty($info)){
               continue;
            }
            $keywordInfo = $this->getPurchaser($info->title,$v['project_id']);
            if($keywordInfo !== false){
                continue;
            }
            echo date('Y-m-d H:i:s') . '开始:'.$v['project_id'] . PHP_EOL;
            $this->savePurchaser($v['project_id'],$info->title);
            DB::disconnect('custom_mysql');
        }
        return true;
    }

    public function getPurchaser($keyword,$project_id){
        $purchaserModel = new Purchaser();
        return $purchaserModel->read(['keyword'=>$keyword,'project_id'=>$project_id]);
    }

    /**
     * @remark :保存供应商
     * @name   :getPurchaser
     * @author :lyh
     * @method :post
     * @time   :2024/3/5 11:38
     */
    public function savePurchaser($project_id,$keyword,$row = 10){
        $url = 'https://admin.hagro.cn/api/company_list';
        $data = [
            'prod_desc'=>$keyword,
            'total'=>$row ?? 10,
        ];
        ksort($data);
        $token = 'company_list+'.date('Y-m-d').'+'.http_build_query($data);
        echo date('Y-m-d H:i:s') . '加密token:'.md5($token) . PHP_EOL;
        $param = [
            'prod_desc'=>$keyword,
            'token'=>md5($token),
            'total'=>$this->param['row'] ?? 10,
        ];
        $res = http_post($url,json_encode($param));
        echo date('Y-m-d H:i:s') . json_encode($res) . PHP_EOL;
        if(!empty($res) && $res['code'] == 200){
            $saveData = [
                'project_id'=>$project_id,
                'keyword'=>$keyword,
                'data'=>json_encode($res['data'])
            ];
            $purchaserModel = new Purchaser();
            $purchaserModel->add($saveData);
        }
        return true;
    }
}