zepengdev
2025-06-22 806d1e414165943b3e2a4d8454015c0c0285f846
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
package com.oying.modules.pc.product.rest;
 
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oying.annotation.Log;
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.ProductAdminService;
import com.oying.modules.pc.product.service.ProductImageService;
import com.oying.modules.pc.product.service.ProductLabelService;
import com.oying.modules.pc.product.service.ProductService;
import com.oying.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
 
/**
 * @author lzp
 * @date 2025-04-30
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "商品")
@RequestMapping("/api/pc/product")
public class ProductController {
 
    private final ProductService productService;
    private final ProductAdminService productAdminService;
    private final ProductImageService productImageService;
    private final ProductLabelService productLabelService;
 
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    // @PreAuthorize("@el.check('product:list')")
    public void exportProduct(HttpServletResponse response, ProductQueryCriteria criteria) throws IOException {
        productService.download(productService.queryAll(criteria), response);
    }
 
    @GetMapping(value = "/list")
    @ApiOperation("查询商品")
    // @PreAuthorize("@el.check('product:list')")
    public ResponseEntity<?> getProducts(ProductQueryCriteria criteria) {
        return ResponseEntity.ok(R.success(productService.queryAll(criteria)));
    }
 
    @GetMapping(value = "/batch")
    @ApiOperation("查询商品(批量)")
    // @PreAuthorize("@el.check('product:list')")
    public ResponseEntity<?> getProductsByIds(@RequestParam(value = "ids") List<Long> ids) {
        return ResponseEntity.ok(R.success(productService.listByIds(ids)));
    }
 
    @GetMapping(value = "/page")
    @ApiOperation("查询商品")
    // @PreAuthorize("@el.check('product:list')")
    public ResponseEntity<?> getProductsByPage(ProductQueryCriteria criteria) {
        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
        return ResponseEntity.ok(R.success(productService.queryAll(criteria, page)));
    }
 
    @GetMapping(value = "/{productId}}")
    @ApiOperation("查询商品")
    // @PreAuthorize("@el.check('product:list')")
    public ResponseEntity<?> getProduct(@PathVariable Long productId) {
        Product product = productService.getById(productId);
        if (ObjectUtil.isNotEmpty(product)) {
            product.setLabels(productLabelService.queryLabelsByProductId(productId));
        }
        return ResponseEntity.ok(R.success(product));
    }
 
    @GetMapping(value = "/{productId}}/details")
    @ApiOperation("查询商品")
    // @PreAuthorize("@el.check('product:list')")
    public ResponseEntity<?> getProductDetails(@PathVariable Long productId) {
        Product product = productService.getById(productId);
        if (ObjectUtil.isNotEmpty(product)) {
            product.setImages(productImageService.queryImagesByProductId(productId));
            product.setLabels(productLabelService.queryLabelsByProductId(productId));
        }
        return ResponseEntity.ok(R.success(product));
    }
 
    @GetMapping(value = "/{productId}}/images")
    @ApiOperation("查询商品")
    // @PreAuthorize("@el.check('product:list')")
    public ResponseEntity<?> getProductImages(@PathVariable Long productId) {
        return ResponseEntity.ok(R.success(productImageService.queryImagesByProductId(productId)));
    }
 
 
    @GetMapping(value = "/{productId}}/labels")
    @ApiOperation("查询商品")
    // @PreAuthorize("@el.check('product:list')")
    public ResponseEntity<?> getProductLabels(@PathVariable Long productId) {
        return ResponseEntity.ok(R.success(productLabelService.queryLabelsByProductId(productId)));
    }
 
    @PostMapping
    @Log("新增商品")
    @ApiOperation("新增商品")
    //@PreAuthorize("@el.check('merchant:product:add')")
    public ResponseEntity<?> createProduct(@Validated @RequestBody ProductMerchantCreateRequest request) {
        productAdminService.create(request);
        return ResponseEntity.noContent().build();
    }
 
    @PutMapping
    @Log("修改商品")
    @ApiOperation("修改商品")
    // @PreAuthorize("@el.check('product:edit')")
    public ResponseEntity<?> updateProduct(@Validated @RequestBody ProductMerchantUpdateRequest request) {
        productAdminService.update(request);
        return ResponseEntity.noContent().build();
    }
 
    @DeleteMapping
    @Log("删除商品")
    @ApiOperation("删除商品")
    // @PreAuthorize("@el.check('product:del')")
    public ResponseEntity<?> deleteProduct(@ApiParam(value = "传ID数组[]") @RequestBody List<Long> ids) {
        productAdminService.delete(ids);
        return ResponseEntity.noContent().build();
    }
}