xin
2025-05-30 347909bae241fff128b628ea6d12992d7e5b4b10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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<StoreCategory> categoryList = storeCategoryService.queryAll(criteria);
 
        List<CustomerStoreCategoryView> 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<Long>> 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));
    }
}