迁移购物车模块
This commit is contained in:
@@ -1,46 +0,0 @@
|
||||
package cn.iocoder.mall.order.biz.dao;
|
||||
|
||||
import cn.iocoder.mall.order.biz.dataobject.CartItemDO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface CartMapper {
|
||||
|
||||
CartItemDO selectById(@Param("id") Integer id);
|
||||
|
||||
List<CartItemDO> selectByIds(@Param("ids") Collection<Integer> ids);
|
||||
|
||||
CartItemDO selectByUserIdAndSkuIdAndStatus(@Param("userId") Integer userId,
|
||||
@Param("skuId") Integer skuId,
|
||||
@Param("status") Integer status);
|
||||
|
||||
Integer selectQuantitySumByUserIdAndStatus(@Param("userId") Integer userId,
|
||||
@Param("status") Integer status);
|
||||
|
||||
List<CartItemDO> selectByUserIdAndStatusAndSelected(@Param("userId") Integer userId,
|
||||
@Param("status") Integer status,
|
||||
@Param("selected") Boolean selected);
|
||||
//
|
||||
// List<CartItemDO> selectListByTitleLike(@Param("title") String title,
|
||||
// @Param("offset") Integer offset,
|
||||
// @Param("limit") Integer limit);
|
||||
|
||||
// Integer selectCountByTitleLike(@Param("title") String title);
|
||||
|
||||
void insert(CartItemDO cartItemDO);
|
||||
|
||||
int update(CartItemDO cartItemDO);
|
||||
|
||||
int updateQuantity(@Param("id") Integer id,
|
||||
@Param("quantityIncr") Integer quantityIncr);
|
||||
|
||||
int updateListByUserIdAndSkuId(@Param("userId") Integer userId,
|
||||
@Param("skuIds") Collection<Integer> skuIds,
|
||||
@Param("selected") Boolean selected,
|
||||
@Param("status") Integer status);
|
||||
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
package cn.iocoder.mall.order.biz.dataobject;
|
||||
|
||||
import cn.iocoder.common.framework.dataobject.BaseDO;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 购物车的商品信息
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class CartItemDO extends BaseDO {
|
||||
|
||||
// ========= 基础字段 BEGIN =========
|
||||
|
||||
/**
|
||||
* 编号,唯一自增。
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 状态
|
||||
*
|
||||
* 1-正常
|
||||
* 2-主动删除
|
||||
* 3-下单删除
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 商品在购物车中的删除时间
|
||||
*/
|
||||
private Date deleteTime;
|
||||
/**
|
||||
* 是否选中
|
||||
*/
|
||||
private Boolean selected;
|
||||
|
||||
// ========= 基础字段 END =========
|
||||
|
||||
// ========= 买家信息 BEGIN =========
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer userId;
|
||||
|
||||
// ========= 买家信息 END =========
|
||||
|
||||
// ========= 商品信息 BEGIN =========
|
||||
|
||||
/**
|
||||
* 商品 SPU 编号
|
||||
*/
|
||||
private Integer spuId;
|
||||
/**
|
||||
* 商品 SKU 编号
|
||||
*/
|
||||
private Integer skuId;
|
||||
/**
|
||||
* 商品购买数量
|
||||
*/
|
||||
private Integer quantity;
|
||||
|
||||
// TODO 冗余字段
|
||||
|
||||
|
||||
// ========= 商品信息 END =========
|
||||
|
||||
// ========= 交易信息 BEGIN =========
|
||||
|
||||
/**
|
||||
* 订单编号
|
||||
*/
|
||||
private Integer orderId;
|
||||
/**
|
||||
* 订单创建时间
|
||||
*/
|
||||
private Date orderCreateTime;
|
||||
|
||||
// ========= 交易信息 BEGIN =========
|
||||
|
||||
// ========= 优惠信息 BEGIN =========
|
||||
|
||||
// /**
|
||||
// * 商品营销活动编号
|
||||
// */
|
||||
// private Integer activityId;
|
||||
// /**
|
||||
// * 商品营销活动类型
|
||||
// */
|
||||
// private Integer activityType;
|
||||
|
||||
// ========= 优惠信息 END =========
|
||||
|
||||
}
|
||||
@@ -45,95 +45,6 @@ public class CartServiceImpl implements CartService {
|
||||
@Autowired
|
||||
private CartMapper cartMapper;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("Duplicates")
|
||||
public Boolean add(Integer userId, Integer skuId, Integer quantity) {
|
||||
// 查询 SKU 是否合法
|
||||
ProductSkuBO sku = productSpuService.getProductSku(skuId);
|
||||
if (sku == null
|
||||
|| CommonStatusEnum.DISABLE.getValue().equals(sku.getStatus())) { // sku 被禁用
|
||||
throw ServiceExceptionUtil.exception(OrderErrorCodeEnum.CARD_ITEM_SKU_NOT_FOUND.getCode());
|
||||
}
|
||||
// TODO 芋艿,后续基于商品是否上下架进一步完善。
|
||||
// 查询 CartItemDO
|
||||
CartItemDO item = cartMapper.selectByUserIdAndSkuIdAndStatus(userId, skuId, CartItemStatusEnum.ENABLE.getValue());
|
||||
// 存在,则进行数量更新
|
||||
if (item != null) {
|
||||
return updateQuantity0(item, sku, quantity);
|
||||
}
|
||||
// 不存在,则进行插入
|
||||
return add0(userId, sku, quantity);
|
||||
}
|
||||
|
||||
private Boolean add0(Integer userId, ProductSkuBO sku, Integer quantity) {
|
||||
// 校验库存
|
||||
if (quantity > sku.getQuantity()) {
|
||||
throw ServiceExceptionUtil.exception(OrderErrorCodeEnum.CARD_ITEM_SKU_NOT_FOUND.getCode());
|
||||
}
|
||||
// 创建 CartItemDO 对象,并进行保存。
|
||||
CartItemDO item = new CartItemDO()
|
||||
// 基础字段
|
||||
.setStatus(CartItemStatusEnum.ENABLE.getValue()).setSelected(true)
|
||||
// 买家信息
|
||||
.setUserId(userId)
|
||||
// 商品信息
|
||||
.setSpuId(sku.getSpuId()).setSkuId(sku.getId()).setQuantity(quantity);
|
||||
item.setCreateTime(new Date());
|
||||
cartMapper.insert(item);
|
||||
// 返回成功
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("Duplicates")
|
||||
public Boolean updateQuantity(Integer userId, Integer skuId, Integer quantity) {
|
||||
// 查询 SKU 是否合法
|
||||
ProductSkuBO sku = productSpuService.getProductSku(skuId);
|
||||
if (sku == null
|
||||
|| CommonStatusEnum.DISABLE.getValue().equals(sku.getStatus())) { // sku 被禁用
|
||||
throw ServiceExceptionUtil.exception(OrderErrorCodeEnum.CARD_ITEM_SKU_NOT_FOUND.getCode());
|
||||
}
|
||||
// 查询 CartItemDO
|
||||
CartItemDO item = cartMapper.selectByUserIdAndSkuIdAndStatus(userId, skuId, CartItemStatusEnum.ENABLE.getValue());
|
||||
if (item == null) {
|
||||
throw ServiceExceptionUtil.exception(OrderErrorCodeEnum.CARD_ITEM_NOT_FOUND.getCode());
|
||||
}
|
||||
// TODO 芋艿,后续基于商品是否上下架进一步完善。
|
||||
return updateQuantity0(item, sku, quantity);
|
||||
}
|
||||
|
||||
private Boolean updateQuantity0(CartItemDO item, ProductSkuBO sku, Integer quantity) {
|
||||
// 校验库存
|
||||
if (item.getQuantity() + quantity > sku.getQuantity()) {
|
||||
throw ServiceExceptionUtil.exception(OrderErrorCodeEnum.CARD_ITEM_SKU_NOT_FOUND.getCode());
|
||||
}
|
||||
// 更新 CartItemDO
|
||||
cartMapper.updateQuantity(item.getId(), quantity);
|
||||
// 返回成功
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean updateSelected(Integer userId, Collection<Integer> skuIds, Boolean selected) {
|
||||
// 更新 CartItemDO 们
|
||||
cartMapper.updateListByUserIdAndSkuId(userId, skuIds, selected, null);
|
||||
// 返回成功
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean deleteList(Integer userId, List<Integer> skuIds) {
|
||||
// 更新 CartItemDO 们
|
||||
cartMapper.updateListByUserIdAndSkuId(userId, skuIds, null, CartItemStatusEnum.DELETE_BY_MANUAL.getValue());
|
||||
// 返回成功
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean deleteAll(Integer userId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer count(Integer userId) {
|
||||
return cartMapper.selectQuantitySumByUserIdAndStatus(userId, CartItemStatusEnum.ENABLE.getValue());
|
||||
|
||||
Reference in New Issue
Block a user