|
|
@@ -318,12 +318,14 @@ public class BrandRecommendService {
|
|
|
Map<String, Integer> colIdx = buildColIdx(rows, BRAND_CSV_COLS);
|
|
|
|
|
|
int success = 0, skipped = 0, failed = 0;
|
|
|
+ // Track names already in this batch to avoid duplicate-key violation within same import
|
|
|
+ Set<String> batchNames = new HashSet<>();
|
|
|
for (int i = 1; i < rows.size(); i++) {
|
|
|
String[] cols = rows.get(i);
|
|
|
try {
|
|
|
String name = cell(cols, colIdx, "品牌名称");
|
|
|
if (name == null || name.isEmpty()) { failed++; continue; }
|
|
|
- if (brandRepository.existsByNameAndIdNot(name, null)) { skipped++; continue; }
|
|
|
+ if (!batchNames.add(name) || brandRepository.existsByNameAndIdNot(name, null)) { skipped++; continue; }
|
|
|
brandRepository.save(Brand.builder()
|
|
|
.name(name)
|
|
|
.functionIndication(cell(cols, colIdx, "功能主治"))
|
|
|
@@ -387,7 +389,7 @@ public class BrandRecommendService {
|
|
|
}
|
|
|
|
|
|
public String exportBrandsCsv(Set<Integer> ids) {
|
|
|
- List<Brand> brands = ids.isEmpty() ? List.of() : brandRepository.findAllById(ids);
|
|
|
+ List<Brand> brands = (ids != null && !ids.isEmpty()) ? brandRepository.findAllById(ids) : List.of();
|
|
|
StringBuilder sb = new StringBuilder(BRANDS_CSV_HEADER).append("\n");
|
|
|
List<Function<Brand, String>> extractors = List.of(
|
|
|
b -> esc(b.getName()),
|
|
|
@@ -415,12 +417,13 @@ public class BrandRecommendService {
|
|
|
Map<String, Integer> colIdx = buildColIdx(rows, RULES_CSV_COLS);
|
|
|
|
|
|
int success = 0, skipped = 0, failed = 0;
|
|
|
+ Set<String> batchKw = new HashSet<>();
|
|
|
for (int i = 1; i < rows.size(); i++) {
|
|
|
String[] cols = rows.get(i);
|
|
|
try {
|
|
|
String kw = cell(cols, colIdx, "关键词");
|
|
|
if (kw == null || kw.isEmpty()) { failed++; continue; }
|
|
|
- if (ruleRepository.existsByKeywordAndIdNot(kw, null)) { skipped++; continue; }
|
|
|
+ if (!batchKw.add(kw) || ruleRepository.existsByKeywordAndIdNot(kw, null)) { skipped++; continue; }
|
|
|
|
|
|
int brandId = cellInt(cols, colIdx, "品牌ID");
|
|
|
String brandName = cell(cols, colIdx, "品牌名称");
|
|
|
@@ -444,7 +447,7 @@ public class BrandRecommendService {
|
|
|
}
|
|
|
|
|
|
public String exportRulesCsv(Set<Integer> ids) {
|
|
|
- List<BrandRecommendRule> rules = ids.isEmpty() ? List.of() : ruleRepository.findAllById(ids);
|
|
|
+ List<BrandRecommendRule> rules = (ids != null && !ids.isEmpty()) ? ruleRepository.findAllById(ids) : List.of();
|
|
|
Set<Integer> brandIds = rules.stream().map(BrandRecommendRule::getBrandId).collect(Collectors.toSet());
|
|
|
Map<Integer, String> brandNames = brandRepository.findAllById(brandIds).stream()
|
|
|
.collect(Collectors.toMap(Brand::getId, Brand::getName));
|