zepengdev
2025-07-19 f6239d1029da4c51ba3b2cec790f7399a210a3df
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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.annotation.Log;
import com.oying.utils.R;
import com.oying.modules.pc.store.domain.StoreCategory;
import com.oying.modules.pc.store.domain.dto.*;
import com.oying.modules.pc.store.service.StoreCategoryService;
import com.oying.modules.pc.store.view.StoreCategoryMerchantView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
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/merchant/store/{storeId}/category")
public class StoreCategoryMerchantController {
 
    private final StoreCategoryService storeCategoryService;
 
    @GetMapping(value = "/list")
    @ApiOperation("查询店铺类目")
    //@PreAuthorize("@el.check('merchant:storeCategory:list')" +
    //        " and @storeMerchantOwnershipService.check(#storeId)")
    public ResponseEntity<?> getStoreCategories(@PathVariable Long storeId,
                                                @RequestParam(value = "recursive", required = false) Boolean recursive) {
 
        StoreCategoryQueryCriteria criteria = new StoreCategoryQueryCriteria();
        criteria.setStoreId(storeId);
        List<StoreCategory> storeCategoryList = storeCategoryService.queryAll(criteria);
        List<StoreCategoryMerchantView> viewList = Optional.ofNullable(storeCategoryList).orElse(ListUtil.empty()).stream().map(i -> {
            StoreCategoryMerchantView view = new StoreCategoryMerchantView();
            BeanUtils.copyProperties(i, view);
            return view;
        }).collect(Collectors.toList());
 
        if (BooleanUtil.isFalse(recursive)) {
            return ResponseEntity.ok(R.success(viewList));
        }
 
        TreeNodeConfig config = new TreeNodeConfig();
        config.setIdKey("categoryId");
        config.setWeightKey("sortWeight");
        //config.setDeep(3);
 
        List<Tree<Long>> tree = TreeUtil.build(viewList, 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));
    }
 
    @GetMapping(value = "/{categoryId}")
    @ApiOperation("查询店铺类目")
    //@PreAuthorize("@el.check('merchant:storeCategory:list')" +
    //        " and @storeMerchantOwnershipService.check(#storeId)")
    public ResponseEntity<?> getStoreCategoryById(@PathVariable Long categoryId,
                                     @RequestParam(value = "recursive", required = false) Boolean recursive) {
 
        StoreCategory storeCategory = storeCategoryService.getById(categoryId);
        StoreCategoryMerchantView view = new StoreCategoryMerchantView();
        BeanUtils.copyProperties(storeCategory, view);
 
        /*LambdaQueryWrapper<StoreCategory> wrapper = new LambdaQueryWrapper<StoreCategory>()
                .eq(StoreCategory::getParentId, categoryId);
        List<StoreCategory> subStoreCategoryList = storeCategoryService.list(wrapper);*/
 
        return ResponseEntity.ok(R.success(view));
    }
 
    @PostMapping
    @Log("新增店铺类目")
    @ApiOperation("新增店铺类目")
    //@PreAuthorize("@el.check('storeCategory:add')" +
    //        " and @storeMerchantOwnershipService.check(#storeId)")
    public ResponseEntity<?> createStoreCategory(@PathVariable Long storeId,
                                                 @Validated @RequestBody StoreCategoryMerchantCreateRequest request) {
 
        StoreCategory resources = new StoreCategory();
        BeanUtils.copyProperties(request, resources);
        resources.setStoreId(storeId);
        storeCategoryService.create(resources);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
 
    @PutMapping(value = "/{categoryId}")
    @Log("修改店铺类目")
    @ApiOperation("修改店铺类目")
    //@PreAuthorize("@el.check('storeCategory:edit')" +
    //        " and @storeMerchantOwnershipService.check(#storeId)")
    public ResponseEntity<?> updateStoreCategory(@PathVariable Long storeId,
                                                 @PathVariable Long categoryId,
                                                 @Validated @RequestBody StoreCategoryMerchantUpdateRequest request) {
 
        StoreCategory resources = new StoreCategory();
        BeanUtils.copyProperties(request, resources);
        resources.setCategoryId(categoryId);
        resources.setStoreId(storeId);
        storeCategoryService.update(resources);
        return ResponseEntity.noContent().build();
    }
 
    @DeleteMapping
    @Log("删除店铺类目")
    @ApiOperation("删除店铺类目")
    /*@PreAuthorize("@el.check('storeCategory:del')" +
            " and @storeMerchantOwnershipService.check(#storeId)")*/
    public ResponseEntity<?> deleteStoreCategory(@ApiParam(value = "传ID数组[]") @RequestBody List<Long> ids) {
        storeCategoryService.deleteAll(ids);
        return ResponseEntity.noContent().build();
    }
 
    @PostMapping(value = "/delete")
    @Log("删除店铺类目")
    @ApiOperation("删除店铺类目")
    /*@PreAuthorize("@el.check('storeCategory:del')" +
            " and @storeMerchantOwnershipService.check(#storeId)")*/
    public ResponseEntity<?> delStoreCategory(@ApiParam(value = "传ID数组[]") @RequestBody List<Long> ids) {
        storeCategoryService.deleteAll(ids);
        return ResponseEntity.noContent().build();
    }
}