|
|
@@ -1,5 +1,6 @@
|
|
|
package com.yami.shop.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
import com.alibaba.excel.context.AnalysisContext;
|
|
|
import com.alibaba.excel.event.AnalysisEventListener;
|
|
|
@@ -10,6 +11,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.yami.shop.bean.dto.ShopCategoryExcelDTO;
|
|
|
import com.yami.shop.bean.model.*;
|
|
|
import com.yami.shop.bean.param.CategoryProductDTO;
|
|
|
+import com.yami.shop.bean.vo.ListCategoryForUserVO;
|
|
|
import com.yami.shop.common.exception.GlobalException;
|
|
|
import com.yami.shop.common.util.R;
|
|
|
import com.yami.shop.dao.*;
|
|
|
@@ -27,9 +29,7 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.InputStream;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -57,9 +57,67 @@ public class ShopCategoryServiceImpl extends ServiceImpl<ShopCategoryMapper, Sho
|
|
|
public List<ShopCategory> treeShopCategory(Long shopId, String name) {
|
|
|
// 获取所有未删除的前台类目
|
|
|
List<ShopCategory> shopCategories = shopCategoryMapper.selectListAll(shopId, name);
|
|
|
- // 构建树形结构
|
|
|
- return shopCategories; // 假设根节点的parentCode为"0"
|
|
|
-// return buildCategoryTree(shopCategories, "0"); // 假设根节点的parentCode为"0"
|
|
|
+// // 构建树形结构
|
|
|
+// return shopCategories; // 假设根节点的parentCode为"0"
|
|
|
+//// return buildCategoryTree(shopCategories, "0"); // 假设根节点的parentCode为"0"
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public List<ShopCategory> listCategoryForUser(Long shopId) {
|
|
|
+// List<ShopCategory> shopCategories = shopCategoryMapper.selectList(new LambdaQueryWrapper<ShopCategory>()
|
|
|
+// .eq(ShopCategory::getShopId, shopId)
|
|
|
+// .eq(ShopCategory::getIsDelete, Boolean.FALSE)
|
|
|
+// .orderByAsc(ShopCategory::getNum));
|
|
|
+// if (CollectionUtil.isEmpty(shopCategories)) {
|
|
|
+// return Collections.emptyList();
|
|
|
+// }
|
|
|
+
|
|
|
+ Map<Long, List<ShopCategory>> parentMap = shopCategories.stream()
|
|
|
+ .collect(Collectors.groupingBy(ShopCategory::getPid));
|
|
|
+
|
|
|
+ // 初始层级为一级分类(grade = 1)
|
|
|
+ return buildCategoryTree(parentMap.getOrDefault(0L, Collections.emptyList()), parentMap, 0);
|
|
|
+ }
|
|
|
+ private List<ShopCategory> buildCategoryTree(List<ShopCategory> categories, Map<Long, List<ShopCategory>> parentMap, int currentLevel) {
|
|
|
+ if (CollectionUtil.isEmpty(categories)) return Collections.emptyList();
|
|
|
+ return categories.stream()
|
|
|
+ .map(category -> {
|
|
|
+ // 直接使用ShopCategory而不是ListCategoryForUserVO
|
|
|
+ ShopCategory shopCategory = new ShopCategory();
|
|
|
+ shopCategory.setId(category.getId());
|
|
|
+ shopCategory.setName(category.getName());
|
|
|
+ shopCategory.setNum(category.getNum());
|
|
|
+ shopCategory.setPid(category.getPid());
|
|
|
+ shopCategory.setIcon(category.getIcon());
|
|
|
+ shopCategory.setLevel(currentLevel);
|
|
|
+
|
|
|
+ // 判断是否为二级或三级分类,并设置level字段(原productBeBound)
|
|
|
+ int level = 0;
|
|
|
+ if (currentLevel == 1 || currentLevel == 2) {
|
|
|
+ // 三级分类直接绑定商品
|
|
|
+ if (currentLevel == 2) {
|
|
|
+ level = 1;
|
|
|
+ }
|
|
|
+ // 二级分类若无子分类(三级分类),则允许绑定商品
|
|
|
+ else if (currentLevel == 1) {
|
|
|
+ List<ShopCategory> children = parentMap.getOrDefault(category.getId(), Collections.emptyList());
|
|
|
+ level = children.isEmpty() ? 1 : 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ shopCategory.setLevel(level);
|
|
|
+
|
|
|
+ // 递归构建子分类树,层级 +1
|
|
|
+ List<ShopCategory> children = parentMap.getOrDefault(category.getId(), Collections.emptyList());
|
|
|
+
|
|
|
+ children.sort(Comparator.comparingInt(ShopCategory::getNum));
|
|
|
+
|
|
|
+ shopCategory.setChildren(buildCategoryTree(children, parentMap, currentLevel + 1));
|
|
|
+
|
|
|
+ return shopCategory;
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -167,6 +225,14 @@ public class ShopCategoryServiceImpl extends ServiceImpl<ShopCategoryMapper, Sho
|
|
|
BeanUtils.copyProperties(page, res);
|
|
|
List<Category> categories = page.getRecords().stream().map((sc) -> {
|
|
|
Category category = new Category();
|
|
|
+
|
|
|
+ Integer count = shopCategoryMapper.selectAppCount(sc.getShopId(), sc.getCode());
|
|
|
+ if (count == 0){
|
|
|
+ category.setHasSecond(false);
|
|
|
+ }else {
|
|
|
+ category.setHasSecond(true);
|
|
|
+ }
|
|
|
+
|
|
|
category.setCategoryId(sc.getId());
|
|
|
category.setShopId(sc.getShopId());
|
|
|
category.setParentId(sc.getPid());
|