From 74b2581c8a9be309ca6454da3ff2c56501368771 Mon Sep 17 00:00:00 2001
From: Pu Zhibing <393733352@qq.com>
Date: 星期四, 09 一月 2025 09:30:21 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/ShoppingCartServiceImpl.java |  427 +++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 298 insertions(+), 129 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 3c5614f..36d8d92 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
@@ -142,7 +142,8 @@
 		}
 		List<Integer> 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<ShoppingCart> list = this.list(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getAppUserId, userid)
+				.in(ShoppingCart::getGoodsId, goodsIds).eq(ShoppingCart::getStatus, 1));
 		//构建返回数据
 		List<MyShoppingCartVo> page = buildDetail(appUser, shopId, list, null);
 		return page;
@@ -156,7 +157,7 @@
 	 * @param shopId
 	 * @return
 	 */
-	public Price getPrice(AppUser appUser, Integer goodsId, Integer shopId){
+	public Price getPrice(AppUser appUser, Integer goodsId, Integer type, Integer shopId){
 		//获取支付价格
 		//秒杀活动>门店特价>地区价格>会员价格
 		//判断是否有秒杀活动
@@ -165,7 +166,8 @@
 		info.setGoodsId(goodsId);
 		info.setVip(appUser.getVipId());
 		GoodsSeckill goodsSeckill = seckillActivityInfoClient.getSeckillActivityInfo(info).getData();
-		if(null == goodsSeckill){
+		//没有秒杀活动或者添加的普通商品则不使用秒杀活动价格
+		if(null == goodsSeckill || type == 1){
 			//没有秒杀价,则判断门店特价
 			GetGoodsBargainPrice goodsBargainPrice = new GetGoodsBargainPrice();
 			goodsBargainPrice.setGoodsId(goodsId);
@@ -238,18 +240,34 @@
 				area.setProvinceCode(appUser.getProvinceCode());
 				area.setVip(appUser.getVipId());
 				GoodsArea goodsArea = goodsAreaClient.getGoodsArea(area).getData();
-				price.setEarnSpendingPoints(goodsArea.getEarnSpendingPoints());
-				price.setSuperiorSubcommission(goodsArea.getSuperiorSubcommission());
-				price.setSuperiorRebatePoints(goodsArea.getSuperiorRebatePoints());
-				price.setSuperiorType(goodsArea.getSuperiorType());
-				price.setSuperiorPriceType(goodsArea.getSuperiorPriceType());
-				price.setServuceShopCharges(goodsArea.getServuceShopCharges());
-				price.setServuceShopPoints(goodsArea.getServuceShopPoints());
-				price.setTechnicianPoints(goodsArea.getTechnicianPoints());
-				price.setBoundShopCharges(goodsArea.getBoundShopCharges());
-				price.setBoundShopPoints(goodsArea.getBoundShopPoints());
-				price.setBoundShopSuperiorsCharges(goodsArea.getBoundShopSuperiorsCharges());
-				price.setBoundShopSuperiorsPoints(goodsArea.getBoundShopSuperiorsPoints());
+				if(null != goodsArea){
+					price.setEarnSpendingPoints(goodsArea.getEarnSpendingPoints());
+					price.setSuperiorSubcommission(goodsArea.getSuperiorSubcommission());
+					price.setSuperiorRebatePoints(goodsArea.getSuperiorRebatePoints());
+					price.setSuperiorType(goodsArea.getSuperiorType());
+					price.setSuperiorPriceType(goodsArea.getSuperiorPriceType());
+					price.setServuceShopCharges(goodsArea.getServuceShopCharges());
+					price.setServuceShopPoints(goodsArea.getServuceShopPoints());
+					price.setTechnicianPoints(goodsArea.getTechnicianPoints());
+					price.setBoundShopCharges(goodsArea.getBoundShopCharges());
+					price.setBoundShopPoints(goodsArea.getBoundShopPoints());
+					price.setBoundShopSuperiorsCharges(goodsArea.getBoundShopSuperiorsCharges());
+					price.setBoundShopSuperiorsPoints(goodsArea.getBoundShopSuperiorsPoints());
+				}else{
+					GoodsVip goodsVip = goodsVipClient.getGoodsVip(goodsId, appUser.getVipId()).getData();
+					price.setEarnSpendingPoints(goodsVip.getEarnSpendingPoints());
+					price.setSuperiorSubcommission(goodsVip.getSuperiorSubcommission());
+					price.setSuperiorRebatePoints(goodsVip.getSuperiorRebatePoints());
+					price.setSuperiorType(goodsVip.getSuperiorType());
+					price.setSuperiorPriceType(goodsVip.getSuperiorPriceType());
+					price.setServuceShopCharges(goodsVip.getServuceShopCharges());
+					price.setServuceShopPoints(goodsVip.getServuceShopPoints());
+					price.setTechnicianPoints(goodsVip.getTechnicianPoints());
+					price.setBoundShopCharges(goodsVip.getBoundShopCharges());
+					price.setBoundShopPoints(goodsVip.getBoundShopPoints());
+					price.setBoundShopSuperiorsCharges(goodsVip.getBoundShopSuperiorsCharges());
+					price.setBoundShopSuperiorsPoints(goodsVip.getBoundShopSuperiorsPoints());
+				}
 			}
 		}else{
 			//构建价格数据
@@ -288,9 +306,18 @@
 	@Override
 	public Long addGoods(ShoppingCart shoppingCart) {
 		Long userid = tokenService.getLoginUserApplet().getUserid();
-		shoppingCart.setAppUserId(userid);
-		this.save(shoppingCart);
-		return shoppingCart.getId();
+		ShoppingCart one = this.getOne(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getAppUserId, userid)
+				.eq(ShoppingCart::getGoodsId, shoppingCart.getGoodsId()).eq(ShoppingCart::getType, shoppingCart.getType()).eq(ShoppingCart::getStatus, 1));
+		if(null != one){
+			one.setNumber(one.getNumber() + shoppingCart.getNumber());
+			this.updateById(one);
+			return one.getId();
+		}else{
+			shoppingCart.setAppUserId(userid);
+			shoppingCart.setStatus(1);
+			this.save(shoppingCart);
+			return shoppingCart.getId();
+		}
 	}
 
 
@@ -336,12 +363,15 @@
 			JSONArray objects = JSON.parseArray(goodsJson);
 			Long id = objects.getJSONObject(0).getLong("id");
 			Integer num1 = objects.getJSONObject(0).getInteger("num");
+			Integer type = objects.getJSONObject(0).getInteger("type");
 			ShoppingCart shoppingCart = new ShoppingCart();
 			shoppingCart.setAppUserId(userid);
 			shoppingCart.setGoodsId(id.intValue());
 			shoppingCart.setNumber(num1);
-			Long shoppingCarId = addGoods(shoppingCart);
-			confirmOrder.setGoodsJson("[{\"id\": " + shoppingCarId + ", \"num\": " + num1 + "}]");
+			shoppingCart.setType(type);
+			shoppingCart.setStatus(0);
+			this.save(shoppingCart);
+			confirmOrder.setGoodsJson("[{\"id\": " + shoppingCart.getId() + ", \"num\": " + num1 + ",\"type\":" + type + "}]");
 		}
 		AppUser appUser = appUserClient.getAppUserById(userid);
 		Integer shopId = confirmOrder.getShopId();
@@ -362,22 +392,110 @@
 		confirmOrderVo.setShopName(shop.getName());
 		//现金支付
 		if(confirmOrder.getPaymentType() == 1){
-			BigDecimal bigDecimal = goodsList.stream().map(MyShoppingCartVo::getCash).reduce(BigDecimal::add).get();
+			BigDecimal bigDecimal = BigDecimal.ZERO;
+			for (MyShoppingCartVo myShoppingCartVo : goodsList) {
+				bigDecimal = bigDecimal.add(myShoppingCartVo.getCash().multiply(new BigDecimal(myShoppingCartVo.getNumber())));
+			}
 			confirmOrderVo.setOrderMoney(bigDecimal);
 		}else{
-			int sum = goodsList.stream().mapToInt(MyShoppingCartVo::getPoint).sum();
+			int sum = 0;
+			for (MyShoppingCartVo myShoppingCartVo : goodsList) {
+				sum += (myShoppingCartVo.getPoint() * myShoppingCartVo.getNumber());
+			}
 			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));
