后端 + 前端:购物车详情

This commit is contained in:
YunaiV
2019-04-13 22:53:44 +08:00
parent fa5ea5dfd9
commit b2abc625d1
16 changed files with 497 additions and 144 deletions

View File

@@ -3,18 +3,20 @@ package cn.iocoder.mall.order.application.controller.users;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.order.api.CartService;
import cn.iocoder.mall.order.api.OrderService;
import cn.iocoder.mall.order.api.bo.CalcOrderPriceBO;
import cn.iocoder.mall.order.api.bo.CartItemBO;
import cn.iocoder.mall.order.api.dto.CalcOrderPriceDTO;
import cn.iocoder.mall.order.application.convert.CartConvert;
import cn.iocoder.mall.order.application.vo.UsersCartDetailVO;
import cn.iocoder.mall.order.application.vo.UsersOrderConfirmCreateVO;
import cn.iocoder.mall.user.sdk.context.UserSecurityContextHolder;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
@RestController
@RequestMapping("users/cart")
@@ -26,8 +28,8 @@ public class UsersCartController {
private OrderService orderService;
@PostMapping("add")
public CommonResult<Integer> add(@Param("skuId") Integer skuId,
@Param("quantity") Integer quantity) {
public CommonResult<Integer> add(@RequestParam("skuId") Integer skuId,
@RequestParam("quantity") Integer quantity) {
// 添加到购物车
CommonResult<Boolean> addResult = cartService.add(UserSecurityContextHolder.getContext().getUserId(),
skuId, quantity);
@@ -40,8 +42,8 @@ public class UsersCartController {
}
@PostMapping("update_quantity")
public CommonResult<Integer> updateQuantity(@Param("skuId") Integer skuId,
@Param("quantity") Integer quantity) {
public CommonResult<UsersCartDetailVO> updateQuantity(@RequestParam("skuId") Integer skuId, // TODO 芋艿,先暂用这个 VO 。等促销活动出来后,做调整
@RequestParam("quantity") Integer quantity) {
// 添加到购物车
CommonResult<Boolean> updateQuantityResult = cartService.updateQuantity(UserSecurityContextHolder.getContext().getUserId(),
skuId, quantity);
@@ -49,24 +51,22 @@ public class UsersCartController {
if (updateQuantityResult.isError()) {
return CommonResult.error(updateQuantityResult);
}
// 获得目前购物车商品总数量
// TODO 芋艿,需要改成价格计算
return cartService.count(UserSecurityContextHolder.getContext().getUserId());
// 获得目前购物车明细
return getCartDetail();
}
@PostMapping("update_selected")
public CommonResult<Integer> updateSelected(@Param("skuId") Integer skuId,
@Param("selected") Boolean selected) {
public CommonResult<UsersCartDetailVO> updateSelected(@RequestParam("skuIds") Set<Integer> skuIds, // TODO 芋艿,先暂用这个 VO 。等促销活动出来后,做调整
@RequestParam("selected") Boolean selected) {
// 添加到购物车
CommonResult<Boolean> updateSelectedResult = cartService.updateSelected(UserSecurityContextHolder.getContext().getUserId(),
skuId, selected);
skuIds, selected);
// 添加失败,则直接返回错误
if (updateSelectedResult.isError()) {
return CommonResult.error(updateSelectedResult);
}
// 获得目前购物车商品总数量
// TODO 芋艿,需要改成价格计算
return cartService.count(UserSecurityContextHolder.getContext().getUserId());
// 获得目前购物车明细
return getCartDetail();
}
@GetMapping("count")
@@ -75,25 +75,33 @@ public class UsersCartController {
}
@GetMapping("/list")
public CommonResult<UsersOrderConfirmCreateVO> list() {
// 获得购物车中所有的
List<CartItemBO> cartItems = cartService.list(UserSecurityContextHolder.getContext().getUserId(), null);
public CommonResult<UsersCartDetailVO> list() { // TODO 芋艿,先暂用这个 VO 。等促销活动出来后,做调整
return getCartDetail();
}
private CommonResult<UsersCartDetailVO> getCartDetail() {
// 获得购物车中选中的
List<CartItemBO> cartItems = cartService.list(UserSecurityContextHolder.getContext().getUserId(), null).getData();
// 购物车为空时,构造空的 UsersOrderConfirmCreateVO 返回
if (cartItems.isEmpty()) {
UsersOrderConfirmCreateVO result = new UsersOrderConfirmCreateVO();
UsersCartDetailVO result = new UsersCartDetailVO();
result.setItemGroups(Collections.emptyList());
result.setFee(new UsersOrderConfirmCreateVO.Fee(0, 0, 0, 0));
result.setFee(new UsersCartDetailVO.Fee(0, 0, 0, 0));
return CommonResult.success(result);
}
// 购物车非空时,获得具体结果
return null;
// 计算商品价格
CommonResult<CalcOrderPriceBO> calcOrderPriceResult = list0(cartItems);
if (calcOrderPriceResult.isError()) {
return CommonResult.error(calcOrderPriceResult);
}
// 执行数据拼装
return CommonResult.success(CartConvert.INSTANCE.convert2(calcOrderPriceResult.getData()));
}
@GetMapping("/confirm_create_order")
public CommonResult<UsersOrderConfirmCreateVO> getConfirmCreateOrder() {
// 获得购物车中选中的
List<CartItemBO> cartItems = cartService.list(UserSecurityContextHolder.getContext().getUserId(), true);
List<CartItemBO> cartItems = cartService.list(UserSecurityContextHolder.getContext().getUserId(), true).getData();
// 购物车为空时,构造空的 UsersOrderConfirmCreateVO 返回
if (cartItems.isEmpty()) {
UsersOrderConfirmCreateVO result = new UsersOrderConfirmCreateVO();
@@ -101,9 +109,24 @@ public class UsersCartController {
result.setFee(new UsersOrderConfirmCreateVO.Fee(0, 0, 0, 0));
return CommonResult.success(result);
}
// 购物车非空时,获得具体结果
// return CommonResult.success(CartConvert.INSTANCE.convert(calcOrderPriceResult.getData()));
return null;
// 计算商品价格
CommonResult<CalcOrderPriceBO> calcOrderPriceResult = list0(cartItems);
if (calcOrderPriceResult.isError()) {
return CommonResult.error(calcOrderPriceResult);
}
// 执行数据拼装
return CommonResult.success(CartConvert.INSTANCE.convert(calcOrderPriceResult.getData()));
}
private CommonResult<CalcOrderPriceBO> list0(List<CartItemBO> cartItems) {
// 创建计算的 DTO
CalcOrderPriceDTO calcOrderPriceDTO = new CalcOrderPriceDTO()
.setItems(new ArrayList<>(cartItems.size()));
for (CartItemBO item : cartItems) {
calcOrderPriceDTO.getItems().add(new CalcOrderPriceDTO.Item(item.getSkuId(), item.getQuantity(), item.getSelected()));
}
// 执行计算
return cartService.calcOrderPrice(calcOrderPriceDTO);
}
public CommonResult<Object> confirmOrder() {

View File

@@ -1,6 +1,7 @@
package cn.iocoder.mall.order.application.convert;
import cn.iocoder.mall.order.api.bo.CalcOrderPriceBO;
import cn.iocoder.mall.order.application.vo.UsersCartDetailVO;
import cn.iocoder.mall.order.application.vo.UsersOrderConfirmCreateVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@@ -12,4 +13,6 @@ public interface CartConvert {
UsersOrderConfirmCreateVO convert(CalcOrderPriceBO calcOrderPriceBO);
UsersCartDetailVO convert2(CalcOrderPriceBO calcOrderPriceBO);
}

View File

@@ -1,26 +0,0 @@
package cn.iocoder.mall.order.application.vo;
public class FeeMessageVO {
/**
* 总价
*/
private Integer originalTotal;
/**
* 优惠总价
*
* 注意,满多少元包邮,不算在优惠中。
*/
private Integer discountTotal;
/**
* 邮费
*/
private Integer postageTotal;
/**
* 最终价格
*
* 计算公式 = 总价 - 优惠总价 + 邮费
*/
private Integer presentTotal;
}

View File

@@ -0,0 +1,167 @@
package cn.iocoder.mall.order.application.vo;
import cn.iocoder.mall.product.api.bo.ProductAttrAndValuePairBO;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.List;
@ApiModel(value = "购物车明细 VO")
@Data
@Accessors(chain = true)
public class UsersCartDetailVO {
/**
* 商品分组数组
*/
private List<ItemGroup> itemGroups;
/**
* 费用
*/
private Fee fee;
/**
* 商品分组
*
* 多个商品,参加同一个活动,从而形成分组。
*/
@Data
@Accessors(chain = true)
public static class ItemGroup {
// TODO 优惠活动
private Object activity;
/**
* 商品数组
*/
private List<Sku> items;
}
@Data
@Accessors(chain = true)
public static class Sku {
// SKU 自带信息
/**
* sku 编号
*/
private Integer id;
/**
* SPU 信息
*/
private Spu spu;
/**
* 图片地址
*/
private String picURL;
/**
* 规格值数组
*/
private List<ProductAttrAndValuePairBO> attrs; // TODO 后面改下
/**
* 价格,单位:分
*/
private Integer price;
/**
* 库存数量
*/
private Integer quantity;
// 非 SKU 自带信息
/**
* 购买数量
*/
private Integer buyQuantity;
/**
* 是否选中
*/
private Boolean selected;
}
@Data
@Accessors(chain = true)
public static class Spu {
/**
* SPU 编号
*/
private Integer id;
// ========== 基本信息 =========
/**
* SPU 名字
*/
private String name;
/**
* 分类编号
*/
private Integer cid;
/**
* 商品主图地址
*
* 数组,以逗号分隔
*
* 建议尺寸800*800像素你可以拖拽图片调整顺序最多上传15张
*/
private List<String> picUrls;
}
/**
* 费用(合计)
*/
@Data
@Accessors(chain = true)
public static class Fee {
/**
* 总价
*/
private Integer originalTotal;
/**
* 优惠总价
*
* 注意,满多少元包邮,不算在优惠中。
*/
private Integer discountTotal;
/**
* 邮费
*/
private Integer postageTotal;
/**
* 最终价格
*
* 计算公式 = 总价 - 优惠总价 + 邮费
*/
private Integer presentTotal;
public Fee() {
}
public Fee(Integer originalTotal, Integer discountTotal, Integer postageTotal, Integer presentTotal) {
this.originalTotal = originalTotal;
this.discountTotal = discountTotal;
this.postageTotal = postageTotal;
this.presentTotal = presentTotal;
}
}
/**
* 邮费信息
*/
@Data
@Accessors(chain = true)
public static class Postage {
/**
* 需要满足多少钱,可以包邮。单位:分
*/
private Integer threshold;
}
}

View File

@@ -1,5 +0,0 @@
package cn.iocoder.mall.order.application.vo;
public class UsersCartItemVO {
}

View File

@@ -1,11 +0,0 @@
package cn.iocoder.mall.order.application.vo;
import java.util.List;
public class UsersCartListVO {
private List<UsersCartItemVO> items;
private FeeMessageVO feeMessage;
}