package com.oying.modules.pc.store.rest; import cn.hutool.core.collection.ListUtil; import cn.hutool.core.lang.tree.Tree; import cn.hutool.core.lang.tree.TreeNodeConfig; import cn.hutool.core.lang.tree.TreeUtil; import cn.hutool.core.util.BooleanUtil; import com.oying.utils.R; import com.oying.modules.pc.store.domain.StoreCategory; import com.oying.modules.pc.store.domain.dto.StoreCategoryQueryCriteria; import com.oying.modules.pc.store.service.StoreCategoryService; import com.oying.modules.pc.store.view.CustomerStoreCategoryView; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; /** * @author lzp * @date 2025-04-24 **/ @Api(tags = "店铺类目(客户端)") @RestController @RequiredArgsConstructor @RequestMapping("/api/pc/customer/store/{storeId}/category") public class StoreCategoryCustomerController { private final StoreCategoryService storeCategoryService; @GetMapping(value = "/list") @ApiOperation("查询店铺类目") public ResponseEntity getList(@PathVariable("storeId") Long storeId, @RequestParam(value = "recursive", required = false) Boolean recursive) { StoreCategoryQueryCriteria criteria = new StoreCategoryQueryCriteria(); criteria.setStoreId(storeId); criteria.setActive(1); List categoryList = storeCategoryService.queryAll(criteria); List categoryViewList = Optional.ofNullable(categoryList).orElse(ListUtil.empty()).stream().map(i -> { CustomerStoreCategoryView view = new CustomerStoreCategoryView(); view.setCategoryId(i.getCategoryId()); view.setParentId(i.getParentId()); view.setName(i.getName()); view.setSortWeight(i.getSortWeight()); return view; }).collect(Collectors.toList()); if (BooleanUtil.isFalse(recursive)) { return ResponseEntity.ok(R.success(categoryViewList)); } TreeNodeConfig config = new TreeNodeConfig(); config.setIdKey("categoryId"); config.setWeightKey("sortWeight"); //config.setDeep(3); List> tree = TreeUtil.build(categoryViewList, 0L, config, (c, treeNode) -> { treeNode.setId(c.getCategoryId()); treeNode.setParentId(c.getParentId()); treeNode.setName(c.getName()); treeNode.setWeight(c.getSortWeight()); }); return ResponseEntity.ok(R.success(tree)); } }