From 2ccdf86ac2599562ca994bc4047ded2d925e8bae Mon Sep 17 00:00:00 2001
From: Pu Zhibing <393733352@qq.com>
Date: 星期五, 29 十一月 2024 16:33:12 +0800
Subject: [PATCH] 开发确认购物车订单逻辑

---
 ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/ShoppingCartServiceImpl.java |  307 ++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 265 insertions(+), 42 deletions(-)

diff --git a/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/ShoppingCartServiceImpl.java b/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/ShoppingCartServiceImpl.java
index 1f254ea..a3e1a00 100644
--- a/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/ShoppingCartServiceImpl.java
+++ b/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/ShoppingCartServiceImpl.java
@@ -1,26 +1,41 @@
 package com.ruoyi.order.service.impl;
 
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONArray;
+import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.account.api.feignClient.AppUserClient;
+import com.ruoyi.account.api.feignClient.UserAddressClient;
+import com.ruoyi.account.api.feignClient.UserCouponClient;
 import com.ruoyi.account.api.model.AppUser;
+import com.ruoyi.account.api.model.UserAddress;
+import com.ruoyi.account.api.vo.CouponInfoVo;
+import com.ruoyi.account.api.vo.PaymentUserCoupon;
+import com.ruoyi.common.core.domain.R;
 import com.ruoyi.common.core.web.domain.AjaxResult;
 import com.ruoyi.common.security.service.TokenService;
 import com.ruoyi.order.mapper.ShoppingCartMapper;
+import com.ruoyi.order.service.OrderGoodService;
+import com.ruoyi.order.service.OrderService;
 import com.ruoyi.order.service.ShoppingCartService;
-import com.ruoyi.order.vo.MyShoppingCartVo;
-import com.ruoyi.order.vo.SetGoodsNumber;
+import com.ruoyi.order.vo.*;
 import com.ruoyi.other.api.domain.*;
 import com.ruoyi.other.api.feignClient.*;
 import com.ruoyi.other.api.vo.GetGoodsBargainPrice;
 import com.ruoyi.other.api.vo.GetSeckillActivityInfo;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
+import model.Order;
+import model.OrderGood;
 import model.ShoppingCart;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
@@ -52,6 +67,30 @@
 	@Resource
 	private GoodsBargainPriceClient goodsBargainPriceClient;
 	
+	@Resource
+	private OrderService orderService;
+	
+	@Resource
+	private OrderGoodService orderGoodService;
+	
+	@Resource
+	private ShopClient shopClient;
+	
+	@Resource
+	private OrderActivityInfoClient orderActivityInfoClient;
+	
+	@Resource
+	private BaseSettingClient baseSettingClient;
+	
+	@Resource
+	private UserAddressClient userAddressClient;
+	
+	@Resource
+	private UserCouponClient userCouponClient;
+	
+	@Resource
+	private SystemConfigClient systemConfigClient;
+	
 	
 	
 	
@@ -75,44 +114,8 @@
 		List<Long> goodsIds = data.stream().map(Goods::getId).collect(Collectors.toList());
 		//查询符合商品类型的商品数据
 		List<ShoppingCart> list = this.list(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getAppUserId, userid).in(ShoppingCart::getGoodsId, goodsIds));
-		List<MyShoppingCartVo> page = new ArrayList<>();
 		//构建返回数据
-		for (ShoppingCart shoppingCart : list) {
-			Goods goods = data.stream().filter(s -> s.getId().equals(shoppingCart.getGoodsId())).findFirst().get();
-			MyShoppingCartVo vo = new MyShoppingCartVo();
-			vo.setId(shoppingCart.getId());
-			vo.setHomePicture(goods.getHomePagePicture());
-			vo.setName(goods.getName());
-			
-			//获取支付价格
-			Price price = getPrice(appUser, shoppingCart.getGoodsId(), shopId);
-			if(null == price){
-				//使用商品的基础价格
-				price.setCash(1 == goods.getCashPayment() ? goods.getSellingPrice() : null);
-				price.setPoint(1 == goods.getPointPayment() ? goods.getIntegral() : null);
-			}
-			//构建价格展示内容
-			String sellingPrice = "";
-			if(null != price.getCash() && null != price.getPoint()){
-				sellingPrice = price.getCash() + "或" + price.getPoint() + "积分";
-			}
-			if(null != price.getCash() && null == price.getPoint()){
-				sellingPrice = price.getCash() + "";
-			}
-			if(null == price.getCash() && null != price.getPoint()){
-				sellingPrice = price.getPoint() + "积分";
-			}
-			vo.setSellingPrice(sellingPrice);
-			vo.setEndTime(price.getEndTime());
-			vo.setOriginalPrice(goods.getOriginalPrice().toString());
-			vo.setNumber(shoppingCart.getNumber());
-			GoodsShop goodsShop = new GoodsShop();
-			goodsShop.setGoodsId(shoppingCart.getGoodsId());
-			goodsShop.setShopId(shopId);
-			GoodsShop goodsShop1 = goodsShopClient.getGoodsShop(goodsShop).getData();
-			vo.setVerifiable(null == goodsShop1 ? false : true);
-			page.add(vo);
-		}
+		List<MyShoppingCartVo> page = buildDetail(appUser, shopId, list, null);
 		return page;
 	}
 	
