package com.webmanage.controller;
|
|
import com.webmanage.common.Result;
|
import com.webmanage.dto.CartItemDTO;
|
import com.webmanage.dto.CartQueryDTO;
|
import com.webmanage.service.CartService;
|
import com.webmanage.vo.CartVO;
|
import com.webmanage.vo.CartItemVO;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiParam;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.validation.annotation.Validated;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.annotation.Resource;
|
import javax.validation.Valid;
|
import javax.validation.constraints.Min;
|
import javax.validation.constraints.NotNull;
|
import java.util.List;
|
|
/**
|
* 购物车管理Controller
|
*/
|
@Slf4j
|
@RestController
|
@RequestMapping("/api/cart")
|
@Api(tags = "购物车管理")
|
@Validated
|
public class CartController {
|
|
@Resource
|
private CartService cartService;
|
|
@PostMapping("/add")
|
@ApiOperation("添加商品到购物车")
|
public Result<Object> addToCart(@Valid @RequestBody CartItemDTO cartItemDTO,
|
@RequestParam @NotNull Long userId,
|
@RequestParam @NotNull Long unitId) {
|
try {
|
boolean result = cartService.addToCart(userId, unitId, cartItemDTO);
|
if (result) {
|
return Result.success("添加商品到购物车成功");
|
} else {
|
return Result.error("添加商品到购物车失败");
|
}
|
} catch (Exception e) {
|
log.error("添加商品到购物车失败", e);
|
return Result.error("添加商品到购物车失败:" + e.getMessage());
|
}
|
}
|
|
@DeleteMapping("/remove")
|
@ApiOperation("从购物车移除商品")
|
public Result<Object> removeFromCart(@RequestParam @NotNull Long userId,
|
@RequestParam @NotNull Long unitId,
|
@RequestParam @NotNull Long pricingId) {
|
try {
|
boolean result = cartService.removeFromCart(userId, unitId, pricingId);
|
if (result) {
|
return Result.success("从购物车移除商品成功");
|
} else {
|
return Result.error("从购物车移除商品失败");
|
}
|
} catch (Exception e) {
|
log.error("从购物车移除商品失败", e);
|
return Result.error("从购物车移除商品失败:" + e.getMessage());
|
}
|
}
|
|
@PutMapping("/update")
|
@ApiOperation("更新购物车商品数量")
|
public Result<Object> updateCartItemQuantity(@RequestParam @NotNull Long userId,
|
@RequestParam @NotNull Long unitId,
|
@RequestParam @NotNull Long pricingId,
|
@RequestParam @NotNull @Min(1) Integer quantity) {
|
try {
|
boolean result = cartService.updateCartItemQuantity(userId, unitId, pricingId, quantity);
|
if (result) {
|
return Result.success("更新购物车商品数量成功");
|
} else {
|
return Result.error("更新购物车商品数量失败");
|
}
|
} catch (Exception e) {
|
log.error("更新购物车商品数量失败", e);
|
return Result.error("更新购物车商品数量失败:" + e.getMessage());
|
}
|
}
|
|
@DeleteMapping("/clear")
|
@ApiOperation("清空购物车")
|
public Result<Object> clearCart(@RequestParam @NotNull Long userId,
|
@RequestParam @NotNull Long unitId) {
|
try {
|
boolean result = cartService.clearCart(userId, unitId);
|
if (result) {
|
return Result.success("清空购物车成功");
|
} else {
|
return Result.error("清空购物车失败");
|
}
|
} catch (Exception e) {
|
log.error("清空购物车失败", e);
|
return Result.error("清空购物车失败:" + e.getMessage());
|
}
|
}
|
|
@GetMapping("/info")
|
@ApiOperation("获取购物车信息")
|
public Result<Object> getCart(@RequestParam @NotNull Long userId,
|
@RequestParam @NotNull Long unitId) {
|
try {
|
CartVO cart = cartService.getCart(userId, unitId);
|
return Result.success(cart);
|
} catch (Exception e) {
|
log.error("获取购物车信息失败", e);
|
return Result.error("获取购物车信息失败:" + e.getMessage());
|
}
|
}
|
|
@GetMapping("/items")
|
@ApiOperation("获取购物车商品列表")
|
public Result<Object> getCartItems(@RequestParam @NotNull Long userId,
|
@RequestParam @NotNull Long unitId) {
|
try {
|
List<CartItemVO> items = cartService.getCartItems(userId, unitId);
|
return Result.success(items);
|
} catch (Exception e) {
|
log.error("获取购物车商品列表失败", e);
|
return Result.error("获取购物车商品列表失败:" + e.getMessage());
|
}
|
}
|
|
@DeleteMapping("/batch-remove")
|
@ApiOperation("批量删除购物车商品")
|
public Result<Object> batchRemoveFromCart(@RequestParam @NotNull Long userId,
|
@RequestParam @NotNull Long unitId,
|
@RequestBody List<Long> pricingIds) {
|
try {
|
boolean result = cartService.batchRemoveFromCart(userId, unitId, pricingIds);
|
if (result) {
|
return Result.success("批量删除购物车商品成功");
|
} else {
|
return Result.error("批量删除购物车商品失败");
|
}
|
} catch (Exception e) {
|
log.error("批量删除购物车商品失败", e);
|
return Result.error("批量删除购物车商品失败:" + e.getMessage());
|
}
|
}
|
|
@GetMapping("/count")
|
@ApiOperation("获取购物车商品数量")
|
public Result<Object> getCartItemCount(@RequestParam @NotNull Long userId,
|
@RequestParam @NotNull Long unitId) {
|
try {
|
Integer count = cartService.getCartItemCount(userId, unitId);
|
return Result.success(count);
|
} catch (Exception e) {
|
log.error("获取购物车商品数量失败", e);
|
return Result.error("获取购物车商品数量失败:" + e.getMessage());
|
}
|
}
|
|
@PostMapping("/sync-to-db")
|
@ApiOperation("同步Redis购物车数据到数据库")
|
public Result<Object> syncCartToDatabase(@RequestParam @NotNull Long userId,
|
@RequestParam @NotNull Long unitId) {
|
try {
|
boolean result = cartService.syncCartToDatabase(userId, unitId);
|
if (result) {
|
return Result.success("同步购物车数据到数据库成功");
|
} else {
|
return Result.error("同步购物车数据到数据库失败");
|
}
|
} catch (Exception e) {
|
log.error("同步购物车数据到数据库失败", e);
|
return Result.error("同步购物车数据到数据库失败:" + e.getMessage());
|
}
|
}
|
|
@PostMapping("/load-from-db")
|
@ApiOperation("从数据库加载购物车数据到Redis")
|
public Result<Object> loadCartFromDatabase(@RequestParam @NotNull Long userId,
|
@RequestParam @NotNull Long unitId) {
|
try {
|
boolean result = cartService.loadCartFromDatabase(userId, unitId);
|
if (result) {
|
return Result.success("从数据库加载购物车数据成功");
|
} else {
|
return Result.error("从数据库加载购物车数据失败");
|
}
|
} catch (Exception e) {
|
log.error("从数据库加载购物车数据失败", e);
|
return Result.error("从数据库加载购物车数据失败:" + e.getMessage());
|
}
|
}
|
|
@GetMapping("/consistency")
|
@ApiOperation("检查购物车数据一致性")
|
public Result<Object> checkCartConsistency(@RequestParam @NotNull Long userId,
|
@RequestParam @NotNull Long unitId) {
|
try {
|
boolean isConsistent = cartService.checkCartConsistency(userId, unitId);
|
if (isConsistent) {
|
return Result.success("购物车数据一致");
|
} else {
|
return Result.error("购物车数据不一致");
|
}
|
} catch (Exception e) {
|
log.error("检查购物车数据一致性失败", e);
|
return Result.error("检查购物车数据一致性失败:" + e.getMessage());
|
}
|
}
|
}
|