From 913291f95f4c09ee6c744a47ee705505f721f949 Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Tue, 23 May 2023 14:59:47 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BA=91=E4=BB=93?= =?UTF-8?q?=E5=95=86=E5=93=81=E5=85=B3=E8=81=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/store/product/CloudProductDao.php | 30 +++++++++++++++++++ .../model/store/product/CloudProduct.php | 30 +++++++++++++++++++ app/common/model/system/merchant/Merchant.php | 5 ++++ .../store/product/ProductRepository.php | 5 ++-- app/controller/api/server/StoreProduct.php | 3 ++ app/event.php | 1 + app/listener/CloudProduct.php | 22 ++++++++++++++ 7 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 app/common/dao/store/product/CloudProductDao.php create mode 100644 app/common/model/store/product/CloudProduct.php create mode 100644 app/listener/CloudProduct.php diff --git a/app/common/dao/store/product/CloudProductDao.php b/app/common/dao/store/product/CloudProductDao.php new file mode 100644 index 00000000..bc0f6e8e --- /dev/null +++ b/app/common/dao/store/product/CloudProductDao.php @@ -0,0 +1,30 @@ + +// +---------------------------------------------------------------------- + +namespace app\common\dao\store\product; + +use app\common\dao\BaseDao; +use app\common\model\store\product\CloudProduct; + +class CloudProductDao extends BaseDao +{ + + protected function getModel(): string + { + return CloudProduct::class; + } + + public function search(array $where) + { + } + +} diff --git a/app/common/model/store/product/CloudProduct.php b/app/common/model/store/product/CloudProduct.php new file mode 100644 index 00000000..27239f36 --- /dev/null +++ b/app/common/model/store/product/CloudProduct.php @@ -0,0 +1,30 @@ + +// +---------------------------------------------------------------------- + +namespace app\common\model\store\product; + +use app\common\model\BaseModel; + +class CloudProduct extends BaseModel +{ + + public static function tablePk(): string + { + return 'mer_id'; + } + + public static function tableName(): string + { + return 'cloud_product'; + } + +} diff --git a/app/common/model/system/merchant/Merchant.php b/app/common/model/system/merchant/Merchant.php index 4881e983..451e8688 100644 --- a/app/common/model/system/merchant/Merchant.php +++ b/app/common/model/system/merchant/Merchant.php @@ -28,6 +28,11 @@ use app\common\repositories\store\StoreActivityRepository; class Merchant extends BaseModel { + const TypeStore = 10; //镇街店铺 + const TypeCloudWarehouse = 11; //里海云仓 + const TypeSupplyChain = 12; //市级供应链 + const TypePlatform = 13; //供销平台 + /** * @return string * @author xaboy diff --git a/app/common/repositories/store/product/ProductRepository.php b/app/common/repositories/store/product/ProductRepository.php index dda43ccb..ee1f7a11 100644 --- a/app/common/repositories/store/product/ProductRepository.php +++ b/app/common/repositories/store/product/ProductRepository.php @@ -241,8 +241,8 @@ class ProductRepository extends BaseRepository $product['mer_labels'] = $data['mer_labels']; app()->make(SpuRepository::class)->create($product, $result->product_id, $activity_id, $productType); } - $product = $result; - event('product.create',compact('product')); +// $product = $result; +// event('product.create',compact('product')); return $result->product_id; }); } @@ -1568,6 +1568,7 @@ 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){ diff --git a/app/controller/api/server/StoreProduct.php b/app/controller/api/server/StoreProduct.php index 45c55c6b..4b91fda5 100644 --- a/app/controller/api/server/StoreProduct.php +++ b/app/controller/api/server/StoreProduct.php @@ -120,6 +120,9 @@ class StoreProduct extends BaseController $data['mer_status'] = ($merchant['is_del'] || !$merchant['mer_state'] || !$merchant['status']) ? 0 : 1; $data['mer_id'] = $merId; $this->repository->edit($id, $data, $merId, 0, 1); + if ($data['status'] == 0) { + event('product.sell', ['source_mer_id' => $merId, 'product_id' => $id, 'status' => $data['status']]); + } return app('json')->success('编辑成功'); } diff --git a/app/event.php b/app/event.php index 06e044fe..0f53cf6b 100644 --- a/app/event.php +++ b/app/event.php @@ -65,6 +65,7 @@ return [ 'community_address'=>[\app\listener\CommunityAddress::class], // 'order.paySuccess'=>[\app\listener\OrderPaySuccess::class], 'product.create'=>[\app\listener\ProductCreate::class], + 'product.sell'=>[\app\listener\CloudProduct::class], //商品上下架 ], 'subscribe' => [], diff --git a/app/listener/CloudProduct.php b/app/listener/CloudProduct.php new file mode 100644 index 00000000..db06db2a --- /dev/null +++ b/app/listener/CloudProduct.php @@ -0,0 +1,22 @@ +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']]); + } + +} From ea733f9fb8660463f3619d6486028c772570e12d Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Tue, 23 May 2023 15:09:52 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BA=91=E4=BB=93?= =?UTF-8?q?=E5=95=86=E5=93=81=E5=85=B3=E8=81=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/model/store/product/CloudProduct.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/common/model/store/product/CloudProduct.php b/app/common/model/store/product/CloudProduct.php index 27239f36..ec392fc4 100644 --- a/app/common/model/store/product/CloudProduct.php +++ b/app/common/model/store/product/CloudProduct.php @@ -19,7 +19,7 @@ class CloudProduct extends BaseModel public static function tablePk(): string { - return 'mer_id'; + return 'product_id'; } public static function tableName(): string From 6b54927d33b8f6113f59990e763e1aad853d2170 Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Tue, 23 May 2023 15:51:30 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=95=86=E5=93=81?= =?UTF-8?q?=E4=B8=8A=E6=9E=B6=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/repositories/store/product/ProductRepository.php | 3 +++ app/controller/api/server/StoreProduct.php | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/common/repositories/store/product/ProductRepository.php b/app/common/repositories/store/product/ProductRepository.php index ee1f7a11..ee13c430 100644 --- a/app/common/repositories/store/product/ProductRepository.php +++ b/app/common/repositories/store/product/ProductRepository.php @@ -284,6 +284,9 @@ 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']]); + } app()->make(SpuRepository::class)->changeStatus($id, $productType); }); } diff --git a/app/controller/api/server/StoreProduct.php b/app/controller/api/server/StoreProduct.php index 4b91fda5..45c55c6b 100644 --- a/app/controller/api/server/StoreProduct.php +++ b/app/controller/api/server/StoreProduct.php @@ -120,9 +120,6 @@ class StoreProduct extends BaseController $data['mer_status'] = ($merchant['is_del'] || !$merchant['mer_state'] || !$merchant['status']) ? 0 : 1; $data['mer_id'] = $merId; $this->repository->edit($id, $data, $merId, 0, 1); - if ($data['status'] == 0) { - event('product.sell', ['source_mer_id' => $merId, 'product_id' => $id, 'status' => $data['status']]); - } return app('json')->success('编辑成功'); } From 5c84d962769945a54dcff0cbf7fe6441a7c24b84 Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Tue, 23 May 2023 17:18:28 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BA=91=E4=BB=93?= =?UTF-8?q?=E5=95=86=E5=93=81=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/dao/BaseDao.php | 12 +++++++ .../store/product/ProductRepository.php | 34 ++++++++----------- app/listener/CloudProduct.php | 28 ++++++++++++--- 3 files changed, 49 insertions(+), 25 deletions(-) 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); + } + } + } } } From 0f0883c1a5d0f602fc06bd1aaa754f93bac690cf Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Tue, 23 May 2023 17:50:45 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BA=91=E4=BB=93?= =?UTF-8?q?=E5=95=86=E5=93=81=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/repositories/store/product/SpuRepository.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/common/repositories/store/product/SpuRepository.php b/app/common/repositories/store/product/SpuRepository.php index 3ce2c637..19ec533b 100644 --- a/app/common/repositories/store/product/SpuRepository.php +++ b/app/common/repositories/store/product/SpuRepository.php @@ -19,6 +19,7 @@ use crmeb\jobs\SyncProductTopJob; use crmeb\services\CopyCommand; use crmeb\services\RedisCacheService; use think\exception\ValidateException; +use think\facade\Db; use think\facade\Log; use app\common\repositories\BaseRepository; use app\common\dao\store\product\SpuDao; @@ -162,11 +163,14 @@ class SpuRepository extends BaseRepository $where['spu_status'] = 1; $where['mer_status'] = 1; $RedisCacheService = app()->make(RedisCacheService::class); - $exists=$RedisCacheService->exists('CloudMerchanSpu'.$where['mer_id']); + $exists=$RedisCacheService->exists('CloudMerchantSpu'.$where['mer_id']); if ($exists){ - $Spu_arr=$RedisCacheService->SRANDMEMBER('CloudMerchanSpu'.$where['mer_id'], 10); + $Spu_arr=$RedisCacheService->SRANDMEMBER('CloudMerchantSpu'.$where['mer_id'], 10); $where['product_id'] =$Spu_arr; + } else { + $where['product_id'] = Db::name('cloud_product')->where('mer_id', $where['mer_id'])->where('status', 1)->orderRand()->limit(10)->column('product_id'); } + unset($where['mer_id']); $query = $this->dao->search($where); $count = 0; From 9bdc500954c85364be9692afe5ef03e30b26e016 Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Wed, 24 May 2023 11:58:07 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E4=BA=91=E4=BB=93=E5=95=86=E5=93=81?= =?UTF-8?q?=E6=8C=89=E5=8C=BA=E5=9F=9F+=E5=88=86=E7=B1=BB=E6=A3=80?= =?UTF-8?q?=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../store/product/SpuRepository.php | 26 ++++---- app/controller/api/Common.php | 3 +- .../api/store/product/CloudWarehouse.php | 66 +++++++++++++++++++ route/api.php | 1 + 4 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 app/controller/api/store/product/CloudWarehouse.php diff --git a/app/common/repositories/store/product/SpuRepository.php b/app/common/repositories/store/product/SpuRepository.php index 19ec533b..c94bf4be 100644 --- a/app/common/repositories/store/product/SpuRepository.php +++ b/app/common/repositories/store/product/SpuRepository.php @@ -158,30 +158,32 @@ class SpuRepository extends BaseRepository return compact('count', 'list'); } - public function getApiCloudSearch($where, $page, $limit) + public function getApiCloudSearch($where, $page, $limit, $rand = true) { $where['spu_status'] = 1; $where['mer_status'] = 1; - $RedisCacheService = app()->make(RedisCacheService::class); - $exists=$RedisCacheService->exists('CloudMerchantSpu'.$where['mer_id']); - if ($exists){ - $Spu_arr=$RedisCacheService->SRANDMEMBER('CloudMerchantSpu'.$where['mer_id'], 10); - $where['product_id'] =$Spu_arr; - } else { - $where['product_id'] = Db::name('cloud_product')->where('mer_id', $where['mer_id'])->where('status', 1)->orderRand()->limit(10)->column('product_id'); + if ($rand) { + $RedisCacheService = app()->make(RedisCacheService::class); + $exists=$RedisCacheService->exists('CloudMerchantSpu'.$where['mer_id']); + if ($exists){ + $Spu_arr=$RedisCacheService->SRANDMEMBER('CloudMerchantSpu'.$where['mer_id'], 10); + $where['product_id'] =$Spu_arr; + } else { + //TODO 后期优化随机查询 + $where['product_id'] = Db::name('cloud_product')->where('mer_id', $where['mer_id'])->where('status', 1)->orderRand()->limit(10)->column('product_id'); + } + unset($where['mer_id']); } - unset($where['mer_id']); $query = $this->dao->search($where); - $count = 0; -// $Sql=$query->page($page, $limit)->setOption('field', [])->field($this->productFiled)->fetchSql(true); + $count = $query->count(); $query->with([ 'merchant' => function ($query) { $query->field($this->merchantFiled)->with(['type_name']); }, 'issetCoupon', ]); - $list = $query->setOption('field', [])->field($this->productFiled)->select(); + $list = $query->setOption('field', [])->field($this->productFiled)->page($page)->limit($limit)->select(); $append = ['stop_time','svip_price','show_svip_info','is_svip_price']; $list->append($append); diff --git a/app/controller/api/Common.php b/app/controller/api/Common.php index 02938c65..c404164f 100644 --- a/app/controller/api/Common.php +++ b/app/controller/api/Common.php @@ -496,7 +496,8 @@ class Common extends BaseController public function get_cloud_shop($street_code){ $find=DB::name('merchant')->alias('m')->where('m.type_id',11)->where('m.is_del',0)->where('m.status',1) ->join('merchant_address a','a.mer_id=m.mer_id and a.street_id='.$street_code) - ->field('m.mer_id')->find(); + ->join('merchant_category c','m.category_id=c.merchant_category_id') + ->field('m.mer_id,category_id,category_name')->select(); return app('json')->success($find??[]); } } diff --git a/app/controller/api/store/product/CloudWarehouse.php b/app/controller/api/store/product/CloudWarehouse.php new file mode 100644 index 00000000..605ecd6b --- /dev/null +++ b/app/controller/api/store/product/CloudWarehouse.php @@ -0,0 +1,66 @@ +repository = $repository; + $this->merchantDao = $merchantDao; + $this->spuRepository = $spuRepository; + } + + /** + * 指定类型的云仓商品列表 + * @return mixed + */ + public function index() + { + $params = $this->request->params(['category_id', 'street_code', 'order', ['product_type', 0]]); + $search = [ + 'street_id' => $params['street_code'], + 'type_id' => Merchant::TypeStore, + 'category_id' => $params['category_id'], + ]; + $merchantIds = $this->merchantDao->search($search)->column('mer_id'); + [$page, $limit] = $this->getPage(); + if (empty($merchantIds)) { + return app('json')->success(['count' => 0, 'list' => []]); + } + $where['mer_ids'] = $merchantIds; + $where['product_type'] = $params['product_type']; + $where['is_gift_bag'] = 0; + $where['order'] = $params['order'] ?: 'sort'; + $products = $this->spuRepository->getApiCloudSearch($where, $page, $limit, false); + return app('json')->success($products); + } + +} \ No newline at end of file diff --git a/route/api.php b/route/api.php index e71be2c7..3796ce86 100644 --- a/route/api.php +++ b/route/api.php @@ -532,6 +532,7 @@ Route::group('api/', function () { //test Route::any('store/test', 'api.Test/test'); Route::get('subscribe', 'api.Common/subscribe'); + Route::resource('store/product/cloudWarehouse', 'api.store.product.CloudWarehouse'); })->middleware(UserTokenMiddleware::class, false);