@@ -157,14 +160,30 @@
 					}else{
 						price.setCash(goodsVip.getSellingPrice());
 						price.setPoint(goodsVip.getIntegral());
+						price.setCashPayment(goodsVip.getCashPayment() == 1 ? true : false);
+						price.setPointPayment(goodsVip.getPointPayment() == 1 ? true : false);
+						price.setEarnSpendingPoints(goodsVip.getEarnSpendingPoints());
 					}
 				}else{
 					price.setCash(goodsArea.getSellingPrice());
 					price.setPoint(goodsArea.getIntegral());
+					price.setCashPayment(goodsArea.getCashPayment() == 1 ? true : false);
+					price.setPointPayment(goodsArea.getPointPayment() == 1 ? true : false);
+					price.setEarnSpendingPoints(goodsArea.getEarnSpendingPoints());
 				}
 			}else{
 				price.setCash(bargainPriceDetail.getSellingPrice());
 				price.setPoint(bargainPriceDetail.getIntegral());
+				price.setCashPayment(bargainPriceDetail.getSellingPrice() != null ? true : false);
+				price.setPointPayment(bargainPriceDetail.getIntegral() != null ? true : false);
+				//门店特价,消费积分使用会员等级的消费积分
+				GoodsArea area = new GoodsArea();
+				area.setDistrictsCode(appUser.getDistrictCode());
+				area.setCityCode(appUser.getCityCode());
+				area.setProvinceCode(appUser.getProvinceCode());
+				area.setVip(appUser.getVipId());
+				GoodsArea goodsArea = goodsAreaClient.getGoodsArea(area).getData();
+				price.setEarnSpendingPoints(goodsArea.getEarnSpendingPoints());
 			}
 		}else{
 			//构建价格数据
@@ -178,7 +197,10 @@
 			if(goodsSeckill.getCashPayment() == 0 && goodsSeckill.getPointPayment() == 1){
 				price.setPoint(goodsSeckill.getIntegral());
 			}
+			price.setCashPayment(goodsSeckill.getCashPayment() == 1 ? true : false);
+			price.setPointPayment(goodsSeckill.getPointPayment() == 1 ? true : false);
 			price.setEndTime(goodsSeckill.getEndTime());
+			price.setEarnSpendingPoints(goodsSeckill.getEarnSpendingPoints());
 		}
 		return price;
 	}
@@ -198,6 +220,18 @@
 		 * 获取结束时间
 		 */
 		private Long endTime;
+		/**
+		 * 现金支付
+		 */
+		private Boolean cashPayment;
+		/**
+		 * 积分支付
+		 */
+		private Boolean pointPayment;
+		/**
+		 * 可获得消费积分
+		 */
+		private Integer earnSpendingPoints;
 	}
 	
 
@@ -215,15 +249,204 @@
 	 * @return
 	 */
 	@Override
