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.0 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::COLLECT_STORE_NUM_USER.$userId,1);
  16. //店铺被收藏数自增
  17. $storeIncr = $ssdb->exec('incr', SsdbKeys::COLLECT_NUM_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::COLLECT_STORE_NUM_USER.$userId,'-1');
  32. //店铺被收藏数自减
  33. $storeIncr = $ssdb->exec('incr', SsdbKeys::COLLECT_NUM_STORE.$userId,'-1');
  34. return $userDel && $userIncr && $storeIncr;
  35. }
  36. public function countByUser($userId)
  37. {
  38. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  39. $num = $ssdb->exec('get',SsdbKeys::COLLECT_STORE_NUM_USER.$userId);
  40. return $num;
  41. }
  42. public function countByStore($storeId)
  43. {
  44. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  45. $num = $ssdb->exec('get',SsdbKeys::COLLECT_NUM_STORE.$storeId);
  46. return $num;
  47. }
  48. }