后端 + 前端:添加到购物车
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
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> selectListByStatus(@Param("status") Integer status);
|
||||
//
|
||||
// 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);
|
||||
|
||||
}
|
||||
@@ -13,4 +13,5 @@ import org.springframework.stereotype.Repository;
|
||||
public interface OrderCancelMapper {
|
||||
|
||||
int insert(OrderCancelDO orderCancelDO);
|
||||
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ public class CartItemDO extends BaseDO {
|
||||
/**
|
||||
* 订单编号
|
||||
*/
|
||||
private String orderId;
|
||||
private Integer orderId;
|
||||
/**
|
||||
* 订单创建时间
|
||||
*/
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
package cn.iocoder.mall.order.biz.mock;
|
||||
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.product.api.ProductSpuService;
|
||||
import cn.iocoder.mall.product.api.bo.ProductSkuDetailBO;
|
||||
import cn.iocoder.mall.product.api.bo.ProductSpuBO;
|
||||
import cn.iocoder.mall.product.api.bo.ProductSpuDetailBO;
|
||||
import cn.iocoder.mall.product.api.bo.ProductSpuPageBO;
|
||||
import cn.iocoder.mall.product.api.dto.ProductSpuAddDTO;
|
||||
import cn.iocoder.mall.product.api.dto.ProductSpuPageDTO;
|
||||
import cn.iocoder.mall.product.api.dto.ProductSpuUpdateDTO;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Sin
|
||||
* @time 2019-03-24 15:24
|
||||
*/
|
||||
public class ProductSpuServiceMock implements ProductSpuService {
|
||||
|
||||
@Override
|
||||
public CommonResult<ProductSpuDetailBO> getProductSpuDetail(Integer id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<ProductSpuDetailBO> addProductSpu(Integer adminId, ProductSpuAddDTO productSpuAddDTO) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> updateProductSpu(Integer adminId, ProductSpuUpdateDTO productSpuUpdateDTO) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> updateProductSpuSort(Integer adminId, Integer spuId, Integer sort) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<ProductSpuPageBO> getProductSpuPage(ProductSpuPageDTO productSpuPageDTO) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<List<ProductSpuBO>> getProductSpuList(Collection<Integer> ids) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<List<ProductSkuDetailBO>> getProductSkuDetailList(Collection<Integer> ids) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.iocoder.mall.order.biz.service;
|
||||
|
||||
import cn.iocoder.common.framework.constant.CommonStatusEnum;
|
||||
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.order.api.CartService;
|
||||
@@ -7,12 +8,17 @@ import cn.iocoder.mall.order.api.bo.CalcOrderPriceBO;
|
||||
import cn.iocoder.mall.order.api.bo.CartBO;
|
||||
import cn.iocoder.mall.order.api.bo.CartItemBO;
|
||||
import cn.iocoder.mall.order.api.bo.OrderCreateBO;
|
||||
import cn.iocoder.mall.order.api.constant.CartItemStatusEnum;
|
||||
import cn.iocoder.mall.order.api.constant.OrderErrorCodeEnum;
|
||||
import cn.iocoder.mall.order.api.dto.CalcOrderPriceDTO;
|
||||
import cn.iocoder.mall.order.biz.convert.CartConvert;
|
||||
import cn.iocoder.mall.order.biz.dao.CartMapper;
|
||||
import cn.iocoder.mall.order.biz.dataobject.CartItemDO;
|
||||
import cn.iocoder.mall.product.api.ProductSpuService;
|
||||
import cn.iocoder.mall.product.api.bo.ProductSkuBO;
|
||||
import cn.iocoder.mall.product.api.bo.ProductSkuDetailBO;
|
||||
import com.alibaba.dubbo.config.annotation.Reference;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
@@ -28,19 +34,97 @@ public class CartServiceImpl implements CartService {
|
||||
@Reference(validation = "true")
|
||||
private ProductSpuService productSpuService;
|
||||
|
||||
@Autowired
|
||||
private CartMapper cartMapper;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("Duplicates")
|
||||
public CommonResult<Boolean> add(Integer userId, Integer skuId, Integer quantity) {
|
||||
return null;
|
||||
// 查询 SKU 是否合法
|
||||
CommonResult<ProductSkuBO> skuResult = productSpuService.getProductSku(skuId);
|
||||
if (skuResult.isError()) {
|
||||
return CommonResult.error(skuResult);
|
||||
}
|
||||
ProductSkuBO sku = skuResult.getData();
|
||||
if (sku == null
|
||||
|| CommonStatusEnum.DISABLE.getValue().equals(sku.getStatus())) { // sku 被禁用
|
||||
return ServiceExceptionUtil.error(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 CommonResult<Boolean> add0(Integer userId, ProductSkuBO sku, Integer quantity) {
|
||||
// 校验库存
|
||||
if (quantity > sku.getQuantity()) {
|
||||
return ServiceExceptionUtil.error(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 CommonResult.success(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("Duplicates")
|
||||
public CommonResult<Boolean> updateQuantity(Integer userId, Integer skuId, Integer quantity) {
|
||||
return null;
|
||||
// 查询 SKU 是否合法
|
||||
CommonResult<ProductSkuBO> skuResult = productSpuService.getProductSku(skuId);
|
||||
if (skuResult.isError()) {
|
||||
return CommonResult.error(skuResult);
|
||||
}
|
||||
ProductSkuBO sku = skuResult.getData();
|
||||
if (sku == null
|
||||
|| CommonStatusEnum.DISABLE.getValue().equals(sku.getStatus())) { // sku 被禁用
|
||||
return ServiceExceptionUtil.error(OrderErrorCodeEnum.CARD_ITEM_SKU_NOT_FOUND.getCode());
|
||||
}
|
||||
// 查询 CartItemDO
|
||||
CartItemDO item = cartMapper.selectByUserIdAndSkuIdAndStatus(userId, skuId, CartItemStatusEnum.ENABLE.getValue());
|
||||
if (item == null) {
|
||||
return ServiceExceptionUtil.error(OrderErrorCodeEnum.CARD_ITEM_NOT_FOUND.getCode());
|
||||
}
|
||||
// TODO 芋艿,后续基于商品是否上下架进一步完善。
|
||||
return updateQuantity0(item, sku, quantity);
|
||||
}
|
||||
|
||||
private CommonResult<Boolean> updateQuantity0(CartItemDO item, ProductSkuBO sku, Integer quantity) {
|
||||
// 校验库存
|
||||
if (item.getQuantity() + quantity > sku.getQuantity()) {
|
||||
return ServiceExceptionUtil.error(OrderErrorCodeEnum.CARD_ITEM_SKU_NOT_FOUND.getCode());
|
||||
}
|
||||
// 更新 CartItemDO
|
||||
cartMapper.updateQuantity(item.getId(), quantity);
|
||||
// 返回成功
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> updateSelected(Integer userId, Integer skuId) {
|
||||
return null;
|
||||
public CommonResult<Boolean> updateSelected(Integer userId, Integer skuId, Boolean selected) {
|
||||
// 查询 CartItemDO
|
||||
CartItemDO item = cartMapper.selectByUserIdAndSkuIdAndStatus(userId, skuId, CartItemStatusEnum.ENABLE.getValue());
|
||||
if (item == null) {
|
||||
return ServiceExceptionUtil.error(OrderErrorCodeEnum.CARD_ITEM_NOT_FOUND.getCode());
|
||||
}
|
||||
// 更新 CartItemDO
|
||||
CartItemDO updateCartItem = new CartItemDO().setId(item.getId()).setSelected(selected);
|
||||
cartMapper.update(updateCartItem);
|
||||
// 返回成功
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -54,8 +138,8 @@ public class CartServiceImpl implements CartService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Integer> count(Integer userId, String nobody, Integer shopId) {
|
||||
return null;
|
||||
public CommonResult<Integer> count(Integer userId) {
|
||||
return CommonResult.success(cartMapper.selectQuantitySumByUserIdAndStatus(userId, CartItemStatusEnum.ENABLE.getValue()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="cn.iocoder.mall.order.biz.dao.CartMapper">
|
||||
|
||||
<sql id="FIELDS">
|
||||
id, status, delete_time, selected, user_id,
|
||||
spu_id, sku_id, quantity, order_id, order_create_time,
|
||||
create_time
|
||||
</sql>
|
||||
|
||||
<select id="selectById" parameterType="Integer" resultType="CartItemDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM cart_item
|
||||
WHERE id = #{id}
|
||||
-- AND deleted = 0
|
||||
</select>
|
||||
|
||||
<select id="selectByIds" resultType="CartItemDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM cart_item
|
||||
WHERE id IN
|
||||
<foreach item="id" collection="ids" separator="," open="(" close=")" index="">
|
||||
#{id}
|
||||
</foreach>
|
||||
-- AND deleted = 0
|
||||
</select>
|
||||
|
||||
<select id="selectByUserIdAndSkuIdAndStatus" resultType="CartItemDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM cart_item
|
||||
WHERE user_id = #{userId}
|
||||
AND sku_id = #{skuId}
|
||||
AND status = #{status}
|
||||
-- AND deleted = 0
|
||||
LIMIT 1
|
||||
</select>
|
||||
|
||||
<select id="selectQuantitySumByUserIdAndStatus" resultType="Integer">
|
||||
SELECT
|
||||
SUM(quantity)
|
||||
FROM cart_item
|
||||
WHERE user_id = #{userId}
|
||||
AND status = #{status}
|
||||
-- AND deleted = 0
|
||||
</select>
|
||||
|
||||
<insert id="insert" parameterType="CartItemDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
|
||||
INSERT INTO cart_item (
|
||||
status, delete_time, selected, user_id,
|
||||
spu_id, sku_id, quantity, order_id, order_create_time,
|
||||
create_time
|
||||
) VALUES (
|
||||
#{status}, #{deleteTime}, #{selected}, #{userId},
|
||||
#{spuId}, #{skuId}, #{quantity}, #{orderId}, #{orderCreateTime},
|
||||
#{createTime}
|
||||
)
|
||||
</insert>
|
||||
|
||||
<update id="update" parameterType="CartItemDO">
|
||||
UPDATE cart_item
|
||||
<set>
|
||||
<if test="status != null">
|
||||
status = #{status},
|
||||
</if>
|
||||
<if test="deleteTime != null">
|
||||
delete_time = #{deleteTime},
|
||||
</if>
|
||||
<if test="selected != null">
|
||||
selected = #{selected},
|
||||
</if>
|
||||
<if test="quantity != null">
|
||||
quantity = #{quantity},
|
||||
</if>
|
||||
<if test="orderId != null">
|
||||
order_id = #{orderId},
|
||||
</if>
|
||||
<if test="orderCreateTime != null">
|
||||
order_create_time = #{orderCreateTime},
|
||||
</if>
|
||||
<if test="price != null">
|
||||
price = #{price},
|
||||
</if>
|
||||
<if test="quantity != null">
|
||||
quantity = #{quantity},
|
||||
</if>
|
||||
</set>
|
||||
WHERE id = #{id}
|
||||
</update>
|
||||
|
||||
<update id="updateQuantity" parameterType="CartItemDO">
|
||||
UPDATE cart_item
|
||||
SET quantity = quantity + #{quantityIncr}
|
||||
WHERE id = #{id}
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user