xin
2025-09-17 6d31d535d737ed26c4d9d61cd4e0b5483cb9b0ba
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
package com.oying.modules.pc.product.rest;
 
import com.oying.modules.pc.product.domain.Product;
import com.oying.modules.pc.product.domain.dto.ProductInventoryUpdateRequest;
import com.oying.modules.pc.product.service.ProductInventoryService;
import com.oying.utils.R;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
 
import java.util.Optional;
 
/**
 * @author lzp
 * @date 2025-09-10
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "ε•†ε“εΊ“ε­˜")
@RequestMapping("/api/pc/product/{productId}/inventory")
public class ProductInventoryController {
 
    private final ProductInventoryService productInventoryService;
 
    @GetMapping
    public ResponseEntity<?> getStockInfo(@PathVariable Long productId) {
        return ResponseEntity.ok(R.success(productInventoryService.getProductById(productId)));
    }
 
    @PutMapping
    public ResponseEntity<?> setStock(@PathVariable Long productId, @RequestBody ProductInventoryUpdateRequest request) {
        Product product = productInventoryService.setStockQuantity(Optional.ofNullable(request.getProductId()).orElse(productId), request.getQuantity(), request.getVersion());
        return ResponseEntity.ok(R.success(product));
    }
 
    @PostMapping("/increase")
    public ResponseEntity<?> increaseStock(@PathVariable Long productId, @RequestBody ProductInventoryUpdateRequest request) {
        Product product = productInventoryService.increaseStock(Optional.ofNullable(request.getProductId()).orElse(productId), request.getAmount());
        return ResponseEntity.ok(R.success(product));
    }
 
    @PostMapping("/decrease")
    public ResponseEntity<?> decreaseStock(@PathVariable Long productId, @RequestBody ProductInventoryUpdateRequest request) {
        Product product = productInventoryService.decreaseStock(Optional.ofNullable(request.getProductId()).orElse(productId), request.getAmount());
        return ResponseEntity.ok(R.success(product));
    }
 
    @GetMapping("/sufficient")
    public ResponseEntity<?> checkStockSufficient(@PathVariable Long productId, @RequestParam Integer requiredAmount) {
        boolean isSufficient = productInventoryService.isStockSufficient(productId, requiredAmount);
        return ResponseEntity.ok(R.success(isSufficient));
    }
}