fix: GetGoodsViewData 使用 GoodsSpecType.name 通过值匹配确定维度
parent
4683862688
commit
8ea0c1a229
|
|
@ -510,27 +510,59 @@ class SeatSkuService extends BaseService
|
||||||
return $seatSpecMap;
|
return $seatSpecMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 查询每个 spec_base_id 对应的 GoodsSpecValue
|
// 2. 查询 GoodsSpecType 获取维度映射(name => index)
|
||||||
$specBaseIds = array_column($specs, 'id');
|
$specTypes = \think\facade\Db::name('GoodsSpecType')
|
||||||
$specValues = \think\facade\Db::name('GoodsSpecValue')
|
->where('goods_id', $goodsId)
|
||||||
->whereIn('goods_spec_base_id', $specBaseIds)
|
->order('id', 'asc')
|
||||||
->order('id', 'asc') // 按插入顺序,确保维度顺序一致
|
|
||||||
->select()
|
->select()
|
||||||
->toArray();
|
->toArray();
|
||||||
|
|
||||||
// 3. 按 spec_base_id 分组,按 SPEC_DIMS 顺序映射维度
|
// 构建 name => index 映射
|
||||||
// 不依赖 type 字段,而是按插入顺序匹配 SPEC_DIMS
|
$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 = [];
|
$specByBaseId = [];
|
||||||
foreach ($specValues as $sv) {
|
foreach ($specValues as $sv) {
|
||||||
$baseId = $sv['goods_spec_base_id'];
|
$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])) {
|
if (!isset($specByBaseId[$baseId])) {
|
||||||
$specByBaseId[$baseId] = [];
|
$specByBaseId[$baseId] = [];
|
||||||
}
|
}
|
||||||
$idx = count($specByBaseId[$baseId]); // 当前维度索引
|
|
||||||
$dimName = self::SPEC_DIMS[$idx] ?? ('dim_' . $idx);
|
|
||||||
$specByBaseId[$baseId][] = [
|
$specByBaseId[$baseId][] = [
|
||||||
'type' => $dimName, // 使用 SPEC_DIMS 顺序推断维度名
|
'type' => $dimName,
|
||||||
'value' => $sv['value'] ?? '',
|
'value' => $value,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue