You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于多维度实现产品处方分析?Laravel代码位置筛选问题求助

问题解决与多维度产品处方分析实现

一、修复位置筛选失效问题

1. 移除无效关联

你的代码中join('addresses as product_address', 'products.id', '=', 'product_address.userID')属于错误关联:产品ID(products.id)无法对应地址表的用户ID(product_address.userID),这会触发笛卡尔积生成大量冗余数据,直接导致位置筛选逻辑失效,需直接删除该关联。

2. 校验数据一致性

排查sellers.barangay字段存储值与$nearbyBarangaysGroups中名称的一致性(比如大小写、空格、特殊字符),可通过统一格式避免匹配失败:

$barangayList = collect($nearbyBarangaysGroups)
    ->flatten()
    ->map(fn($name) => strtolower(trim($name)))
    ->unique()
    ->toArray();

// 查询时统一格式匹配
->whereRaw('LOWER(TRIM(sellers.barangay)) IN (?)', [$barangayList])

3. 改用左连接保留无评论产品

原代码用内连接join('reviews')会过滤掉无评论的产品,若需完整的产品池,改用左连接:

->leftJoin('reviews', 'products.id', '=', 'reviews.productID')

二、实现多维度产品处方分析

整合评分、价格、位置优先级、销量、分类,通过加权综合得分排序实现精准的产品分析/推荐:

1. 核心优化代码示例

// 定义区域优先级分组,数字越小优先级越高
$locationGroups = [
    1 => ["Dolores", "Juliana", "Del Pilar", "San Jose", "Santo Rosario", "San Nicolas", "Santo Niño", "Santa Lucia", "Magliman", "Santa Teresita", "San Agustin", "San Felipe"],
    2 => ["Alasas", "Baliti", "Bulaon", "Calulut", "Dela Paz Norte", "Dela Paz Sur", "Del Carmen", "Del Rosario", "Lara", "Maimpis", "Pulung Bulo", "Lourdes", "Quebiawan", "Saguin", "Malino", "Malpitic", "Pandaras", "Panipuan", "San Isidro", "San Juan", "San Pedro Cutud"]
];

// 处理区域名称为统一格式
$barangayList = collect($locationGroups)
    ->flatten()
    ->map(fn($name) => strtolower(trim($name)))
    ->unique()
    ->toArray();

// 转换分组名称为小写,用于CASE判断
$group1Lower = collect($locationGroups[1])->map(fn($name) => strtolower(trim($name)))->toArray();
$group2Lower = collect($locationGroups[2])->map(fn($name) => strtolower(trim($name)))->toArray();

$products = Products::selectRaw("
    products.*,
    sellers.shop_name,
    products.status,
    products.price,
    sellers.barangay as seller_location,
    COALESCE(reviews.star, 0) as star,
    products.category,
    products.sales_count,
    -- 标记区域优先级
    CASE
        WHEN LOWER(TRIM(sellers.barangay)) IN ('" . implode("','", $group1Lower) . "') THEN 1
        WHEN LOWER(TRIM(sellers.barangay)) IN ('" . implode("','", $group2Lower) . "') THEN 2
        ELSE 3
    END as location_priority,
    -- 计算综合得分(权重可根据业务调整)
    (
        (COALESCE(reviews.star, 0)/5)*0.4 + -- 评分权重40%
        (1 - (products.price/(SELECT COALESCE(MAX(price), 1) FROM products WHERE category = products.category)))*0.2 + -- 价格权重20%(价格越低得分越高)
        (products.sales_count/(SELECT COALESCE(MAX(sales_count), 1) FROM products WHERE category = products.category))*0.3 + -- 销量权重30%
        (1/location_priority)*0.1 -- 位置权重10%
    ) as composite_score
")
->where('products.stocks', '>', 0)
->where('products.status', 'Approved')
->where('product_name', 'like', "%$searchQuery%")
->whereRaw('LOWER(TRIM(sellers.barangay)) IN (?)', [$barangayList])
->join('sellers', 'products.sellerID', '=', 'sellers.id')
->leftJoin('reviews', 'products.id', '=', 'reviews.productID')
// 可选:动态分类筛选(需传入$category参数)
->when(isset($category), function($query) use ($category) {
    $query->where('products.category', $category);
})
// 按综合得分排序,实现多维度平衡推荐
->orderByDesc('composite_score')
->paginate(50);

2. 关键说明

  • 区域优先级:通过CASE WHEN给不同区域组标记优先级,确保更近区域的产品优先展示。
  • 综合得分:根据业务需求调整各维度权重,实现评分、价格、销量、位置的平衡分析。
  • 销量整合:假设products表有sales_count字段存储累计销量,若需实时统计,可关联orders表聚合计算。
  • 分类筛选:通过when()实现动态分类过滤,满足不同分类下的产品分析需求。

内容的提问来源于stack exchange,提问作者Sequoia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 17:58:11