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.

68 lines
2.7 KiB

5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Model\v3\Market;
  4. use App\Model\v3\Store;
  5. use App\Service\v3\Interfaces\CollectStoreServiceInterface;
  6. use App\Constants\v3\SsdbKeys;
  7. use App\TaskWorker\SSDBTask;
  8. use Hyperf\DbConnection\Db;
  9. use Hyperf\Utils\ApplicationContext;
  10. class CollectStoreService implements CollectStoreServiceInterface
  11. {
  12. public function do($userId,$storeId)
  13. {
  14. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  15. //收藏店铺
  16. $userSet = $ssdb->exec('set', SsdbKeys::COLLECT_STORE_USER.$userId.'_store_'.$storeId,time());
  17. //用户收藏数量自增
  18. $userIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE_USER.$userId,1);
  19. //店铺被收藏数自增
  20. $storeIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE.$storeId,1);
  21. return $userSet && $userIncr && $storeIncr;
  22. }
  23. public function check($userId,$storeId)
  24. {
  25. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  26. return $ssdb->exec('exists', SsdbKeys::COLLECT_STORE_USER.$userId.'_store_'.$storeId);
  27. }
  28. public function undo($userId,$storeId)
  29. {
  30. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  31. //取消收藏店铺
  32. $userDel = $ssdb->exec('del', SsdbKeys::COLLECT_STORE_USER.$userId.'_store_'.$storeId,time());
  33. //用户收藏数量自减
  34. $userIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE_USER.$userId,'-1');
  35. //店铺被收藏数自减
  36. $storeIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE.$userId,'-1');
  37. return $userDel && $userIncr && $storeIncr;
  38. }
  39. //获取用户收藏店铺数量
  40. public function countByUser($userId)
  41. {
  42. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  43. $count = $ssdb->exec('get',SsdbKeys::COUNT_COLLECT_STORE_USER.$userId);
  44. return $count;
  45. }
  46. //获取店铺被收藏数量
  47. public function countByStore($storeId)
  48. {
  49. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  50. $count = $ssdb->exec('get',SsdbKeys::COUNT_COLLECT_STORE.$storeId);
  51. return $count;
  52. }
  53. public function getListByUser($userId)
  54. {
  55. $marketIds = Db::table('lanzu_user_collection')->where('user_id',$userId)->pluck('market_id')->toArray();
  56. $storeIds = Db::table('lanzu_user_collection')->where('user_id',$userId)->pluck('store_id')->toArray();
  57. $res = Market::query()->with(['stores' => function($query) use ($storeIds) {
  58. $query->whereIn('lanzu_store.id',$storeIds);
  59. }])
  60. ->whereIn('id',$marketIds)
  61. ->get();
  62. return $res;
  63. }
  64. }