CreatedUploadImageJob.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\jobs;
  8. use app\models\Goods;
  9. use app\models\UploadGoodsImage;
  10. use yii\base\BaseObject;
  11. use yii\queue\JobInterface;
  12. /**
  13. * 以图搜图-上传图片
  14. */
  15. class CreatedUploadImageJob extends BaseObject implements JobInterface
  16. {
  17. public int $store_id;
  18. public function execute($queue){
  19. // 1.获取已上传过的商品ID
  20. $goodsId = UploadGoodsImage::find()->where(['store_id'=>$this->store_id,'is_delete'=>0])->select('goods_id')->column();
  21. // 2.获取未上传的商品
  22. $needUploadGoods = Goods::find()->where(['store_id'=>$this->store_id,'is_delete'=>0,'status'=>1,'product_type'=>0])->andWhere(['not in','id',$goodsId])->limit(1000)->select('id,cover_pic,name,store_id')->asArray()->all();
  23. if(count($needUploadGoods) <= 0){
  24. // 没有要需要上传的图片
  25. return true;
  26. }
  27. // 组装 写入数据库的数据
  28. $insertSql = '';
  29. foreach($needUploadGoods as $item){
  30. $str = "INSERT INTO ".UploadGoodsImage::tableName()."(`store_id`,`goods_id`,`pic_url`,`status`,`image_name`,`created_at`) VALUES (".$this->store_id.",".$item['id'].",'".$item['cover_pic']."',0,'".$item['id']."-".$item['name']."',".time().");";
  31. $insertSql .= $str;
  32. }
  33. $connection = \Yii::$app->db;
  34. $command = $connection->createCommand($insertSql);
  35. $result = $command->query();
  36. // 写入真正的上传图片队列
  37. foreach($needUploadGoods as $value){
  38. queue_push(new UplaodImageJob($value));
  39. }
  40. \Yii::error('队列执行,写入数据 => ' . count($result) . '条');
  41. }
  42. }