get(SSDBTask::class); //收藏店铺 $userSet = $ssdb->exec('set', SsdbKeys::COLLECT_STORE_USER.$userId.'_store_'.$storeId,time()); //用户收藏数量自增 $userIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE_USER.$userId,1); //店铺被收藏数自增 $storeIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE.$storeId,1); //起线程记录到数据库 go(function () use ($userId,$storeId){ $marketId = Store::query()->where('id',$storeId)->value('market_id'); UserCollection::query()->withTrashed()->updateOrCreate( [ 'store_id' => $storeId, 'market_id' => $marketId, 'user_id' => $userId ], [ 'deleted_at' => null ] ); }); return $userSet && $userIncr && $storeIncr; } public function check($userId,$storeId) { $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); return $ssdb->exec('exists', SsdbKeys::COLLECT_STORE_USER.$userId.'_store_'.$storeId); } public function undo($userId,$storeId) { $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); //取消收藏店铺 $userDel = $ssdb->exec('del', SsdbKeys::COLLECT_STORE_USER.$userId.'_store_'.$storeId,time()); //用户收藏数量自减 $userIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE_USER.$userId,'-1'); //店铺被收藏数自减 $storeIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE.$storeId,'-1'); //起线程删除数据库 go(function () use ($userId,$storeId){ UserCollection::query()->where([ ['user_id','=',$userId], ['store_id','=',$storeId], ]) ->delete(); }); return $userDel && $userIncr && $storeIncr; } //获取用户收藏店铺数量 public function countByUser($userId) { $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); $count = $ssdb->exec('get',SsdbKeys::COUNT_COLLECT_STORE_USER.$userId); return $count; } //获取店铺被收藏数量 public function countByStore($storeId) { $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); $count = $ssdb->exec('get',SsdbKeys::COUNT_COLLECT_STORE.$storeId); return $count; } public function getListByUser($userId) { $marketIds = UserCollection::query()->where('user_id',$userId)->pluck('market_id')->toArray(); $storeIds = UserCollection::query()->where('user_id',$userId)->pluck('store_id')->toArray(); $res = Market::query()->with(['stores' => function($query) use ($storeIds) { $query->whereIn('id',$storeIds); }]) ->whereIn('id',$marketIds) ->get(); return $res; } }