+		//总优惠金额
+		BigDecimal activityAmount = BigDecimal.ZERO;
+
+		//减去优惠券优惠金额
+		CouponInfoVo couponInfoVo = null;
+		if(null != confirmOrder.getCouponId() && 2 != confirmOrder.getPaymentType()){
+			couponInfoVo = userCouponClient.getCouponInfo(confirmOrder.getCouponId()).getData();
+			String forGoodIds = couponInfoVo.getForGoodIds();
+			String[] split = forGoodIds.split(",");
+			List<String> parseArray = Arrays.asList(split);
+			//全部商品
+			if("-1".equals(forGoodIds)){
+				//满减
+				if(1 == couponInfoVo.getCouponType() && couponInfoVo.getConditionAmount().compareTo(paymentMoney) <= 0){
+					paymentMoney = paymentMoney.subtract(couponInfoVo.getDiscountAmount());
+					activityAmount = activityAmount.add(couponInfoVo.getDiscountAmount());
+				}
+				//代金券
+				if(2 == couponInfoVo.getCouponType()){
+					paymentMoney = paymentMoney.subtract(couponInfoVo.getMoneyAmount());
+					activityAmount = activityAmount.add(couponInfoVo.getMoneyAmount());
+					if(paymentMoney.compareTo(BigDecimal.ZERO) < 0){
+						paymentMoney = BigDecimal.ZERO;
+					}
+				}
+				//折扣券
+				if(3 == couponInfoVo.getCouponType()){
+					BigDecimal paymentMoney1 = couponInfoVo.getDiscount().divide(new BigDecimal(10)).multiply(paymentMoney);
+					BigDecimal bigDecimal = paymentMoney.subtract(paymentMoney1).setScale(2, RoundingMode.HALF_EVEN);
+					paymentMoney = paymentMoney1;
+					activityAmount = activityAmount.add(bigDecimal);
+				}
+			}else{
+				//部分商品,需要计算参与优惠商品的支付金额,然后再对商品进行优惠券处理
+				paymentMoney = BigDecimal.ZERO;
+				BigDecimal goodsMoney = BigDecimal.ZERO;
+				for (MyShoppingCartVo myShoppingCartVo : goodsList) {
+					String goodsId = myShoppingCartVo.getGoodsId().toString();
+					BigDecimal cash = myShoppingCartVo.getCash();
+					if(parseArray.contains(goodsId)){
+						goodsMoney = goodsMoney.add(cash);
+					}else{
+						paymentMoney = paymentMoney.add(cash);
+					}
+				}
+
+				//满减
+				if(1 == couponInfoVo.getCouponType() && couponInfoVo.getConditionAmount().compareTo(goodsMoney) <= 0){
+					goodsMoney = goodsMoney.subtract(couponInfoVo.getDiscountAmount());
+					activityAmount = activityAmount.add(couponInfoVo.getDiscountAmount());
+				}
+				//代金券
+				if(2 == couponInfoVo.getCouponType()){
+					goodsMoney = goodsMoney.subtract(couponInfoVo.getMoneyAmount());
+					activityAmount = activityAmount.add(couponInfoVo.getMoneyAmount());
+					if(goodsMoney.compareTo(BigDecimal.ZERO) < 0){
+						goodsMoney = BigDecimal.ZERO;
+					}
+				}
+				//折扣券
+				if(3 == couponInfoVo.getCouponType()){
+					BigDecimal paymentMoney1 = couponInfoVo.getDiscount().divide(new BigDecimal(10)).multiply(goodsMoney);
+					BigDecimal bigDecimal = goodsMoney.subtract(paymentMoney1).setScale(2, RoundingMode.HALF_EVEN);
+					goodsMoney = paymentMoney1;
+					activityAmount = activityAmount.add(bigDecimal);
+				}
+				paymentMoney = paymentMoney.add(goodsMoney);
+			}
 		}
