Commit c7f275e68574fc879943a1625cf028f5c19019c0
1 parent
b68f966e
购物车缓存清理
Showing
6 changed files
with
70 additions
and
23 deletions
myapp-common/src/main/java/com/b2c/myapp/common/api/shoppingCart/input/ShoppingCartListInput.java
... | ... | @@ -13,7 +13,7 @@ import java.util.Date; |
13 | 13 | * @createTime 2016-11-23 17:19:18 |
14 | 14 | * @author template |
15 | 15 | */ |
16 | -public class ShoppingCartListInput extends BaseQuery implements Serializable{ | |
16 | +public class ShoppingCartListInput implements Serializable{ | |
17 | 17 | public ShoppingCartListInput() { |
18 | 18 | } |
19 | 19 | /** |
... | ... |
myapp-web/src/main/java/com/b2c/myapp/api/ShoppingCartApi.java
... | ... | @@ -113,7 +113,16 @@ public class ShoppingCartApi extends BaseRestFulApi { |
113 | 113 | public BaseOutput<CartInfo> getCarts(@RequestBody ShoppingCartListInput shoppingCartListInput){ |
114 | 114 | BaseOutput<CartInfo> output = BaseOutput.success(); |
115 | 115 | try { |
116 | - output.setData(shoppingCartService.getCarts(shoppingCartListInput)); | |
116 | + List<ShoppingCart> shoppingCarts = null; | |
117 | + if(null == shoppingCartListInput.getSkus()||shoppingCartListInput.getSkus().length==0){ | |
118 | + //购物车 | |
119 | + shoppingCarts = shoppingCartService.getCartsByShopBuyerIdCache(shoppingCartListInput.getShopBuyerId()); | |
120 | + }else{ | |
121 | + //订单确认页 | |
122 | + shoppingCarts = shoppingCartService.listBySkus(shoppingCartListInput.getShopBuyerId(),shoppingCartListInput.getSkus()); | |
123 | + } | |
124 | + | |
125 | + output.setData(shoppingCartService.getCarts(shoppingCartListInput.getShopBuyerId(),shoppingCarts)); | |
117 | 126 | } catch (Exception e) { |
118 | 127 | handleException(output, e); |
119 | 128 | } |
... | ... |
myapp-web/src/main/java/com/b2c/myapp/dao/ShoppingCartDao.java
... | ... | @@ -5,6 +5,8 @@ import com.b2c.myapp.common.utils.base.BaseDao; |
5 | 5 | import com.b2c.myapp.domain.ShoppingCart; |
6 | 6 | import org.apache.ibatis.annotations.Param; |
7 | 7 | |
8 | +import java.util.List; | |
9 | + | |
8 | 10 | /** |
9 | 11 | * <br /> |
10 | 12 | * @createTime 2016-11-23 17:19:18 |
... | ... | @@ -14,4 +16,12 @@ public interface ShoppingCartDao extends BaseDao<ShoppingCart,Long> { |
14 | 16 | int clearCarts(Long shopBuyerId); |
15 | 17 | int delCarts(@Param("shopBuyerId") Long shopBuyerId,@Param("skus") String... skus); |
16 | 18 | int updateCart(ShoppingCart shoppingCart); |
19 | + | |
20 | + /** | |
21 | + * 批量查询SKU购物项 | |
22 | + * @param shopBuyerId | |
23 | + * @param skus | |
24 | + * @return | |
25 | + */ | |
26 | + List<ShoppingCart> listBySkus(@Param("shopBuyerId") Long shopBuyerId,@Param("skus") String[] skus); | |
17 | 27 | } |
... | ... |
myapp-web/src/main/java/com/b2c/myapp/service/ShoppingCartService.java
... | ... | @@ -4,6 +4,7 @@ import com.b2c.myapp.common.api.shoppingCart.input.ShoppingCartListInput; |
4 | 4 | import com.b2c.myapp.common.api.shoppingCart.output.CartInfo; |
5 | 5 | import com.b2c.myapp.common.api.shoppingCart.output.ShoppingCartOutput; |
6 | 6 | import com.b2c.myapp.domain.ShoppingCart; |
7 | +import org.apache.ibatis.annotations.Param; | |
7 | 8 | |
8 | 9 | import java.util.List; |
9 | 10 | |
... | ... | @@ -43,9 +44,25 @@ public interface ShoppingCartService extends BaseService<ShoppingCart, Long> { |
43 | 44 | |
44 | 45 | /** |
45 | 46 | * 查询购物车列表 |
46 | - * @param shoppingCartListInput | |
47 | + * @param shopBuyerId | |
48 | + * @param shoppingCarts | |
49 | + * @return | |
50 | + */ | |
51 | + CartInfo getCarts(Long shopBuyerId,List<ShoppingCart> shoppingCarts); | |
52 | + | |
53 | + /** | |
54 | + * 实时获取购物项详情 | |
55 | + * @param shopBuyerId | |
56 | + * @return | |
57 | + */ | |
58 | + public List<ShoppingCart> getCartsByShopBuyerIdCache(Long shopBuyerId); | |
59 | + | |
60 | + /** | |
61 | + * 批量查询SKU购物项 | |
62 | + * @param shopBuyerId | |
63 | + * @param skus | |
47 | 64 | * @return |
48 | 65 | */ |
49 | - CartInfo getCarts(ShoppingCartListInput shoppingCartListInput); | |
66 | + List<ShoppingCart> listBySkus(Long shopBuyerId,String[] skus); | |
50 | 67 | |
51 | 68 | } |
... | ... |
myapp-web/src/main/java/com/b2c/myapp/service/impl/ShoppingCartServiceImpl.java
1 | 1 | package com.b2c.myapp.service.impl; |
2 | 2 | |
3 | -import com.b2c.myapp.common.api.shop.input.ShopListInput; | |
4 | 3 | import com.b2c.myapp.common.api.shopBuyer.output.ShopBuyerKeyOutput; |
5 | 4 | import com.b2c.myapp.common.api.shoppingCart.input.ShoppingCartListInput; |
6 | 5 | import com.b2c.myapp.common.api.shoppingCart.output.CartInfo; |
7 | 6 | import com.b2c.myapp.common.api.shoppingCart.output.ShoppingCartOutput; |
8 | 7 | import com.b2c.myapp.common.utils.base.BaseDao; |
9 | -import com.b2c.myapp.dao.ShopDao; | |
8 | +import com.b2c.myapp.dao.ShoppingCartDao; | |
10 | 9 | import com.b2c.myapp.domain.Shop; |
11 | -import com.b2c.myapp.domain.ShopBuyer; | |
12 | 10 | import com.b2c.myapp.domain.ShoppingCart; |
13 | 11 | import com.b2c.myapp.rpc.TitanClientRPCService; |
14 | 12 | import com.b2c.myapp.service.ShopBuyerService; |
15 | 13 | import com.b2c.myapp.service.ShopService; |
16 | 14 | import com.b2c.myapp.service.ShoppingCartService; |
17 | -import com.b2c.myapp.dao.ShoppingCartDao; | |
18 | - | |
19 | 15 | import com.b2c.myapp.utils.BeanConver; |
20 | 16 | import com.diligrp.titan.sdk.constant.UnitEnum; |
21 | 17 | import com.diligrp.titan.sdk.domain.Product; |
22 | 18 | import com.diligrp.titan.sdk.domain.Sku; |
23 | -import com.diligrp.website.util.format.MoneyUtils; | |
24 | 19 | import org.apache.commons.collections.CollectionUtils; |
20 | +import org.apache.ibatis.annotations.Param; | |
25 | 21 | import org.slf4j.Logger; |
26 | 22 | import org.slf4j.LoggerFactory; |
27 | 23 | import org.springframework.beans.factory.annotation.Autowired; |
... | ... | @@ -32,7 +28,6 @@ import org.springframework.stereotype.Service; |
32 | 28 | |
33 | 29 | import java.math.BigDecimal; |
34 | 30 | import java.util.ArrayList; |
35 | -import java.util.Collections; | |
36 | 31 | import java.util.List; |
37 | 32 | import java.util.Map; |
38 | 33 | |
... | ... | @@ -124,21 +119,13 @@ public class ShoppingCartServiceImpl extends BaseServiceImpl<ShoppingCart, Long> |
124 | 119 | /** |
125 | 120 | * 查询购物车列表 |
126 | 121 | * |
127 | - * @param shoppingCartListInput | |
122 | + * @param shopBuyerId | |
123 | + * @param shoppingCarts | |
128 | 124 | * @return |
129 | 125 | */ |
130 | 126 | @Override |
131 | - public CartInfo getCarts(ShoppingCartListInput shoppingCartListInput) { | |
132 | - List<ShoppingCart> shoppingCarts = null; | |
133 | - if(null == shoppingCartListInput.getSkus()||shoppingCartListInput.getSkus().length==0){ | |
134 | - //购物车 | |
135 | - shoppingCarts = getCartsByShopBuyerIdCache(shoppingCartListInput.getShopBuyerId()); | |
136 | - }else{ | |
137 | - //订单确认页 | |
138 | - shoppingCarts = list(shoppingCartListInput); | |
139 | - } | |
127 | + public CartInfo getCarts(Long shopBuyerId,List<ShoppingCart> shoppingCarts) { | |
140 | 128 | List<ShoppingCartOutput> shoppingCartOutputs = BeanConver.copeList(shoppingCarts,ShoppingCartOutput.class); |
141 | - | |
142 | 129 | List<String> skus = new ArrayList<>(); |
143 | 130 | for (ShoppingCartOutput shoppingCartOutput:shoppingCartOutputs |
144 | 131 | ) { |
... | ... | @@ -162,7 +149,7 @@ public class ShoppingCartServiceImpl extends BaseServiceImpl<ShoppingCart, Long> |
162 | 149 | CartInfo cartInfo = new CartInfo(); |
163 | 150 | cartInfo.setShoppingCartOutputs(shoppingCartOutputs); |
164 | 151 | |
165 | - ShopBuyerKeyOutput shopBuyerKeyOutput = shopBuyerService.getShopBuyerKeys(shoppingCartListInput.getShopBuyerId()); | |
152 | + ShopBuyerKeyOutput shopBuyerKeyOutput = shopBuyerService.getShopBuyerKeys(shopBuyerId); | |
166 | 153 | Shop shop = shopService.get(shopBuyerKeyOutput.getShopId()); |
167 | 154 | if(shop != null){ |
168 | 155 | cartInfo.setPostage(shop.getPostage()); |
... | ... | @@ -178,13 +165,26 @@ public class ShoppingCartServiceImpl extends BaseServiceImpl<ShoppingCart, Long> |
178 | 165 | * @param shopBuyerId |
179 | 166 | * @return |
180 | 167 | */ |
168 | + @Override | |
181 | 169 | @Cacheable(value = "thirtyMinutes", key = "#root.getTarget().redisKey()+':carts:shopBuyerId:' + #shopBuyerId") |
182 | 170 | public List<ShoppingCart> getCartsByShopBuyerIdCache(Long shopBuyerId) { |
183 | 171 | ShoppingCart shoppingCart = new ShoppingCart(); |
184 | 172 | shoppingCart.setShopBuyerId(shopBuyerId); |
173 | + shoppingCart.setYn(1); | |
185 | 174 | return shoppingCartDao.list(shoppingCart); |
186 | 175 | } |
187 | 176 | |
177 | + /** | |
178 | + * 批量查询SKU购物项 | |
179 | + * | |
180 | + * @param shopBuyerId | |
181 | + * @param skus | |
182 | + * @return | |
183 | + */ | |
184 | + @Override | |
185 | + public List<ShoppingCart> listBySkus(Long shopBuyerId,String[] skus) { | |
186 | + return shoppingCartDao.listBySkus(shopBuyerId,skus); | |
187 | + } | |
188 | 188 | |
189 | 189 | |
190 | 190 | } |
... | ... |
myapp-web/src/main/resources/mybatis/ShoppingCartMapper.xml
... | ... | @@ -231,6 +231,17 @@ |
231 | 231 | </select> |
232 | 232 | |
233 | 233 | <!-- 查询,通过条件 --> |
234 | + <select id="listBySkus" resultMap="ShoppingCartMap"> | |
235 | + <![CDATA[SELECT]]> | |
236 | + <include refid="QUERY_COLUMN_LIST"/> | |
237 | + <include refid="QUERY_FROM_TABLE"/> | |
238 | + WHERE shop_buyer_id = #{shopBuyerId} AND sku IN | |
239 | + <foreach collection="skus" item="sku" open="(" separator="," close=")"> | |
240 | + #{sku} | |
241 | + </foreach> | |
242 | + AND yn = 1 | |
243 | + </select> | |
244 | + <!-- 查询,通过条件 --> | |
234 | 245 | <select id="list" parameterType="com.b2c.myapp.domain.ShoppingCart" resultMap="ShoppingCartMap"> |
235 | 246 | <![CDATA[SELECT]]> |
236 | 247 | <include refid="QUERY_COLUMN_LIST"/> |
... | ... |