Commit afbd3382ae7c8374ad37410b6aef34a0101af122
1 parent
70f3e3ed
Add product-function relation and optimize service
Showing
10 changed files
with
111 additions
and
12 deletions
shop/src/main/java/com/canrd/shop/mapper/ProductFunctionMapper.java
... | ... | @@ -4,6 +4,9 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
4 | 4 | import com.canrd.shop.module.dto.ProductCategoryDO; |
5 | 5 | import com.canrd.shop.module.dto.ProductFunctionDO; |
6 | 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 | 12 | * @author hongtao.zhao |
... | ... | @@ -11,4 +14,5 @@ import org.apache.ibatis.annotations.Mapper; |
11 | 14 | */ |
12 | 15 | @Mapper |
13 | 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
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
shop/src/main/java/com/canrd/shop/service/ProductFunctionService.java
1 | 1 | package com.canrd.shop.service; |
2 | 2 | |
3 | 3 | import com.baomidou.mybatisplus.extension.service.IService; |
4 | -import com.canrd.shop.module.dto.ProductCategoryDO; | |
5 | 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 | 10 | * @author hongtao.zhao |
9 | 11 | * @since 2023-06-10 |
10 | 12 | */ |
11 | 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 | 1 | package com.canrd.shop.service.impl; |
2 | 2 | |
3 | 3 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
4 | -import com.canrd.shop.mapper.ProductCategoryMapper; | |
5 | 4 | import com.canrd.shop.mapper.ProductFunctionMapper; |
6 | -import com.canrd.shop.module.dto.ProductCategoryDO; | |
7 | 5 | import com.canrd.shop.module.dto.ProductFunctionDO; |
8 | -import com.canrd.shop.service.ProductCategoryService; | |
9 | 6 | import com.canrd.shop.service.ProductFunctionService; |
7 | +import org.apache.ibatis.annotations.Param; | |
10 | 8 | import org.springframework.stereotype.Service; |
11 | 9 | |
10 | +import java.util.List; | |
11 | + | |
12 | 12 | /** |
13 | 13 | * @author hongtao.zhao |
14 | 14 | * @since 2023-06-10 |
15 | 15 | */ |
16 | 16 | @Service |
17 | 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 | 18 | import com.canrd.shop.converter.ProductConverter; |
19 | 19 | import com.canrd.shop.mapper.ProductMapper; |
20 | 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 | 22 | import com.canrd.shop.service.*; |
26 | 23 | import com.google.common.collect.Lists; |
27 | 24 | import com.google.common.collect.Sets; |
28 | 25 | import lombok.extern.slf4j.Slf4j; |
29 | 26 | import org.apache.commons.lang3.BooleanUtils; |
27 | +import org.apache.ibatis.annotations.Param; | |
30 | 28 | import org.springframework.beans.factory.annotation.Autowired; |
31 | 29 | import org.springframework.stereotype.Service; |
32 | 30 | |
... | ... | @@ -77,6 +75,12 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im |
77 | 75 | @Autowired |
78 | 76 | private IEbCategorysRelationService ebCategorysRelationService; |
79 | 77 | |
78 | + @Autowired | |
79 | + private ProductCategoryService categoryService; | |
80 | + | |
81 | + @Autowired | |
82 | + private ProductFunctionService functionService; | |
83 | + | |
80 | 84 | /** |
81 | 85 | * 通过ID查询单条数据 |
82 | 86 | * <p> |
... | ... | @@ -134,6 +138,36 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im |
134 | 138 | .list(); |
135 | 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 | 171 | List<EbCategorysRelation> cateRelations = ebCategorysRelationService.lambdaQuery() |
138 | 172 | .in(EbCategorysRelation::getCategoryId, categoryIds) |
139 | 173 | .list(); | ... | ... |
shop/src/main/resources/application-local.yml
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 | 18 | \ No newline at end of file | ... | ... |