+
+		//查询当前是否有订单活动
+		List<OrderActivityInfo> orderActivityInfo = orderActivityInfoClient.getNowOrderActivityInfo(appUser.getVipId()).getData();
+		//满XX才打折,只有现金才能优惠
+		if(null != orderActivityInfo && confirmOrder.getPaymentType() == 1){
+			for (OrderActivityInfo activityInfo : orderActivityInfo) {
+				if(activityInfo.getConditionAmount().compareTo(paymentMoney) <= 0){
+					confirmOrderVo.setActivityName(activityInfo.getActivityName());
+					//优惠后的支付金额
+					BigDecimal multiply = activityInfo.getDiscount().divide(new BigDecimal(10)).multiply(paymentMoney);
+					//优惠金额
+					BigDecimal bigDecimal = paymentMoney.subtract(multiply).setScale(2, RoundingMode.HALF_EVEN);
+					paymentMoney = multiply;
+					activityAmount = activityAmount.add(bigDecimal);
+					break;
+				}
+			}
+
+		}
+		confirmOrderVo.setDiscountAmount(activityAmount);
 		BaseSetting baseSetting = baseSettingClient.getBaseSetting(4).getData();
 		confirmOrderVo.setUseSimultaneously(JSON.parseObject(baseSetting.getContent()).getInteger("status") == 1);
 		int earnPoint = goodsList.stream().mapToInt(MyShoppingCartVo::getEarnSpendingPoints).sum();
@@ -474,6 +592,7 @@
 			Goods goods = goodsClient.getGoodsById(shoppingCart.getGoodsId()).getData();
 			MyShoppingCartVo vo = new MyShoppingCartVo();
 			vo.setId(shoppingCart.getId().toString());
+			vo.setType(shoppingCart.getType());
 			vo.setGoodsId(goods.getId());
 			vo.setHomePicture(goods.getHomePagePicture());
 			vo.setName(goods.getName());
@@ -488,7 +607,7 @@
 				}
 			}
 			//获取支付价格
