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.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.apache.commons.lang3.ObjectUtils; 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(@RequestBody List ids) { return ResponseEntity.ok(R.success(productService.listByIds(ids))); } @GetMapping(value = "/page") @ApiOperation("查询商品") // @PreAuthorize("@el.check('product:list')") public ResponseEntity getProductsByPage(ProductQueryCriteria criteria) { Page 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(@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 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 ResponseEntity deleteProduct(@ApiParam(value = "传ID数组[]") @RequestBody List ids) { productAdminService.delete(ids); return ResponseEntity.noContent().build(); } }