You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
2.1 KiB

  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Service\v3\Interfaces\CollectStoreServiceInterface;
  4. use App\Constants\v3\SsdbKeys;
  5. use App\TaskWorker\SSDBTask;
  6. use Hyperf\Utils\ApplicationContext;
  7. class CollectStoreService implements CollectStoreServiceInterface
  8. {
  9. public function do($userId,$storeId)
  10. {
  11. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  12. //收藏店铺
  13. $userSet = $ssdb->exec('set', SsdbKeys::COLLECT_STORE_USER.$userId.'_store_'.$storeId,time());
  14. //用户收藏数量自增
  15. $userIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE_USER.$userId,1);
  16. //店铺被收藏数自增
  17. $storeIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE.$storeId,1);
  18. return $userSet && $userIncr && $storeIncr;
  19. }
  20. public function check($userId,$storeId)
  21. {
  22. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  23. return $ssdb->exec('exists', SsdbKeys::COLLECT_STORE_USER.$userId.'_store_'.$storeId);
  24. }
  25. public function undo($userId,$storeId)
  26. {
  27. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  28. //取消收藏店铺
  29. $userDel = $ssdb->exec('del', SsdbKeys::COLLECT_STORE_USER.$userId.'_store_'.$storeId,time());
  30. //用户收藏数量自减
  31. $userIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE_USER.$userId,'-1');
  32. //店铺被收藏数自减
  33. $storeIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE.$userId,'-1');
  34. return $userDel && $userIncr && $storeIncr;
  35. }
  36. //获取用户收藏店铺数量
  37. public function countByUser($userId)
  38. {
  39. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  40. $count = $ssdb->exec('get',SsdbKeys::COUNT_COLLECT_STORE_USER.$userId);
  41. return $count;
  42. }
  43. //获取店铺被收藏数量
  44. public function countByStore($storeId)
  45. {
  46. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  47. $count = $ssdb->exec('get',SsdbKeys::COUNT_COLLECT_STORE.$storeId);
  48. return $count;
  49. }
  50. }