Fork me on GitHub

web商城———购物车实现

SSM框架web商城之购物车demo:购物车实体:cart,购物项,集合,总金额;cartItem,购物项:要购买的商品信息和购买数量的总体描述,商品,数量,小计。

实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package net.zzqd.mmall.pojo;

import java.util.Date;

public class Cart {
private Integer id;

private Integer userId;

private Integer productId;

private Integer quantity;

private Integer checked;

private Date createTime;

private Date updateTime;

public Cart(Integer id, Integer userId, Integer productId, Integer quantity, Integer checked, Date createTime, Date updateTime) {
this.id = id;
this.userId = userId;
this.productId = productId;
this.quantity = quantity;
this.checked = checked;
this.createTime = createTime;
this.updateTime = updateTime;
}

public Cart() {
super();
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public Integer getUserId() {
return userId;
}

public void setUserId(Integer userId) {
this.userId = userId;
}

public Integer getProductId() {
return productId;
}

public void setProductId(Integer productId) {
this.productId = productId;
}

public Integer getQuantity() {
return quantity;
}

public void setQuantity(Integer quantity) {
this.quantity = quantity;
}

public Integer getChecked() {
return checked;
}

public void setChecked(Integer checked) {
this.checked = checked;
}

public Date getCreateTime() {
return createTime;
}

public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

public Date getUpdateTime() {
return updateTime;
}

public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}

控制层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package net.zzqd.mmall.controller.portal;

import net.zzqd.mmall.common.Const;
import net.zzqd.mmall.common.ResponseCode;
import net.zzqd.mmall.common.ServerResponse;
import net.zzqd.mmall.pojo.User;
import net.zzqd.mmall.service.ICartService;
import net.zzqd.mmall.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
* @author zqnh
* @date 2019/7/28 on 20:25.
*/
@Controller
@RequestMapping("/cart/")
public class CartController
{

@Autowired
private ICartService iCartService;

@RequestMapping("list.do")
@ResponseBody
public ServerResponse<CartVo> list(HttpSession session)
{
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null)
{
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.list(user.getId());
}


@RequestMapping("add.do")
@ResponseBody
public ServerResponse<CartVo> add(HttpSession session, Integer count, Integer productId)
{
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null)
{
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.add(user.getId(),productId,count);
}

@RequestMapping("update.do")
@ResponseBody
public ServerResponse<CartVo> update(HttpSession session, Integer count, Integer productId)
{
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null)
{
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.update(user.getId(),productId,count);
}
@RequestMapping("delete_product.do")
@ResponseBody
public ServerResponse<CartVo> deleteProduct(HttpSession session, String productIds)
{
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null)
{
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.deleteProduct(user.getId(),productIds);
}

@RequestMapping("select_all.do")
@ResponseBody
public ServerResponse<CartVo> selectAll(HttpSession session)
{
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null)
{
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.selectOrUnSelect(user.getId(),null,Const.Cart.CHECKED);
}

@RequestMapping("un_select_all.do")
@ResponseBody
public ServerResponse<CartVo> unSelectAll(HttpSession session)
{
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null)
{
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.selectOrUnSelect(user.getId(),null,Const.Cart.UN_CHECKED);
}

@RequestMapping("select.do")
@ResponseBody
public ServerResponse<CartVo> Select(HttpSession session,Integer productId)
{
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null)
{
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.selectOrUnSelect(user.getId(),productId,Const.Cart.CHECKED);
}

@RequestMapping("un_select.do")
@ResponseBody
public ServerResponse<CartVo> unSelect(HttpSession session,Integer productId)
{
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null)
{
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.selectOrUnSelect(user.getId(),productId,Const.Cart.UN_CHECKED);
}


//全选
//全反选
//单独选
//单独反选
//查询当前用户的购物车里面的产品数量,如果一个产品有10个,那么数量就是10


@RequestMapping("get_cart_product_count.do")
@ResponseBody
public ServerResponse<Integer> getCartProductCount(HttpSession session)
{
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null)
{
return ServerResponse.createBySuccess(0);
}
return iCartService.getCartProductCount(user.getId());
}



}

