Commit afbd3382ae7c8374ad37410b6aef34a0101af122

Authored by 柏杨
1 parent 70f3e3ed

Add product-function relation and optimize service

shop/src/main/java/com/canrd/shop/mapper/ProductFunctionMapper.java
@@ -4,6 +4,9 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; @@ -4,6 +4,9 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4 import com.canrd.shop.module.dto.ProductCategoryDO; 4 import com.canrd.shop.module.dto.ProductCategoryDO;
5 import com.canrd.shop.module.dto.ProductFunctionDO; 5 import com.canrd.shop.module.dto.ProductFunctionDO;
6 import org.apache.ibatis.annotations.Mapper; 6 import org.apache.ibatis.annotations.Mapper;
  7 +import org.apache.ibatis.annotations.Param;
  8 +
  9 +import java.util.List;
7 10
8 /** 11 /**
9 * @author hongtao.zhao 12 * @author hongtao.zhao
@@ -11,4 +14,5 @@ import org.apache.ibatis.annotations.Mapper; @@ -11,4 +14,5 @@ import org.apache.ibatis.annotations.Mapper;
11 */ 14 */
12 @Mapper 15 @Mapper
13 public interface ProductFunctionMapper extends BaseMapper<ProductFunctionDO> { 16 public interface ProductFunctionMapper extends BaseMapper<ProductFunctionDO> {
  17 + List<ProductFunctionDO> getFirstFunction(@Param("id") String productId);
14 } 18 }
shop/src/main/java/com/canrd/shop/module/dto/FunctionDO.java 0 → 100644
  1 +package com.canrd.shop.module.dto;
  2 +
  3 +import com.baomidou.mybatisplus.annotation.TableField;
  4 +import com.baomidou.mybatisplus.annotation.TableName;
  5 +import lombok.*;
  6 +import lombok.experimental.SuperBuilder;
  7 +
  8 +@TableName("productfunctionrelation")
  9 +@Data
  10 +@AllArgsConstructor
  11 +@ToString
  12 +@NoArgsConstructor
  13 +@EqualsAndHashCode(callSuper = false)
  14 +@SuperBuilder
  15 +public class FunctionDO {
  16 + @TableField(value = "productId")
  17 + private String productId;
  18 + @TableField(value = "functionId")
  19 + private String functionId;
  20 +}
  21 +
shop/src/main/java/com/canrd/shop/module/dto/ProductFunctionDO.java
@@ -24,6 +24,4 @@ public class ProductFunctionDO { @@ -24,6 +24,4 @@ public class ProductFunctionDO {
24 private String id; 24 private String id;
25 25
26 private String name; 26 private String name;
27 -  
28 -  
29 } 27 }
shop/src/main/java/com/canrd/shop/module/vo/ProductCrumbsVO.java 0 → 100644
  1 +package com.canrd.shop.module.vo;
  2 +
  3 +import lombok.*;
  4 +import lombok.experimental.SuperBuilder;
  5 +
  6 +@Data
  7 +@AllArgsConstructor
  8 +@ToString
  9 +@NoArgsConstructor
  10 +@EqualsAndHashCode(callSuper = false)
  11 +@SuperBuilder
  12 +public class ProductCrumbsVO {
  13 + String category1;
  14 + String category2;
  15 + String function;
  16 +}
