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
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
package com.oying.modules.pc.product.rest;
 
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oying.annotation.Log;
import com.oying.utils.R;
import com.oying.modules.pc.product.domain.Product;
import com.oying.modules.pc.product.domain.dto.ProductMerchantCreateRequest;
import com.oying.modules.pc.product.domain.dto.ProductMerchantUpdateRequest;
import com.oying.modules.pc.product.domain.dto.ProductQueryCriteria;
import com.oying.modules.pc.product.service.ProductMerchantService;
import com.oying.modules.pc.product.service.ProductService;
import com.oying.modules.pc.product.view.ProductMerchantDetailsView;
import com.oying.modules.pc.product.view.ProductMerchantSimpleView;
import com.oying.utils.PageResult;
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.ResponseEntity;
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-30
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "商品(商户端)")
@RequestMapping("/api/pc/merchant/store/{storeId}/product")
public class ProductMerchantController {
 
    private final ProductService productService;
    private final ProductMerchantService productMerchantService;
 
    @GetMapping(value = "/page")
    @ApiOperation("获取指定商户店铺的商品列表(支持分页)")
    /*@PreAuthorize("@el.check('merchant:product:page') " +
            "and @storeMerchantOwnershipService.check(#storeId)")*/
    public ResponseEntity<?> query(@PathVariable Long storeId,
                                   ProductQueryCriteria criteria) {
 
        criteria.setStoreId(storeId);
        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
        PageResult<Product> productPageResult = productService.queryAll(criteria, page);
        List<Product> productList = Optional.ofNullable(productPageResult.getContent()).orElse(ListUtil.empty());
        PageResult<ProductMerchantSimpleView> viewPageResult = new PageResult<>(
                productList.stream().map(i -> {
                    ProductMerchantSimpleView view = new ProductMerchantSimpleView();
                    BeanUtils.copyProperties(i, view);
                    return view;
                }).collect(Collectors.toList()),
                productPageResult.getTotalElements());
        return ResponseEntity.ok(R.success(viewPageResult));
    }
 
    @GetMapping(value = "/{productId}")
    @ApiOperation("查询商品")
    /*@PreAuthorize("@el.check('merchant:product:byProductId') " +
            "and @storeMerchantOwnershipService.check(#storeId)")*/
    public ResponseEntity<?> getById(@PathVariable Long productId) {
        return ResponseEntity.ok(R.success(productService.getById(productId)));
    }
 
    @GetMapping(value = "/{productId}/details")
    @ApiOperation("查询商品详情")
    /*@PreAuthorize("@el.check('merchant:product:byProductId') " +
            "and @storeMerchantOwnershipService.check(#storeId)")*/
    public ResponseEntity<?> getDetailsById(@PathVariable Long productId) {
        Product product = productService.getById(productId);
        ProductMerchantDetailsView view = null;
        if (product != null) {
            view = new ProductMerchantDetailsView();
            BeanUtils.copyProperties(product, view);
            view.setMainImageList(ListUtil.empty());
            view.setLabelList(ListUtil.empty());
        }
        return ResponseEntity.ok(R.success(view));
    }
 
    @PostMapping
    @Log("新增商品")
    @ApiOperation("新增商品")
    //@PreAuthorize("@el.check('merchant:product:add') " +
    //        "and @storeMerchantOwnershipService.check(#storeId)")
    public ResponseEntity<?> createProduct(@PathVariable Long storeId,
                                           @Validated @RequestBody ProductMerchantCreateRequest request) {
 
        productMerchantService.create(storeId, request);
        return ResponseEntity.noContent().build();
    }
 
    @PutMapping(value = "/{productId}")
    @Log("修改商品")
    @ApiOperation("修改商品")
    /*@PreAuthorize("@el.check('merchant:product:edit') " +
            "and @storeMerchantOwnershipService.check(#storeId)")*/
    public ResponseEntity<?> updateProduct(@PathVariable Long productId,
                                           @Validated @RequestBody ProductMerchantUpdateRequest request) {
 
        productMerchantService.update(productId, request);
        return ResponseEntity.noContent().build();
    }
 
    @DeleteMapping
    @Log("批量删除商品")
    @ApiOperation("批量删除商品")
    //@PreAuthorize("@el.check('merchant:product:batchDel') " +
    //        "and @storeMerchantOwnershipService.check(#storeId)")
    public ResponseEntity<?> deleteProduct(@ApiParam(value = "传ID数组[]") @RequestBody List<Long> ids) {
        productMerchantService.batchDelete(ids);
        return ResponseEntity.noContent().build();
    }
 
    @PutMapping(value = "/{productId}/on")
    @Log("上架商品")
    @ApiOperation("上架商品")
    /*@PreAuthorize("@el.check('merchant:product:on') " +
            "and @storeMerchantOwnershipService.check(#storeId)")*/
    public ResponseEntity<?> putOnShelf(@PathVariable("productId") Long productId) {
        productMerchantService.putOnShelf(productId);
        return ResponseEntity.noContent().build();
    }
 
    @PutMapping(value = "/{productId}/off")
    @Log("下架商品")
    @ApiOperation("下架商品")
    /*@PreAuthorize("@el.check('merchant:product:off') " +
            "and @storeMerchantOwnershipService.check(#storeId)")*/
    public ResponseEntity<?> takeOffShelf(@PathVariable("productId") Long productId) {
        productMerchantService.takeOffShelf(productId);
        return ResponseEntity.noContent().build();
    }
}