From a8d2cb07f6440dc54dc4005b0b06d5a47cb1517d Mon Sep 17 00:00:00 2001
From: luodangjia <luodangjia>
Date: 星期一, 16 十二月 2024 13:34:03 +0800
Subject: [PATCH] 12.16

---
 ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/ShoppingCartServiceImpl.java |  483 ++++++++++++++++++++++++++++++++---------------------
 1 files changed, 293 insertions(+), 190 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 52e020d..9e1a950 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
@@ -6,10 +6,7 @@
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.account.api.feignClient.*;
-import com.ruoyi.account.api.model.AppUser;
-import com.ruoyi.account.api.model.BalanceChangeRecord;
-import com.ruoyi.account.api.model.UserAddress;
-import com.ruoyi.account.api.model.UserPoint;
+import com.ruoyi.account.api.model.*;
 import com.ruoyi.account.api.vo.CouponInfoVo;
 import com.ruoyi.account.api.vo.PaymentUserCoupon;
 import com.ruoyi.account.api.vo.PaymentUserCouponVo;
@@ -20,6 +17,7 @@
 import com.ruoyi.order.model.Order;
 import com.ruoyi.order.model.OrderGood;
 import com.ruoyi.order.model.ShoppingCart;
+import com.ruoyi.order.service.CommissionService;
 import com.ruoyi.order.service.OrderGoodService;
 import com.ruoyi.order.service.OrderService;
 import com.ruoyi.order.service.ShoppingCartService;
@@ -29,7 +27,6 @@
 import com.ruoyi.other.api.vo.GetGoodsBargainPrice;
 import com.ruoyi.other.api.vo.GetGoodsShopByGoodsIds;
 import com.ruoyi.other.api.vo.GetSeckillActivityInfo;
-import lombok.Data;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
@@ -37,72 +34,80 @@
 import java.math.RoundingMode;
 import java.text.SimpleDateFormat;
 import java.time.LocalDateTime;
-import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.stream.Collectors;
 
 @Service
 public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
-	
+
 	@Resource
 	private TokenService tokenService;
-	
+
 	@Resource
 	private GoodsClient goodsClient;
-	
+
 	@Resource
 	private GoodsShopClient goodsShopClient;
-	
+
 	@Resource
 	private AppUserClient appUserClient;
-	
+
 	@Resource
 	private GoodsAreaClient goodsAreaClient;
-	
+
 	@Resource
 	private GoodsVipClient goodsVipClient;
-	
+
 	@Resource
 	private SeckillActivityInfoClient seckillActivityInfoClient;
-	
+
 	@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;
-	
+
 	@Resource
 	private UserPointClient userPointClient;
-	
+
 	@Resource
 	private BalanceChangeRecordClient balanceChangeRecordClient;
