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/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..ec392fc4 --- /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 'product_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..f8f28b8c 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; }); } @@ -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); }); } @@ -1569,12 +1572,7 @@ class ProductRepository extends BaseRepository throw new ValidateException('商品spu更新出错'); }else{ 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(); @@ -1596,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/common/repositories/store/product/SpuRepository.php b/app/common/repositories/store/product/SpuRepository.php index 3ce2c637..c94bf4be 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; @@ -157,27 +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('CloudMerchanSpu'.$where['mer_id']); - if ($exists){ - $Spu_arr=$RedisCacheService->SRANDMEMBER('CloudMerchanSpu'.$where['mer_id'], 10); - $where['product_id'] =$Spu_arr; + 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']); } $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/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..7276a92a --- /dev/null +++ b/app/listener/CloudProduct.php @@ -0,0 +1,40 @@ +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); + } + } + } + } + +} 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);