|
@@ -274,137 +274,4 @@ public class ShopProductServiceImpl extends ServiceImpl<ShopProductMapper, ShopP
|
|
currentCombination.remove(currentCombination.size() - 1);
|
|
currentCombination.remove(currentCombination.size() - 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 生成更结构化的规格组合
|
|
|
|
- *
|
|
|
|
- * @param specList 规格列表
|
|
|
|
- * @return 结构化的规格组合结果
|
|
|
|
- */
|
|
|
|
- @Override
|
|
|
|
- public RPCBaseResponse<List<Map<String, Object>>> generateStructuredSpecCombinations(List<InsertProductSpecDto> specList) {
|
|
|
|
- if (specList == null || specList.isEmpty()) {
|
|
|
|
- return RPCBaseResponse.error("规格参数为空");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 限制最多输入3组规格
|
|
|
|
- if (specList.size() > 3) {
|
|
|
|
- return RPCBaseResponse.error("最多只能输入3组规格");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- try {
|
|
|
|
- // 自动清除之前的缓存
|
|
|
|
- clearSpecCache();
|
|
|
|
-
|
|
|
|
- // 验证每组规格
|
|
|
|
- for (InsertProductSpecDto spec : specList) {
|
|
|
|
- // 验证规格名称
|
|
|
|
- if (ObjectUtil.isEmpty(spec.getSpecName())) {
|
|
|
|
- return RPCBaseResponse.error("规格名称不能为空");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 验证规格值集合
|
|
|
|
- if (CollectionUtil.isEmpty(spec.getSpecValues())) {
|
|
|
|
- return RPCBaseResponse.error("规格[" + spec.getSpecName() + "]的规格值不能为空");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 限制最多只能有10个规格值
|
|
|
|
- if (spec.getSpecValues().size() > 8) {
|
|
|
|
- return RPCBaseResponse.error("规格[" + spec.getSpecName() + "]最多只能有8个规格值");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 处理所有规格
|
|
|
|
- List<List<SpecValueWithNameDto>> specValueList = new ArrayList<>();
|
|
|
|
- for (InsertProductSpecDto insertProductSpecDto : specList) {
|
|
|
|
- String specName = insertProductSpecDto.getSpecName();
|
|
|
|
- List<SpecValueDto> specValues = insertProductSpecDto.getSpecValues();
|
|
|
|
-
|
|
|
|
- // 为每个规格值创建DTO
|
|
|
|
- List<SpecValueWithNameDto> specValueWithNames = specValues.stream()
|
|
|
|
- .map(specValue -> new SpecValueWithNameDto(specName, specValue.getSpecValueName()))
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
- specValueList.add(specValueWithNames);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 生成规格组合
|
|
|
|
- List<List<SpecValueWithNameDto>> combinations = buildCombinations(specValueList);
|
|
|
|
-
|
|
|
|
- // 转换为新格式的结果,每个组合为一个Map,包含规格对象数组和库存
|
|
|
|
- List<Map<String, Object>> resultList = combinations.stream()
|
|
|
|
- .map(combination -> {
|
|
|
|
- Map<String, Object> resultMap = new HashMap<>();
|
|
|
|
- List<Map<String, String>> specItems = new ArrayList<>();
|
|
|
|
-
|
|
|
|
- // 将规格值转换为对象数组
|
|
|
|
- for (SpecValueWithNameDto item : combination) {
|
|
|
|
- Map<String, String> specItem = new HashMap<>();
|
|
|
|
- specItem.put("specName", item.getSpecName());
|
|
|
|
- specItem.put("specValueName", item.getSpecValueName());
|
|
|
|
- specItems.add(specItem);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 添加规格数组到结果中
|
|
|
|
- for (int i = 0; i < specItems.size(); i++) {
|
|
|
|
- resultMap.put(String.valueOf(i), specItems.get(i));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 添加默认库存为0
|
|
|
|
- resultMap.put("inventory", "0");
|
|
|
|
-
|
|
|
|
- // 日志记录
|
|
|
|
- StringBuilder debug = new StringBuilder("组合: ");
|
|
|
|
- for (Map<String, String> spec : specItems) {
|
|
|
|
- debug.append(spec.get("specName")).append("=").append(spec.get("specValueName")).append(", ");
|
|
|
|
- }
|
|
|
|
- log.debug(debug.toString());
|
|
|
|
-
|
|
|
|
- return resultMap;
|
|
|
|
- })
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
-
|
|
|
|
- log.info("生成规格组合总数: {}", resultList.size());
|
|
|
|
- return RPCBaseResponse.success("规格组合生成成功", resultList);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.error("生成规格组合出错", e);
|
|
|
|
- return RPCBaseResponse.error("生成规格组合失败:" + e.getMessage());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 递归构建规格值的组合
|
|
|
|
- *
|
|
|
|
- * @param specValuesList 规格值列表
|
|
|
|
- * @return 所有组合的列表
|
|
|
|
- */
|
|
|
|
- private List<List<SpecValueWithNameDto>> buildCombinations(List<List<SpecValueWithNameDto>> specValuesList) {
|
|
|
|
- // 基本情况:如果只有一组规格值,则直接返回包装后的列表
|
|
|
|
- if (specValuesList.size() == 1) {
|
|
|
|
- return specValuesList.get(0).stream()
|
|
|
|
- .map(value -> {
|
|
|
|
- List<SpecValueWithNameDto> combination = new ArrayList<>();
|
|
|
|
- combination.add(value);
|
|
|
|
- return combination;
|
|
|
|
- })
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 取出第一组规格值
|
|
|
|
- List<SpecValueWithNameDto> first = specValuesList.get(0);
|
|
|
|
-
|
|
|
|
- // 递归处理剩余的规格值
|
|
|
|
- List<List<SpecValueWithNameDto>> restCombinations = buildCombinations(specValuesList.subList(1, specValuesList.size()));
|
|
|
|
-
|
|
|
|
- // 合并第一组与剩余组合
|
|
|
|
- List<List<SpecValueWithNameDto>> result = new ArrayList<>();
|
|
|
|
- for (SpecValueWithNameDto value : first) {
|
|
|
|
- for (List<SpecValueWithNameDto> restCombination : restCombinations) {
|
|
|
|
- List<SpecValueWithNameDto> newCombination = new ArrayList<>();
|
|
|
|
- newCombination.add(value);
|
|
|
|
- newCombination.addAll(restCombination);
|
|
|
|
- result.add(newCombination);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
}
|
|
}
|