|
|
@@ -0,0 +1,76 @@
|
|
|
+package cn.hobbystocks.auc.service.impl;
|
|
|
+
|
|
|
+import cn.hobbystocks.auc.common.utils.DateUtils;
|
|
|
+import cn.hobbystocks.auc.convert.DiamondPositionConvert;
|
|
|
+import cn.hobbystocks.auc.convert.SpuCategoryConvert;
|
|
|
+import cn.hobbystocks.auc.domain.DiamondPosition;
|
|
|
+import cn.hobbystocks.auc.domain.SpuCategory;
|
|
|
+import cn.hobbystocks.auc.mapper.DiamondPositionMapper;
|
|
|
+import cn.hobbystocks.auc.mapper.SpuCategoryMapper;
|
|
|
+import cn.hobbystocks.auc.request.CategoryQueryRequest;
|
|
|
+import cn.hobbystocks.auc.request.DiamondPositionPageRequest;
|
|
|
+import cn.hobbystocks.auc.request.DiamondPositionSaveRequest;
|
|
|
+import cn.hobbystocks.auc.response.DiamondPositionResponse;
|
|
|
+import cn.hobbystocks.auc.response.SpuChildCategoryResponse;
|
|
|
+import cn.hobbystocks.auc.response.SpuMainCategoryResponse;
|
|
|
+import cn.hobbystocks.auc.service.IDiamondPositionService;
|
|
|
+import cn.hobbystocks.auc.service.SpuCategoryService;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class SpuCategoryServiceImpl implements SpuCategoryService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private DiamondPositionMapper diamondPositionMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private SpuCategoryMapper spuCategoryMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<SpuMainCategoryResponse> queryAllCategory(CategoryQueryRequest request) {
|
|
|
+ // 1. 查询所有启用的主分类
|
|
|
+ List<SpuCategory> mainCategoryList = spuCategoryMapper.selectAllMainCategory();
|
|
|
+ if (CollectionUtils.isEmpty(mainCategoryList)) {
|
|
|
+ return Lists.newArrayList();
|
|
|
+ }
|
|
|
+ List<SpuMainCategoryResponse> spuMainCategoryResponses = mainCategoryList.stream().map(SpuCategoryConvert.INSTANCE::toSpuMainCategoryResponse).collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 2. 提取所有主分类ID,批量查询子分类
|
|
|
+ List<Long> mainCategoryIds = spuMainCategoryResponses.stream()
|
|
|
+ .map(SpuMainCategoryResponse::getCategoryId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ List<SpuCategory> allChildCategoryList = spuCategoryMapper.selectChildCategoryByParentIds(mainCategoryIds);
|
|
|
+
|
|
|
+ // 3. 按主分类ID分组子分类
|
|
|
+ Map<Long, List<SpuCategory>> childCategoryMap = allChildCategoryList.stream()
|
|
|
+ .collect(Collectors.groupingBy(SpuCategory::getParentId)); // 注意:子分类VO需加parentId字段
|
|
|
+
|
|
|
+ // 4. 嵌套子分类到对应主分类
|
|
|
+ spuMainCategoryResponses.forEach(main -> {
|
|
|
+ List<SpuCategory> childList = childCategoryMap.getOrDefault(main.getCategoryId(), Lists.newArrayList());
|
|
|
+ // 子分类按排序号升序
|
|
|
+ childList.sort(Comparator.comparingInt(SpuCategory::getSort));
|
|
|
+ List<SpuChildCategoryResponse> spuChildCategoryResponses = childList.stream().map(SpuCategoryConvert.INSTANCE::toSpuChildCategoryResponse).collect(Collectors.toList());
|
|
|
+
|
|
|
+ main.setChildCategoryList(spuChildCategoryResponses);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 5. 主分类按排序号升序
|
|
|
+ spuMainCategoryResponses.sort(Comparator.comparingInt(SpuMainCategoryResponse::getSort));
|
|
|
+ return spuMainCategoryResponses;
|
|
|
+ }
|
|
|
+}
|