|
@@ -20,9 +20,12 @@ import com.izouma.dingdong.service.merchant.GoodsService;
|
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
+import javax.persistence.Column;
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
import java.util.Comparator;
|
|
import java.util.Comparator;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
|
|
@Service
|
|
@Service
|
|
|
@AllArgsConstructor
|
|
@AllArgsConstructor
|
|
@@ -35,7 +38,6 @@ public class ShoppingCartService {
|
|
|
private MerchantSettingsRepo merchantSettingsRepo;
|
|
private MerchantSettingsRepo merchantSettingsRepo;
|
|
|
private FullReductionRepo fullReductionRepo;
|
|
private FullReductionRepo fullReductionRepo;
|
|
|
private OrderGoodsSpecRepo orderGoodsSpecRepo;
|
|
private OrderGoodsSpecRepo orderGoodsSpecRepo;
|
|
|
- private GoodsService goodsService;
|
|
|
|
|
|
|
|
|
|
/*
|
|
/*
|
|
|
创建购物车
|
|
创建购物车
|
|
@@ -66,7 +68,6 @@ public class ShoppingCartService {
|
|
|
BigDecimal sub = orderGoodsSpec.getGoodsPrice().subtract(orderGoodsSpec.getGoodsRealPrice()).multiply(BigDecimal.valueOf(orderGoodsSpec.getNum()));
|
|
BigDecimal sub = orderGoodsSpec.getGoodsPrice().subtract(orderGoodsSpec.getGoodsRealPrice()).multiply(BigDecimal.valueOf(orderGoodsSpec.getNum()));
|
|
|
|
|
|
|
|
//包装费
|
|
//包装费
|
|
|
-
|
|
|
|
|
BigDecimal packingPrice = BigDecimal.ZERO;
|
|
BigDecimal packingPrice = BigDecimal.ZERO;
|
|
|
|
|
|
|
|
if (ObjectUtil.isNotNull(goods.getPackingPrice())) {
|
|
if (ObjectUtil.isNotNull(goods.getPackingPrice())) {
|
|
@@ -85,6 +86,7 @@ public class ShoppingCartService {
|
|
|
.firstBuy(merchant.getFirstOrder())
|
|
.firstBuy(merchant.getFirstOrder())
|
|
|
.enabled(true)
|
|
.enabled(true)
|
|
|
.build();
|
|
.build();
|
|
|
|
|
+ shoppingCart.setId(-1L);
|
|
|
} else {
|
|
} else {
|
|
|
//商品总价(原价)
|
|
//商品总价(原价)
|
|
|
shoppingCart.setGoodsTotal(shoppingCart.getGoodsTotal().add(orderGoodsSpec.getGoodsPrice().multiply(BigDecimal.valueOf(orderGoodsSpec.getNum()))));
|
|
shoppingCart.setGoodsTotal(shoppingCart.getGoodsTotal().add(orderGoodsSpec.getGoodsPrice().multiply(BigDecimal.valueOf(orderGoodsSpec.getNum()))));
|
|
@@ -153,14 +155,190 @@ public class ShoppingCartService {
|
|
|
shoppingCart.setRealAmount(real);
|
|
shoppingCart.setRealAmount(real);
|
|
|
shoppingCart.setReducedAmount(sub2);
|
|
shoppingCart.setReducedAmount(sub2);
|
|
|
|
|
|
|
|
- shoppingCart = shoppingCartRepo.save(shoppingCart);
|
|
|
|
|
|
|
+ ShoppingCart cart = shoppingCartRepo.save(shoppingCart);
|
|
|
|
|
+
|
|
|
|
|
+ this.changeOrderSpec(cart, orderGoodsSpec);
|
|
|
|
|
+
|
|
|
|
|
+ return cart;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ //改订单规格
|
|
|
|
|
+ public void changeOrderSpec(ShoppingCart cart, OrderGoodsSpec orderGoodsSpec) {
|
|
|
|
|
+
|
|
|
|
|
+ orderGoodsSpec.setShoppingCartId(cart.getId());
|
|
|
|
|
+ List<OrderGoodsSpec> orderGoodsSpecs = cart.getOrderGoodsSpecs();
|
|
|
|
|
+
|
|
|
|
|
+ boolean f = true;
|
|
|
|
|
+ for (OrderGoodsSpec o : orderGoodsSpecs) {//商品id相同
|
|
|
|
|
+ if (!o.getId().equals(orderGoodsSpec.getId()))
|
|
|
|
|
+ if (orderGoodsSpec.getGoodsId().equals(o.getGoodsId())) {
|
|
|
|
|
+ //且规格id都相同或都为空
|
|
|
|
|
+ List<Long> specificationId = orderGoodsSpec.getSpecificationId();
|
|
|
|
|
+ List<Long> specificationId1 = o.getSpecificationId();
|
|
|
|
|
+ if ((CollUtil.isEmpty(specificationId) && CollUtil.isEmpty(specificationId1)) || specificationId.equals(specificationId1)) {
|
|
|
|
|
+ //原有数量加上现在买的数量
|
|
|
|
|
+ o.setNum(o.getNum() + orderGoodsSpec.getNum());
|
|
|
|
|
+ // o.setGoodsPrice(o.getGoodsPrice().add(orderGoodsSpec.getGoodsPrice()));
|
|
|
|
|
+ // o.setGoodsRealPrice(o.getGoodsRealPrice().add(orderGoodsSpec.getGoodsPrice()));
|
|
|
|
|
+
|
|
|
|
|
+ orderGoodsSpecRepo.save(o);
|
|
|
|
|
+ orderGoodsSpecRepo.deleteById(orderGoodsSpec.getId());
|
|
|
|
|
+ f = false;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (f) {
|
|
|
|
|
+ orderGoodsSpecRepo.save(orderGoodsSpec);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //计算价钱
|
|
|
|
|
+ public ShoppingCart calculatePrice(Long cartId, Long userId) {
|
|
|
|
|
+
|
|
|
|
|
+ //购物车
|
|
|
|
|
+ ShoppingCart cart = shoppingCartRepo.findById(cartId).orElseThrow(new BusinessException("无购物车"));
|
|
|
|
|
+ List<OrderGoodsSpec> specs = cart.getOrderGoodsSpecs();
|
|
|
|
|
+
|
|
|
|
|
+ //商品总价 商品实际
|
|
|
|
|
+ BigDecimal goodsTotal = BigDecimal.ZERO;
|
|
|
|
|
+ BigDecimal goodsReal = BigDecimal.ZERO;
|
|
|
|
|
+
|
|
|
|
|
+ //满减
|
|
|
|
|
+ boolean flag = true;
|
|
|
|
|
+ //包装费
|
|
|
|
|
+ BigDecimal packingPrice = BigDecimal.ZERO;
|
|
|
|
|
+
|
|
|
|
|
+ for (OrderGoodsSpec s : specs) {
|
|
|
|
|
+ //商品总价 商品实际
|
|
|
|
|
+ goodsTotal = goodsTotal.add(s.getGoodsPrice().multiply(new BigDecimal(s.getNum())));
|
|
|
|
|
+ goodsReal = goodsReal.add(s.getGoodsRealPrice().multiply(new BigDecimal(s.getNum())));
|
|
|
|
|
+
|
|
|
|
|
+ Goods goods = goodsRepo.findById(s.getGoodsId()).orElseThrow(new BusinessException("无商品"));
|
|
|
|
|
+ //满减
|
|
|
|
|
+ if (!goods.getIsFullReduction()) {
|
|
|
|
|
+ flag = false;
|
|
|
|
|
+ cart.setReducedAmount(goodsTotal.subtract(goodsReal));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //包装费
|
|
|
|
|
+ if (ObjectUtil.isNotNull(goods.getPackingPrice())) {
|
|
|
|
|
+ packingPrice = goods.getPackingPrice().multiply(BigDecimal.valueOf(s.getNum()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //总价 商品总价+包装费(可参与满减金额)
|
|
|
|
|
+ BigDecimal total = goodsTotal.add(packingPrice);
|
|
|
|
|
+
|
|
|
|
|
+ //按商家创建购物车
|
|
|
|
|
+ Long merchantId = specs.get(0).getGoods().getMerchantId();
|
|
|
|
|
+ //商家信息
|
|
|
|
|
+ MerchantSettings merchant = merchantSettingsRepo.findByMerchantId(merchantId).orElseThrow(new BusinessException("无商户"));
|
|
|
|
|
+
|
|
|
|
|
+ if (flag) {
|
|
|
|
|
+ //参与满减
|
|
|
|
|
+ List<FullReduction> fullReductions = fullReductionRepo.findAllByMerchantId(merchantId);
|
|
|
|
|
+ fullReductions.sort(Comparator.comparing(FullReduction::getFullAmount));
|
|
|
|
|
+ for (FullReduction f : fullReductions) {
|
|
|
|
|
+ if (f.getFullAmount().compareTo(total) <= 0) {
|
|
|
|
|
+ //满减金额
|
|
|
|
|
+ cart.setFullReduction(f.getMinusAmount());
|
|
|
|
|
+ //不是折扣,是满减,就置为减去的金额
|
|
|
|
|
+ cart.setReducedAmount(f.getMinusAmount());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //查找是不是第一次购买
|
|
|
|
|
+ //List<OrderInfo> userOrders = orderInfoRepo.findAllByUserId(userId);
|
|
|
|
|
+ List<OrderInfo> userOrdersMerchant = orderInfoRepo.findAllByUserIdAndMerchantId(userId, merchantId);
|
|
|
|
|
+ //是新用户也是首单
|
|
|
|
|
+// if (CollUtil.isEmpty(userOrders)) {
|
|
|
|
|
+// shoppingCart.setNewUser(merchant.getNewUser());
|
|
|
|
|
+// shoppingCart.setFirstBuy(merchant.getFirstOrder());
|
|
|
|
|
+// } else {
|
|
|
|
|
+ //首单
|
|
|
|
|
+ if (CollUtil.isEmpty(userOrdersMerchant)) {
|
|
|
|
|
+ //shoppingCart.setNewUser(BigDecimal.ZERO);
|
|
|
|
|
+ cart.setFirstBuy(merchant.getFirstOrder());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ //非首单设置为0
|
|
|
|
|
+ //shoppingCart.setNewUser(BigDecimal.ZERO);
|
|
|
|
|
+ cart.setFirstBuy(BigDecimal.ZERO);
|
|
|
|
|
+ }
|
|
|
|
|
+// }
|
|
|
|
|
|
|
|
- orderGoodsSpec.setShoppingCartId(shoppingCart.getId());
|
|
|
|
|
|
|
+ //获取配送费
|
|
|
|
|
+ cart.setDeliveryAmount(BigDecimal.TEN);
|
|
|
|
|
+
|
|
|
|
|
+ //需要减去的金额=首单+折扣/满减
|
|
|
|
|
+ BigDecimal sub2 = cart.getReducedAmount();
|
|
|
|
|
+ if (ObjectUtil.isNotNull(cart.getFirstBuy())) {
|
|
|
|
|
+ sub2 = sub2.add(cart.getFirstBuy());
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- orderGoodsSpecRepo.save(orderGoodsSpec);
|
|
|
|
|
|
|
+ cart.setGoodsTotal(goodsTotal);
|
|
|
|
|
+ cart.setReducedAmount(sub2);
|
|
|
|
|
+ cart.setRealAmount(total.subtract(sub2).add(cart.getDeliveryAmount()));
|
|
|
|
|
+
|
|
|
|
|
+ return shoppingCartRepo.save(cart);
|
|
|
|
|
|
|
|
- return shoppingCart;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ //加规格
|
|
|
|
|
+ public ShoppingCart add(Long userId, OrderGoodsSpec orderGoodsSpec) {
|
|
|
|
|
+ //按商家创建购物车
|
|
|
|
|
+ Long merchantId = goodsRepo.findById(orderGoodsSpec.getGoodsId()).orElseThrow(new BusinessException("无商品")).getMerchantId();
|
|
|
|
|
+
|
|
|
|
|
+ //商家信息
|
|
|
|
|
+ //MerchantSettings merchant = merchantSettingsRepo.findByMerchantId(merchantId).orElseThrow(new BusinessException("无商户"));
|
|
|
|
|
+
|
|
|
|
|
+ //查找是不是第一次购买
|
|
|
|
|
+ userRepo.findById(userId).orElseThrow(new BusinessException("无用户"));
|
|
|
|
|
+
|
|
|
|
|
+ //购物车中是否已有
|
|
|
|
|
+ ShoppingCart shoppingCart = shoppingCartRepo.findByUserIdAndMerchantId(userId, merchantId);
|
|
|
|
|
+ //加规格
|
|
|
|
|
+ List<OrderGoodsSpec> specs = CollUtil.newArrayList();
|
|
|
|
|
+ specs.add(orderGoodsSpec);
|
|
|
|
|
+
|
|
|
|
|
+ if (ObjectUtil.isNull(shoppingCart)) {
|
|
|
|
|
+ shoppingCart = ShoppingCart.builder()
|
|
|
|
|
+ .merchantId(merchantId)
|
|
|
|
|
+ .userId(userId)
|
|
|
|
|
+ .goodsTotal(orderGoodsSpec.getGoodsPrice().multiply(BigDecimal.valueOf(orderGoodsSpec.getNum())))
|
|
|
|
|
+ .reducedAmount(BigDecimal.ZERO)
|
|
|
|
|
+ .orderGoodsSpecs(specs)
|
|
|
|
|
+ .packingPrice(BigDecimal.ZERO)
|
|
|
|
|
+ .fullReduction(BigDecimal.ZERO)
|
|
|
|
|
+ .firstBuy(BigDecimal.ZERO)
|
|
|
|
|
+ .enabled(true)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ shoppingCart.setId(-1L);
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
|
|
+ specs.addAll(shoppingCart.getOrderGoodsSpecs());
|
|
|
|
|
+ shoppingCart.setOrderGoodsSpecs(specs);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ShoppingCart save = shoppingCartRepo.save(shoppingCart);
|
|
|
|
|
+
|
|
|
|
|
+ this.changeOrderSpec(save, orderGoodsSpec);
|
|
|
|
|
+
|
|
|
|
|
+ return this.calculatePrice(save.getId(), userId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //减规格
|
|
|
|
|
+ public ShoppingCart sub(Long specId, Long userId) {
|
|
|
|
|
+ OrderGoodsSpec spec = orderGoodsSpecRepo.findById(specId).orElseThrow(new BusinessException("无订单详情"));
|
|
|
|
|
+
|
|
|
|
|
+ Long cartId = spec.getShoppingCartId();
|
|
|
|
|
+ //if (spec.getShoppingCartId() == null) {
|
|
|
|
|
+ orderGoodsSpecRepo.deleteById(specId);
|
|
|
|
|
+ //}
|
|
|
|
|
+ //shoppingCartRepo.findById(spec.getShoppingCartId());
|
|
|
|
|
+ return this.calculatePrice(cartId, userId);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
}
|
|
}
|