RecommendedSuppliers.php
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* @remark :
* @name :RecommendedSuppliers.php
* @author :lyh
* @method :post
* @time :2024/3/5 11:27
*/
namespace App\Console\Commands\Suppliers;
use App\Models\Product\Keyword;
use App\Models\Project\DeployBuild;
use App\Models\Purchaser\Purchaser;
use App\Models\Purchaser\PurchaserInfo;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
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 $v){
echo date('Y-m-d H:i:s') . 'project_id:'.$v['project_id'] . PHP_EOL;
ProjectServer::useProject($v['project_id']);
$title = $this->getKeywords($v['project_id']);
echo date('Y-m-d H:i:s') . '开始:'.$v['project_id'] . PHP_EOL;
$this->savePurchaser($v['project_id'],$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://fob.ai.cc/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) && isset($res['code']) && $res['code'] == 200 && !empty($res['data'])){
//保存多条数据
$saveData = [
'project_id'=>$project_id,
'keyword'=>$keyword,
'data'=>json_encode($res['data'])
];
$purchaserModel = new Purchaser();
$purchaserModel->add($saveData);
$this->savePurchaserInfo($project_id,$keyword,$res['data']);
}else{
$title = $this->getKeywords($project_id);
$this->savePurchaser($project_id,$title);
}
return true;
}
/**
* @remark :取关键词
* @name :getKeywords
* @author :lyh
* @method :post
* @time :2024/7/1 18:07
*/
public function getKeywords($project_id){
$info = Keyword::inRandomOrder()->first();
$keywordInfo = $this->getPurchaser($info->title,$project_id);
if($keywordInfo !== false){
$this->getKeywords($project_id);
}
return $info->title;
}
/**
* @remark :保存供应商详情
* @name :savePurchaserInfo
* @author :lyh
* @method :post
* @time :2024/5/29 16:38
*/
public function savePurchaserInfo($project_id,$keyword,$data){
$purchaserInfoModel = new PurchaserInfo();
foreach ($data as $k =>$v){
$v['project_id'] = $project_id;
$v['keyword'] = $keyword;
$v['email'] = json_encode($v['email'],JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$v['mobile'] = json_encode($v['mobile'],JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$v['social_media'] = json_encode($v['social_media'],JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
// $v['created_at'] = date('Y-m-d H:i:s');
// $v['updated_at'] = $v['created_at'];
$param = $v;
$info = $purchaserInfoModel->read(['keyword'=>$keyword,'buyer'=>$v['buyer'],'project_id'=>$project_id],['id']);
if($info === false){
$purchaserInfoModel->add($param);
}
}
return true;
}
}