|
@@ -1,14 +1,14 @@
|
|
|
package com.caimei.www.service.impl;
|
|
|
|
|
|
-import com.caimei.www.mapper.ShoppingCartDao;
|
|
|
+import com.caimei.www.mapper.ShoppingDao;
|
|
|
import com.caimei.www.mapper.UserDao;
|
|
|
import com.caimei.www.pojo.JsonModel;
|
|
|
import com.caimei.www.pojo.order.ActivityPrice;
|
|
|
import com.caimei.www.pojo.order.CartItem;
|
|
|
import com.caimei.www.pojo.order.CartSupplier;
|
|
|
import com.caimei.www.pojo.order.LadderPrice;
|
|
|
-import com.caimei.www.pojo.user.User;
|
|
|
-import com.caimei.www.service.ShoppingCartService;
|
|
|
+import com.caimei.www.pojo.user.Account;
|
|
|
+import com.caimei.www.service.ShoppingService;
|
|
|
import com.caimei.www.utils.ImageUtil;
|
|
|
import com.caimei.www.utils.PriceUtil;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -16,7 +16,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
import java.util.stream.IntStream;
|
|
@@ -28,11 +28,11 @@ import java.util.stream.IntStream;
|
|
|
* @date : 2020/7/23
|
|
|
*/
|
|
|
@Service
|
|
|
-public class ShoppingCartServiceImpl implements ShoppingCartService {
|
|
|
+public class ShoppingServiceImpl implements ShoppingService {
|
|
|
@Value("${caimei.wwwDomain}")
|
|
|
private String domain;
|
|
|
@Resource
|
|
|
- private ShoppingCartDao shoppingCartDao;
|
|
|
+ private ShoppingDao shoppingDao;
|
|
|
@Resource
|
|
|
private UserDao userDao;
|
|
|
|
|
@@ -45,7 +45,7 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
|
|
|
@Override
|
|
|
public JsonModel<List<CartItem>> getHeaderCart(Integer userId) {
|
|
|
if (userId == null || userId == 0) { return JsonModel.error("参数异常", null);}
|
|
|
- List<CartItem> headCarts = shoppingCartDao.getHeadCarts(userId);
|
|
|
+ List<CartItem> headCarts = shoppingDao.getHeadCarts(userId);
|
|
|
return JsonModel.success(headCarts);
|
|
|
}
|
|
|
|
|
@@ -56,21 +56,24 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- public JsonModel<List<CartSupplier>> getShoppingCarts(Integer userId) {
|
|
|
+ public JsonModel<Map<String, Object>> getShoppingCarts(Integer userId) {
|
|
|
if (userId == null || userId == 0) { return JsonModel.error("参数异常", null);}
|
|
|
- List<CartSupplier> cartSuppliers = shoppingCartDao.getCartSuppliers(userId);
|
|
|
- User user = userDao.getUserById(userId);
|
|
|
+ List<CartSupplier> cartSuppliers = shoppingDao.getCartSuppliers(userId);
|
|
|
+ List<CartItem> invalidCarts = new ArrayList<>();
|
|
|
+ Account account = userDao.getUserById(userId);
|
|
|
cartSuppliers.forEach(supplier -> {
|
|
|
- List<CartItem> cartItems = shoppingCartDao.getShoppingCartBySupplierId(supplier.getId(), userId);
|
|
|
+ List<CartItem> cartItems = shoppingDao.getShoppingCartBySupplierId(supplier.getId(), userId);
|
|
|
AtomicReference<Double> supplierPrice = new AtomicReference<>(0d);
|
|
|
AtomicInteger itemCount = new AtomicInteger();
|
|
|
- cartItems.forEach(cartItem -> {
|
|
|
+ Iterator<CartItem> iterator = cartItems.iterator();
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ CartItem cartItem = iterator.next();
|
|
|
cartItem.setImage(ImageUtil.getImageURL("product", cartItem.getImage(), 0, domain));
|
|
|
if (cartItem.getValidFlag() == 2) {
|
|
|
// 已上架
|
|
|
cartItem.setStatus(0);
|
|
|
// 价格是否可见
|
|
|
- boolean priceVisible = (cartItem.getPriceFlag() == 0 || (cartItem.getPriceFlag() == 2 && user.getIdentity() ==2));
|
|
|
+ boolean priceVisible = (cartItem.getPriceFlag() == 0 || (cartItem.getPriceFlag() == 2 && account.getIdentity() ==2));
|
|
|
if (priceVisible) {
|
|
|
// 设置商品价格
|
|
|
setCartItemPrice(cartItem, userId);
|
|
@@ -95,6 +98,7 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
|
|
|
} else if (cartItem.getValidFlag() == 9) {
|
|
|
// 已冻结
|
|
|
cartItem.setStatus(2);
|
|
|
+
|
|
|
} else if (cartItem.getValidFlag() == 3) {
|
|
|
// 已下架
|
|
|
cartItem.setStatus(3);
|
|
@@ -105,13 +109,19 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
|
|
|
// 库存不足
|
|
|
cartItem.setStatus(7);
|
|
|
}
|
|
|
+ invalidCarts.add(cartItem);
|
|
|
+ iterator.remove();
|
|
|
}
|
|
|
- });
|
|
|
+ }
|
|
|
supplier.setCartList(cartItems);
|
|
|
supplier.setTotalPrice(supplierPrice.get());
|
|
|
supplier.setCount(itemCount.get());
|
|
|
});
|
|
|
- return JsonModel.success(cartSuppliers);
|
|
|
+ cartSuppliers.removeIf(supplier -> (null != supplier && supplier.getCount() == 0));
|
|
|
+ Map<String, Object> resultMap = new HashMap<>();
|
|
|
+ resultMap.put("list", cartSuppliers);
|
|
|
+ resultMap.put("invalid", invalidCarts);
|
|
|
+ return JsonModel.success(resultMap);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -121,22 +131,23 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
|
|
|
*/
|
|
|
private void setCartItemPrice(CartItem cartItem, Integer userId) {
|
|
|
// 活动价
|
|
|
- ActivityPrice activity = shoppingCartDao.getActivityPriceByProductId(cartItem.getProductId());
|
|
|
+ ActivityPrice activity = shoppingDao.getActivityPriceByProductId(cartItem.getProductId());
|
|
|
if (null != activity) {
|
|
|
cartItem.setPrice(activity.getPrice());
|
|
|
cartItem.setActivityFlag(1);
|
|
|
+ cartItem.setLadderFlag(0);
|
|
|
} else if (cartItem.getLadderFlag() == 1) {
|
|
|
// 阶梯价
|
|
|
- List<LadderPrice> ladderPrices = shoppingCartDao.getladderPricesByProductId(cartItem.getProductId());
|
|
|
+ List<LadderPrice> ladderPrices = shoppingDao.getladderPricesByProductId(cartItem.getProductId());
|
|
|
if (!CollectionUtils.isEmpty(ladderPrices)){
|
|
|
IntStream.range(0, ladderPrices.size()).forEach(i->{
|
|
|
boolean isThisLadder;
|
|
|
- if (null != ladderPrices.get(i+1)) {
|
|
|
- ladderPrices.get(i).setMaxNum(ladderPrices.get(i+1).getBuyNum());
|
|
|
- isThisLadder = (cartItem.getNumber()>ladderPrices.get(i).getBuyNum() && cartItem.getNumber()<ladderPrices.get(i).getMaxNum());
|
|
|
- } else {
|
|
|
+ if (i == ladderPrices.size()-1) {
|
|
|
ladderPrices.get(i).setMaxNum(0);
|
|
|
isThisLadder = (cartItem.getNumber()>ladderPrices.get(i).getBuyNum());
|
|
|
+ } else {
|
|
|
+ ladderPrices.get(i).setMaxNum(ladderPrices.get(i+1).getBuyNum());
|
|
|
+ isThisLadder = (cartItem.getNumber()>ladderPrices.get(i).getBuyNum() && cartItem.getNumber()<ladderPrices.get(i).getMaxNum());
|
|
|
}
|
|
|
if (isThisLadder){
|
|
|
cartItem.setPrice(ladderPrices.get(i).getBuyPrice());
|
|
@@ -149,7 +160,7 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
|
|
|
}
|
|
|
} else {
|
|
|
// 复购价
|
|
|
- Double repurchase = shoppingCartDao.getRepurchasePrice(cartItem.getProductId(), userId);
|
|
|
+ Double repurchase = shoppingDao.getRepurchasePrice(cartItem.getProductId(), userId);
|
|
|
if (null != repurchase && repurchase>0) {
|
|
|
cartItem.setPrice(repurchase);
|
|
|
}
|