// 查询产品关联的所有seller // $sellers = static::getBuilder($data) // ->join('sellers as s', 'products.seller_id', 's.id') // ->select(['s.*']) // ->get()->toArray();//这么查product做主表,seller会重复 !isset($data['page']) && $data['page'] = 1; !isset($data['per_page']) && $data['per_page'] = 100000; $sellers = SellerRepo::getSellersByFilter($data); $sellers_format = SellerResource::collection($sellers)->jsonSerialize(); if (count($sellers_format)>1) { array_multisort(array_column($sellers_format, 'position'), SORT_ASC, array_column($sellers_format, 'id'), SORT_ASC, $sellers_format); } return $sellers_format; } public static function list($data = []) { return static::getBuilder($data)->paginate($data['per_page'] ?? 20); } public static function autocomplete($name, $seller_id='') { $products = Product::query()->with('description') ->whereHas('description', function ($query) use ($seller_id) { $query->where('seller_id', $seller_id); }) ->whereHas('description', function ($query) use ($name) { $query->where('name', 'like', "%{$name}%"); }) ->limit(10)->get(); return \Beike\Admin\Http\Resources\ProductSimple::collection($products)->jsonSerialize(); } /** * 获取商品ID获取单个商品名称 * * @param $id * @return HigherOrderBuilderProxy|mixed|string */ public static function getNameById($id) { $product = Product::query()->find($id); if ($product) { return $product->description->name; } return ''; } /** * 通过商品ID获取商品名称 * @param $id * @return mixed|string */ public static function getName($id) { return self::getNameById($id); } /** * 获取所有商品ID和名称列表 * * @return array|null */ public static function getAllProductsWithName(): ?array { if (self::$allProductsWithName !== null) { return self::$allProductsWithName; } $items = []; $products = static::getBuilder()->select('id')->limit(10)->get();//如果要使用,必须改为查询单页ID一条记录,否则全部查会非常慢非常耗时,导致内存溢出 foreach ($products as $product) { $items[$product->id] = [ 'id' => $product->id, 'name' => $product->description->name ?? '', ]; } return self::$allProductsWithName = $items; } /** * @param $productIds * @return array */ public static function getNames($productIds): array { $products = self::getListByProductIds($productIds); return $products->map(function ($product) { return [ 'id' => $product->id, 'name' => $product->description->name ?? '', ]; })->toArray(); } /** * 通过商品ID获取商品列表 * @return array|Builder[]|Collection */ public static function getListByProductIds($productIds) { if (empty($productIds)) { return []; } $products = Product::query() ->with(['description']) ->whereIn('id', $productIds) ->orderByRaw('FIELD(id, ' . implode(',', $productIds) . ')') ->get(); return $products; } public static function DeleteByIds($ids) { self::updateStatusByIds($ids, 0);//先下架 Product::query()->whereIn('id', $ids)->delete(); } public static function updateStatusByIds($ids, $status) { Product::query()->whereIn('id', $ids)->update(['active' => $status]); } public static function forceDeleteTrashed() { $products = Product::onlyTrashed(); $productsIds = $products->pluck('id')->toArray(); ProductRelation::query()->whereIn('product_id', $productsIds)->orWhere('relation_id', $productsIds)->delete(); ProductAttribute::query()->whereIn('product_id', $productsIds)->delete(); ProductCategory::query()->whereIn('product_id', $productsIds)->delete(); ProductSku::query()->whereIn('product_id', $productsIds)->delete(); ProductDescription::query()->whereIn('product_id', $productsIds)->delete(); $products->forceDelete(); } public static function viewAdd(Product $product) { // $minutes = system_setting('base.product_view_minutes', 1); // $count = $product->views()->where('session_id', get_session_id())->where('created_at', '>', now()->subMinutes($minutes))->count(); // // 如果当前session_id对该商品$minutes分钟内有个访问记录,则不重复记录访问次数。 // if ($count) { // return; // } $product->views()->create([ 'customer_id' => current_customer()->id ?? 0, 'ip' => request()->getClientIp(), 'session_id' => get_session_id(), ]); } } Server Error
500
Server Error