| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\jobs;
- use app\models\Goods;
- use app\models\UploadGoodsImage;
- use yii\base\BaseObject;
- use yii\queue\JobInterface;
- /**
- * 以图搜图-上传图片
- */
- class CreatedUploadImageJob extends BaseObject implements JobInterface
- {
- public int $store_id;
- public function execute($queue){
- // 1.获取已上传过的商品ID
- $goodsId = UploadGoodsImage::find()->where(['store_id'=>$this->store_id,'is_delete'=>0])->select('goods_id')->column();
- // 2.获取未上传的商品
- $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();
- if(count($needUploadGoods) <= 0){
- // 没有要需要上传的图片
- return true;
- }
- // 组装 写入数据库的数据
- $insertSql = '';
- foreach($needUploadGoods as $item){
- $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().");";
- $insertSql .= $str;
- }
- $connection = \Yii::$app->db;
- $command = $connection->createCommand($insertSql);
- $result = $command->query();
- // 写入真正的上传图片队列
- foreach($needUploadGoods as $value){
- queue_push(new UplaodImageJob($value));
- }
- \Yii::error('队列执行,写入数据 => ' . count($result) . '条');
- }
- }
|