-			Price price = getPrice(appUser, shoppingCart.getGoodsId(), shopId);
+			Price price = getPrice(appUser, shoppingCart.getGoodsId(), shoppingCart.getType(), shopId);
 			if(null == price){
 				price = new Price();
 				//使用商品的基础价格
@@ -512,6 +631,7 @@
 			//判断当前数量是否已经超出限购数量(需要计算已经购买的数量)
 			if(null == goods.getPurchaseLimit() || -1 == goods.getPurchaseLimit()){
 				vo.setPurchaseLimit(false);
+				vo.setPurchaseLimitNum(-1);
 			}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());
@@ -560,21 +680,26 @@
 		for (int i = 0; i < objects.size(); i++) {
 			Long id = objects.getJSONObject(i).getLong("id");
 			Integer num1 = objects.getJSONObject(i).getInteger("num");
+			Integer type = objects.getJSONObject(i).getInteger("type");
 			num += num1;
 			ShoppingCart shoppingCart = this.getById(id);
 			//判断当前数量是否已经超出限购数量(需要计算已经购买的数量)
+			Integer goodsSaleNum = orderService.getGoodsSaleNum(shoppingCart.getGoodsId(), type);
 			Goods goods = goodsClient.getGoodsById(shoppingCart.getGoodsId()).getData();
-			if(null != goods.getPurchaseLimit() && -1 != goods.getPurchaseLimit()){
-				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();
-				}
-				if((num1 + sum) > goods.getPurchaseLimit()){
+			if(1 == type){
+				if(null != goods.getPurchaseLimit() && -1 != goods.getPurchaseLimit() && (goodsSaleNum + num1) > goods.getPurchaseLimit()){
 					return R.fail(goods.getName() + "已超出购买上限");
+				}
+			}else{
+				GetSeckillActivityInfo info = new GetSeckillActivityInfo();
+				info.setGoodsId(shoppingCart.getGoodsId());
+				info.setVip(appUser.getVipId());
+				GoodsSeckill goodsSeckill = seckillActivityInfoClient.getSeckillActivityInfo(info).getData();
+				if(null != goodsSeckill ){
+					SeckillActivityInfo activityInfo = seckillActivityInfoClient.getSeckillActivityInfoById(goodsSeckill.getSeckillActivityInfoId()).getData();
+					if(null != activityInfo.getMaxNum() && -1 != activityInfo.getMaxNum() && (goodsSaleNum + num1) > activityInfo.getMaxNum()){
+						return R.fail(goods.getName() + "已超出秒杀活动购买上限");
+					}
 				}
 			}
 			ids.add(id);
@@ -719,20 +844,27 @@
 		}
 
 		//查询当前是否有订单活动
-		OrderActivityInfo orderActivityInfo = orderActivityInfoClient.getNowOrderActivityInfo(appUser.getVipId()).getData();
+		List<OrderActivityInfo> orderActivityInfo = orderActivityInfoClient.getNowOrderActivityInfo(appUser.getVipId()).getData();
 		BaseSetting baseSetting = baseSettingClient.getBaseSetting(4).getData();
 		//系统活动设置(优惠券和活动能否同时使用)
-		boolean useSimultaneously = baseSetting.getContent().equals("1");
+		Integer status = JSON.parseObject(baseSetting.getContent()).getInteger("status");
 		//满XX才打折,只有现金才能优惠
 		//如果使用优惠券,则需要判断是否可以和同时使用,且活动满足使用条件。
 		//没有使用优惠券,只需要判断是都满足使用条件
-		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);
-			discount = orderActivityInfo.getDiscount();
-			paymentMoney = paymentMoney1;
-			activityAmount = activityAmount.add(bigDecimal);
+		OrderActivityInfo orderActivityInfo1 = null;
+		if((1 == status || null == shoppingCartPayment.getUserCouponId()) &&
+				null != orderActivityInfo && shoppingCartPayment.getPaymentType() != 3){
+			for (OrderActivityInfo activityInfo : orderActivityInfo) {
+				if(activityInfo.getConditionAmount().compareTo(paymentMoney) <= 0){
+					BigDecimal paymentMoney1 = activityInfo.getDiscount().divide(new BigDecimal(10)).multiply(paymentMoney);
+					BigDecimal bigDecimal = paymentMoney.subtract(paymentMoney1).setScale(2, RoundingMode.HALF_EVEN);
+					discount = activityInfo.getDiscount();
+					paymentMoney = paymentMoney1;
+					activityAmount = activityAmount.add(bigDecimal);
+					orderActivityInfo1 = activityInfo;
+					break;
+				}
+			}
 		}
 
 		//可获得的消费积分
