Commit e606e34123a3d7d460f0ed7dbf46870a0085bff8
1 parent
dcc6aa0c
fix: 搜索判断是否空值
Showing
1 changed file
with
63 additions
and
32 deletions
shop/src/main/java/com/canrd/shop/service/impl/ProductServiceImpl.java
@@ -374,71 +374,96 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | @@ -374,71 +374,96 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | ||
374 | } | 374 | } |
375 | 375 | ||
376 | private void suppleProducts(List<ProductDO> records) { | 376 | private void suppleProducts(List<ProductDO> records) { |
377 | + if (records == null || records.isEmpty()) { // 【修改1】先检查 records 是否为空 | ||
378 | + return; | ||
379 | + } | ||
380 | + | ||
377 | Boolean productPriceShow = switchControlService.getEnabledByName(SwitchControlConstants.PRODUCT_PRICE_SHOW); | 381 | Boolean productPriceShow = switchControlService.getEnabledByName(SwitchControlConstants.PRODUCT_PRICE_SHOW); |
378 | - List<String> productIds = records.stream().map(ProductDO::getId).collect(Collectors.toList()); | 382 | + |
383 | + List<String> productIds = records.stream() | ||
384 | + .map(ProductDO::getId) | ||
385 | + .filter(Objects::nonNull) // 【修改2】确保 productId 不为 null | ||
386 | + .collect(Collectors.toList()); | ||
387 | + | ||
388 | + if (productIds.isEmpty()) { // 【修改3】如果没有有效的 productId,则直接返回 | ||
389 | + return; | ||
390 | + } | ||
391 | + | ||
379 | List<TicketTypeDO> tickeyTypeDOList = ticketTypeService.lambdaQuery() | 392 | List<TicketTypeDO> tickeyTypeDOList = ticketTypeService.lambdaQuery() |
380 | .func(wrapper -> { | 393 | .func(wrapper -> { |
381 | - if (CollUtil.isEmpty(productIds)){ | 394 | + if (CollUtil.isEmpty(productIds)) { |
382 | wrapper.apply("1!=1"); | 395 | wrapper.apply("1!=1"); |
383 | - }else { | 396 | + } else { |
384 | wrapper.in(TicketTypeDO::getProductId, productIds); | 397 | wrapper.in(TicketTypeDO::getProductId, productIds); |
385 | } | 398 | } |
386 | }) | 399 | }) |
387 | .list(); | 400 | .list(); |
388 | - Map<String,BigDecimal> pId2ttMinPriceMap = new HashMap<>(); | ||
389 | - Map<String,BigDecimal> pId3ttMaxPriceMap = new HashMap<>(); | 401 | + |
402 | + Map<String, BigDecimal> pId2ttMinPriceMap = new HashMap<>(); | ||
403 | + Map<String, BigDecimal> pId3ttMaxPriceMap = new HashMap<>(); | ||
404 | + | ||
390 | for (TicketTypeDO ticketTypeDO : tickeyTypeDOList) { | 405 | for (TicketTypeDO ticketTypeDO : tickeyTypeDOList) { |
391 | - if (Objects.isNull(pId2ttMinPriceMap.get(ticketTypeDO.getProductId()))){ | ||
392 | - pId2ttMinPriceMap.put(ticketTypeDO.getProductId(), ticketTypeDO.getPrice()); | ||
393 | - }else { | ||
394 | - if (pId2ttMinPriceMap.get(ticketTypeDO.getProductId()).compareTo(ticketTypeDO.getPrice())>0){ | ||
395 | - pId2ttMinPriceMap.put(ticketTypeDO.getProductId(), ticketTypeDO.getPrice()); | ||
396 | - } | ||
397 | - } | ||
398 | - if (Objects.isNull(pId3ttMaxPriceMap.get(ticketTypeDO.getProductId()))){ | ||
399 | - pId3ttMaxPriceMap.put(ticketTypeDO.getProductId(), ticketTypeDO.getPrice()); | ||
400 | - }else { | ||
401 | - if (pId3ttMaxPriceMap.get(ticketTypeDO.getProductId()).compareTo(ticketTypeDO.getPrice())<0){ | ||
402 | - pId3ttMaxPriceMap.put(ticketTypeDO.getProductId(), ticketTypeDO.getPrice()); | ||
403 | - } | ||
404 | - } | 406 | + String productId = ticketTypeDO.getProductId(); |
407 | + if (productId == null) continue; // 【修改4】确保 productId 不为空 | ||
408 | + | ||
409 | + pId2ttMinPriceMap.compute(productId, (key, val) -> | ||
410 | + (val == null || ticketTypeDO.getPrice().compareTo(val) < 0) ? ticketTypeDO.getPrice() : val); | ||
411 | + | ||
412 | + pId3ttMaxPriceMap.compute(productId, (key, val) -> | ||
413 | + (val == null || ticketTypeDO.getPrice().compareTo(val) > 0) ? ticketTypeDO.getPrice() : val); | ||
405 | } | 414 | } |
406 | 415 | ||
407 | records.forEach(product -> { | 416 | records.forEach(product -> { |
408 | - if (productPriceShow){ | ||
409 | - if (Objects.nonNull(pId2ttMinPriceMap.get(product.getId()))){ | 417 | + if (product == null || product.getId() == null) { // 【修改5】检查 product 是否为空 |
418 | + return; | ||
419 | + } | ||
420 | + | ||
421 | + if (productPriceShow) { | ||
422 | + if (pId2ttMinPriceMap.containsKey(product.getId())) { // 【修改6】确保 key 存在 | ||
410 | BigDecimal originMinPrice = pId2ttMinPriceMap.get(product.getId()) | 423 | BigDecimal originMinPrice = pId2ttMinPriceMap.get(product.getId()) |
411 | .divide(new BigDecimal("0.7"), 2, RoundingMode.HALF_UP) | 424 | .divide(new BigDecimal("0.7"), 2, RoundingMode.HALF_UP) |
412 | .divide(new BigDecimal("0.5"), 2, RoundingMode.HALF_UP) | 425 | .divide(new BigDecimal("0.5"), 2, RoundingMode.HALF_UP) |
413 | .multiply(new BigDecimal("0.148")) | 426 | .multiply(new BigDecimal("0.148")) |
414 | .setScale(2, RoundingMode.HALF_UP); | 427 | .setScale(2, RoundingMode.HALF_UP); |
428 | + | ||
415 | BigDecimal computedPrice = originMinPrice | 429 | BigDecimal computedPrice = originMinPrice |
416 | .multiply(new BigDecimal("0.5")) | 430 | .multiply(new BigDecimal("0.5")) |
417 | .setScale(2, RoundingMode.HALF_UP); | 431 | .setScale(2, RoundingMode.HALF_UP); |
432 | + | ||
418 | product.setPrice(computedPrice); | 433 | product.setPrice(computedPrice); |
419 | product.setOriginMinPrice(originMinPrice); | 434 | product.setOriginMinPrice(originMinPrice); |
420 | } | 435 | } |
421 | - if (Objects.nonNull(pId3ttMaxPriceMap.get(product.getId()))){ | 436 | + |
437 | + if (pId3ttMaxPriceMap.containsKey(product.getId())) { // 【修改7】确保 key 存在 | ||
422 | BigDecimal originMaxPrice = pId3ttMaxPriceMap.get(product.getId()) | 438 | BigDecimal originMaxPrice = pId3ttMaxPriceMap.get(product.getId()) |
423 | .divide(new BigDecimal("0.7"), 2, RoundingMode.HALF_UP) | 439 | .divide(new BigDecimal("0.7"), 2, RoundingMode.HALF_UP) |
424 | .divide(new BigDecimal("0.5"), 2, RoundingMode.HALF_UP) | 440 | .divide(new BigDecimal("0.5"), 2, RoundingMode.HALF_UP) |
425 | .multiply(new BigDecimal("0.148")) | 441 | .multiply(new BigDecimal("0.148")) |
426 | .setScale(2, RoundingMode.HALF_UP); | 442 | .setScale(2, RoundingMode.HALF_UP); |
443 | + | ||
427 | BigDecimal computedPrice = originMaxPrice | 444 | BigDecimal computedPrice = originMaxPrice |
428 | .multiply(new BigDecimal("0.5")) | 445 | .multiply(new BigDecimal("0.5")) |
429 | .setScale(2, RoundingMode.HALF_UP); | 446 | .setScale(2, RoundingMode.HALF_UP); |
447 | + | ||
430 | product.setMaxPrice(computedPrice); | 448 | product.setMaxPrice(computedPrice); |
431 | product.setOriginMaxPrice(originMaxPrice); | 449 | product.setOriginMaxPrice(originMaxPrice); |
432 | } | 450 | } |
433 | } | 451 | } |
434 | - //将productDO的productimageliststore解析为map | ||
435 | - List<Map> maps = JSON.parseArray(product.getProductimageliststore(), Map.class); | ||
436 | - Map map = maps.get(0); | ||
437 | - product.setImageFileKey(map.get("fileKey").toString()); | ||
438 | 452 | ||
453 | + // 解析 productimageliststore | ||
454 | + if (product.getProductimageliststore() != null) { // 【修改8】确保不为 null | ||
455 | + List<Map> maps = JSON.parseArray(product.getProductimageliststore(), Map.class); | ||
456 | + if (maps != null && !maps.isEmpty()) { // 【修改9】确保 maps 不是空的 | ||
457 | + Map map = maps.get(0); | ||
458 | + if (map != null && map.get("fileKey") != null) { // 【修改10】确保 map 及 fileKey 存在 | ||
459 | + product.setImageFileKey(map.get("fileKey").toString()); | ||
460 | + } | ||
461 | + } | ||
462 | + } | ||
439 | }); | 463 | }); |
440 | } | 464 | } |
441 | 465 | ||
466 | + | ||
442 | @Override | 467 | @Override |
443 | public ServerResult listBySimilar(ProductQueryVO productQueryVO){ | 468 | public ServerResult listBySimilar(ProductQueryVO productQueryVO){ |
444 | if(StringUtils.isBlank(productQueryVO.getKeyword())){ | 469 | if(StringUtils.isBlank(productQueryVO.getKeyword())){ |
@@ -478,7 +503,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | @@ -478,7 +503,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | ||
478 | if (StringUtils.isNotBlank(productQueryVO.getKeyword())) { | 503 | if (StringUtils.isNotBlank(productQueryVO.getKeyword())) { |
479 | // String keyword = productQueryVO.getKeyword().trim(); | 504 | // String keyword = productQueryVO.getKeyword().trim(); |
480 | 505 | ||
481 | - // 先精确匹配完整的关键字 | 506 | + // 先精准匹配 |
482 | List<TicketTypeDO> tickeyTypeDOList = ticketTypeService.lambdaQuery() | 507 | List<TicketTypeDO> tickeyTypeDOList = ticketTypeService.lambdaQuery() |
483 | .like(TicketTypeDO::getRank, keyword) | 508 | .like(TicketTypeDO::getRank, keyword) |
484 | .list(); | 509 | .list(); |
@@ -493,23 +518,29 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | @@ -493,23 +518,29 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | ||
493 | } | 518 | } |
494 | 519 | ||
495 | if (productIds.isEmpty()) { | 520 | if (productIds.isEmpty()) { |
496 | - // 如果没有匹配到,分词处理 | ||
497 | - String[] keywords = keyword.split("\\s+"); | 521 | + // 如果没有匹配到,进行分词处理 |
522 | + String[] keywords = Arrays.stream(keyword.split("\\s+")) | ||
523 | + .filter(StringUtils::isNotBlank) | ||
524 | + .toArray(String[]::new); | ||
498 | 525 | ||
499 | queryWapper.and(subQueryWapper -> { | 526 | queryWapper.and(subQueryWapper -> { |
500 | - // 查询包含所有分词的记录 | 527 | + // **必须包含所有分词** |
501 | for (String key : keywords) { | 528 | for (String key : keywords) { |
502 | - subQueryWapper.like("p.name", key).or(); | 529 | + subQueryWapper.like("p.name", key); |
503 | } | 530 | } |
504 | }); | 531 | }); |
532 | + | ||
505 | } else { | 533 | } else { |
506 | Set<String> finalProductIds = productIds; | 534 | Set<String> finalProductIds = productIds; |
507 | 535 | ||
508 | queryWapper.and(subQueryWapper -> { | 536 | queryWapper.and(subQueryWapper -> { |
509 | - subQueryWapper.like("p.name", keyword).or().in(CollUtil.isNotEmpty(finalProductIds), "p.id", finalProductIds); | 537 | + subQueryWapper.like("p.name", keyword) |
538 | + .or() | ||
539 | + .in(CollUtil.isNotEmpty(finalProductIds), "p.id", finalProductIds); | ||
510 | }); | 540 | }); |
511 | } | 541 | } |
512 | } | 542 | } |
543 | + | ||
513 | List<ProductDO> productDOS = this.baseMapper.queryList(queryWapper); | 544 | List<ProductDO> productDOS = this.baseMapper.queryList(queryWapper); |
514 | // Split the keyword into individual words | 545 | // Split the keyword into individual words |
515 | String[] keywords = keyword.split("\\s+"); | 546 | String[] keywords = keyword.split("\\s+"); |