|
@@ -1,6 +1,7 @@
|
|
|
package shop.alien.second.service.impl;
|
|
package shop.alien.second.service.impl;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
@@ -10,11 +11,14 @@ import org.apache.poi.util.StringUtil;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import shop.alien.entity.second.SecondGoodsCategory;
|
|
import shop.alien.entity.second.SecondGoodsCategory;
|
|
|
|
|
+import shop.alien.entity.store.LifeFans;
|
|
|
import shop.alien.mapper.second.SecondGoodsCategoryMapper;
|
|
import shop.alien.mapper.second.SecondGoodsCategoryMapper;
|
|
|
import shop.alien.second.service.SecondGoodsCategoryService;
|
|
import shop.alien.second.service.SecondGoodsCategoryService;
|
|
|
import shop.alien.util.common.JwtUtil;
|
|
import shop.alien.util.common.JwtUtil;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 二手商品服务实现类
|
|
* 二手商品服务实现类
|
|
@@ -44,6 +48,16 @@ public class SecondGoodsCategoryServiceImpl extends ServiceImpl<SecondGoodsCateg
|
|
|
@Override
|
|
@Override
|
|
|
public Integer insertSecondGoodsCategory(SecondGoodsCategory category) throws Exception {
|
|
public Integer insertSecondGoodsCategory(SecondGoodsCategory category) throws Exception {
|
|
|
try {
|
|
try {
|
|
|
|
|
+ QueryWrapper<SecondGoodsCategory> queryWrapper = new QueryWrapper<>();
|
|
|
|
|
+ queryWrapper.eq("parent_id", category.getParentId());
|
|
|
|
|
+ queryWrapper.orderByDesc("category_sort").last("LIMIT 1");
|
|
|
|
|
+ SecondGoodsCategory lastCategory = this.getOne(queryWrapper);
|
|
|
|
|
+
|
|
|
|
|
+ if(lastCategory == null || lastCategory.getCategorySort() == null) {
|
|
|
|
|
+ category.setCategorySort(1);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ category.setCategorySort(lastCategory.getCategorySort() + 1);
|
|
|
|
|
+ }
|
|
|
return mapper.insert(category);
|
|
return mapper.insert(category);
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("SecondGoodsCategoryServiceImpl.insertSecondGoodsCategory Error Mgs={}", e.getMessage());
|
|
log.error("SecondGoodsCategoryServiceImpl.insertSecondGoodsCategory Error Mgs={}", e.getMessage());
|
|
@@ -90,4 +104,38 @@ public class SecondGoodsCategoryServiceImpl extends ServiceImpl<SecondGoodsCateg
|
|
|
throw new Exception("更新二级商品分类失败", e);
|
|
throw new Exception("更新二级商品分类失败", e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<SecondGoodsCategory> querySecondGoodsTree() throws Exception {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return buildTree(mapper.querySecondGoodsInfo());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("SecondGoodsCategoryServiceImpl.querySecondGoodsByParentId Error Mgs={}", e.getMessage());
|
|
|
|
|
+ throw new Exception("查询二级商品分类失败", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 构建树的核心逻辑
|
|
|
|
|
+ public List<SecondGoodsCategory> buildTree(List<SecondGoodsCategory> nodes) {
|
|
|
|
|
+ Map<Integer, SecondGoodsCategory> nodeMap = nodes.stream()
|
|
|
|
|
+ .collect(Collectors.toMap(SecondGoodsCategory::getId, n -> n));
|
|
|
|
|
+
|
|
|
|
|
+ List<SecondGoodsCategory> roots = nodes.stream()
|
|
|
|
|
+ .filter(n -> -1 == n.getParentId()) // 假设根节点的parentId为-1
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+
|
|
|
|
|
+ for (SecondGoodsCategory root : roots) {
|
|
|
|
|
+ addChildrenRecursive(root, nodeMap);
|
|
|
|
|
+ }
|
|
|
|
|
+ return roots;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void addChildrenRecursive(SecondGoodsCategory parent, Map<Integer, SecondGoodsCategory> nodeMap) {
|
|
|
|
|
+ List<SecondGoodsCategory> children = nodeMap.values().stream()
|
|
|
|
|
+ .filter(child -> child.getParentId().equals(parent.getId()))
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ parent.setChildren(children);
|
|
|
|
|
+ children.forEach(child -> addChildrenRecursive(child, nodeMap));
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|