fix: GetGoodsViewData 使用 GoodsSpecType.name 通过值匹配确定维度

pull/19/head
Council 2026-04-21 12:46:59 +08:00
parent 4683862688
commit 8ea0c1a229
1 changed files with 43 additions and 11 deletions

View File

@ -510,27 +510,59 @@ class SeatSkuService extends BaseService
return $seatSpecMap;
}
// 2. 查询每个 spec_base_id 对应的 GoodsSpecValue
$specBaseIds = array_column($specs, 'id');
$specValues = \think\facade\Db::name('GoodsSpecValue')
->whereIn('goods_spec_base_id', $specBaseIds)
->order('id', 'asc') // 按插入顺序,确保维度顺序一致
// 2. 查询 GoodsSpecType 获取维度映射name => index
$specTypes = \think\facade\Db::name('GoodsSpecType')
->where('goods_id', $goodsId)
->order('id', 'asc')
->select()
->toArray();
// 3. 按 spec_base_id 分组,按 SPEC_DIMS 顺序映射维度
// 不依赖 type 字段,而是按插入顺序匹配 SPEC_DIMS
// 构建 name => index 映射
$dimIndexByName = [];
$dimValuesByName = []; // name => [value1, value2, ...]
foreach ($specTypes as $idx => $type) {
$dimName = $type['name'] ?? '';
if (!empty($dimName)) {
$dimIndexByName[$dimName] = $idx;
// 解析 value JSON 数组
$values = json_decode($type['value'] ?? '[]', true);
$dimValuesByName[$dimName] = [];
foreach ($values as $v) {
if (isset($v['name'])) {
$dimValuesByName[$dimName][] = $v['name'];
}
}
}
}
// 3. 查询每个 spec_base_id 对应的 GoodsSpecValue
$specBaseIds = array_column($specs, 'id');
$specValues = \think\facade\Db::name('GoodsSpecValue')
->whereIn('goods_spec_base_id', $specBaseIds)
->select()
->toArray();
// 4. 按 spec_base_id 分组,通过值匹配确定维度
$specByBaseId = [];
foreach ($specValues as $sv) {
$baseId = $sv['goods_spec_base_id'];
$value = $sv['value'] ?? '';
// 通过值匹配找到对应的维度名
$dimName = '';
foreach ($dimValuesByName as $name => $values) {
if (in_array($value, $values)) {
$dimName = $name;
break;
}
}
if (!isset($specByBaseId[$baseId])) {
$specByBaseId[$baseId] = [];
}
$idx = count($specByBaseId[$baseId]); // 当前维度索引
$dimName = self::SPEC_DIMS[$idx] ?? ('dim_' . $idx);
$specByBaseId[$baseId][] = [
'type' => $dimName, // 使用 SPEC_DIMS 顺序推断维度名
'value' => $sv['value'] ?? '',
'type' => $dimName,
'value' => $value,
];
}