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.

98 lines
3.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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\Model\v3\UserCollection;
  6. use App\Service\v3\Interfaces\CollectStoreServiceInterface;
  7. use App\Constants\v3\SsdbKeys;
  8. use App\TaskWorker\SSDBTask;
  9. use Hyperf\DbConnection\Db;
  10. use Hyperf\Utils\ApplicationContext;
  11. class CollectStoreService implements CollectStoreServiceInterface
  12. {
  13. public function do($userId,$storeId)
  14. {
  15. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  16. //收藏店铺
  17. $userSet = $ssdb->exec('set', SsdbKeys::COLLECT_STORE_USER.$userId.'_store_'.$storeId,time());
  18. //用户收藏数量自增
  19. $userIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE_USER.$userId,1);
  20. //店铺被收藏数自增
  21. $storeIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE.$storeId,1);
  22. //起线程记录到数据库
  23. go(function () use ($userId,$storeId){
  24. $marketId = Store::query()->where('id',$storeId)->value('market_id');
  25. UserCollection::query()->withTrashed()->updateOrCreate(
  26. [
  27. 'store_id' => $storeId,
  28. 'market_id' => $marketId,
  29. 'user_id' => $userId
  30. ],
  31. [
  32. 'deleted_at' => null
  33. ]
  34. );
  35. });
  36. return $userSet && $userIncr && $storeIncr;
  37. }
  38. public function check($userId,$storeId)
  39. {
  40. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  41. return $ssdb->exec('exists', SsdbKeys::COLLECT_STORE_USER.$userId.'_store_'.$storeId);
  42. }
  43. public function undo($userId,$storeId)
  44. {
  45. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  46. //取消收藏店铺
  47. $userDel = $ssdb->exec('del', SsdbKeys::COLLECT_STORE_USER.$userId.'_store_'.$storeId,time());
  48. //用户收藏数量自减
  49. $userIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE_USER.$userId,'-1');
  50. //店铺被收藏数自减
  51. $storeIncr = $ssdb->exec('incr', SsdbKeys::COUNT_COLLECT_STORE.$storeId,'-1');
  52. //起线程删除数据库
  53. go(function () use ($userId,$storeId){
  54. UserCollection::query()->where([
  55. ['user_id','=',$userId],
  56. ['store_id','=',$storeId],
  57. ])
  58. ->delete();
  59. });
  60. return $userDel && $userIncr && $storeIncr;
  61. }
  62. //获取用户收藏店铺数量
  63. public function countByUser($userId)
  64. {
  65. $marketIds = Market::query()->pluck('id');
  66. return UserCollection::query()->where(['user_id' => $userId])->whereIn('market_id', $marketIds)->count();
  67. // $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  68. // $count = $ssdb->exec('get',SsdbKeys::COUNT_COLLECT_STORE_USER.$userId);
  69. // return $count;
  70. }
  71. //获取店铺被收藏数量
  72. public function countByStore($storeId)
  73. {
  74. return UserCollection::query()->where(['store_id' => $storeId])->count();
  75. // $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  76. // $count = $ssdb->exec('get',SsdbKeys::COUNT_COLLECT_STORE.$storeId);
  77. // return $count;
  78. }
  79. public function getListByUser($userId)
  80. {
  81. $marketIds = UserCollection::query()->where('user_id',$userId)->pluck('market_id')->toArray();
  82. $storeIds = UserCollection::query()->where('user_id',$userId)->pluck('store_id')->toArray();
  83. $res = Market::query()->with(['stores' => function($query) use ($storeIds) {
  84. $query->whereIn('id',$storeIds);
  85. }])
  86. ->whereIn('id',$marketIds)
  87. ->get();
  88. return $res;
  89. }
  90. }