package com.webmanage.controller;
|
|
import com.webmanage.common.Result;
|
import com.webmanage.entity.Product;
|
import com.webmanage.service.ProductService;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiParam;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.util.List;
|
|
/**
|
* 产品管理控制器
|
*
|
* @author webmanage
|
* @date 2024-08-07
|
*/
|
@Slf4j
|
@RestController
|
@RequestMapping("/product")
|
@Api(tags = "产品管理")
|
public class ProductController {
|
|
@Autowired
|
private ProductService productService;
|
|
/**
|
* 获取产品列表
|
*/
|
@GetMapping("/list")
|
@ApiOperation("获取产品列表")
|
public Result<List<Product>> getProductList(
|
@ApiParam("产品名称") @RequestParam(required = false) String productName,
|
@ApiParam("产品类型") @RequestParam(required = false) String productType,
|
@ApiParam("产品状态") @RequestParam(required = false) String status,
|
@ApiParam("提供者ID") @RequestParam(required = false) Long providerId) {
|
|
try {
|
List<Product> productList = productService.getProductList(productName, productType, status, providerId);
|
return Result.success(productList);
|
} catch (Exception e) {
|
log.error("获取产品列表失败", e);
|
return Result.error("获取产品列表失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 根据ID获取产品详情
|
*/
|
@GetMapping("/{id}")
|
@ApiOperation("根据ID获取产品详情")
|
public Result<Product> getProductById(@ApiParam("产品ID") @PathVariable Long id) {
|
try {
|
Product product = productService.getProductById(id);
|
if (product == null) {
|
return Result.error("产品不存在");
|
}
|
return Result.success(product);
|
} catch (Exception e) {
|
log.error("获取产品详情失败", e);
|
return Result.error("获取产品详情失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 创建产品
|
*/
|
@PostMapping
|
@ApiOperation("创建产品")
|
public Result<Boolean> createProduct(@RequestBody Product product) {
|
try {
|
boolean result = productService.createProduct(product);
|
if (result) {
|
return Result.success("产品创建成功", true);
|
} else {
|
return Result.error("产品创建失败");
|
}
|
} catch (Exception e) {
|
log.error("创建产品失败", e);
|
return Result.error("创建产品失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 更新产品信息
|
*/
|
@PutMapping("/{id}")
|
@ApiOperation("更新产品信息")
|
public Result<Boolean> updateProduct(@ApiParam("产品ID") @PathVariable Long id, @RequestBody Product product) {
|
try {
|
product.setId(id);
|
boolean result = productService.updateProduct(product);
|
if (result) {
|
return Result.success("产品更新成功", true);
|
} else {
|
return Result.error("产品更新失败");
|
}
|
} catch (Exception e) {
|
log.error("更新产品失败", e);
|
return Result.error("更新产品失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 删除产品
|
*/
|
@DeleteMapping("/{id}")
|
@ApiOperation("删除产品")
|
public Result<Boolean> deleteProduct(@ApiParam("产品ID") @PathVariable Long id) {
|
try {
|
boolean result = productService.deleteProduct(id);
|
if (result) {
|
return Result.success("产品删除成功", true);
|
} else {
|
return Result.error("产品删除失败");
|
}
|
} catch (Exception e) {
|
log.error("删除产品失败", e);
|
return Result.error("删除产品失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 更新产品状态
|
*/
|
@PutMapping("/{id}/status")
|
@ApiOperation("更新产品状态")
|
public Result<Boolean> updateProductStatus(
|
@ApiParam("产品ID") @PathVariable Long id,
|
@ApiParam("产品状态") @RequestParam String status) {
|
try {
|
boolean result = productService.updateProductStatus(id, status);
|
if (result) {
|
return Result.success("产品状态更新成功", true);
|
} else {
|
return Result.error("产品状态更新失败");
|
}
|
} catch (Exception e) {
|
log.error("更新产品状态失败", e);
|
return Result.error("更新产品状态失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 更新产品审核状态
|
*/
|
@PutMapping("/{id}/audit-status")
|
@ApiOperation("更新产品审核状态")
|
public Result<Boolean> updateProductAuditStatus(
|
@ApiParam("产品ID") @PathVariable Long id,
|
@ApiParam("审核状态") @RequestParam String auditStatus) {
|
try {
|
boolean result = productService.updateProductAuditStatus(id, auditStatus);
|
if (result) {
|
return Result.success("产品审核状态更新成功", true);
|
} else {
|
return Result.error("产品审核状态更新失败");
|
}
|
} catch (Exception e) {
|
log.error("更新产品审核状态失败", e);
|
return Result.error("更新产品审核状态失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 检查产品编码是否存在
|
*/
|
@GetMapping("/check-code")
|
@ApiOperation("检查产品编码是否存在")
|
public Result<Boolean> checkProductCodeExists(
|
@ApiParam("产品编码") @RequestParam String productCode,
|
@ApiParam("排除的产品ID") @RequestParam(required = false) Long excludeId) {
|
try {
|
boolean exists = productService.checkProductCodeExists(productCode, excludeId);
|
return Result.success(exists);
|
} catch (Exception e) {
|
log.error("检查产品编码失败", e);
|
return Result.error("检查产品编码失败: " + e.getMessage());
|
}
|
}
|
}
|