zepengdev
2025-06-20 218b9211344b208c425e098e1e51568c66eb1c80
oying-system/src/main/java/com/oying/modules/pc/product/rest/ProductController.java
@@ -1,73 +1,141 @@
package com.oying.modules.pc.product.rest;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
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.service.ProductService;
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 java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oying.utils.PageResult;
import java.io.IOException;
import java.util.List;
/**
* @author lzp
* @date 2025-04-30
**/
 * @author lzp
 * @date 2025-04-30
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "商品")
@RequestMapping("/api/product")
@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')")
    // @PreAuthorize("@el.check('product:list')")
    public void exportProduct(HttpServletResponse response, ProductQueryCriteria criteria) throws IOException {
        productService.download(productService.queryAll(criteria), response);
    }
    @GetMapping
    @GetMapping(value = "/list")
    @ApiOperation("查询商品")
    @PreAuthorize("@el.check('product:list')")
    public R<PageResult<Product>> queryProduct(ProductQueryCriteria criteria){
    // @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 R.success(productService.queryAll(criteria,page));
        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('product:add')")
    public R<?> createProduct(@Validated @RequestBody Product resources){
        productService.create(resources);
        return R.success();
    //@PreAuthorize("@el.check('merchant:product:add')")
    public ResponseEntity<?> createProduct(@PathVariable Long storeId,
                                           @Validated @RequestBody ProductMerchantCreateRequest request) {
        request.setStoreId(ObjectUtils.defaultIfNull(request.getStoreId(), storeId));
        productAdminService.create(request);
        return ResponseEntity.noContent().build();
    }
    @PutMapping
    @Log("修改商品")
    @ApiOperation("修改商品")
    @PreAuthorize("@el.check('product:edit')")
    public R<?> updateProduct(@Validated @RequestBody Product resources){
        productService.update(resources);
        return R.success();
    // @PreAuthorize("@el.check('product:edit')")
    public ResponseEntity<?> updateProduct(@PathVariable Long productId,
                                           @Validated @RequestBody ProductMerchantUpdateRequest request) {
        request.setProductId(ObjectUtils.defaultIfNull(request.getProductId(), productId));
        productAdminService.update(request);
        return ResponseEntity.noContent().build();
    }
    @DeleteMapping
    @Log("删除商品")
    @ApiOperation("删除商品")
    @PreAuthorize("@el.check('product:del')")
    public R<?> deleteProduct(@ApiParam(value = "传ID数组[]") @RequestBody List<Long> ids) {
        productService.deleteAll(ids);
        return R.success();
    // @PreAuthorize("@el.check('product:del')")
    public ResponseEntity<?> deleteProduct(@ApiParam(value = "传ID数组[]") @RequestBody List<Long> ids) {
        productAdminService.delete(ids);
        return ResponseEntity.noContent().build();
    }
}