shop/src/main/java/com/canrd/shop/module/vo/ProductVO.java
@@ -51,6 +51,8 @@ public class ProductVO implements Serializable { @@ -51,6 +51,8 @@ public class ProductVO implements Serializable {
51 51
52 private String metakeywords; 52 private String metakeywords;
53 53
  54 + private ProductCrumbsVO productCrumbsVO;
  55 +
54 private String name; 56 private String name;
55 57
56 private String englishname; 58 private String englishname;
shop/src/main/java/com/canrd/shop/service/ProductFunctionService.java
1 package com.canrd.shop.service; 1 package com.canrd.shop.service;
2 2
3 import com.baomidou.mybatisplus.extension.service.IService; 3 import com.baomidou.mybatisplus.extension.service.IService;
4 -import com.canrd.shop.module.dto.ProductCategoryDO;  
5 import com.canrd.shop.module.dto.ProductFunctionDO; 4 import com.canrd.shop.module.dto.ProductFunctionDO;
  5 +import org.apache.ibatis.annotations.Param;
  6 +
  7 +import java.util.List;
6 8
7 /** 9 /**
8 * @author hongtao.zhao 10 * @author hongtao.zhao
9 * @since 2023-06-10 11 * @since 2023-06-10
10 */ 12 */
11 public interface ProductFunctionService extends IService<ProductFunctionDO> { 13 public interface ProductFunctionService extends IService<ProductFunctionDO> {
  14 + List<ProductFunctionDO> getFirstFunction(String id);
12 } 15 }
shop/src/main/java/com/canrd/shop/service/impl/ProductFunctionServiceImpl.java
1 package com.canrd.shop.service.impl; 1 package com.canrd.shop.service.impl;
2 2
3 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 3 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
4 -import com.canrd.shop.mapper.ProductCategoryMapper;  
5 import com.canrd.shop.mapper.ProductFunctionMapper; 4 import com.canrd.shop.mapper.ProductFunctionMapper;
6 -import com.canrd.shop.module.dto.ProductCategoryDO;  
7 import com.canrd.shop.module.dto.ProductFunctionDO; 5 import com.canrd.shop.module.dto.ProductFunctionDO;
8 -import com.canrd.shop.service.ProductCategoryService;  
9 import com.canrd.shop.service.ProductFunctionService; 6 import com.canrd.shop.service.ProductFunctionService;
  7 +import org.apache.ibatis.annotations.Param;
10 import org.springframework.stereotype.Service; 8 import org.springframework.stereotype.Service;
11 9
  10 +import java.util.List;
  11 +
12 /** 12 /**
13 * @author hongtao.zhao 13 * @author hongtao.zhao
14 * @since 2023-06-10 14 * @since 2023-06-10
15 */ 15 */
16 @Service 16 @Service
17 public class ProductFunctionServiceImpl extends ServiceImpl<ProductFunctionMapper, ProductFunctionDO> implements ProductFunctionService { 17 public class ProductFunctionServiceImpl extends ServiceImpl<ProductFunctionMapper, ProductFunctionDO> implements ProductFunctionService {
  18 + @Override
  19 + public List<ProductFunctionDO> getFirstFunction(@Param("id")String productId) {
  20 + return this.baseMapper.getFirstFunction(productId);
  21 + }
18 } 22 }
shop/src/main/java/com/canrd/shop/service/impl/ProductServiceImpl.java
@@ -18,15 +18,13 @@ import com.canrd.shop.common.utils.StringUtils; @@ -18,15 +18,13 @@ import com.canrd.shop.common.utils.StringUtils;
18 import com.canrd.shop.converter.ProductConverter; 18 import com.canrd.shop.converter.ProductConverter;
19 import com.canrd.shop.mapper.ProductMapper; 19 import com.canrd.shop.mapper.ProductMapper;
20 import com.canrd.shop.module.dto.*; 20 import com.canrd.shop.module.dto.*;
21 -import com.canrd.shop.module.vo.ProductAttributeVO;  
22 -import com.canrd.shop.module.vo.ProductQueryVO;  
23 -import com.canrd.shop.module.vo.ProductVO;  
24 -import com.canrd.shop.module.vo.TickeyTypeVO; 21 +import com.canrd.shop.module.vo.*;
25 import com.canrd.shop.service.*; 22 import com.canrd.shop.service.*;
26 import com.google.common.collect.Lists; 23 import com.google.common.collect.Lists;
27 import com.google.common.collect.Sets; 24 import com.google.common.collect.Sets;
28 import lombok.extern.slf4j.Slf4j; 25 import lombok.extern.slf4j.Slf4j;
29 import org.apache.commons.lang3.BooleanUtils; 26 import org.apache.commons.lang3.BooleanUtils;
  27 +import org.apache.ibatis.annotations.Param;
30 import org.springframework.beans.factory.annotation.Autowired; 28 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Service; 29 import org.springframework.stereotype.Service;
32 30
@@ -77,6 +75,12 @@ public class ProductServiceImpl extends ServiceImpl&lt;ProductMapper, ProductDO&gt; im @@ -77,6 +75,12 @@ public class ProductServiceImpl extends ServiceImpl&lt;ProductMapper, ProductDO&gt; im
77 @Autowired 75 @Autowired
78 private IEbCategorysRelationService ebCategorysRelationService; 76 private IEbCategorysRelationService ebCategorysRelationService;
79 77
  78 + @Autowired
  79 + private ProductCategoryService categoryService;
  80 +
  81 + @Autowired
  82 + private ProductFunctionService functionService;
  83 +
80 /** 84 /**
81 * 通过ID查询单条数据 85 * 通过ID查询单条数据
82 * <p> 86 * <p>
@@ -134,6 +138,36 @@ public class ProductServiceImpl extends ServiceImpl&lt;ProductMapper, ProductDO&gt; im @@ -134,6 +138,36 @@ public class ProductServiceImpl extends ServiceImpl&lt;ProductMapper, ProductDO&gt; im
134 .list(); 138 .list();
135 List<String> categoryIds = productcategoryrelations.stream().map(Productcategoryrelation::getCategoryId).collect(Collectors.toList()); 139 List<String> categoryIds = productcategoryrelations.stream().map(Productcategoryrelation::getCategoryId).collect(Collectors.toList());
136 140
  141 + List<ProductCategoryDO> productCategories = categoryService.lambdaQuery()
  142 + .in(ProductCategoryDO::getId, categoryIds)
  143 + .eq(ProductCategoryDO::getParentId, "33ba6943c8bf4ca082614f299865341c")
  144 + .list();
  145 + ProductCategoryDO firstCategory = productCategories.get(0);
  146 + List<ProductCategoryDO> productCategories2 = categoryService.lambdaQuery()
  147 + .in(ProductCategoryDO::getId, categoryIds)
  148 + .eq(ProductCategoryDO::getParentId, firstCategory.getId())
  149 + .list();
  150 +// ProductCategoryDO secondCategory = productCategories2.get(0);
  151 + ProductCategoryDO secondCategory = productCategories2.stream()
  152 + .filter(category -> !"Not specified".equals(category.getName()))
  153 + .findFirst()
  154 + .orElse(productCategories2.get(0)); // 如果找不到,使用列表第一个元素
  155 + // 从 functionService 获取 functionId
  156 + List<ProductFunctionDO> functions = functionService.getFirstFunction(productVO.getId());
  157 + ProductFunctionDO selectedFunction = null;
  158 + if(functions != null && functions.size() > 0) {
  159 + selectedFunction = functions.stream()
  160 + .filter(function -> !"Not specified".equals(function.getName()))
  161 + .findFirst()
  162 + .orElse(functions.get(0)); // 如果找不到,使用列表第一个元素
  163 + }
  164 + ProductCrumbsVO productCrumbsVO = ProductCrumbsVO.builder()
  165 + .category1(firstCategory.getName()) //
  166 + .category2(secondCategory.getName())
  167 + .function(selectedFunction.getName())
  168 + .build();
  169 + productVO.setProductCrumbsVO(productCrumbsVO);
  170 +
137 List<EbCategorysRelation> cateRelations = ebCategorysRelationService.lambdaQuery() 171 List<EbCategorysRelation> cateRelations = ebCategorysRelationService.lambdaQuery()
138 .in(EbCategorysRelation::getCategoryId, categoryIds) 172 .in(EbCategorysRelation::getCategoryId, categoryIds)
139 .list(); 173 .list();
shop/src/main/resources/application-local.yml
@@ -63,7 +63,7 @@ spring: @@ -63,7 +63,7 @@ spring:
63 username: root 63 username: root
64 64
65 65
66 -logging:  
67 - config: classpath:log4j2-dev.xml 66 +#logging:
  67 +# config: classpath:log4jj2-dev.xml
68 openai: 68 openai:
69 token: Bearer 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 69 token: Bearer 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
70 \ No newline at end of file 70 \ No newline at end of file
shop/src/main/resources/mapper/ProductFunctionMapper.xml 0 → 100644
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3 +<mapper namespace="com.canrd.shop.mapper.ProductFunctionMapper">
  4 +
  5 +<!-- <select id="getFirstFunction" resultType="com.canrd.shop.module.dto.ProductFunctionDO"-->
  6 +<!-- parameterType="java.lang.String">-->
  7 + <select id="getFirstFunction" resultType="com.canrd.shop.module.dto.ProductFunctionDO"
  8 + parameterType="java.lang.String">
  9 + SELECT pf.*
  10 + FROM productfunction pf
  11 + WHERE pf.id IN (
  12 + SELECT pfr.functionId
  13 + FROM productfunctionrelation pfr
  14 + WHERE pfr.productId = #{id}
  15 + );
  16 + </select>
  17 +</mapper>
0 \ No newline at end of file 18 \ No newline at end of file