Commit 752e49560dec7b972b4025d16b9987f4e2744cfb
Merge remote-tracking branch 'origin/master'
# Conflicts: # shop/src/main/java/com/canrd/shop/service/impl/ProductServiceImpl.java
Showing
12 changed files
with
182 additions
and
36 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
... | ... | @@ -6,7 +6,6 @@ import com.alibaba.fastjson2.JSON; |
6 | 6 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
7 | 7 | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
8 | 8 | import com.baomidou.mybatisplus.core.metadata.IPage; |
9 | -import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; | |
10 | 9 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
11 | 10 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
12 | 11 | import com.canrd.shop.common.constant.PriceConstants; |
... | ... | @@ -18,15 +17,14 @@ import com.canrd.shop.common.utils.StringUtils; |
18 | 17 | import com.canrd.shop.converter.ProductConverter; |
19 | 18 | import com.canrd.shop.mapper.ProductMapper; |
20 | 19 | 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; | |
20 | +import com.canrd.shop.module.vo.*; | |
25 | 21 | import com.canrd.shop.service.*; |
26 | 22 | import com.google.common.collect.Lists; |
27 | 23 | import com.google.common.collect.Sets; |
28 | 24 | import lombok.extern.slf4j.Slf4j; |
25 | +import org.apache.commons.collections4.CollectionUtils; | |
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 | |
... | ... | @@ -78,6 +76,12 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im |
78 | 76 | private IEbCategorysRelationService ebCategorysRelationService; |
79 | 77 | |
80 | 78 | @Autowired |
79 | + private ProductCategoryService categoryService; | |
80 | + | |
81 | + @Autowired | |
82 | + private ProductFunctionService functionService; | |
83 | + | |
84 | + @Autowired | |
81 | 85 | private IJournalCategoryRelationService journalCategoryService; |
82 | 86 | @Autowired |
83 | 87 | private IJournalService journalService; |
... | ... | @@ -139,6 +143,50 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im |
139 | 143 | .list(); |
140 | 144 | List<String> categoryIds = productcategoryrelations.stream().map(Productcategoryrelation::getCategoryId).collect(Collectors.toList()); |
141 | 145 | |
146 | + if (CollectionUtils.isNotEmpty(categoryIds)) { | |
147 | + // 查询第一级分类 | |
148 | + List<ProductCategoryDO> productCategories = categoryService.lambdaQuery() | |
149 | + .in(ProductCategoryDO::getId, categoryIds) | |
150 | + .eq(ProductCategoryDO::getParentId, "33ba6943c8bf4ca082614f299865341c") | |
151 | + .list(); | |
152 | + ProductCategoryDO firstCategory = null; | |
153 | + if (productCategories.isEmpty()) { | |
154 | + firstCategory = findParentCategory(categoryIds, "33ba6943c8bf4ca082614f299865341c"); | |
155 | + } else { | |
156 | + firstCategory = productCategories.get(0); | |
157 | + } | |
158 | + | |
159 | + if (firstCategory != null) { | |
160 | + // 查询第二级分类 | |
161 | + List<ProductCategoryDO> productCategories2 = categoryService.lambdaQuery() | |
162 | + .in(ProductCategoryDO::getId, categoryIds) | |
163 | + .eq(ProductCategoryDO::getParentId, firstCategory.getId()) | |
164 | + .list(); | |
165 | + ProductCategoryDO secondCategory = productCategories2.stream() | |
166 | + .filter(category -> !"Not specified".equals(category.getName())) | |
167 | + .findFirst() | |
168 | + .orElse(productCategories2.isEmpty() ? null : productCategories2.get(0)); // 如果找不到,使用列表第一个元素或 null | |
169 | + | |
170 | + // 获取功能信息 | |
171 | + List<ProductFunctionDO> functions = functionService.getFirstFunction(productVO.getId()); | |
172 | + ProductFunctionDO selectedFunction = null; | |
173 | + if (CollectionUtils.isNotEmpty(functions)){ | |
174 | + selectedFunction = functions.stream() | |
175 | + .filter(function -> !"Not specified".equals(function.getName())) | |
176 | + .findFirst() | |
177 | + .orElse(functions.get(0)); // 如果找不到,使用列表第一个元素 | |
178 | + } | |
179 | + | |
180 | + // 构建面包屑信息 | |
181 | + ProductCrumbsVO productCrumbsVO = ProductCrumbsVO.builder() | |
182 | + .category1(firstCategory.getName()) | |
183 | + .category2(secondCategory != null ? secondCategory.getName() : null) | |
184 | + .function(selectedFunction != null ? selectedFunction.getName() : null) | |
185 | + .build(); | |
186 | + productVO.setProductCrumbsVO(productCrumbsVO); | |
187 | + } | |
188 | + } | |
189 | + | |
142 | 190 | List<EbCategorysRelation> cateRelations = ebCategorysRelationService.lambdaQuery() |
143 | 191 | .in(EbCategorysRelation::getCategoryId, categoryIds) |
144 | 192 | .list(); |
... | ... | @@ -148,20 +196,22 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im |
148 | 196 | TreeMap<String, Integer> cateId2Relevance = new TreeMap<>(relatedCateId2Relevance); |
149 | 197 | StringJoiner orderByJoiner = new StringJoiner(" when "," order by case pc.id when "," else 0 end "); |
150 | 198 | cateId2Relevance.forEach((k,v)->orderByJoiner.add("'"+k+"'"+" then "+v)); |
151 | - QueryWrapper<ProductDO> objectQueryWrapper = new QueryWrapper<ProductDO>() | |
152 | - .eq("ismarketable",true) | |
153 | - .in("pc.id", cateId2Relevance.keySet()) | |
154 | - .select("distinct p.id","pc.id") | |
155 | - .last(orderByJoiner.toString()); | |
156 | - | |
157 | - List<ProductDO> relatedProducts = this | |
158 | - .list(objectQueryWrapper); | |
159 | - List<String> relatedProductIds = relatedProducts.stream() | |
160 | - .map(ProductDO::getId) | |
161 | - .distinct() | |
162 | - .limit(10) | |
163 | - .collect(Collectors.toList()); | |
164 | - productVO.setRelatedProductIds(relatedProductIds); | |
199 | + if(CollUtil.isNotEmpty(cateId2Relevance)) { | |
200 | + QueryWrapper<ProductDO> objectQueryWrapper = new QueryWrapper<ProductDO>() | |
201 | + .eq("ismarketable",true) | |
202 | + .in("pc.id", cateId2Relevance.keySet()) | |
203 | + .select("distinct p.id","pc.id") | |
204 | + .last(orderByJoiner.toString()); | |
205 | + | |
206 | + List<ProductDO> relatedProducts = this | |
207 | + .list(objectQueryWrapper); | |
208 | + List<String> relatedProductIds = relatedProducts.stream() | |
209 | + .map(ProductDO::getId) | |
210 | + .distinct() | |
211 | + .limit(10) | |
212 | + .collect(Collectors.toList()); | |
213 | + productVO.setRelatedProductIds(relatedProductIds); | |
214 | + } | |
165 | 215 | |
166 | 216 | List<JournalCategoryRelation> journalCategoryRelations = journalCategoryService.lambdaQuery() |
167 | 217 | .in(JournalCategoryRelation::getCategoryId, categoryIds) |
... | ... | @@ -171,12 +221,43 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im |
171 | 221 | .in(Journal::getId, journalIds) |
172 | 222 | .page(new Page<>(1,10)).getRecords(); |
173 | 223 | productVO.setJournals(journals); |
224 | + | |
174 | 225 | productVO.setPriceShow(productPriceShow); |
175 | 226 | |
176 | 227 | |
177 | 228 | return ServerResult.success(productVO); |
178 | 229 | } |
179 | 230 | |
231 | + //查找一级分类 | |
232 | + private ProductCategoryDO findParentCategory(List<String> categoryIds, String targetParentId) { | |
233 | + for (String categoryId : categoryIds) { | |
234 | + // 查询当前 categoryId 的数据 | |
235 | + ProductCategoryDO currentCategory = categoryService.lambdaQuery() | |
236 | + .eq(ProductCategoryDO::getId, categoryId) | |
237 | + .one(); | |
238 | + if (currentCategory != null) { | |
239 | + // 如果当前分类的 parentId 等于目标 parentId,返回当前分类 | |
240 | + if (targetParentId.equals(currentCategory.getParentId())) { | |
241 | + return currentCategory; | |
242 | + } | |
243 | + // 否则递归查找 parentId 的分类 | |
244 | + ProductCategoryDO parentCategory = categoryService.lambdaQuery() | |
245 | + .eq(ProductCategoryDO::getId, currentCategory.getParentId()) | |
246 | + .one(); | |
247 | + if (parentCategory != null) { | |
248 | + ProductCategoryDO foundCategory = findParentCategory( | |
249 | + Collections.singletonList(parentCategory.getId()), | |
250 | + targetParentId | |
251 | + ); | |
252 | + if (foundCategory != null) { | |
253 | + return foundCategory; | |
254 | + } | |
255 | + } | |
256 | + } | |
257 | + } | |
258 | + return null; // 如果未找到符合条件的分类,返回 null | |
259 | + } | |
260 | + | |
180 | 261 | /** |
181 | 262 | * 分页查询 |
182 | 263 | * |
... | ... | @@ -235,7 +316,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im |
235 | 316 | } |
236 | 317 | } |
237 | 318 | |
238 | - List<Productcategoryrelation> productcategoryrelations = productCategoryRelationService.lambdaQuery() | |
319 | + /*List<Productcategoryrelation> productcategoryrelations = productCategoryRelationService.lambdaQuery() | |
239 | 320 | .in(Productcategoryrelation::getProductId, productIds) |
240 | 321 | .list(); |
241 | 322 | List<String> categoryIds = productcategoryrelations.stream().map(Productcategoryrelation::getCategoryId).collect(Collectors.toList()); |
... | ... | @@ -251,7 +332,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im |
251 | 332 | Map<String,List<EbCategorysRelation>> prodId2relCateIds = productId2cateIds.entrySet().stream() |
252 | 333 | .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().stream() |
253 | 334 | .flatMap(cateId -> Optional.ofNullable(cateId2relCates.get(cateId)).orElse(Collections.emptyList()).stream()) |
254 | - .collect(Collectors.toList()))); | |
335 | + .collect(Collectors.toList())));*/ | |
255 | 336 | records.forEach(product -> { |
256 | 337 | if (productPriceShow){ |
257 | 338 | if (Objects.nonNull(pId2ttMinPriceMap.get(product.getId()))){ |
... | ... | @@ -264,8 +345,8 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im |
264 | 345 | product.setImageFileKey(map.get("fileKey").toString()); |
265 | 346 | |
266 | 347 | |
267 | - List<EbCategorysRelation> ebCategorysRelations = prodId2relCateIds.get(product.getId()); | |
268 | - if (CollUtil.isNotEmpty(ebCategorysRelations)){ | |
348 | + /*List<EbCategorysRelation> ebCategorysRelations = prodId2relCateIds.get(product.getId());*/ | |
349 | + /*if (CollUtil.isNotEmpty(ebCategorysRelations)){ | |
269 | 350 | Map<String,Integer> relatedCateId2Relevance = ebCategorysRelations.stream() |
270 | 351 | .collect(Collectors.toMap(EbCategorysRelation::getRelatedCategoryId, EbCategorysRelation::getRelevance,(a, b) -> a)); |
271 | 352 | TreeMap<String, Integer> cateId2Relevance = new TreeMap<>(relatedCateId2Relevance); |
... | ... | @@ -283,7 +364,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im |
283 | 364 | .limit(10) |
284 | 365 | .collect(Collectors.toList()); |
285 | 366 | product.setRelatedProductIds(relatedProductIds); |
286 | - } | |
367 | + }*/ | |
287 | 368 | }); |
288 | 369 | } |
289 | 370 | ... | ... |
shop/src/main/resources/application-local.yml
... | ... | @@ -59,11 +59,11 @@ spring: |
59 | 59 | testOnReturn: true |
60 | 60 | password: 123456 |
61 | 61 | time-between-eviction-runs-millis: 1000 |
62 | - url: jdbc:mysql://127.0.0.1:3306/canrd_overseas?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&useSSL=false&autoReconnect=true&failOverReadOnly=false&maxReconnects=10&allowMultiQueries=true&useAffectedRows=true&autoReconnectForPools=true | |
62 | + url: jdbc:mysql://192.168.31.242:13306/canrd_overseas?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&useSSL=false&autoReconnect=true&failOverReadOnly=false&maxReconnects=10&allowMultiQueries=true&useAffectedRows=true&autoReconnectForPools=true | |
63 | 63 | username: root |
64 | 64 | |
65 | 65 | |
66 | -logging: | |
67 | - config: classpath:log4j2-dev.xml | |
66 | +#logging: | |
67 | +# config: classpath:log4jj2-dev.xml | |
68 | 68 | openai: |
69 | 69 | token: Bearer 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 |
70 | 70 | \ No newline at end of file | ... | ... |
shop/src/main/resources/application.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 | ... | ... |
shop/src/main/resources/mapper/ProductMapper.xml
... | ... | @@ -7,7 +7,7 @@ |
7 | 7 | </resultMap> |
8 | 8 | |
9 | 9 | <select id="queryAll" resultMap="BaseResultMap"> |
10 | - select distinct p.id,p.name,p.productImageListStore,p.htmlFilePath,pc.id as pcid | |
10 | + select distinct p.id,p.name,p.productImageListStore,p.htmlFilePath | |
11 | 11 | from product p |
12 | 12 | left join productcategoryrelation pcr on p.id=pcr.productId |
13 | 13 | left join productcategory pc on pc.id=pcr.categoryId |
... | ... | @@ -17,7 +17,7 @@ |
17 | 17 | </select> |
18 | 18 | |
19 | 19 | <select id="queryList" resultMap="BaseResultMap"> |
20 | - select distinct p.id,p.name,p.productImageListStore,p.htmlFilePath,p.productCategory_id,pc.id as pcid | |
20 | + select distinct p.id,p.name,p.productImageListStore,p.htmlFilePath,p.productCategory_id | |
21 | 21 | from product p |
22 | 22 | left join productcategoryrelation pcr on p.id=pcr.productId |
23 | 23 | left join productcategory pc on pc.id=pcr.categoryId | ... | ... |