diff --git a/app/common/dao/BaseDao.php b/app/common/dao/BaseDao.php index 36f04a97..ba6b1480 100644 --- a/app/common/dao/BaseDao.php +++ b/app/common/dao/BaseDao.php @@ -307,4 +307,16 @@ abstract class BaseDao $query->where('is_del', $isDel); })->count($this->getPk()) > 0; } + + /** + * 批量新增或更新 + * @param $list + * @return Collection + * @throws \Exception + */ + public function saveAll($list) + { + return ($this->getModel()::getInstance())->saveAll($list); + } + } diff --git a/app/common/repositories/store/product/ProductRepository.php b/app/common/repositories/store/product/ProductRepository.php index ee13c430..f8f28b8c 100644 --- a/app/common/repositories/store/product/ProductRepository.php +++ b/app/common/repositories/store/product/ProductRepository.php @@ -285,7 +285,7 @@ class ProductRepository extends BaseRepository app()->make(SpuRepository::class)->baseUpdate($spuData, $id, 0, $productType); event('product.update',compact('id')); if ($data['status'] == 0) { - event('product.sell', ['source_mer_id' => $merId, 'product_id' => $id, 'status' => $data['status']]); + event('product.sell', ['source_mer_id' => $merId, 'product_id' => [$id], 'status' => $data['status']]); } app()->make(SpuRepository::class)->changeStatus($id, $productType); }); @@ -1571,14 +1571,8 @@ class ProductRepository extends BaseRepository Db::rollback(); throw new ValidateException('商品spu更新出错'); }else{ - event('product.sell', ['source_mer_id' => $merId, 'product_id' => $id, 'status' => $status]); if ($product->product_type==0){ - $RedisCacheService = app()->make(RedisCacheService::class); - if ($status==1){ - $RedisCacheService->SADD ('CloudMerchanSpu'.$product['mer_id'], $product['product_id']); - }else{ - $RedisCacheService->SREM ('CloudMerchanSpu'.$product['mer_id'], $product['product_id']); - } + event('product.sell', ['source_mer_id' => $merId, 'product_id' => [$id], 'status' => $status]); } } Db::commit(); @@ -1600,20 +1594,20 @@ class ProductRepository extends BaseRepository if ($status == 1 && $product['product_type'] == 3) throw new ValidateException('ID:'.$product->product_id . ' 商品正在参与助力活动'); } - $this->dao->updates($id,[$field => $status]); - if ($product->product_type==0){ - $RedisCacheService = app()->make(RedisCacheService::class); - if ($status==1){ - foreach ($id as $k=>$v){ - $RedisCacheService->SADD ('CloudMerchanSpu'.$product['mer_id'], $v); - } - }else{ - foreach ($id as $k=>$v){ - $RedisCacheService->SREM ('CloudMerchanSpu'.$product['mer_id'], $v); - } + Db::startTrans(); + try { + if ($this->dao->updates($id,[$field => $status]) === false) { + throw new \Exception('商品操作出错'); } + if ($product->product_type==0){ + event('product.sell', ['source_mer_id' => $merId, 'product_id' => $id, 'status' => $status]); + } + Db::commit(); + Queue::push(ChangeSpuStatusJob::class,['id' => $id,'product_type'=> $product_type]); + } catch (\Exception $e) { + Db::rollback(); + throw new ValidateException($e->getMessage()); } - Queue::push(ChangeSpuStatusJob::class,['id' => $id,'product_type'=> $product_type]); } /** diff --git a/app/listener/CloudProduct.php b/app/listener/CloudProduct.php index db06db2a..7276a92a 100644 --- a/app/listener/CloudProduct.php +++ b/app/listener/CloudProduct.php @@ -6,17 +6,35 @@ namespace app\listener; use app\common\dao\store\product\CloudProductDao; use app\common\model\system\merchant\Merchant; +use crmeb\services\RedisCacheService; class CloudProduct { public function handle($event) { - $merchant = Merchant::getDB()->where('mer_id', $event['source_mer_id'])->find(); - $cloudMerchant = Merchant::getDB()->where(['type_id' => Merchant::TypeCloudWarehouse, 'category_id' => $merchant->category_id, 'status' => 1, 'mer_state' => 1])->value('mer_id'); - $where = ['mer_id' => $cloudMerchant, 'source_mer_id' => $event['source_mer_id'], 'product_id' => $event['product_id']]; - $cloudProduct = app()->make(CloudProductDao::class)->findOrCreate($where); - $cloudProduct->save(['status' => $event['status']]); + $categoryId = Merchant::getDB()->where('mer_id', $event['source_mer_id'])->value('category_id'); + $cloudMerchant = Merchant::getDB()->where(['type_id' => Merchant::TypeCloudWarehouse, 'category_id' => $categoryId, 'status' => 1, 'mer_state' => 1])->value('mer_id'); + if ($cloudMerchant) { + $list = []; + foreach ($event['product_id'] as $productId) { + $list[] = [ + 'mer_id' => $cloudMerchant, + 'source_mer_id' => $event['source_mer_id'], + 'product_id' => $productId, + 'status' => $event['status'], + ]; + } + app()->make(CloudProductDao::class)->saveAll($list); + $RedisCacheService = app()->make(RedisCacheService::class); + foreach ($event['product_id'] as $productId) { + if ($event['status'] == 1) { + $RedisCacheService->SADD('CloudMerchantSpu' . $cloudMerchant, $productId); + } else { + $RedisCacheService->SREM('CloudMerchantSpu' . $cloudMerchant, $productId); + } + } + } } }