|
@@ -1,17 +1,25 @@
|
|
|
package com.caimei.www.service.impl;
|
|
|
|
|
|
import com.caimei.www.mapper.ShoppingCartDao;
|
|
|
+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.utils.ImageUtil;
|
|
|
import com.caimei.www.utils.PriceUtil;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
+import java.util.stream.IntStream;
|
|
|
|
|
|
/**
|
|
|
* Description
|
|
@@ -25,6 +33,8 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
|
|
|
private String domain;
|
|
|
@Resource
|
|
|
private ShoppingCartDao shoppingCartDao;
|
|
|
+ @Resource
|
|
|
+ private UserDao userDao;
|
|
|
|
|
|
/**
|
|
|
* 顶部购物车数据
|
|
@@ -49,10 +59,100 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
|
|
|
public JsonModel<List<CartSupplier>> getShoppingCarts(Integer userId) {
|
|
|
if (userId == null || userId == 0) { return JsonModel.error("参数异常", null);}
|
|
|
List<CartSupplier> cartSuppliers = shoppingCartDao.getCartSuppliers(userId);
|
|
|
+ User user = userDao.getUserById(userId);
|
|
|
cartSuppliers.forEach(supplier -> {
|
|
|
List<CartItem> cartItems = shoppingCartDao.getShoppingCartBySupplierId(supplier.getId(), userId);
|
|
|
- supplier.setCartList(null);
|
|
|
+ AtomicReference<Double> supplierPrice = new AtomicReference<>(0d);
|
|
|
+ AtomicInteger itemCount = new AtomicInteger();
|
|
|
+ cartItems.forEach(cartItem -> {
|
|
|
+ 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));
|
|
|
+ if (priceVisible) {
|
|
|
+ // 设置商品价格
|
|
|
+ setCartItemPrice(cartItem, userId);
|
|
|
+ // 该供应商下价格累加
|
|
|
+ supplierPrice.updateAndGet(v -> v + PriceUtil.add(supplierPrice, cartItem.getPrice()).doubleValue());
|
|
|
+ itemCount.incrementAndGet();
|
|
|
+ } else {
|
|
|
+ // 失效商品
|
|
|
+ if (cartItem.getPriceFlag() == 1){
|
|
|
+ // 未公开价格
|
|
|
+ cartItem.setStatus(6);
|
|
|
+ } else if (cartItem.getPriceFlag() == 2) {
|
|
|
+ // 价格仅会员可见
|
|
|
+ cartItem.setStatus(5);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 失效商品
|
|
|
+ if (cartItem.getValidFlag() == 0) {
|
|
|
+ // 后台逻辑删除
|
|
|
+ cartItem.setStatus(1);
|
|
|
+ } else if (cartItem.getValidFlag() == 9) {
|
|
|
+ // 已冻结
|
|
|
+ cartItem.setStatus(2);
|
|
|
+ } else if (cartItem.getValidFlag() == 3) {
|
|
|
+ // 已下架
|
|
|
+ cartItem.setStatus(3);
|
|
|
+ } else if (cartItem.getStock() == null || cartItem.getStock() == 0) {
|
|
|
+ // 售罄
|
|
|
+ cartItem.setStatus(4);
|
|
|
+ } else if (cartItem.getStock() != null && (cartItem.getStock() < cartItem.getMin() || cartItem.getStock() < cartItem.getNumber())) {
|
|
|
+ // 库存不足
|
|
|
+ cartItem.setStatus(7);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ supplier.setCartList(cartItems);
|
|
|
+ supplier.setTotalPrice(supplierPrice.get());
|
|
|
+ supplier.setCount(itemCount.get());
|
|
|
});
|
|
|
return JsonModel.success(cartSuppliers);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置购物车商品价格
|
|
|
+ * @param cartItem
|
|
|
+ * @param userId
|
|
|
+ */
|
|
|
+ private void setCartItemPrice(CartItem cartItem, Integer userId) {
|
|
|
+ // 活动价
|
|
|
+ ActivityPrice activity = shoppingCartDao.getActivityPriceByProductId(cartItem.getProductId());
|
|
|
+ if (null != activity) {
|
|
|
+ cartItem.setPrice(activity.getPrice());
|
|
|
+ cartItem.setActivityFlag(1);
|
|
|
+ } else if (cartItem.getLadderFlag() == 1) {
|
|
|
+ // 阶梯价
|
|
|
+ List<LadderPrice> ladderPrices = shoppingCartDao.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 {
|
|
|
+ ladderPrices.get(i).setMaxNum(0);
|
|
|
+ isThisLadder = (cartItem.getNumber()>ladderPrices.get(i).getBuyNum());
|
|
|
+ }
|
|
|
+ if (isThisLadder){
|
|
|
+ cartItem.setPrice(ladderPrices.get(i).getBuyPrice());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ cartItem.setMin(ladderPrices.get(0).getBuyNum());
|
|
|
+ cartItem.setLadderPrices(ladderPrices);
|
|
|
+ } else {
|
|
|
+ cartItem.setLadderFlag(0);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 复购价
|
|
|
+ Double repurchase = shoppingCartDao.getRepurchasePrice(cartItem.getProductId(), userId);
|
|
|
+ if (null != repurchase && repurchase>0) {
|
|
|
+ cartItem.setPrice(repurchase);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|