Explorar o código

PC端商品分类修改

zjy hai 3 meses
pai
achega
0fd0a43c00

+ 6 - 3
alien-entity/src/main/java/shop/alien/entity/second/SecondGoodsCategory.java

@@ -2,14 +2,14 @@ package shop.alien.entity.second;
 
 
 import com.baomidou.mybatisplus.annotation.*;
-import com.fasterxml.jackson.annotation.JsonFormat;
 import com.fasterxml.jackson.annotation.JsonInclude;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
 import java.io.Serializable;
-import java.math.BigDecimal;
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.List;
 
 /**
  * 二手商品类别表
@@ -64,4 +64,7 @@ public class SecondGoodsCategory implements Serializable {
     @TableField("updated_user_id")
     @ApiModelProperty(value = "修改人ID")
     private Integer updatedUserId;
-}
+
+    @TableField(exist = false)
+    private List<SecondGoodsCategory> children = new ArrayList<>();
+}

+ 2 - 0
alien-entity/src/main/java/shop/alien/mapper/second/SecondGoodsCategoryMapper.java

@@ -14,4 +14,6 @@ public interface SecondGoodsCategoryMapper extends BaseMapper<SecondGoodsCategor
      * 自定义分页查询
      */
     List<SecondGoodsCategory> querySecondGoodsByParentId(@Param("parentId") Integer parentId, @Param("categoryState") Integer categoryState);
+
+    List<SecondGoodsCategory> querySecondGoodsInfo();
 }

+ 24 - 0
alien-entity/src/main/resources/mapper/second/SecondGoodsCategoryMapper.xml

@@ -48,4 +48,28 @@
             </if>
         ORDER BY category_sort, created_time
     </select>
+
+    <!-- 根据上级ID查询二手商品类别表 -->
+    <select id="querySecondGoodsInfo" resultType="shop.alien.entity.second.SecondGoodsCategory">
+        SELECT
+        id,
+        category_name,
+        category_url,
+        parent_id,
+        delete_flag,
+        created_time,
+        created_user_id,
+        updated_time,
+        updated_user_id,
+        category_sort,
+        case when category_state = 0 then '显示'
+        when category_state = 1 then '隐藏'
+        else '未知'
+        end as category_state
+        FROM
+        second_goods_category
+        WHERE
+        delete_flag = 0
+        ORDER BY category_sort, created_time
+    </select>
 </mapper>

+ 8 - 0
alien-second/src/main/java/shop/alien/second/platform/PlatformSecondCategoryController.java

@@ -10,6 +10,8 @@ import shop.alien.entity.result.R;
 import shop.alien.entity.second.SecondGoodsCategory;
 import shop.alien.second.service.SecondGoodsCategoryService;
 
+import java.util.List;
+
 @Slf4j
 @Api(tags = {"二手平台-商品类型"})
 @ApiSort(9)
@@ -44,4 +46,10 @@ public class PlatformSecondCategoryController {
             return R.data("修改商品类型成功");
         }
     }
+
+    @ApiOperation("搜索商品类型树形结构")
+    @PostMapping("/querySecondGoodsTree")
+    public R<List<SecondGoodsCategory>> querySecondGoodsTree() throws Exception {
+        return R.data(service.querySecondGoodsTree(), "查询成功");
+    }
 }

+ 2 - 0
alien-second/src/main/java/shop/alien/second/service/SecondGoodsCategoryService.java

@@ -21,4 +21,6 @@ public interface SecondGoodsCategoryService extends IService<SecondGoodsCategory
 
     Integer updateSecondGoodsCategory(SecondGoodsCategory category) throws Exception;
 
+    List<SecondGoodsCategory> querySecondGoodsTree() throws Exception;
+
 }

+ 48 - 0
alien-second/src/main/java/shop/alien/second/service/impl/SecondGoodsCategoryServiceImpl.java

@@ -1,6 +1,7 @@
 package shop.alien.second.service.impl;
 
 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.extension.service.impl.ServiceImpl;
 import lombok.RequiredArgsConstructor;
@@ -10,11 +11,14 @@ import org.apache.poi.util.StringUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import shop.alien.entity.second.SecondGoodsCategory;
+import shop.alien.entity.store.LifeFans;
 import shop.alien.mapper.second.SecondGoodsCategoryMapper;
 import shop.alien.second.service.SecondGoodsCategoryService;
 import shop.alien.util.common.JwtUtil;
 
 import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * 二手商品服务实现类
@@ -44,6 +48,16 @@ public class SecondGoodsCategoryServiceImpl extends ServiceImpl<SecondGoodsCateg
     @Override
     public Integer insertSecondGoodsCategory(SecondGoodsCategory category) throws Exception {
         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);
         } catch (Exception e) {
             log.error("SecondGoodsCategoryServiceImpl.insertSecondGoodsCategory Error Mgs={}", e.getMessage());
@@ -90,4 +104,38 @@ public class SecondGoodsCategoryServiceImpl extends ServiceImpl<SecondGoodsCateg
             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));
+    }
 }