@@ -799,7 +931,7 @@
 			order.setCouponJson(JSON.toJSONString(couponInfoVo));
 		}
 		if(activityAmount.compareTo(BigDecimal.ZERO) > 0){
-			order.setActivityJson(JSON.toJSONString(orderActivityInfo));
+			order.setActivityJson(JSON.toJSONString(orderActivityInfo1));
 		}
 		if(null != shoppingCartPayment.getUserAddressId()){
 			UserAddress address = userAddressClient.getUserAddressById(shoppingCartPayment.getUserAddressId()).getData();
@@ -839,19 +971,23 @@
 			OrderGood orderGood = new OrderGood();
 			orderGood.setGoodsId(myShoppingCartVo.getGoodsId());
 			orderGood.setOrderId(order.getId());
-			GetSeckillActivityInfo info = new GetSeckillActivityInfo();
-			info.setGoodsId(myShoppingCartVo.getGoodsId());
-			info.setVip(appUser.getVipId());
-			GoodsSeckill goodsSeckill = seckillActivityInfoClient.getSeckillActivityInfo(info).getData();
-			if(null != goodsSeckill){
-				orderGood.setSeckillJson(JSON.toJSONString(goodsSeckill));
-			}
 			for (int i = 0; i < objects.size(); i++) {
 				Long id = objects.getJSONObject(i).getLong("id");
 				if(myShoppingCartVo.getId().equals(id.toString())){
 					ShoppingCart shoppingCart = this.getById(id);
 					Integer num1 = objects.getJSONObject(i).getInteger("num");
+					Integer type = objects.getJSONObject(i).getInteger("type");
 					orderGood.setNum(num1);
+					orderGood.setType(type);
+					if(2 == type){
+						GetSeckillActivityInfo info = new GetSeckillActivityInfo();
+						info.setGoodsId(myShoppingCartVo.getGoodsId());
+						info.setVip(appUser.getVipId());
+						GoodsSeckill goodsSeckill = seckillActivityInfoClient.getSeckillActivityInfo(info).getData();
+						if(null != goodsSeckill){
+							orderGood.setSeckillJson(JSON.toJSONString(goodsSeckill));
+						}
+					}
 					Goods goods1 = goodsClient.getGoodsById(shoppingCart.getGoodsId()).getData();
 					orderGood.setGoodJson(JSON.toJSONString(goods1));
 					break;
@@ -881,15 +1017,17 @@
 			//调起微信支付
 			String goodsNames = goodsList.stream().map(MyShoppingCartVo::getName).collect(Collectors.joining("\n"));
 			UniPayResult uniPayResult = PaymentUtil.uniPay(order.getOrderNumber(), paymentMoney.doubleValue(), order.getOrderType() == 1 ? "购买服务商品" : "购买单品商品",
-					goodsNames, "", "/order/shopping-cart/shoppingCartPaymentCallback", appUser.getWxOpenid(), "");
+					goodsNames, "", "/order/shopping-cart/shoppingCartPaymentCallback", appUser.getWxOpenid(), null);
 			if(null == uniPayResult || !"100".equals(uniPayResult.getRa_Code())){
 				return R.fail(null == uniPayResult ? "支付失败" : uniPayResult.getRb_CodeMsg());
 			}
 			String rc_result = uniPayResult.getRc_Result();
+			JSONObject jsonObject = JSON.parseObject(rc_result);
+			jsonObject.put("orderId", order.getId().toString());
 			//将支付数据添加到redis队列中,便于定时任务去校验是否完成支付,没有完成支付支付,15分钟后关闭订单。
 			long second = LocalDateTime.now().plusMinutes(15).toEpochSecond(ZoneOffset.UTC);
 			redisTemplate.opsForZSet().add("OrderPayment", order.getOrderNumber(), second);
-			return R.ok(rc_result);
+			return R.ok(jsonObject.toJSONString());
 		}
 		//账户余额
 		BigDecimal redPacketAmount = BigDecimal.ZERO;
@@ -942,15 +1080,17 @@
 					appUser.setTransferablePoint(appUser.getTransferablePoint() + earnPoint1);
 				}
 
-				UserPoint userPoint = new UserPoint();
-				userPoint.setType(1);
-				userPoint.setHistoricalPoint(appUser.getLavePoint() - earnPoint);
-				userPoint.setVariablePoint(earnPoint);
-				userPoint.setBalance(appUser.getLavePoint());
-				userPoint.setCreateTime(LocalDateTime.now());
-				userPoint.setAppUserId(appUser.getId());
-				userPoint.setObjectId(order.getId());
-				userPointClient.saveUserPoint(userPoint);
+				if(earnPoint > 0){
+					UserPoint userPoint = new UserPoint();
+					userPoint.setType(1);
+					userPoint.setHistoricalPoint(appUser.getLavePoint() - earnPoint);
+					userPoint.setVariablePoint(earnPoint);
+					userPoint.setBalance(appUser.getLavePoint());
+					userPoint.setCreateTime(LocalDateTime.now());
+					userPoint.setAppUserId(appUser.getId());
+					userPoint.setObjectId(order.getId());
+					userPointClient.saveUserPoint(userPoint);
+				}
 			}
 			appUser.setShopAmount(appUser.getShopAmount().add(paymentMoney).setScale(2, RoundingMode.HALF_EVEN));
 			appUser.setLastShopTime(LocalDateTime.now());
