Commit 6048cbb280b859484d4ae3c9fc9989338f170400
1 parent
926db6dd
refactor(product): 重构产品列表查询逻辑
- 提取公共方法 suppleProducts 补充产品信息- 优化产品价格显示逻辑,移除重复代码 - 统一处理产品图片列表解析 - 删除冗余变量,简化代码结构
Showing
1 changed file
with
30 additions
and
21 deletions
shop/src/main/java/com/canrd/shop/service/impl/ProductServiceImpl.java
@@ -160,10 +160,39 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | @@ -160,10 +160,39 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | ||
160 | } | 160 | } |
161 | Page page = new Page<>(productQueryVO.getPageNo(), productQueryVO.getPageSize()); | 161 | Page page = new Page<>(productQueryVO.getPageNo(), productQueryVO.getPageSize()); |
162 | IPage<ProductDO> iPage = productMapper.selectAll(page,queryWapper); | 162 | IPage<ProductDO> iPage = productMapper.selectAll(page,queryWapper); |
163 | + suppleProducts(iPage.getRecords()); | ||
163 | productQueryVO.setTotal(Long.valueOf(iPage.getTotal()).intValue()); | 164 | productQueryVO.setTotal(Long.valueOf(iPage.getTotal()).intValue()); |
164 | return ServerResult.success().setData(PageUtils.getPageReturn(iPage.getRecords(), productQueryVO)); | 165 | return ServerResult.success().setData(PageUtils.getPageReturn(iPage.getRecords(), productQueryVO)); |
165 | } | 166 | } |
166 | 167 | ||
168 | + private void suppleProducts(List<ProductDO> records) { | ||
169 | + Boolean productPriceShow = switchControlService.getEnabledByName(SwitchControlConstants.PRODUCT_PRICE_SHOW); | ||
170 | + List<String> productIds = records.stream().map(ProductDO::getId).collect(Collectors.toList()); | ||
171 | + List<TicketTypeDO> tickeyTypeDOList = ticketTypeService.lambdaQuery().in(TicketTypeDO::getProductId, productIds).list(); | ||
172 | + Map<String,List<TicketTypeDO>> pId2ttDOsMap = tickeyTypeDOList.stream().collect(Collectors.groupingBy(TicketTypeDO::getProductId)); | ||
173 | + Map<String,BigDecimal> pId2ttMinPriceMap = new HashMap<>(); | ||
174 | + for (TicketTypeDO ticketTypeDO : tickeyTypeDOList) { | ||
175 | + if (Objects.isNull(pId2ttMinPriceMap.get(ticketTypeDO.getProductId()))){ | ||
176 | + pId2ttMinPriceMap.put(ticketTypeDO.getProductId(), ticketTypeDO.getPrice()); | ||
177 | + }else { | ||
178 | + if (pId2ttMinPriceMap.get(ticketTypeDO.getProductId()).compareTo(ticketTypeDO.getPrice())>0){ | ||
179 | + pId2ttMinPriceMap.put(ticketTypeDO.getProductId(), ticketTypeDO.getPrice()); | ||
180 | + } | ||
181 | + } | ||
182 | + } | ||
183 | + records.forEach(product -> { | ||
184 | + if (productPriceShow){ | ||
185 | + if (Objects.nonNull(pId2ttMinPriceMap.get(product.getId()))){ | ||
186 | + product.setPrice(pId2ttMinPriceMap.get(product.getId())); | ||
187 | + } | ||
188 | + } | ||
189 | + //将productDO的productimageliststore解析为map | ||
190 | + List<Map> maps = JSON.parseArray(product.getProductimageliststore(), Map.class); | ||
191 | + Map map = maps.get(0); | ||
192 | + product.setImageFileKey(map.get("fileKey").toString()); | ||
193 | + }); | ||
194 | + } | ||
195 | + | ||
167 | @Override | 196 | @Override |
168 | public ServerResult listBySimilar(ProductQueryVO productQueryVO){ | 197 | public ServerResult listBySimilar(ProductQueryVO productQueryVO){ |
169 | if(StringUtils.isBlank(productQueryVO.getKeyword())){ | 198 | if(StringUtils.isBlank(productQueryVO.getKeyword())){ |
@@ -181,24 +210,15 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | @@ -181,24 +210,15 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | ||
181 | } | 210 | } |
182 | Set<String> productIds = null; | 211 | Set<String> productIds = null; |
183 | Map<String,List<TicketTypeDO>> pId2ttDOsMap = null; | 212 | Map<String,List<TicketTypeDO>> pId2ttDOsMap = null; |
184 | - Map<String,BigDecimal> pId2ttMinPriceMap = null; | ||
185 | boolean needLikeMatch = false; | 213 | boolean needLikeMatch = false; |
186 | if(StringUtils.isNotBlank(productQueryVO.getKeyword())){ | 214 | if(StringUtils.isNotBlank(productQueryVO.getKeyword())){ |
187 | List<TicketTypeDO> tickeyTypeDOList = ticketTypeService.lambdaQuery().like(TicketTypeDO::getRank, productQueryVO.getKeyword()).list(); | 215 | List<TicketTypeDO> tickeyTypeDOList = ticketTypeService.lambdaQuery().like(TicketTypeDO::getRank, productQueryVO.getKeyword()).list(); |
188 | productIds = tickeyTypeDOList.stream().map(TicketTypeDO::getProductId).collect(Collectors.toSet()); | 216 | productIds = tickeyTypeDOList.stream().map(TicketTypeDO::getProductId).collect(Collectors.toSet()); |
189 | needLikeMatch = true; | 217 | needLikeMatch = true; |
190 | pId2ttDOsMap = new HashMap<>(); | 218 | pId2ttDOsMap = new HashMap<>(); |
191 | - pId2ttMinPriceMap = new HashMap<>(); | ||
192 | for (TicketTypeDO ticketTypeDO : tickeyTypeDOList) { | 219 | for (TicketTypeDO ticketTypeDO : tickeyTypeDOList) { |
193 | pId2ttDOsMap.computeIfAbsent(ticketTypeDO.getProductId(), k -> new ArrayList<>()); | 220 | pId2ttDOsMap.computeIfAbsent(ticketTypeDO.getProductId(), k -> new ArrayList<>()); |
194 | pId2ttDOsMap.get(ticketTypeDO.getProductId()).add(ticketTypeDO); | 221 | pId2ttDOsMap.get(ticketTypeDO.getProductId()).add(ticketTypeDO); |
195 | - if (Objects.isNull(pId2ttMinPriceMap.get(ticketTypeDO.getProductId()))){ | ||
196 | - pId2ttMinPriceMap.put(ticketTypeDO.getProductId(), ticketTypeDO.getPrice()); | ||
197 | - }else { | ||
198 | - if (pId2ttMinPriceMap.get(ticketTypeDO.getProductId()).compareTo(ticketTypeDO.getPrice())>0){ | ||
199 | - pId2ttMinPriceMap.put(ticketTypeDO.getProductId(), ticketTypeDO.getPrice()); | ||
200 | - } | ||
201 | - } | ||
202 | } | 222 | } |
203 | Set<String> finalProductIds = productIds; | 223 | Set<String> finalProductIds = productIds; |
204 | queryWapper.and(subQueryWapper -> { | 224 | queryWapper.and(subQueryWapper -> { |
@@ -216,13 +236,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | @@ -216,13 +236,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | ||
216 | } | 236 | } |
217 | }); | 237 | }); |
218 | List<ProductDO> similarProductList = Lists.newArrayList(); | 238 | List<ProductDO> similarProductList = Lists.newArrayList(); |
219 | - Boolean productPriceShow = switchControlService.getEnabledByName(SwitchControlConstants.PRODUCT_PRICE_SHOW); | ||
220 | for(ProductDO product:productDOS){ | 239 | for(ProductDO product:productDOS){ |
221 | - if (productPriceShow){ | ||
222 | - if (Objects.nonNull(pId2ttMinPriceMap.get(product.getId()))){ | ||
223 | - product.setPrice(pId2ttMinPriceMap.get(product.getId())); | ||
224 | - } | ||
225 | - } | ||
226 | if(accurateProductIdSet.contains(product.getId())){ | 240 | if(accurateProductIdSet.contains(product.getId())){ |
227 | continue; | 241 | continue; |
228 | } | 242 | } |
@@ -251,12 +265,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | @@ -251,12 +265,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | ||
251 | List<ProductDO> allProductList = Lists.newArrayList(); | 265 | List<ProductDO> allProductList = Lists.newArrayList(); |
252 | allProductList.addAll(accurateProducts); | 266 | allProductList.addAll(accurateProducts); |
253 | allProductList.addAll(similarProductList); | 267 | allProductList.addAll(similarProductList); |
254 | - allProductList.forEach(productDO -> { | ||
255 | - //将productDO的productimageliststore解析为map | ||
256 | - List<Map> maps = JSON.parseArray(productDO.getProductimageliststore(), Map.class); | ||
257 | - Map map = maps.get(0); | ||
258 | - productDO.setImageFileKey(map.get("fileKey").toString()); | ||
259 | - }); | 268 | + suppleProducts(allProductList); |
260 | return pager(productQueryVO,allProductList); | 269 | return pager(productQueryVO,allProductList); |
261 | } | 270 | } |
262 | 271 |