-	
-	
-	
-	
-	
-	
+
+	@Resource
+	private CommissionService commissionService;
+
+	@Resource
+	private VipSettingClient vipSettingClient;
+
+	@Resource
+	private UserChangeLogClient userChangeLogClient;
+
+
+
+
+
+
 	/**
 	 * 获取购物车列表
 	 * @param type
@@ -125,8 +130,8 @@
 		List<MyShoppingCartVo> page = buildDetail(appUser, shopId, list, null);
 		return page;
 	}
-	
-	
+
+
 	/**
 	 * 获取支付价格
 	 * @param appUser
@@ -148,8 +153,11 @@
 			GetGoodsBargainPrice goodsBargainPrice = new GetGoodsBargainPrice();
 			goodsBargainPrice.setGoodsId(goodsId);
 			goodsBargainPrice.setVip(appUser.getVipId());
-			goodsBargainPrice.setShopId(shopId);
-			GoodsBargainPriceDetail bargainPriceDetail = goodsBargainPriceClient.getGoodsBargainPrice(goodsBargainPrice).getData();
+			GoodsBargainPriceDetail bargainPriceDetail = null;
+			if (shopId != null){
+				goodsBargainPrice.setShopId(shopId);
+				bargainPriceDetail = goodsBargainPriceClient.getGoodsBargainPrice(goodsBargainPrice).getData();
+			}
 			if(null == bargainPriceDetail){
 				//没有门店特价,判断地区价格配置
 				GoodsArea area = new GoodsArea();
@@ -167,8 +175,8 @@
 					}else{
 						price.setCash(goodsVip.getSellingPrice());
 						price.setPoint(goodsVip.getIntegral());
-						price.setCashPayment(goodsVip.getCashPayment() == 1 ? true : false);
-						price.setPointPayment(goodsVip.getPointPayment() == 1 ? true : false);
+						price.setCashPayment(goodsVip.getCashPayment() == 1);
+						price.setPointPayment(goodsVip.getPointPayment() == 1);
 						price.setEarnSpendingPoints(goodsVip.getEarnSpendingPoints());
 						price.setSuperiorSubcommission(goodsVip.getSuperiorSubcommission());
 						price.setSuperiorRebatePoints(goodsVip.getSuperiorRebatePoints());
@@ -184,8 +192,8 @@
 				}else{
 					price.setCash(goodsArea.getSellingPrice());
 					price.setPoint(goodsArea.getIntegral());
-					price.setCashPayment(goodsArea.getCashPayment() == 1 ? true : false);
-					price.setPointPayment(goodsArea.getPointPayment() == 1 ? true : false);
+					price.setCashPayment(goodsArea.getCashPayment() == 1);
+					price.setPointPayment(goodsArea.getPointPayment() == 1);
 					price.setEarnSpendingPoints(goodsArea.getEarnSpendingPoints());
 					price.setSuperiorSubcommission(goodsArea.getSuperiorSubcommission());
 					price.setSuperiorRebatePoints(goodsArea.getSuperiorRebatePoints());
@@ -201,8 +209,8 @@
 			}else{
 				price.setCash(bargainPriceDetail.getSellingPrice());
 				price.setPoint(bargainPriceDetail.getIntegral());
-				price.setCashPayment(bargainPriceDetail.getSellingPrice() != null ? true : false);
-				price.setPointPayment(bargainPriceDetail.getIntegral() != null ? true : false);
+				price.setCashPayment(bargainPriceDetail.getSellingPrice() != null);
+				price.setPointPayment(bargainPriceDetail.getIntegral() != null);
 				//门店特价,消费积分使用会员等级的消费积分
 				GoodsArea area = new GoodsArea();
 				area.setDistrictsCode(appUser.getDistrictCode());
@@ -234,8 +242,8 @@
 			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.setCashPayment(goodsSeckill.getCashPayment() == 1);
+			price.setPointPayment(goodsSeckill.getPointPayment() == 1);
 			price.setEndTime(goodsSeckill.getEndTime());
 			price.setEarnSpendingPoints(goodsSeckill.getEarnSpendingPoints());
 			price.setSuperiorSubcommission(goodsSeckill.getSuperiorSubcommission());
@@ -251,85 +259,19 @@
 		}
 		return price;
 	}
-	
-	
-	@Data
-	class Price {
-		/**
-		 * 现金
-		 */
-		private BigDecimal cash;
-		/**
-		 * 积分
-		 */
-		private Integer point;
-		/**
-		 * 获取结束时间
-		 */
-		private Long endTime;
-		/**
-		 * 现金支付
-		 */
-		private Boolean cashPayment;
-		/**
-		 * 积分支付
-		 */
-		private Boolean pointPayment;
-		/**
-		 * 可获得消费积分
-		 */
-		private Integer earnSpendingPoints;
-		/**
-		 * 上级获得分佣金额
-		 */
-		private BigDecimal superiorSubcommission;
-		/**
-		 * 上级获得返佣积分
-		 */
-		private Integer superiorRebatePoints;
-		/**
-		 * 获取返佣积分上级类型(1=直推上级,2=直帮上级)
-		 */
-		private Integer superiorType;
-		/**
-		 * 核销门店可获得服务费
-		 */
-		private BigDecimal servuceShopCharges;
-		/**
-		 * 核销门店可获得服务积分
-		 */
-		private Integer servuceShopPoints;
-		/**
-		 * 技师可获得服务积分
-		 */
-		private Integer technicianPoints;
-		/**
-		 * 绑定门店可获得分佣金额
-		 */
-		private BigDecimal boundShopCharges;
-		/**
-		 * 绑定门店可获得返佣积分
-		 */
-		private Integer boundShopPoints;
-		/**
-		 * 绑定门店上级门店可获得分佣金额
-		 */
-		private BigDecimal boundShopSuperiorsCharges;
-		/**
-		 * 绑定门店上级门店可获得返佣积分
-		 */
-		private Integer boundShopSuperiorsPoints;
-	}
-	
+
+
+
 
 	@Override
