column('goods_id'); } /** * 是否收藏 * * @param int $accountId * @param int $goodsId * @param string $type goods|auction|special * @return bool */ public function isCollected(int $accountId, int $goodsId, string $type): bool { return AccountRecord::where('account_id', $accountId) ->where('goods_id', $goodsId) ->where('type', $type) ->where('action', AccountRecord::ACTION_COLLECT) ->where('is_collected', AccountRecord::COLLECTED) ->count() > 0; } /** * 获取用户收藏记录 * * @param int $accountId * @param string $type * @param int $page * @param int $size * @return Collection * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function fetchCollection(int $accountId, string $type = '', int $page = 1, int $size = 20): Collection { return AccountRecord::with(['goods']) ->when(!empty($type), function ($q) use ($type) { $q->where('type', $type); }) ->where('account_id', $accountId) ->where('is_collected', AccountRecord::COLLECTED) ->where('type', $type) ->page($page, $size) ->select(); } /** * 批量查询是否收藏 * 返回收藏的ID列表 * * @param int $accountId 用户ID * @param array $ids 记录ID * @return array */ public function findCollectionByIds(int $accountId, array $ids): array { return AccountRecord::where('account_id', $accountId) ->where('id', 'in', $ids) ->where('is_collected', AccountRecord::COLLECTED) ->where('type', AccountRecord::ACTION_COLLECT) ->column('id'); } }