作者 lyh

gx

1 <?php 1 <?php
2 /** 2 /**
3 * @remark : 3 * @remark :
4 - * @name :PurchaserJob.php 4 + * @name :RecommendedSuppliers.php
5 * @author :lyh 5 * @author :lyh
6 * @method :post 6 * @method :post
7 - * @time :2024/3/4 11:06 7 + * @time :2024/3/5 11:27
8 */ 8 */
9 9
10 -namespace App\Jobs; 10 +namespace App\Console\Commands\Suppliers;
11 11
12 use App\Models\Com\Purchaser; 12 use App\Models\Com\Purchaser;
13 -use Illuminate\Bus\Queueable;  
14 -use Illuminate\Contracts\Queue\ShouldQueue;  
15 -use Illuminate\Foundation\Bus\Dispatchable;  
16 -use Illuminate\Queue\InteractsWithQueue;  
17 -use Illuminate\Queue\SerializesModels; 13 +use App\Models\Product\Keyword;
  14 +use App\Models\Project\Project;
  15 +use App\Services\ProjectServer;
  16 +use Illuminate\Console\Command;
  17 +use Illuminate\Support\Facades\DB;
  18 +use Illuminate\Support\Facades\Log;
18 19
19 -class PurchaserJob implements ShouldQueue 20 +class RecommendedSuppliers extends Command
20 { 21 {
21 - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;  
22 - public $tries = 3; // 可配置任务重试次数 22 + /**
  23 + * The name and signature of the console command.
  24 + *
  25 + * @var string
  26 + */
  27 + protected $signature = 'recommended_suppliers';
23 28
24 - protected $param; 29 + /**
  30 + * The console command description.
  31 + *
  32 + * @var string
  33 + */
  34 + protected $description = '推荐供应商';
25 35
26 /** 36 /**
27 - * Create a new job instance. 37 + * Create a new command instance.
28 * 38 *
29 - * @param CopyImageFile $event  
30 * @return void 39 * @return void
31 */ 40 */
32 - public function __construct($data) 41 + public function __construct()
33 { 42 {
34 - $this->param = $data; 43 + parent::__construct();
35 } 44 }
36 45
  46 +
37 /** 47 /**
38 - * Handle the event.  
39 - *  
40 - * @param UpdateHtml $event  
41 - * @return void 48 + * @return bool
42 */ 49 */
43 public function handle() 50 public function handle()
44 { 51 {
45 - $this->param['keyword'] = 'led'; 52 + $projectModel = new Project();
  53 + $project_list = $projectModel->list(['is_supplier'=>1]);//TODO::已开启推荐供应商
  54 + foreach ($project_list as $k => $v){
  55 + echo date('Y-m-d H:i:s') . 'project_id:'.$v['id'] . PHP_EOL;
  56 + ProjectServer::useProject($v['id']);
  57 + $keywordInfo = Keyword::inRandomOrder()->first();
  58 + if(empty($keywordInfo)){
  59 + continue;
  60 + }
  61 + $this->getPurchaser($v['id'],$keywordInfo['title']);
  62 + DB::disconnect('custom_mysql');
  63 + }
  64 + return true;
  65 + }
  66 +
  67 +
  68 + /**
  69 + * @remark :保存供应商
  70 + * @name :getPurchaser
  71 + * @author :lyh
  72 + * @method :post
  73 + * @time :2024/3/5 11:38
  74 + */
  75 + public function getPurchaser($project_id,$keyword,$row = 10){
46 $url = 'https://admin.hagro.cn/api/company_list'; 76 $url = 'https://admin.hagro.cn/api/company_list';
47 $data = [ 77 $data = [
48 - 'prod_desc'=>$this->param['keyword'],  
49 - 'total'=>$this->param['row'] ?? 10, 78 + 'prod_desc'=>$keyword,
  79 + 'total'=>$row ?? 10,
50 ]; 80 ];
51 arsort($data); 81 arsort($data);
52 $token = 'company_list+'.date('Y-m-d').'+'.http_build_query($data); 82 $token = 'company_list+'.date('Y-m-d').'+'.http_build_query($data);
53 $param = [ 83 $param = [
54 - 'prod_desc'=>$this->param['keyword'], 84 + 'prod_desc'=>$keyword,
55 'token'=>md5($token), 85 'token'=>md5($token),
56 'total'=>$this->param['row'] ?? 10, 86 'total'=>$this->param['row'] ?? 10,
57 ]; 87 ];
58 $res = http_post($url,json_encode($param)); 88 $res = http_post($url,json_encode($param));
59 if(!empty($res) && $res['code'] == 200){ 89 if(!empty($res) && $res['code'] == 200){
60 $saveData = [ 90 $saveData = [
61 - 'project_id'=>$this->param['project_id'],  
62 - 'keyword'=>$this->param['keyword'], 91 + 'project_id'=>$project_id,
  92 + 'keyword'=>$keyword,
63 'data'=>json_encode($res['data']) 93 'data'=>json_encode($res['data'])
64 ]; 94 ];
65 $purchaserModel = new Purchaser(); 95 $purchaserModel = new Purchaser();
@@ -336,14 +336,14 @@ class ComController extends BaseController @@ -336,14 +336,14 @@ class ComController extends BaseController
336 * @time :2024/3/4 10:10 336 * @time :2024/3/4 10:10
337 */ 337 */
338 public function recommendedPurchaser(){ 338 public function recommendedPurchaser(){
339 - $data = [];  
340 $purchaserModel = new Purchaser(); 339 $purchaserModel = new Purchaser();
341 - $info = $purchaserModel->read(['project_id'=>$this->user['project_id']]);  
342 - if($info === false){  
343 - PurchaserJob::dispatch(['keyword'=>$this->param['keyword'] ?? 'led','row'=>$this->param['row'] ?? 10,'project_id'=>$this->user['project_id']]);  
344 - }else{  
345 - $data = json_decode($info['data']); 340 + $lists = $purchaserModel->lists(['project_id'=>$this->user['project_id']],$this->page,$this->row);
  341 + if(!empty($lists) && !empty($lists['list'])){
  342 + foreach ($lists['list'] as $k => $v){
  343 + $v['data'] = json_decode($v['data']);
  344 + $lists['list'][$k] = $v;
  345 + }
346 } 346 }
347 - $this->response('数据生成中',Code::SUCCESS,$data); 347 + $this->response('success',Code::SUCCESS,$lists);
348 } 348 }
349 } 349 }