|
|
|
|
@ -0,0 +1,184 @@
|
|
|
|
|
// 定义包名,用于组织代码结构
|
|
|
|
|
package com.macro.mall.portal.controller;
|
|
|
|
|
|
|
|
|
|
// 导入通用结果类,用于统一返回操作结果
|
|
|
|
|
import com.macro.mall.common.api.CommonResult;
|
|
|
|
|
// 导入购物车项实体类,用于封装购物车中的商品信息
|
|
|
|
|
import com.macro.mall.model.OmsCartItem;
|
|
|
|
|
// 导入购物车商品实体类,用于封装购物车中商品的详细信息
|
|
|
|
|
import com.macro.mall.portal.domain.CartProduct;
|
|
|
|
|
// 导入购物车促销项实体类,用于封装购物车中商品的促销信息
|
|
|
|
|
import com.macro.mall.portal.domain.CartPromotionItem;
|
|
|
|
|
// 导入购物车项服务接口,用于访问购物车项数据
|
|
|
|
|
import com.macro.mall.portal.service.OmsCartItemService;
|
|
|
|
|
// 导入会员服务接口,用于访问会员数据
|
|
|
|
|
import com.macro.mall.portal.service.UmsMemberService;
|
|
|
|
|
// 导入Swagger注解,用于生成API文档
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
|
// 导入Spring框架的注解,用于声明组件和自动注入
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
// 导入Java的List接口,用于操作列表数据
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 购物车管理Controller
|
|
|
|
|
* 该类负责处理购物车相关的增删改查操作
|
|
|
|
|
* Created by macro on 2018/8/2.
|
|
|
|
|
*/
|
|
|
|
|
@Controller
|
|
|
|
|
@Api(tags = "OmsCartItemController")
|
|
|
|
|
@Tag(name = "OmsCartItemController", description = "购物车管理")
|
|
|
|
|
@RequestMapping("/cart")
|
|
|
|
|
public class OmsCartItemController {
|
|
|
|
|
// 使用@Autowired注解自动注入OmsCartItemService实例
|
|
|
|
|
@Autowired
|
|
|
|
|
private OmsCartItemService cartItemService;
|
|
|
|
|
// 使用@Autowired注解自动注入UmsMemberService实例
|
|
|
|
|
@Autowired
|
|
|
|
|
private UmsMemberService memberService;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加商品到购物车
|
|
|
|
|
* @param cartItem 购物车项对象,包含商品ID和数量等信息
|
|
|
|
|
* @return 通用结果对象,包含操作结果和数据
|
|
|
|
|
*/
|
|
|
|
|
@ApiOperation("添加商品到购物车")
|
|
|
|
|
@RequestMapping(value = "/add", method = RequestMethod.POST)
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public CommonResult add(@RequestBody OmsCartItem cartItem) {
|
|
|
|
|
// 调用cartItemService的add方法添加商品到购物车
|
|
|
|
|
int count = cartItemService.add(cartItem);
|
|
|
|
|
// 如果添加成功,返回成功的通用结果对象
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
return CommonResult.success(count);
|
|
|
|
|
}
|
|
|
|
|
// 如果添加失败,返回失败的通用结果对象
|
|
|
|
|
return CommonResult.failed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前会员的购物车列表
|
|
|
|
|
* @return 通用结果对象,包含购物车列表数据
|
|
|
|
|
*/
|
|
|
|
|
@ApiOperation("获取当前会员的购物车列表")
|
|
|
|
|
@RequestMapping(value = "/list", method = RequestMethod.GET)
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public CommonResult<List<OmsCartItem>> list() {
|
|
|
|
|
// 调用cartItemService的list方法获取当前会员的购物车列表
|
|
|
|
|
List<OmsCartItem> cartItemList = cartItemService.list(memberService.getCurrentMember().getId());
|
|
|
|
|
// 返回包含购物车列表的通用结果对象
|
|
|
|
|
return CommonResult.success(cartItemList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前会员的购物车列表,包括促销信息
|
|
|
|
|
* @param cartIds 购物车项ID列表,用于筛选特定商品的促销信息
|
|
|
|
|
* @return 通用结果对象,包含购物车促销列表数据
|
|
|
|
|
*/
|
|
|
|
|
@ApiOperation("获取当前会员的购物车列表,包括促销信息")
|
|
|
|
|
@RequestMapping(value = "/list/promotion", method = RequestMethod.GET)
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public CommonResult<List<CartPromotionItem>> listPromotion(@RequestParam(required = false) List<Long> cartIds) {
|
|
|
|
|
// 调用cartItemService的listPromotion方法获取当前会员的购物车列表,包括促销信息
|
|
|
|
|
List<CartPromotionItem> cartPromotionItemList = cartItemService.listPromotion(memberService.getCurrentMember().getId(), cartIds);
|
|
|
|
|
// 返回包含购物车促销列表的通用结果对象
|
|
|
|
|
return CommonResult.success(cartPromotionItemList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改购物车中指定商品的数量
|
|
|
|
|
* @param id 购物车项ID
|
|
|
|
|
* @param quantity 修改后的商品数量
|
|
|
|
|
* @return 通用结果对象,包含操作结果和数据
|
|
|
|
|
*/
|
|
|
|
|
@ApiOperation("修改购物车中指定商品的数量")
|
|
|
|
|
@RequestMapping(value = "/update/quantity", method = RequestMethod.GET)
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public CommonResult updateQuantity(@RequestParam Long id,
|
|
|
|
|
@RequestParam Integer quantity) {
|
|
|
|
|
// 调用cartItemService的updateQuantity方法修改购物车中指定商品的数量
|
|
|
|
|
int count = cartItemService.updateQuantity(id, memberService.getCurrentMember().getId(), quantity);
|
|
|
|
|
// 如果修改成功,返回成功的通用结果对象
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
return CommonResult.success(count);
|
|
|
|
|
}
|
|
|
|
|
// 如果修改失败,返回失败的通用结果对象
|
|
|
|
|
return CommonResult.failed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取购物车中指定商品的规格,用于重选规格
|
|
|
|
|
* @param productId 要获取规格的商品ID
|
|
|
|
|
* @return 通用结果对象,包含商品规格数据
|
|
|
|
|
*/
|
|
|
|
|
@ApiOperation("获取购物车中指定商品的规格,用于重选规格")
|
|
|
|
|
@RequestMapping(value = "/getProduct/{productId}", method = RequestMethod.GET)
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public CommonResult<CartProduct> getCartProduct(@PathVariable Long productId) {
|
|
|
|
|
// 调用cartItemService的getCartProduct方法获取购物车中指定商品的规格
|
|
|
|
|
CartProduct cartProduct = cartItemService.getCartProduct(productId);
|
|
|
|
|
// 返回包含商品规格的通用结果对象
|
|
|
|
|
return CommonResult.success(cartProduct);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改购物车中商品的规格
|
|
|
|
|
* @param cartItem 购物车项对象,包含商品ID和新规格等信息
|
|
|
|
|
* @return 通用结果对象,包含操作结果和数据
|
|
|
|
|
*/
|
|
|
|
|
@ApiOperation("修改购物车中商品的规格")
|
|
|
|
|
@RequestMapping(value = "/update/attr", method = RequestMethod.POST)
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public CommonResult updateAttr(@RequestBody OmsCartItem cartItem) {
|
|
|
|
|
// 调用cartItemService的updateAttr方法修改购物车中商品的规格
|
|
|
|
|
int count = cartItemService.updateAttr(cartItem);
|
|
|
|
|
// 如果修改成功,返回成功的通用结果对象
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
return CommonResult.success(count);
|
|
|
|
|
}
|
|
|
|
|
// 如果修改失败,返回失败的通用结果对象
|
|
|
|
|
return CommonResult.failed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除购物车中的指定商品
|
|
|
|
|
* @param ids 要删除的商品ID列表
|
|
|
|
|
* @return 通用结果对象,包含操作结果和数据
|
|
|
|
|
*/
|
|
|
|
|
@ApiOperation("删除购物车中的指定商品")
|
|
|
|
|
@RequestMapping(value = "/delete", method = RequestMethod.POST)
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public CommonResult delete(@RequestParam("ids") List<Long> ids) {
|
|
|
|
|
// 调用cartItemService的delete方法删除购物车中的指定商品
|
|
|
|
|
int count = cartItemService.delete(memberService.getCurrentMember().getId(), ids);
|
|
|
|
|
// 如果删除成功,返回成功的通用结果对象
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
return CommonResult.success(count);
|
|
|
|
|
}
|
|
|
|
|
// 如果删除失败,返回失败的通用结果对象
|
|
|
|
|
return CommonResult.failed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 清空当前会员的购物车
|
|
|
|
|
* @return 通用结果对象,包含操作结果和数据
|
|
|
|
|
*/
|
|
|
|
|
@ApiOperation("清空当前会员的购物车")
|
|
|
|
|
@RequestMapping(value = "/clear", method = RequestMethod.POST)
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public CommonResult clear() {
|
|
|
|
|
// 调用cartItemService的clear方法清空当前会员的购物车
|
|
|
|
|
int count = cartItemService.clear(memberService.getCurrentMember().getId());
|
|
|
|
|
// 如果清空成功,返回成功的通用结果对象
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
return CommonResult.success(count);
|
|
|
|
|
}
|
|
|
|
|
// 如果清空失败,返回失败的通用结果对象
|
|
|
|
|
return CommonResult.failed();
|
|
|
|
|
}
|
|
|
|
|
}
|