-	public AjaxResult setGoodsNumber(SetGoodsNumber setGoodsNumber) {
+	public R setGoodsNumber(SetGoodsNumber setGoodsNumber) {
 		if(0 >= setGoodsNumber.getNumber()){
-			return AjaxResult.error("修改数量不能小于等于0");
+			return R.fail("修改数量不能小于等于0");
 		}
 		ShoppingCart shoppingCart = this.getById(setGoodsNumber.getId());
+		Goods goods = goodsClient.getGoodsById(shoppingCart.getGoodsId()).getData();
+		if(null != goods.getPurchaseLimit() && -1 != goods.getPurchaseLimit() && goods.getPurchaseLimit() < setGoodsNumber.getNumber()){
+			return R.fail("修改数量不能大于限购数量");
+		}
 		if(null != shoppingCart){
 			shoppingCart.setNumber(setGoodsNumber.getNumber());
 			this.updateById(shoppingCart);
 		}
-		return AjaxResult.success();
+		return R.ok();
+	}
+	
+	
+	/**
+	 * 确认购物车订单
+	 * @param confirmOrder
+	 * @return
+	 */
+	@Override
+	public ConfirmOrderVo confirmOrder(ConfirmOrder confirmOrder) {
+		Long userid = tokenService.getLoginUserApplet().getUserid();
+		AppUser appUser = appUserClient.getAppUserById(userid);
+		Integer shopId = confirmOrder.getShopId();
+		Shop shop = shopClient.getShopById(shopId).getData();
+		String goodsJson = confirmOrder.getGoodsJson();
+		List<Long> ids = new ArrayList<>();
+		JSONArray objects = JSON.parseArray(goodsJson);
+		for (int i = 0; i < objects.size(); i++) {
+			Long id = objects.getJSONObject(i).getLong("id");
+			ids.add(id);
+		}
+		List<ShoppingCart> list = this.listByIds(ids);
+		ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
+		//构建商品明细列表
+		List<MyShoppingCartVo> goodsList = buildDetail(appUser, shopId, list, objects);
+		confirmOrderVo.setGoodsList(goodsList);
+		confirmOrderVo.setShopId(confirmOrder.getShopId());
+		confirmOrderVo.setShopName(shop.getName());
+		//现金支付
+		if(confirmOrder.getPaymentType() == 1){
+			BigDecimal bigDecimal = goodsList.stream().map(MyShoppingCartVo::getCash).reduce(BigDecimal::add).get();
+			confirmOrderVo.setOrderMoney(bigDecimal);
+		}else{
+			int sum = goodsList.stream().mapToInt(MyShoppingCartVo::getPoint).sum();
+			confirmOrderVo.setOrderPoint(sum);
+		}
+		//查询当前是否有订单活动
+		OrderActivityInfo orderActivityInfo = orderActivityInfoClient.getNowOrderActivityInfo(appUser.getVipId()).getData();
+		BigDecimal orderMoney = confirmOrderVo.getOrderMoney();
+		BigDecimal paymentMoney = orderMoney;
+		//满XX才打折,只有现金才能优惠
+		if(null != orderActivityInfo && confirmOrder.getPaymentType() == 1 && orderActivityInfo.getConditionAmount().compareTo(orderMoney) <= 0){
+			confirmOrderVo.setActivityName(orderActivityInfo.getActivityName());
+			paymentMoney = orderActivityInfo.getDiscount().divide(new BigDecimal(10)).multiply(orderMoney);
+			confirmOrderVo.setDiscountAmount(orderMoney.subtract(paymentMoney).setScale(2, RoundingMode.HALF_EVEN));
+		}
+		BaseSetting baseSetting = baseSettingClient.getBaseSetting(4).getData();
+		confirmOrderVo.setUseSimultaneously(baseSetting.getContent().equals("1") ? true : false);
+		int earnPoint = goodsList.stream().mapToInt(MyShoppingCartVo::getEarnSpendingPoints).sum();
+		confirmOrderVo.setEarnPoint(earnPoint);
+		//支付金额,订单金额-订单优惠
+		confirmOrderVo.setPayMoney(paymentMoney);
+		confirmOrderVo.setResidualPoint(appUser.getLavePoint().intValue());
+		//获取默认收货地址
+		UserAddress userAddress = userAddressClient.getDefaultUserAddress(userid).getData();
+		confirmOrderVo.setUserAddress(userAddress);
+		confirmOrderVo.setPaymentType(confirmOrder.getPaymentType());
+		//获取用户优惠券,用户全部优惠券,不能使用的需要标识出来置灰展示
+		PaymentUserCoupon paymentUserCoupon = new PaymentUserCoupon();
+		paymentUserCoupon.setUserId(userid);
+		paymentUserCoupon.setOrderMoney(orderMoney);
+		paymentUserCoupon.setType(confirmOrder.getType());
+		List<CouponInfoVo> data = userCouponClient.getPaymentUserCoupon(paymentUserCoupon).getData();
+		confirmOrderVo.setCoupon(data);
+		//获取快递策略
+		SystemConfig systemConfig = systemConfigClient.getSystemConfig(3).getData();
+		JSONObject jsonObject = JSON.parseObject(systemConfig.getContent());
+		confirmOrderVo.setExpressFee(jsonObject.getBigDecimal("expressFee"));
+		List<Integer> vip = jsonObject.getJSONArray("vip").toList(Integer.class);
+		//包邮条件(所有会员或者满足条件的会员)
+		if(vip.get(0) == -1 || vip.contains(appUser.getVipId())){
+			if(confirmOrder.getPaymentType() == 1){
+				//现金支付,支付金额满足包邮条件
+				BigDecimal cash = jsonObject.getBigDecimal("cash");
+				if(confirmOrderVo.getPayMoney().compareTo(cash) >= 0){
+					confirmOrderVo.setExpressFee(BigDecimal.ZERO);
+				}
+			}else{
+				//积分支付,支付积分是否满足包邮条件
+				Integer point = jsonObject.getInteger("point");
+				if(confirmOrderVo.getOrderPoint().compareTo(point) >= 0){
+					confirmOrderVo.setExpressFee(BigDecimal.ZERO);
+				}
+			}
+		}
+		return confirmOrderVo;
+	}
+	
+	
+	/**
+	 * 构建购物车商品列表
+	 * @param appUser
+	 * @param shopId
+	 * @param list
+	 * @param objects
+	 * @return
+	 */
+	private List<MyShoppingCartVo> buildDetail(AppUser appUser, Integer shopId, List<ShoppingCart> list, JSONArray objects){
+		List<MyShoppingCartVo> page = new ArrayList<>();
+		for (ShoppingCart shoppingCart : list) {
+			Goods goods = goodsClient.getGoodsById(shoppingCart.getGoodsId()).getData();
+			MyShoppingCartVo vo = new MyShoppingCartVo();
+			vo.setId(shoppingCart.getId());
+			vo.setHomePicture(goods.getHomePagePicture());
+			vo.setName(goods.getName());
+			int num = shoppingCart.getNumber();
+			if(null != objects){
+				for (int i = 0; i < objects.size(); i++) {
+					Long id = objects.getJSONObject(i).getLong("id");
+					if(id.equals(shoppingCart.getId())){
+						num = objects.getJSONObject(i).getInteger("num");
+						break;
+					}
+				}
+			}
+			//获取支付价格
+			Price price = getPrice(appUser, shoppingCart.getGoodsId(), shopId);
+			if(null == price){
+				//使用商品的基础价格
+				price.setCash(1 == goods.getCashPayment() ? goods.getSellingPrice() : null);
+				price.setPoint(1 == goods.getPointPayment() ? goods.getIntegral() : null);
+				price.setCashPayment(goods.getCashPayment() == 1 ? true : false);
+				price.setPointPayment(goods.getPointPayment() == 1 ? true : false);
+			}
+			vo.setCash(price.getCash());
+			vo.setPoint(price.getPoint());
+			vo.setCashPayment(price.getCashPayment());
+			vo.setPointPayment(price.getPointPayment());
+			vo.setEndTime(price.getEndTime());
+			vo.setOriginalPrice(goods.getOriginalPrice().toString());
+			vo.setNumber(num);
+			GoodsShop goodsShop = new GoodsShop();
+			goodsShop.setGoodsId(shoppingCart.getGoodsId());
+			goodsShop.setShopId(shopId);
+			GoodsShop goodsShop1 = goodsShopClient.getGoodsShop(goodsShop).getData();
+			vo.setVerifiable(null == goodsShop1 ? false : true);
+			//判断当前数量是否已经超出限购数量(需要计算已经购买的数量)
+			if(null == goods.getPurchaseLimit() || -1 == goods.getPurchaseLimit()){
+				vo.setPurchaseLimit(false);
+			}else{
+				List<Order> orders = orderService.list(new LambdaQueryWrapper<Order>().eq(Order::getAppUserId, appUser.getId()).eq(Order::getDelFlag, 0).in(Order::getOrderStatus, Arrays.asList(4, 8)));
+				List<Long> orderIds = orders.stream().map(Order::getId).collect(Collectors.toList());
+				int sum = 0;
+				if(orderIds.size() > 0){
+					List<OrderGood> orderGoodList = orderGoodService.list(new LambdaQueryWrapper<OrderGood>().in(OrderGood::getOrderId, orderIds)
+							.eq(OrderGood::getGoodsId, shoppingCart.getGoodsId()).eq(OrderGood::getDelFlag, 0));
+					sum = orderGoodList.stream().mapToInt(OrderGood::getNum).sum();
+				}
+				vo.setPurchaseLimit((num + sum) > goods.getPurchaseLimit() ? true : false);
+			}
+			vo.setEarnSpendingPoints(price.getEarnSpendingPoints());
+			vo.setDistributionMode(goods.getDistributionMode());
+			page.add(vo);
+		}
+		return page;
+	}
+	
+	
+	/**
+	 * 购物车支付操作
+	 * @param shoppingCartPayment
+	 * @return
+	 */
+	@Override
+	public R shoppingCartPayment(ShoppingCartPayment shoppingCartPayment) {
+		Long userid = tokenService.getLoginUserApplet().getUserid();
+		AppUser appUser = appUserClient.getAppUserById(userid);
+		Integer shopId = shoppingCartPayment.getShopId();
+		Shop shop = shopClient.getShopById(shopId).getData();
+		String goodsJson = shoppingCartPayment.getGoodsJson();
+		List<Long> ids = new ArrayList<>();
+		JSONArray objects = JSON.parseArray(goodsJson);
+		for (int i = 0; i < objects.size(); i++) {
+			Long id = objects.getJSONObject(i).getLong("id");
+			ids.add(id);
+		}
+		List<ShoppingCart> list = this.listByIds(ids);
+		ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
+		//构建商品明细列表
+		List<MyShoppingCartVo> goodsList = buildDetail(appUser, shopId, list, objects);
+		
+		
+		
+		
+		return null;
 	}
 }

--
Gitblit v1.7.1