-	public void addGoods(ShoppingCart shoppingCart) {
+	public Long addGoods(ShoppingCart shoppingCart) {
 		Long userid = tokenService.getLoginUserApplet().getUserid();
 		shoppingCart.setAppUserId(userid);
 		this.save(shoppingCart);
+		return shoppingCart.getId();
 	}
-	
-	
+
+
 	/**
 	 * 修改购物车数量
 	 * @param setGoodsNumber
@@ -341,18 +283,21 @@
 			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){
+
+			Goods goods = goodsClient.getGoodsById(shoppingCart.getGoodsId()).getData();
+			if(null != goods.getPurchaseLimit() && -1 != goods.getPurchaseLimit() && goods.getPurchaseLimit() < setGoodsNumber.getNumber()){
+				return R.fail("修改数量不能大于限购数量");
+			}
+
 			shoppingCart.setNumber(setGoodsNumber.getNumber());
 			this.updateById(shoppingCart);
 		}
 		return R.ok();
 	}
-	
-	
+
+
 	/**
 	 * 确认购物车订单
 	 * @param confirmOrder
@@ -360,7 +305,22 @@
 	 */
 	@Override
 	public ConfirmOrderVo confirmOrder(ConfirmOrder confirmOrder) {
+		Integer position = confirmOrder.getPosition();
 		Long userid = tokenService.getLoginUserApplet().getUserid();
+		//直接购买商品
+		if(2 == position){
+			//先加入购物车
+			String goodsJson = confirmOrder.getGoodsJson();
+			JSONArray objects = JSON.parseArray(goodsJson);
+			Long id = objects.getJSONObject(0).getLong("id");
+			Integer num1 = objects.getJSONObject(0).getInteger("num");
+			ShoppingCart shoppingCart = new ShoppingCart();
+			shoppingCart.setAppUserId(userid);
+			shoppingCart.setGoodsId(id.intValue());
+			shoppingCart.setNumber(num1);
+			Long shoppingCarId = addGoods(shoppingCart);
+			confirmOrder.setGoodsJson("[{\"id\": " + shoppingCarId + ", \"num\": " + num1 + "}]");
+		}
 		AppUser appUser = appUserClient.getAppUserById(userid);
 		Integer shopId = confirmOrder.getShopId();
 		Shop shop = shopClient.getShopById(shopId).getData();
@@ -397,7 +357,7 @@
 			confirmOrderVo.setDiscountAmount(orderMoney.subtract(paymentMoney).setScale(2, RoundingMode.HALF_EVEN));
 		}
 		BaseSetting baseSetting = baseSettingClient.getBaseSetting(4).getData();
-		confirmOrderVo.setUseSimultaneously(baseSetting.getContent().equals("1") ? true : false);
+		confirmOrderVo.setUseSimultaneously(baseSetting.getContent().equals("1"));
 		int earnPoint = goodsList.stream().mapToInt(MyShoppingCartVo::getEarnSpendingPoints).sum();
 		confirmOrderVo.setEarnPoint(earnPoint);
 		//支付金额,订单金额-订单优惠
@@ -405,6 +365,7 @@
 		confirmOrderVo.setResidualPoint(appUser.getLavePoint().intValue());
 		//获取默认收货地址
 		UserAddress userAddress = userAddressClient.getDefaultUserAddress(userid).getData();
+		userAddress.setIdStr(userAddress.getId().toString());
 		confirmOrderVo.setUserAddress(userAddress);
 		confirmOrderVo.setPaymentType(confirmOrder.getPaymentType());
 		//获取用户优惠券,用户全部优惠券,不能使用的需要标识出来置灰展示
@@ -414,35 +375,37 @@
 		paymentUserCoupon.setType(confirmOrder.getType());
 		if(confirmOrder.getPaymentType() == 1){
 			List<PaymentUserCouponVo> data = userCouponClient.getPaymentUserCoupon(paymentUserCoupon).getData();
-			for (PaymentUserCouponVo couponInfo : data) {
-				List<Integer> forGoodIds = couponInfo.getForGoodIds();
-				//全部商品适用
-				if(null == forGoodIds){
-					//满减券
-					if(1 == couponInfo.getCouponType() && orderMoney.compareTo(couponInfo.getConditionAmount()) >= 0){
-						couponInfo.setAvailable(true);
-					}
-					//代金券和折扣券
-					if(2 == couponInfo.getCouponType() || 3 == couponInfo.getCouponType()){
-						couponInfo.setAvailable(true);
-					}
-				}else{
-					//部分商品适用
-					BigDecimal goodsMoney = BigDecimal.ZERO;
-					for (MyShoppingCartVo myShoppingCartVo : goodsList) {
-						Integer goodsId = myShoppingCartVo.getGoodsId();
-						BigDecimal cash = myShoppingCartVo.getCash();
-						if(forGoodIds.contains(goodsId)){
-							goodsMoney = goodsMoney.add(cash);
+			if(null != data){
+				for (PaymentUserCouponVo couponInfo : data) {
+					List<Integer> forGoodIds = couponInfo.getForGoodIds();
+					//全部商品适用
+					if(null == forGoodIds){
+						//满减券
+						if(1 == couponInfo.getCouponType() && orderMoney.compareTo(couponInfo.getConditionAmount()) >= 0){
+							couponInfo.setAvailable(true);
 						}
-					}
-					//满减
-					if(1 == couponInfo.getCouponType() && couponInfo.getConditionAmount().compareTo(goodsMoney) <= 0){
-						couponInfo.setAvailable(true);
-					}
-					//代金券
-					if(2 == couponInfo.getCouponType() || 3 == couponInfo.getCouponType()){
-						couponInfo.setAvailable(true);
+						//代金券和折扣券
+						if(2 == couponInfo.getCouponType() || 3 == couponInfo.getCouponType()){
+							couponInfo.setAvailable(true);
+						}
+					}else{
+						//部分商品适用
+						BigDecimal goodsMoney = BigDecimal.ZERO;
+						for (MyShoppingCartVo myShoppingCartVo : goodsList) {
+							Integer goodsId = myShoppingCartVo.getGoodsId();
+							BigDecimal cash = myShoppingCartVo.getCash();
+							if(forGoodIds.contains(goodsId)){
+								goodsMoney = goodsMoney.add(cash);
+							}
+						}
+						//满减
+						if(1 == couponInfo.getCouponType() && couponInfo.getConditionAmount().compareTo(goodsMoney) <= 0){
+							couponInfo.setAvailable(true);
+						}
+						//代金券
+						if(2 == couponInfo.getCouponType() || 3 == couponInfo.getCouponType()){
+							couponInfo.setAvailable(true);
+						}
 					}
 				}
 			}
@@ -471,8 +434,8 @@
 		}
 		return confirmOrderVo;
 	}
-	
-	
+
+
 	/**
 	 * 构建购物车商品列表
 	 * @param appUser
@@ -486,7 +449,7 @@
 		for (ShoppingCart shoppingCart : list) {
 			Goods goods = goodsClient.getGoodsById(shoppingCart.getGoodsId()).getData();
 			MyShoppingCartVo vo = new MyShoppingCartVo();
-			vo.setId(shoppingCart.getId());
+			vo.setId(shoppingCart.getId().toString());
 			vo.setGoodsId(goods.getId());
 			vo.setHomePicture(goods.getHomePagePicture());
 			vo.setName(goods.getName());
@@ -503,11 +466,12 @@
 			//获取支付价格
 			Price price = getPrice(appUser, shoppingCart.getGoodsId(), shopId);
 			if(null == price){
+				price = new 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);
+				price.setCashPayment(goods.getCashPayment() == 1);
+				price.setPointPayment(goods.getPointPayment() == 1);
 			}
 			vo.setCash(price.getCash());
 			vo.setPoint(price.getPoint());
@@ -520,7 +484,7 @@
 			goodsShop.setGoodsId(shoppingCart.getGoodsId());
 			goodsShop.setShopId(shopId);
 			GoodsShop goodsShop1 = goodsShopClient.getGoodsShop(goodsShop).getData();
-			vo.setVerifiable(null == goodsShop1 ? false : true);
+			vo.setVerifiable(goods.getAppointStore() != 1 || null != goodsShop1);
 			//判断当前数量是否已经超出限购数量(需要计算已经购买的数量)
 			if(null == goods.getPurchaseLimit() || -1 == goods.getPurchaseLimit()){
 				vo.setPurchaseLimit(false);
@@ -533,7 +497,7 @@
 							.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.setPurchaseLimit((num + sum) > goods.getPurchaseLimit());
 			}
 			vo.setDistributionMode(goods.getDistributionMode());
 			vo.setEarnSpendingPoints(price.getEarnSpendingPoints());
@@ -551,8 +515,8 @@
 		}
 		return page;
 	}
-	
-	
+
+
 	/**
 	 * 购物车支付操作
 	 * @param shoppingCartPayment
@@ -589,7 +553,7 @@
 			}
 			ids.add(id);
 		}
-		
+
 		List<ShoppingCart> list = this.listByIds(ids);
 		//构建商品明细列表
 		List<MyShoppingCartVo> goodsList = buildDetail(appUser, shopId, list, objects);
@@ -619,12 +583,15 @@
 		if(data.size() != goodsList.size()){
 			String goodsName = "";
 			for (Integer goodsId : goodsIds) {
-				if(!collect.contains(goodsId)){
-					goodsName = goodsClient.getGoodsById(goodsId).getData().getName();
+				Goods goods = goodsClient.getGoodsById(goodsId).getData();
+				if(1 == goods.getAppointStore() && !collect.contains(goodsId)){
+					goodsName = goods.getName();
 					break;
 				}
 			}
-			return R.fail(goodsName + "不能在该门店核销");
+			if(StringUtils.isNotEmpty(goodsName)){
+				return R.fail(goodsName + "不能在该门店核销");
+			}
 		}
 		//开始构建支付信息
 		//现金支付的订单金额
@@ -634,9 +601,14 @@
 		//积分支付的订单积分
 		Integer orderPoint = 0;
 		if(3 != shoppingCartPayment.getPaymentType()){
-			orderMoney = goodsList.stream().map(MyShoppingCartVo::getCash).reduce(BigDecimal::add).get();
+			for (MyShoppingCartVo myShoppingCartVo : goodsList) {
+				orderMoney = orderMoney.add(myShoppingCartVo.getCash().multiply(new BigDecimal(myShoppingCartVo.getNumber())));
+			}
 		}else{
 			orderPoint = goodsList.stream().mapToInt(MyShoppingCartVo::getPoint).sum();
+			for (MyShoppingCartVo myShoppingCartVo : goodsList) {
+				orderPoint += (myShoppingCartVo.getPoint() * myShoppingCartVo.getNumber());
+			}
 			if(appUser.getLavePoint().intValue() < orderPoint){
 				return R.fail("账户剩余积分不足");
 			}
@@ -651,7 +623,7 @@
 		BigDecimal discountAmount = BigDecimal.ZERO;
 		//活动优惠金额
 		BigDecimal activityAmount = BigDecimal.ZERO;
-		
+
 		//减去优惠券优惠金额
 		CouponInfoVo couponInfoVo = null;
 		if(null != shoppingCartPayment.getUserCouponId() && 3 != shoppingCartPayment.getPaymentType()){
@@ -694,7 +666,7 @@
 						paymentMoney = paymentMoney.add(cash);
 					}
 				}
-				
+
 				//满减
 				if(1 == couponInfoVo.getCouponType() && couponInfoVo.getConditionAmount().compareTo(goodsMoney) <= 0){
 					goodsMoney = goodsMoney.subtract(couponInfoVo.getDiscountAmount());
@@ -719,16 +691,16 @@
 				paymentMoney = paymentMoney.add(goodsMoney);
 			}
 		}
-		
+
 		//查询当前是否有订单活动
 		OrderActivityInfo orderActivityInfo = orderActivityInfoClient.getNowOrderActivityInfo(appUser.getVipId()).getData();
 		BaseSetting baseSetting = baseSettingClient.getBaseSetting(4).getData();
 		//系统活动设置(优惠券和活动能否同时使用)
-		boolean useSimultaneously = baseSetting.getContent().equals("1") ? true : false;
+		boolean useSimultaneously = baseSetting.getContent().equals("1");
 		//满XX才打折,只有现金才能优惠
 		//如果使用优惠券,则需要判断是否可以和同时使用,且活动满足使用条件。
 		//没有使用优惠券,只需要判断是都满足使用条件
-		if(((useSimultaneously && null != shoppingCartPayment.getUserCouponId()) || null == shoppingCartPayment.getUserCouponId()) &&
+		if((useSimultaneously || null == shoppingCartPayment.getUserCouponId()) &&
 				null != orderActivityInfo && shoppingCartPayment.getPaymentType() != 3 && orderActivityInfo.getConditionAmount().compareTo(paymentMoney) <= 0){
 			BigDecimal paymentMoney1 = orderActivityInfo.getDiscount().divide(new BigDecimal(10)).multiply(paymentMoney);
 			BigDecimal bigDecimal = paymentMoney.subtract(paymentMoney1).setScale(2, RoundingMode.HALF_EVEN);
@@ -736,13 +708,16 @@
 			paymentMoney = paymentMoney1;
 			activityAmount = activityAmount.add(bigDecimal);
 		}
-		
+
 		//可获得的消费积分
-		int earnPoint = goodsList.stream().mapToInt(MyShoppingCartVo::getEarnSpendingPoints).sum();
+		int earnPoint = 0;
+		for (MyShoppingCartVo myShoppingCartVo : goodsList) {
+			earnPoint += (myShoppingCartVo.getEarnSpendingPoints() * myShoppingCartVo.getNumber());
+		}
 		if(null != shoppingCartPayment.getUserAddressId()){
 			userAddressClient.getUserAddressById(shoppingCartPayment.getUserAddressId()).getData();
 		}
-		
+
 		//获取快递策略,计算快递费
 		BigDecimal expressFee = BigDecimal.ZERO;
 		if(null != shoppingCartPayment.getUserAddressId()){
@@ -782,7 +757,7 @@
 		order.setGoodName(goodName.substring(0, goodName.length() - 1));
 		Goods goods = goodsClient.getGoodsById(goodsList.get(0).getGoodsId()).getData();
 		order.setOrderType(goods.getType());
-		order.setOrderStatus(1);
+		order.setOrderStatus(goods.getType() == 1 ? 3 : 1);
 		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
 		order.setOrderNumber("QJS" + getNumber(3) + sdf.format(new Date()));
 		order.setTotalAmount(orderMoney.setScale(2, RoundingMode.HALF_EVEN));
@@ -813,13 +788,22 @@
 		order.setShopId(shoppingCartPayment.getShopId());
 		order.setDelFlag(0);
 		order.setCreateTime(LocalDateTime.now());
-		
+		order.setExpressPayMethod(shoppingCartPayment.getFreightPaymentType());
+
 		if(2 == shoppingCartPayment.getPaymentType()){
 			BigDecimal balance = appUser.getBalance();
-			if(balance.compareTo(paymentMoney.add(expressFee)) < 0){
+			if(balance.compareTo(paymentMoney) < 0){
 				return R.fail("账户余额不足");
 			}
 		}
+		//判断运费支付是否足够
+		if(null != shoppingCartPayment.getFreightPaymentType() && 2 == shoppingCartPayment.getFreightPaymentType() && expressFee.compareTo(BigDecimal.ZERO) > 0){
+			BigDecimal balance = appUser.getBalance();
+			if(balance.compareTo(expressFee) < 0){
+				return R.fail("账户余额不足");
+			}
+		}
+
 		orderService.save(order);
 		//构建订单明细数据
 		for (MyShoppingCartVo myShoppingCartVo : goodsList) {
@@ -835,10 +819,11 @@
 			}
 			for (int i = 0; i < objects.size(); i++) {
 				Long id = objects.getJSONObject(i).getLong("id");
-				if(myShoppingCartVo.getId().equals(id)){
+				if(myShoppingCartVo.getId().equals(id.toString())){
+					ShoppingCart shoppingCart = this.getById(id);
 					Integer num1 = objects.getJSONObject(i).getInteger("num");
 					orderGood.setNum(num1);
-					Goods goods1 = goodsClient.getGoodsById(myShoppingCartVo.getGoodsId()).getData();
+					Goods goods1 = goodsClient.getGoodsById(shoppingCart.getGoodsId()).getData();
 					orderGood.setGoodJson(JSON.toJSONString(goods1));
 					break;
 				}
@@ -858,15 +843,15 @@
 			orderGood.setBoundShopSuperiorsPoints(myShoppingCartVo.getBoundShopSuperiorsPoints());
 			orderGoodService.save(orderGood);
 		}
-		
+
 		//开始构建支付数据
 		//现金支付
 		paymentMoney = paymentMoney.add(expressFee).setScale(2, RoundingMode.HALF_EVEN);
 		if(1 == shoppingCartPayment.getPaymentType()){
 			//调起微信支付 TODO 待完善
-			
-			
-			
+
+
+
 		}
 		//账户余额
 		if(2 == shoppingCartPayment.getPaymentType()){
@@ -900,7 +885,8 @@
 			if(earnPoint > 0){
 				appUser.setShopPoint(appUser.getShopPoint() + earnPoint);
 				appUser.setLavePoint(appUser.getLavePoint() + earnPoint);
-				
+				appUser.setTotalPoint(appUser.getTotalPoint() + earnPoint);
+
 				UserPoint userPoint = new UserPoint();
 				userPoint.setType(1);
 				userPoint.setHistoricalPoint(appUser.getLavePoint() - earnPoint);
@@ -911,6 +897,8 @@
 				userPoint.setObjectId(order.getId());
 				userPointClient.saveUserPoint(userPoint);
 			}
+			appUser.setShopAmount(appUser.getShopAmount().add(paymentMoney).setScale(2, RoundingMode.HALF_EVEN));
+			appUser.setLastShopTime(LocalDateTime.now());
 			appUserClient.editAppUserById(appUser);
 			//构建余额明细变动记录
 			BalanceChangeRecord balanceChangeRecord = new BalanceChangeRecord();
@@ -925,9 +913,14 @@
 			balanceChangeRecordClient.saveBalanceChangeRecord(balanceChangeRecord);
 			//修改订支付状态
 			order.setPayStatus(2);
+			if(goods.getType() == 2 && null == shoppingCartPayment.getUserAddressId()){
+				order.setOrderStatus(2);
+			}
 			orderService.updateById(order);
 			//删除购物车数据
 			this.removeBatchByIds(ids);
+			//检查消费积分满足后升级会员
+			vipUpgrade(appUser);
 		}
 		//积分支付
 		if(3 == shoppingCartPayment.getPaymentType()){
@@ -935,7 +928,13 @@
 			appUser.setLavePoint(lavePoint - orderPoint);
 			appUser.setShopPoint(appUser.getShopPoint() + earnPoint);
 			appUser.setLavePoint(appUser.getLavePoint() + earnPoint);
+			appUser.setTotalPoint(appUser.getTotalPoint() + earnPoint);
+			appUser.setShopAmount(appUser.getShopAmount().add(paymentMoney).setScale(2, RoundingMode.HALF_EVEN));
+			appUser.setLastShopTime(LocalDateTime.now());
 			appUserClient.editAppUserById(appUser);
+			//检查消费积分满足后升级会员
+			vipUpgrade(appUser);
+
 			//构建积分流水记录
 			UserPoint userPoint = new UserPoint();
 			userPoint.setType(4);
@@ -957,16 +956,75 @@
 				userPoint.setObjectId(order.getId());
 				userPointClient.saveUserPoint(userPoint);
 			}
-			//修改订支付状态
-			order.setPayStatus(2);
-			orderService.updateById(order);
-			//删除购物车数据
-			this.removeBatchByIds(ids);
+			//如果有运费,需要先扣除账户积分,再进行支付。支付成功后修改订单状态,未支付成功则回退积分,删除的订单
+			if(expressFee.compareTo(BigDecimal.ZERO) > 0){
+				if(shoppingCartPayment.getFreightPaymentType() == 1){
+					//调起微信支付
+				}
+				if(shoppingCartPayment.getFreightPaymentType() == 2){
+					BigDecimal totalRedPacketAmount = appUser.getTotalRedPacketAmount();
+					BigDecimal totalDistributionAmount = appUser.getTotalDistributionAmount();
+					BigDecimal balance = appUser.getBalance();
+					if(expressFee.compareTo(totalRedPacketAmount) <= 0){
+						totalRedPacketAmount = totalRedPacketAmount.subtract(expressFee);
+						balance = balance.subtract(expressFee);
+						appUser.setTotalRedPacketAmount(totalRedPacketAmount);
+						appUser.setBalance(balance);
+					}else{
+						expressFee = expressFee.subtract(totalRedPacketAmount);
+						totalRedPacketAmount = BigDecimal.ZERO;
+						if(expressFee.compareTo(totalDistributionAmount) <= 0){
+							totalDistributionAmount = totalDistributionAmount.subtract(expressFee);
+							balance = balance.subtract(expressFee);
+							appUser.setTotalRedPacketAmount(totalRedPacketAmount);
+							appUser.setTotalDistributionAmount(totalDistributionAmount);
+							appUser.setBalance(balance);
+						}else{
+							expressFee = expressFee.subtract(totalDistributionAmount);
+							totalDistributionAmount = BigDecimal.ZERO;
+							balance = balance.subtract(expressFee);
+							appUser.setTotalRedPacketAmount(totalRedPacketAmount);
+							appUser.setTotalDistributionAmount(totalDistributionAmount);
+							appUser.setBalance(balance);
+						}
+					}
+
+					appUserClient.editAppUserById(appUser);
+					//构建余额明细变动记录
+					BalanceChangeRecord balanceChangeRecord = new BalanceChangeRecord();
+					balanceChangeRecord.setAppUserId(appUser.getId());
+					balanceChangeRecord.setOrderId(order.getId());
+					balanceChangeRecord.setChangeType(5);
+					balanceChangeRecord.setBeforeAmount(balance.add(paymentMoney));
+					balanceChangeRecord.setChangeAmount(paymentMoney);
+					balanceChangeRecord.setAfterAmount(balance);
+					balanceChangeRecord.setDelFlag(0);
+					balanceChangeRecord.setCreateTime(LocalDateTime.now());
+					balanceChangeRecordClient.saveBalanceChangeRecord(balanceChangeRecord);
+					//修改订支付状态
+					order.setPayStatus(2);
+					if(goods.getType() == 2 && null == shoppingCartPayment.getUserAddressId()){
+						order.setOrderStatus(2);
+					}
+					orderService.updateById(order);
+					//删除购物车数据
+					this.removeBatchByIds(ids);
+				}
+			}else{
+				//修改订支付状态
+				order.setPayStatus(2);
+				if(goods.getType() == 2 && null == shoppingCartPayment.getUserAddressId()){
+					order.setOrderStatus(2);
+				}
+				orderService.updateById(order);
+				//删除购物车数据
+				this.removeBatchByIds(ids);
+			}
 		}
-		return R.ok();
+		return R.ok(order.getId().toString());
 	}
-	
-	
+
+
 	public String getNumber(Integer size){
 		String str = "";
 		for (Integer i = 0; i < size; i++) {
@@ -974,4 +1032,49 @@
 		}
 		return str;
 	}
+
+
+	/**
+	 * 会员等级变化
+	 * @param appUser
+	 */
+	public void vipUpgrade(AppUser appUser){
+		Integer shopPoint = appUser.getShopPoint();
+		Integer vipId = appUser.getVipId();
+		//钻石会员
+		VipSetting vipSetting3 = vipSettingClient.getVipSetting(3).getData();
+		Integer vipLevelUpShopRole = vipSetting3.getVipLevelUpShopRole();
+		Integer vipLevelUpShop = vipSetting3.getVipLevelUpShop();
+		if(null != vipLevelUpShopRole && 1 == vipLevelUpShopRole && appUser.getVipId() < 3 && shopPoint >= vipLevelUpShop){
+			appUser.setVipId(3);
+			appUserClient.editAppUserById(appUser);
+			//添加等级变化记录
+			UserChangeLog userChangeLog = new UserChangeLog();
+			userChangeLog.setDelFlag(0);
+			userChangeLog.setCreateTime(LocalDateTime.now());
+			userChangeLog.setAppUserId(appUser.getId());
+			userChangeLog.setBeforeVipId(vipId);
+			userChangeLog.setAfterVipId(appUser.getVipId());
+			userChangeLog.setChangeType(1);
+			userChangeLogClient.saveUserChangeLog(userChangeLog);
+			return;
+		}
+		//黄金会员
+		VipSetting vipSetting2 = vipSettingClient.getVipSetting(2).getData();
+		vipLevelUpShopRole = vipSetting2.getVipLevelUpShopRole();
+		vipLevelUpShop = vipSetting2.getVipLevelUpShop();
+		if(null != vipLevelUpShopRole && 1 == vipLevelUpShopRole && appUser.getVipId() < 2 && shopPoint >= vipLevelUpShop){
+			appUser.setVipId(2);
+			appUserClient.editAppUserById(appUser);
+			//添加等级变化记录
+			UserChangeLog userChangeLog = new UserChangeLog();
+			userChangeLog.setDelFlag(0);
+			userChangeLog.setCreateTime(LocalDateTime.now());
+			userChangeLog.setAppUserId(appUser.getId());
+			userChangeLog.setBeforeVipId(vipId);
+			userChangeLog.setAfterVipId(appUser.getVipId());
+			userChangeLog.setChangeType(1);
+			userChangeLogClient.saveUserChangeLog(userChangeLog);
+		}
+	}
 }

--
Gitblit v1.7.1