From 1690d4d06b0c20b3606e50e0844ef67e07947a0d Mon Sep 17 00:00:00 2001
From: phpcjl <phpcjl@gmail.com>
Date: 星期二, 10 十二月 2024 14:05:59 +0800
Subject: [PATCH] 1.

---
 ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/ShoppingCartServiceImpl.java |  203 +++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 149 insertions(+), 54 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 cac0a44..a45cea5 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
@@ -3,7 +3,6 @@
 import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
-import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.account.api.feignClient.*;
@@ -15,9 +14,13 @@
 import com.ruoyi.account.api.vo.PaymentUserCoupon;
 import com.ruoyi.account.api.vo.PaymentUserCouponVo;
 import com.ruoyi.common.core.domain.R;
-import com.ruoyi.common.core.web.domain.AjaxResult;
+import com.ruoyi.common.core.utils.StringUtils;
 import com.ruoyi.common.security.service.TokenService;
 import com.ruoyi.order.mapper.ShoppingCartMapper;
+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;
@@ -27,11 +30,7 @@
 import com.ruoyi.other.api.vo.GetGoodsBargainPrice;
 import com.ruoyi.other.api.vo.GetGoodsShopByGoodsIds;
 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;
@@ -39,6 +38,7 @@
 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;
 
@@ -98,6 +98,9 @@
 	
 	@Resource
 	private BalanceChangeRecordClient balanceChangeRecordClient;
+	
+	@Resource
+	private CommissionService commissionService;
 	
 	
 	
@@ -324,10 +327,11 @@
 	
 
 	@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();
 	}
 	
 	
@@ -342,11 +346,14 @@
 			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);
 		}
@@ -361,7 +368,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();
@@ -398,7 +420,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);
 		//支付金额,订单金额-订单优惠
@@ -406,6 +428,7 @@
 		confirmOrderVo.setResidualPoint(appUser.getLavePoint().intValue());
 		//获取默认收货地址
 		UserAddress userAddress = userAddressClient.getDefaultUserAddress(userid).getData();
+		userAddress.setIdStr(userAddress.getId().toString());
 		confirmOrderVo.setUserAddress(userAddress);
 		confirmOrderVo.setPaymentType(confirmOrder.getPaymentType());
 		//获取用户优惠券,用户全部优惠券,不能使用的需要标识出来置灰展示
@@ -415,35 +438,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);
+						}
 					}
 				}
 			}
@@ -487,7 +512,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());
@@ -504,6 +529,7 @@
 			//获取支付价格
 			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);
@@ -521,7 +547,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 ? false : true);
 			//判断当前数量是否已经超出限购数量(需要计算已经购买的数量)
 			if(null == goods.getPurchaseLimit() || -1 == goods.getPurchaseLimit()){
 				vo.setPurchaseLimit(false);
@@ -620,12 +646,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 + "不能在该门店核销");
+			}
 		}
 		//开始构建支付信息
 		//现金支付的订单金额
@@ -795,6 +824,9 @@
 		order.setPaymentAmount(paymentMoney);
 		order.setPoint(orderPoint);
 		order.setPayMethod(shoppingCartPayment.getPaymentType());
+		if(StringUtils.isNotEmpty(shoppingCartPayment.getExpectedDeliveryTime())){
+			order.setExpectedDeliveryTime(shoppingCartPayment.getExpectedDeliveryTime());
+		}
 		if(3 != shoppingCartPayment.getPaymentType() && (fullReductionAmount.compareTo(BigDecimal.ZERO) > 0 ||moneyAmount.compareTo(BigDecimal.ZERO) > 0 || discountAmount.compareTo(BigDecimal.ZERO) > 0)){
 			order.setCouponJson(JSON.toJSONString(couponInfoVo));
 		}
@@ -811,13 +843,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) {
@@ -833,10 +874,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;
 				}
@@ -955,13 +997,66 @@
 				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);
+					orderService.updateById(order);
+					//删除购物车数据
+					this.removeBatchByIds(ids);
+				}
+			}else{
+				//修改订支付状态
+				order.setPayStatus(2);
+				orderService.updateById(order);
+				//删除购物车数据
+				this.removeBatchByIds(ids);
+			}
 		}
-		return R.ok();
+		return R.ok(order.getId().toString());
 	}
 	
 	

--
Gitblit v1.7.1