作者 lyh

gx

<?php
/**
* @remark :
* @name :PurchaserJob.php
* @name :RecommendedSuppliers.php
* @author :lyh
* @method :post
* @time :2024/3/4 11:06
* @time :2024/3/5 11:27
*/
namespace App\Jobs;
namespace App\Console\Commands\Suppliers;
use App\Models\Com\Purchaser;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\Product\Keyword;
use App\Models\Project\Project;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class PurchaserJob implements ShouldQueue
class RecommendedSuppliers extends Command
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 3; // 可配置任务重试次数
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'recommended_suppliers';
protected $param;
/**
* The console command description.
*
* @var string
*/
protected $description = '推荐供应商';
/**
* Create a new job instance.
* Create a new command instance.
*
* @param CopyImageFile $event
* @return void
*/
public function __construct($data)
public function __construct()
{
$this->param = $data;
parent::__construct();
}
/**
* Handle the event.
*
* @param UpdateHtml $event
* @return void
* @return bool
*/
public function handle()
{
$this->param['keyword'] = 'led';
$projectModel = new Project();
$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['id'] . PHP_EOL;
ProjectServer::useProject($v['id']);
$keywordInfo = Keyword::inRandomOrder()->first();
if(empty($keywordInfo)){
continue;
}
$this->getPurchaser($v['id'],$keywordInfo['title']);
DB::disconnect('custom_mysql');
}
return true;
}
/**
* @remark :保存供应商
* @name :getPurchaser
* @author :lyh
* @method :post
* @time :2024/3/5 11:38
*/
public function getPurchaser($project_id,$keyword,$row = 10){
$url = 'https://admin.hagro.cn/api/company_list';
$data = [
'prod_desc'=>$this->param['keyword'],
'total'=>$this->param['row'] ?? 10,
'prod_desc'=>$keyword,
'total'=>$row ?? 10,
];
arsort($data);
$token = 'company_list+'.date('Y-m-d').'+'.http_build_query($data);
$param = [
'prod_desc'=>$this->param['keyword'],
'prod_desc'=>$keyword,
'token'=>md5($token),
'total'=>$this->param['row'] ?? 10,
];
$res = http_post($url,json_encode($param));
if(!empty($res) && $res['code'] == 200){
$saveData = [
'project_id'=>$this->param['project_id'],
'keyword'=>$this->param['keyword'],
'project_id'=>$project_id,
'keyword'=>$keyword,
'data'=>json_encode($res['data'])
];
$purchaserModel = new Purchaser();
... ...
... ... @@ -336,14 +336,14 @@ class ComController extends BaseController
* @time :2024/3/4 10:10
*/
public function recommendedPurchaser(){
$data = [];
$purchaserModel = new Purchaser();
$info = $purchaserModel->read(['project_id'=>$this->user['project_id']]);
if($info === false){
PurchaserJob::dispatch(['keyword'=>$this->param['keyword'] ?? 'led','row'=>$this->param['row'] ?? 10,'project_id'=>$this->user['project_id']]);
}else{
$data = json_decode($info['data']);
$lists = $purchaserModel->lists(['project_id'=>$this->user['project_id']],$this->page,$this->row);
if(!empty($lists) && !empty($lists['list'])){
foreach ($lists['list'] as $k => $v){
$v['data'] = json_decode($v['data']);
$lists['list'][$k] = $v;
}
}
$this->response('数据生成中',Code::SUCCESS,$data);
$this->response('success',Code::SUCCESS,$lists);
}
}
... ...