业务逻辑层(service以及serviceimpl)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package net.zzqd.mmall.service;

import net.zzqd.mmall.common.ServerResponse;
import net.zzqd.mmall.vo.CartVo;

/**
* @author zqnh
* @date 2019/7/28 on 20:32.
*/
public interface ICartService
{
ServerResponse<CartVo> add(Integer userId, Integer productId, Integer count);

ServerResponse<CartVo> update(Integer userId,Integer productId,Integer count);

ServerResponse<CartVo> deleteProduct(Integer userId,String productIds);

public ServerResponse<CartVo> list(Integer userId);

ServerResponse<CartVo> selectOrUnSelect(Integer userId,Integer productId,Integer checked);

ServerResponse<Integer> getCartProductCount(Integer userId);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package net.zzqd.mmall.service.impl;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import net.zzqd.mmall.common.Const;
import net.zzqd.mmall.common.ResponseCode;
import net.zzqd.mmall.common.ServerResponse;
import net.zzqd.mmall.dao.CartMapper;
import net.zzqd.mmall.dao.ProductMapper;
import net.zzqd.mmall.pojo.Cart;
import net.zzqd.mmall.pojo.Product;
import net.zzqd.mmall.service.ICartService;
import net.zzqd.mmall.util.BigDecimalUtil;
import net.zzqd.mmall.util.PropertiesUtil;
import net.zzqd.mmall.vo.CartProductVo;
import net.zzqd.mmall.vo.CartVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
* @author zqnh
* @date 2019/7/28 on 20:32.
*/
@Service("iCartService")
public class CartServiceImpl implements ICartService
{

@Autowired
private CartMapper cartMapper;
@Autowired
private ProductMapper productMapper;

public ServerResponse<CartVo> add(Integer userId, Integer productId, Integer count)
{
if (productId == null || count == null) {
return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
}

Cart cart = cartMapper.selectCartByUserIdProductId(userId, productId);
if (cart == null) {
//这个产品不再这个购物车,需要重新增加一个这个产品的记录
Cart cartItem = new Cart();
cartItem.setQuantity(count);
cartItem.setChecked(Const.Cart.CHECKED);
cartItem.setProductId(productId);
cartItem.setUserId(userId);
cartMapper.insert(cartItem);
}
else {
//代表这个产品再这个购物车里了
//如果产品已存在,数量相加
count = cart.getQuantity() + count;
cart.setQuantity(count);
cartMapper.updateByPrimaryKeySelective(cart);
}
return this.list(userId);
}

public ServerResponse<CartVo> update(Integer userId,Integer productId,Integer count)
{
if (productId == null || count == null) {
return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
}
Cart cart = cartMapper.selectCartByUserIdProductId(userId,productId);
if(cart != null)
{
cart.setQuantity(count);
}
cartMapper.updateByPrimaryKeySelective(cart);
return this.list(userId);
}

public ServerResponse<CartVo> deleteProduct(Integer userId,String productIds)
{
List<String> productList = Splitter.on(",").splitToList(productIds);
if(CollectionUtils.isEmpty(productList))
{
return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
}
cartMapper.deleteByUserIdProductIds(userId,productList);
return this.list(userId);
}

public ServerResponse<CartVo> list(Integer userId)
{
CartVo cartVo = this.getCartVoLimit(userId);
return ServerResponse.createBySuccess(cartVo);
}

public ServerResponse<CartVo> selectOrUnSelect(Integer userId,Integer productId,Integer checked)
{
cartMapper.checkedOrUncheckedProduct(userId,productId,checked);
return this.list(userId);
}

public ServerResponse<Integer> getCartProductCount(Integer userId)
{
if(userId == null)
{
return ServerResponse.createBySuccess(0);
}
return ServerResponse.createBySuccess(cartMapper.selectCartProductCount(userId));
}



private CartVo getCartVoLimit(Integer userId)
{
CartVo cartVo = new CartVo();
List<Cart> cartList = cartMapper.selectCartByUserId(userId);
List<CartProductVo> cartProductVoList = Lists.newArrayList();

BigDecimal cartTotalPrice = new BigDecimal("0");
if(CollectionUtils.isNotEmpty(cartList))
{
for(Cart cartItem:cartList)
{
CartProductVo cartProductVo = new CartProductVo();
cartProductVo.setId(cartItem.getId());
cartProductVo.setUserId(cartItem.getUserId());
cartProductVo.setProductId(cartProductVo.getProductId());

Product product = productMapper.selectByPrimaryKey(cartItem.getProductId());
if(product != null)
{
cartProductVo.setProductMainImage(product.getMainImage());
cartProductVo.setProductName(product.getName());
cartProductVo.setProductSubtitle(product.getSubtitle());
cartProductVo.setProductStatus(product.getStatus());
cartProductVo.setProductPrice(product.getPrice());
cartProductVo.setProductStock(product.getStock());
//判断库存
int buyLimitCount = 0;
if(product.getStock() >=cartItem.getQuantity())
{
//库存充足的时候
buyLimitCount = cartItem.getQuantity();
cartProductVo.setLimiQuantity(Const.Cart.LIMIT_NUM_SUCCESS);
}else
{
buyLimitCount = product.getStock();
cartProductVo.setLimiQuantity(Const.Cart.LIMIT_NUM_FAIL);
//购物车中更新有效库存
Cart cartForQuantity = new Cart();
cartForQuantity.setId(cartItem.getId());
cartForQuantity.setQuantity(buyLimitCount);
cartMapper.updateByPrimaryKeySelective(cartForQuantity);
}
cartProductVo.setQuantiry(buyLimitCount);
//计算总价
cartProductVo.setProductPrice(BigDecimalUtil.mul(product.getPrice().doubleValue(),cartProductVo.getQuantiry()));
cartProductVo.setProductChecked(cartItem.getChecked());
}
if(cartItem.getChecked() == Const.Cart.CHECKED)
{
//如果已经勾选,增加到整个购物车总价中
cartTotalPrice = BigDecimalUtil.add(cartTotalPrice.doubleValue(),cartProductVo.getProductTotalPrice().doubleValue());
}
cartProductVoList.add(cartProductVo);
}
}
cartVo.setCartTotalPrice(cartTotalPrice);
cartVo.setCartProductVoList(cartProductVoList);
cartVo.setAllChecked(this.getAllCheckedStatus(userId));
cartVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix"));

return cartVo;
}

private boolean getAllCheckedStatus(Integer userId)
{
if(userId == null)
{
return false;
}
return cartMapper.selectCartProductCheckedStatusByUserId(userId)==0;
}













}

数据处理层(dao以及mapper)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package net.zzqd.mmall.dao;

import net.zzqd.mmall.pojo.Cart;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface CartMapper {
int deleteByPrimaryKey(Integer id);

int insert(Cart record);

int insertSelective(Cart record);

Cart selectByPrimaryKey(Integer id);

int updateByPrimaryKeySelective(Cart record);

int updateByPrimaryKey(Cart record);

Cart selectCartByUserIdProductId(@Param("userId") Integer userId, @Param("productId")Integer productId);

List<Cart> selectCartByUserId(Integer userId);

int selectCartProductCheckedStatusByUserId(Integer userId);

int deleteByUserIdProductIds(@Param("userId") Integer userId,@Param("productIdList") List<String> productIdList);

int checkedOrUncheckedProduct(@Param("userId") Integer userId,@Param("productId")Integer productId,@Param("checked") Integer checked);

int selectCartProductCount(@Param("userId") Integer userId);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?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="net.zzqd.mmall.dao.CartMapper" >
<resultMap id="BaseResultMap" type="net.zzqd.mmall.pojo.Cart" >
<constructor >
<idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="user_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="product_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="quantity" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="checked" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="create_time" jdbcType="TIMESTAMP" javaType="java.util.Date" />
<arg column="update_time" jdbcType="TIMESTAMP" javaType="java.util.Date" />
</constructor>
</resultMap>
<sql id="Base_Column_List" >
id, user_id, product_id, quantity, checked, create_time, update_time
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from mmall_cart
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from mmall_cart
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="net.zzqd.mmall.pojo.Cart" >
insert into mmall_cart (id, user_id, product_id,
quantity, checked, create_time,
update_time)
values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{productId,jdbcType=INTEGER},
#{quantity,jdbcType=INTEGER}, #{checked,jdbcType=INTEGER},now(),
now())
</insert>
<insert id="insertSelective" parameterType="net.zzqd.mmall.pojo.Cart" >
insert into mmall_cart
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="userId != null" >
user_id,
</if>
<if test="productId != null" >
product_id,
</if>
<if test="quantity != null" >
quantity,
</if>
<if test="checked != null" >
checked,
</if>
<if test="createTime != null" >
create_time,
</if>
<if test="updateTime != null" >
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="userId != null" >
#{userId,jdbcType=INTEGER},
</if>
<if test="productId != null" >
#{productId,jdbcType=INTEGER},
</if>
<if test="quantity != null" >
#{quantity,jdbcType=INTEGER},
</if>
<if test="checked != null" >
#{checked,jdbcType=INTEGER},
</if>
<if test="createTime != null" >
now(),
</if>
<if test="updateTime != null" >
now(),
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="net.zzqd.mmall.pojo.Cart" >
update mmall_cart
<set >
<if test="userId != null" >
user_id = #{userId,jdbcType=INTEGER},
</if>
<if test="productId != null" >
product_id = #{productId,jdbcType=INTEGER},
</if>
<if test="quantity != null" >
quantity = #{quantity,jdbcType=INTEGER},
</if>
<if test="checked != null" >
checked = #{checked,jdbcType=INTEGER},
</if>
<if test="createTime != null" >
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null" >
update_time = now(),
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="net.zzqd.mmall.pojo.Cart" >
update mmall_cart
set user_id = #{userId,jdbcType=INTEGER},
product_id = #{productId,jdbcType=INTEGER},
quantity = #{quantity,jdbcType=INTEGER},
checked = #{checked,jdbcType=INTEGER},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_time = now()
where id = #{id,jdbcType=INTEGER}
</update>

<select id="selectCartByUserIdProductId" resultMap="BaseResultMap" parameterType="map">
select
<include refid="Base_Column_List"></include>
from mmall_cart
where user_id=#{userId}
and product_id=#{productId}
</select>

<select id="selectCartByUserId" resultMap="BaseResultMap" parameterType="int">
select
<include refid="Base_Column_List"/>
from mmall_cart
where user_id=#{userId}
</select>

<select id="selectCartProductCheckedStatusByUserId" resultType="int" parameterType="int">
select count(1) from mmall_cart where checked = 0 and user_id=#{userId}
</select>
<delete id="deleteByUserIdProductIds" parameterType="map" >
delete from mmall_cart
where user_id=#{userId}
<if test="productIdList != null">
and product_id in
<foreach collection="productIdList" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</delete>
<update id="checkedOrUncheckedProduct" parameterType="map">
update mmall_cart
set checked = #{checked}
update _time = now()
where user_id=#{userId}
<if test="procuctId != null">
and product_id=#{productId}
</if>
</update>
<select id="selectCartProductCount" parameterType="int" resultType="int">
select IFNULL( sum(quantity),0) as count from mmall_cart where user_id=#{userId}

</select>

</mapper>

工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package net.zzqd.mmall.vo;

import java.math.BigDecimal;

/**
* @author zqnh
* @date 2019/7/28 on 21:04.
*/
public class CartProductVo
{
//结合了产品和购物车的一个抽象对象
private Integer id;
private Integer userId;
private Integer productId;
private Integer quantiry;//购物车中此商品的数量
private String productName;
private String productSubtitle;
private String productMainImage;
private BigDecimal productPrice;
private Integer productStatus;
private BigDecimal productTotalPrice;
private Integer productStock;
private Integer productChecked;//此商品是否勾选

private String limiQuantity;//限制数量的一个返回结果

public Integer getId()
{
return id;
}

public void setId(Integer id)
{
this.id = id;
}

public Integer getUserId()
{
return userId;
}

public void setUserId(Integer userId)
{
this.userId = userId;
}

public Integer getProductId()
{
return productId;
}

public void setProductId(Integer productId)
{
this.productId = productId;
}

public Integer getQuantiry()
{
return quantiry;
}

public void setQuantiry(Integer quantiry)
{
this.quantiry = quantiry;
}

public String getProductName()
{
return productName;
}

public void setProductName(String productName)
{
this.productName = productName;
}

public String getProductSubtitle()
{
return productSubtitle;
}

public void setProductSubtitle(String productSubtitle)
{
this.productSubtitle = productSubtitle;
}

public String getProductMainImage()
{
return productMainImage;
}

public void setProductMainImage(String productMainImage)
{
this.productMainImage = productMainImage;
}

public BigDecimal getProductPrice()
{
return productPrice;
}

public void setProductPrice(BigDecimal productPrice)
{
this.productPrice = productPrice;
}

public Integer getProductStatus()
{
return productStatus;
}

public void setProductStatus(Integer productStatus)
{
this.productStatus = productStatus;
}

public BigDecimal getProductTotalPrice()
{
return productTotalPrice;
}

public void setProductTotalPrice(BigDecimal productTotalPrice)
{
this.productTotalPrice = productTotalPrice;
}

public Integer getProductStock()
{
return productStock;
}

public void setProductStock(Integer productStock)
{
this.productStock = productStock;
}

public Integer getProductChecked()
{
return productChecked;
}

public void setProductChecked(Integer productChecked)
{
this.productChecked = productChecked;
}

public String getLimiQuantity()
{
return limiQuantity;
}

public void setLimiQuantity(String limiQuantity)
{
this.limiQuantity = limiQuantity;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package net.zzqd.mmall.vo;

import java.math.BigDecimal;
import java.util.List;

/**
* @author zqnh
* @date 2019/7/28 on 21:09.
*/
public class CartVo
{
List<CartProductVo> cartProductVoList;
private BigDecimal cartTotalPrice;
private Boolean allChecked;//是否已经都勾选
private String imageHost;

public List<CartProductVo> getCartProductVoList()
{
return cartProductVoList;
}

public void setCartProductVoList(List<CartProductVo> cartProductVoList)
{
this.cartProductVoList = cartProductVoList;
}

public BigDecimal getCartTotalPrice()
{
return cartTotalPrice;
}

public void setCartTotalPrice(BigDecimal cartTotalPrice)
{
this.cartTotalPrice = cartTotalPrice;
}

public Boolean getAllChecked()
{
return allChecked;
}

public void setAllChecked(Boolean allChecked)
{
this.allChecked = allChecked;
}

public String getImageHost()
{
return imageHost;
}

public void setImageHost(String imageHost)
{
this.imageHost = imageHost;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package net.zzqd.mmall.util;

import java.math.BigDecimal;

/**
* @author zqnh
* @date 2019/7/28 on 21:43.
*/
public class BigDecimalUtil
{
private BigDecimalUtil()
{
//私有构造器,不允许实例化
}
//加法
public static BigDecimal add(double v1,double v2)
{
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.add(b2);
}
//减法
public static BigDecimal sub(double v1,double v2)
{
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2);
}
//乘法
public static BigDecimal mul(double v1,double v2)
{
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.multiply(b2);
}
//除法
public static BigDecimal div(double v1,double v2)
{
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);//四舍五入,保留两位小数

//除不尽的情况
}
}

Const

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package net.zzqd.mmall.common;

import com.google.common.collect.Sets;

import java.util.Set;

/**
* @author zqnh
* @date 2019/7/25 on 13:13.
*/
public class Const
{
public static final String CURRENT_USER="currentUser";

public static final String EMAIL="email";
public static final String USERNAME="username";

public interface ProductListOrderBy
{
Set<String> PRICE_ASC_DESC = Sets.newHashSet("price_desc","price_asc");
}
public interface Cart
{
int CHECKED = 1;//购物车选中状态
int UN_CHECKED = 0;//购物车中未选中

String LIMIT_NUM_FAIL = "LIMIT_NUM_FAIL";
String LIMIT_NUM_SUCCESS = "LIMIT_NUM_SUCCESS";
}

public interface Role
{
int ROLE_CUSTOMER = 0;//普通用户
int ROLE_ADMAIN = 1;//管理员
}

public enum ProductStatusEnum
{
ON_SALE(1,"在线");
private String value;
private int code;
ProductStatusEnum(int code,String value)
{
this.code = code;
this.value=value;
}

public String getValue()
{
return value;
}

public int getCode()
{
return code;
}
}
}

购物车核心类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
private CartVo getCartVoLimit(Integer userId)
{
CartVo cartVo = new CartVo();//实例CartVo
List<Cart> cartList = cartMapper.selectCartByUserId(userId);//根据userId查找购物车
List<CartProductVo> cartProductVoList = Lists.newArrayList();//创建List集合

BigDecimal cartTotalPrice = new BigDecimal("0");//初始购物车总价
if(CollectionUtils.isNotEmpty(cartList))//对cartList判断,如果不是空
{
for(Cart cartItem:cartList)//遍历
{
CartProductVo cartProductVo = new CartProductVo();//创建CartProductVo
cartProductVo.setId(cartItem.getId());//把需要的信息set上
cartProductVo.setUserId(cartItem.getUserId());
cartProductVo.setProductId(cartProductVo.getProductId());

Product product = productMapper.selectByPrimaryKey(cartItem.getProductId());//查询购物车里的产品对象
if(product != null)//如果产品不空
{
cartProductVo.setProductMainImage(product.getMainImage());//继续将信息封装
cartProductVo.setProductName(product.getName());
cartProductVo.setProductSubtitle(product.getSubtitle());
cartProductVo.setProductStatus(product.getStatus());
cartProductVo.setProductPrice(product.getPrice());
cartProductVo.setProductStock(product.getStock());
//判断库存
int buyLimitCount = 0;
if(product.getStock() >=cartItem.getQuantity())
{
//库存充足的时候
buyLimitCount = cartItem.getQuantity();
cartProductVo.setLimiQuantity(Const.Cart.LIMIT_NUM_SUCCESS);
}else
{
buyLimitCount = product.getStock();
cartProductVo.setLimiQuantity(Const.Cart.LIMIT_NUM_FAIL);
//购物车中更新有效库存
Cart cartForQuantity = new Cart();
cartForQuantity.setId(cartItem.getId());
cartForQuantity.setQuantity(buyLimitCount);
cartMapper.updateByPrimaryKeySelective(cartForQuantity);
}
cartProductVo.setQuantiry(buyLimitCount);
//计算总价,当前购物车里产品价格*数量
cartProductVo.setProductPrice(BigDecimalUtil.mul(product.getPrice().doubleValue(),cartProductVo.getQuantiry()));
cartProductVo.setProductChecked(cartItem.getChecked());//进行勾选
}
if(cartItem.getChecked() == Const.Cart.CHECKED)
{
//如果已经勾选,增加到整个购物车总价中
cartTotalPrice = BigDecimalUtil.add(cartTotalPrice.doubleValue(),cartProductVo.getProductTotalPrice().doubleValue());
}
cartProductVoList.add(cartProductVo);//add到上面声明的cartProductVoList
}
}
cartVo.setCartTotalPrice(cartTotalPrice);//封装CartVo
cartVo.setCartProductVoList(cartProductVoList);//填充购物车产品
cartVo.setAllChecked(this.getAllCheckedStatus(userId));//是不是全选
cartVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix"));

return cartVo;
}