@@ -983,30 +1123,53 @@
 			if(expressFee.compareTo(BigDecimal.ZERO) > 0){
 				if(shoppingCartPayment.getFreightPaymentType() == 1){
 					//调起微信支付
-					UniPayResult uniPayResult = PaymentUtil.uniPay(order.getOrderNumber() + appUser.getId(), expressFee.doubleValue(), order.getOrderType() == 1 ? "购买服务商品快递费" : "购买单品商品快递费",
-							"快递费", "", "/order/shopping-cart/shoppingCartMaterialFlowPaymentCallback", appUser.getWxOpenid(), "");
+					UniPayResult uniPayResult = PaymentUtil.uniPay("K" + order.getOrderNumber(), expressFee.doubleValue(), order.getOrderType() == 1 ? "购买服务商品快递费" : "购买单品商品快递费",
+							"快递费", "", "/order/shopping-cart/shoppingCartMaterialFlowPaymentCallback", appUser.getWxOpenid(), null);
 					if(null == uniPayResult || !"100".equals(uniPayResult.getRa_Code())){
 						return R.fail(null == uniPayResult ? "支付失败" : uniPayResult.getRb_CodeMsg());
 					}
 					String rc_result = uniPayResult.getRc_Result();
+					JSONObject jsonObject = JSON.parseObject(rc_result);
+					jsonObject.put("orderId", order.getId().toString());
 					//将支付数据添加到redis队列中,便于定时任务去校验是否完成支付,没有完成支付支付,15分钟后关闭订单。
 					long second = LocalDateTime.now().plusMinutes(15).toEpochSecond(ZoneOffset.UTC);
-					redisTemplate.opsForZSet().add("MaterialFlowPayment", order.getOrderNumber() + appUser.getId(), second);
-					return R.ok(rc_result);
+					redisTemplate.opsForZSet().add("MaterialFlowPayment", "K" + order.getOrderNumber(), second);
+					return R.ok(jsonObject.toJSONString());
 				}
 			}
-			
+
 			Integer lavePoint = appUser.getLavePoint();
+			//扣减订单支付积分
+			appUser.setLavePoint(appUser.getLavePoint() - orderPoint);
+			appUser.setAvailablePoint(appUser.getAvailablePoint() - orderPoint);
+			//可转增积分
+			Integer transferablePoint = appUser.getTransferablePoint();
+			Integer tra = 0;
+			if(transferablePoint > 0){
+				tra = transferablePoint - orderPoint;
+				appUser.setTransferablePoint(tra >= 0 ? tra : 0);
+			}
+			appUser.setTransferablePoint(appUser.getTransferablePoint() - orderPoint);
+
+			//构建积分流水记录
+			UserPoint userPoint = new UserPoint();
+			userPoint.setType(11);
+			userPoint.setHistoricalPoint(lavePoint);
+			userPoint.setVariablePoint(orderPoint);
+			userPoint.setBalance(appUser.getLavePoint());
+			userPoint.setCreateTime(LocalDateTime.now());
+			userPoint.setAppUserId(appUser.getId());
+			userPoint.setObjectId(order.getId());
+			userPoint.setExtention((tra >= 0 ? orderPoint : transferablePoint) + "");
+			userPointClient.saveUserPoint(userPoint);
+
+			lavePoint = appUser.getLavePoint();
 			PointSetting pointSetting = pointSettingClient.getPointSetting(appUser.getVipId()).getData();
 			int earnPoint1 = 0;
 			//计算可用积分比例
 			if(null != pointSetting && 1 == pointSetting.getBuyPointOpen()){
 				earnPoint1 = new BigDecimal(earnPoint).multiply(pointSetting.getBuyPoint().divide(new BigDecimal(100))).intValue();
 			}
-			//扣减订单支付积分
-			appUser.setLavePoint(appUser.getLavePoint() - orderPoint);
-			appUser.setAvailablePoint(appUser.getAvailablePoint() - orderPoint);
-			
 			appUser.setShopPoint(appUser.getShopPoint() + earnPoint);
 			appUser.setLavePoint(appUser.getLavePoint() + earnPoint);
 			appUser.setAvailablePoint(appUser.getAvailablePoint() + earnPoint1);
@@ -1021,16 +1184,17 @@
 			appUserClient.vipUpgrade(appUser.getId());
 
 			//构建积分流水记录
