multi-store/app/queue/redis/CopyProductSend.php

48 lines
1.6 KiB
PHP

<?php
namespace app\queue\redis;
use app\admin\logic\store_product\StoreProductLogic;
use app\common\model\store_product\StoreProduct;
use app\common\model\system_store_storage\SystemStoreStorage;
use Webman\RedisQueue\Consumer;
use support\Log;
/**
* 复制商品
*/
class CopyProductSend implements Consumer
{
// 要消费的队列名
public $queue = 'copy-product';
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
public $connection = 'default';
// 消费
public function consume($data)
{
$store_arr=$data['store_arr'];
$stock=$data['stock']??0;
$admin_id=$data['admin_id']??0;
foreach ($store_arr as $key => $id) {
StoreProductLogic::copy($data['product_id'],$id,0);
if($stock>0){
$stocks=StoreProduct::where('id',$data['product_id'])->value('stock');
if($stocks>=$stock){
SystemStoreStorage::create(['product_id'=>$data['product_id'],'store_id'=>$id,'nums'=>$stock,'admin_id'=>$admin_id]);//设置库存
StoreProduct::where('id',$data['product_id'])->dec('stock',$stock)->update();//减少库存
}else{
SystemStoreStorage::create(['product_id'=>$data['product_id'],'store_id'=>$id,'nums'=>$stock,'admin_id'=>$admin_id,'status'=>-1,'mark'=>'库存不足']);//设置库存
}
}
}
}
public function onConsumeFailure(\Throwable $e, $package)
{
$package['max_attempts']=0;
Log::error('复制商品失败product_id:'.$package['data']['product_id']);
return $package;
}
}