-			UserPoint userPoint = new UserPoint();
-			userPoint.setType(1);
-			userPoint.setHistoricalPoint(lavePoint);
-			Integer point = appUser.getLavePoint() - lavePoint;
-			userPoint.setVariablePoint(point >= 0 ? point : point * -1);
-			userPoint.setBalance(appUser.getLavePoint());
-			userPoint.setCreateTime(LocalDateTime.now());
-			userPoint.setAppUserId(appUser.getId());
-			userPoint.setObjectId(order.getId());
-			userPointClient.saveUserPoint(userPoint);
+			if(earnPoint > 0){
+				userPoint = new UserPoint();
+				userPoint.setType(1);
+				userPoint.setHistoricalPoint(lavePoint);
+				userPoint.setVariablePoint(earnPoint);
+				userPoint.setBalance(appUser.getLavePoint());
+				userPoint.setCreateTime(LocalDateTime.now());
+				userPoint.setAppUserId(appUser.getId());
+				userPoint.setObjectId(order.getId());
+				userPointClient.saveUserPoint(userPoint);
+			}
 			//如果有运费,需要先扣除账户积分,再进行支付。支付成功后修改订单状态,未支付成功则回退积分,删除的订单
 			if(expressFee.compareTo(BigDecimal.ZERO) > 0){
 				if(shoppingCartPayment.getFreightPaymentType() == 2){
@@ -1145,15 +1309,17 @@
 			appUser.setAvailablePoint(appUser.getAvailablePoint() + earnPoint1);
 			appUser.setTotalAvailablePoint(appUser.getTotalAvailablePoint() + earnPoint1);
 			
-			UserPoint userPoint = new UserPoint();
-			userPoint.setType(1);
-			userPoint.setHistoricalPoint(appUser.getLavePoint() - earnPoint);
-			userPoint.setVariablePoint(earnPoint);
-			userPoint.setBalance(appUser.getLavePoint());
-			userPoint.setCreateTime(LocalDateTime.now());
-			userPoint.setAppUserId(appUser.getId());
-			userPoint.setObjectId(order.getId());
-			userPointClient.saveUserPoint(userPoint);
+			if(earnPoint > 0){
+				UserPoint userPoint = new UserPoint();
+				userPoint.setType(1);
+				userPoint.setHistoricalPoint(appUser.getLavePoint() - earnPoint);
+				userPoint.setVariablePoint(earnPoint);
+				userPoint.setBalance(appUser.getLavePoint());
+				userPoint.setCreateTime(LocalDateTime.now());
+				userPoint.setAppUserId(appUser.getId());
+				userPoint.setObjectId(order.getId());
+				userPointClient.saveUserPoint(userPoint);
+			}
 		}
 		appUser.setShopAmount(appUser.getShopAmount().add(paymentMoney).setScale(2, RoundingMode.HALF_EVEN));
 		appUser.setLastShopTime(LocalDateTime.now());
@@ -1184,7 +1350,7 @@
 	@Override
 	public R shoppingCartMaterialFlowPaymentCallback(UniPayCallbackResult uniPayCallbackResult) {
 		String r2_orderNo = uniPayCallbackResult.getR2_OrderNo();
-		r2_orderNo = r2_orderNo.substring(0, 23);
+		r2_orderNo = r2_orderNo.substring(1);
 		Order order = orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNumber, r2_orderNo));
 		if(null == order || order.getPayStatus() == 2){
 			return R.ok();
@@ -1192,17 +1358,12 @@
 		Integer earnPoint = order.getGetPoint();
 		AppUser appUser = appUserClient.getAppUserById(order.getAppUserId());
 		Integer lavePoint = appUser.getLavePoint();
-		Integer orderPoint = order.getPoint();
 		PointSetting pointSetting = pointSettingClient.getPointSetting(appUser.getVipId()).getData();
-		int earnPoint1 = earnPoint;
+		int earnPoint1 = 0;
 		//计算可用积分比例
 		if(null != pointSetting && 1 == pointSetting.getBuyPointOpen()){
-			earnPoint1 = new BigDecimal(earnPoint1).multiply(pointSetting.getBuyPoint().divide(new BigDecimal(100))).intValue();
+			earnPoint1 = new BigDecimal(earnPoint).multiply(pointSetting.getBuyPoint().divide(new BigDecimal(100))).intValue();
 		}
-		//扣减订单支付积分
-		appUser.setLavePoint(appUser.getLavePoint() - orderPoint);
-		appUser.setAvailablePoint(appUser.getAvailablePoint() - orderPoint);
-		
 		appUser.setShopPoint(appUser.getShopPoint() + earnPoint);
 		appUser.setLavePoint(appUser.getLavePoint() + earnPoint);
 		appUser.setAvailablePoint(appUser.getAvailablePoint() + earnPoint1);
@@ -1217,13 +1378,13 @@
 		UserPoint userPoint = new UserPoint();
 		userPoint.setType(1);
 		userPoint.setHistoricalPoint(lavePoint);
-		Integer point = appUser.getLavePoint() - lavePoint;
-		userPoint.setVariablePoint(point >= 0 ? point : point * -1);
+		userPoint.setVariablePoint(earnPoint);
 		userPoint.setBalance(appUser.getLavePoint());
 		userPoint.setCreateTime(LocalDateTime.now());
 		userPoint.setAppUserId(appUser.getId());
 		userPoint.setObjectId(order.getId());
 		userPointClient.saveUserPoint(userPoint);
+
 		//修改订支付状态
 		order.setPayStatus(2);
 		//自提
@@ -1248,34 +1409,42 @@
 		//订单支付数据
 		long second = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);
 		Set<String> orderPayment = redisTemplate.opsForZSet().range("OrderPayment", 0, second);
-		for (String code : orderPayment) {
-			Order order = orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNumber, code));
-			if(null == order || order.getPayStatus() != 1){
-				continue;
-			}
-			//开始执行关闭订单操作
-			CloseOrderResult closeOrderResult = PaymentUtil.closeOrder(code);
-			if((null == closeOrderResult || !closeOrderResult.getRa_Status().equals("100")) &&
-					Arrays.asList("0", "4", "101", "10080000", "10080002", "10083004", "10083005").contains(closeOrderResult.getRb_Code())){
-				redisTemplate.opsForZSet().add("OrderPayment", code, 0);
-				log.error("关闭订单失败:{}---->{}", code, JSON.toJSONString(closeOrderResult));
+		if(orderPayment.size() > 0){
+			List<Order> list = orderService.list(new LambdaQueryWrapper<Order>().in(Order::getOrderNumber, orderPayment));
+			for (Order order : list) {
+				if(null == order || order.getPayStatus() != 1){
+					redisTemplate.opsForZSet().remove("OrderPayment", order.getOrderNumber());
+					continue;
+				}
+				//开始执行关闭订单操作
+				CloseOrderResult closeOrderResult = PaymentUtil.closeOrder(order.getOrderNumber());
+				if((null == closeOrderResult || !closeOrderResult.getRa_Status().equals("100")) &&
+						Arrays.asList("0", "4", "101", "10080000", "10080002", "10083004", "10083005").contains(closeOrderResult.getRb_Code())){
+					redisTemplate.opsForZSet().add("OrderPayment", order.getOrderNumber(), 0);
+					log.error("关闭订单失败:{}---->{}", order.getOrderNumber(), JSON.toJSONString(closeOrderResult));
+				}
+				redisTemplate.opsForZSet().remove("OrderPayment", order.getOrderNumber());
 			}
 		}
 		
 		//快递支付
 		Set<String> materialFlowPayment = redisTemplate.opsForZSet().range("MaterialFlowPayment", 0, second);
-		for (String code : materialFlowPayment) {
-			code = code.substring(0, 23);
-			Order order = orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNumber, code));
-			if(null == order || order.getPayStatus() != 1){
-				continue;
-			}
-			//开始执行关闭订单操作
-			CloseOrderResult closeOrderResult = PaymentUtil.closeOrder(code);
-			if((null == closeOrderResult || !closeOrderResult.getRa_Status().equals("100")) &&
-					Arrays.asList("0", "4", "101", "10080000", "10080002", "10083004", "10083005").contains(closeOrderResult.getRb_Code())){
-				redisTemplate.opsForZSet().add("MaterialFlowPayment", code, 0);
-				log.error("关闭订单失败:{}---->{}", code, JSON.toJSONString(closeOrderResult));
+		if(materialFlowPayment.size() > 0){
+			materialFlowPayment.forEach(s->s.substring(1));
+			List<Order> list = orderService.list(new LambdaQueryWrapper<Order>().in(Order::getOrderNumber, materialFlowPayment));
+			for (Order order : list) {
+				if(null == order || order.getPayStatus() != 1){
+					redisTemplate.opsForZSet().remove("MaterialFlowPayment", order.getOrderNumber());
+					continue;
+				}
+				//开始执行关闭订单操作
+				CloseOrderResult closeOrderResult = PaymentUtil.closeOrder("K" + order.getOrderNumber());
+				if((null == closeOrderResult || !closeOrderResult.getRa_Status().equals("100")) &&
+						Arrays.asList("0", "4", "101", "10080000", "10080002", "10083004", "10083005").contains(closeOrderResult.getRb_Code())){
+					redisTemplate.opsForZSet().add("MaterialFlowPayment", order.getOrderNumber(), 0);
+					log.error("关闭订单失败:{}---->{}", order.getOrderNumber(), JSON.toJSONString(closeOrderResult));
+				}
+				redisTemplate.opsForZSet().remove("MaterialFlowPayment", order.getOrderNumber());
 			}
 		}
 	}

--
Gitblit v1.7.1