luodangjia
2025-01-14 ea6bf9042d9ad4faaf328bdc68a41ef36b47f13d
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/ShoppingCartServiceImpl.java
@@ -144,8 +144,31 @@
      //查询符合商品类型的商品数据
      List<ShoppingCart> list = this.list(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getAppUserId, userid)
            .in(ShoppingCart::getGoodsId, goodsIds).eq(ShoppingCart::getStatus, 1));
      //删除过期的秒杀活动商品
      List<ShoppingCart> list1 = new ArrayList<>();
      for (ShoppingCart shoppingCart : list) {
         if(shoppingCart.getType() == 2){
            GetSeckillActivityInfo info = new GetSeckillActivityInfo();
            info.setGoodsId(shoppingCart.getGoodsId());
            info.setVip(appUser.getVipId());
            GoodsSeckill data1 = seckillActivityInfoClient.getSeckillActivityInfo(info).getData();
            if(null != data1){
               SeckillActivityInfo seckillActivityInfo = seckillActivityInfoClient.getSeckillActivityInfoById(data1.getSeckillActivityInfoId()).getData();
               if(null != seckillActivityInfo && (seckillActivityInfo.getIsShelves() == 1 &&
                     seckillActivityInfo.getStartTime().isBefore(LocalDateTime.now()) && seckillActivityInfo.getEndTime().isAfter(LocalDateTime.now()))){
                  list1.add(shoppingCart);
                  continue;
               }
            }
            this.removeById(shoppingCart.getId());
         }else{
            list1.add(shoppingCart);
         }
      }
      //构建返回数据
      List<MyShoppingCartVo> page = buildDetail(appUser, shopId, list, null);
      List<MyShoppingCartVo> page = buildDetail(appUser, shopId, list1, null);
      return page;
   }
@@ -395,12 +418,14 @@
         BigDecimal bigDecimal = BigDecimal.ZERO;
         for (MyShoppingCartVo myShoppingCartVo : goodsList) {
            bigDecimal = bigDecimal.add(myShoppingCartVo.getCash().multiply(new BigDecimal(myShoppingCartVo.getNumber())));
            myShoppingCartVo.setEarnSpendingPoints(myShoppingCartVo.getEarnSpendingPoints() * myShoppingCartVo.getNumber());
         }
         confirmOrderVo.setOrderMoney(bigDecimal);
      }else{
         int sum = 0;
         for (MyShoppingCartVo myShoppingCartVo : goodsList) {
            sum += (myShoppingCartVo.getPoint() * myShoppingCartVo.getNumber());
            myShoppingCartVo.setEarnSpendingPoints(myShoppingCartVo.getEarnSpendingPoints() * myShoppingCartVo.getNumber());
         }
         confirmOrderVo.setOrderPoint(sum);
      }
@@ -500,13 +525,19 @@
            }
         }
         confirmOrderVo.setActivityName(activityInfo1.getActivityName());
         paymentMoney = paymentMoney.subtract(zyh);
         activityAmount = activityAmount.add(zyh);
         if(null != activityInfo1){
            confirmOrderVo.setActivityName(activityInfo1.getActivityName());
            paymentMoney = paymentMoney.subtract(zyh);
            activityAmount = activityAmount.add(zyh);
         }
      }
      confirmOrderVo.setDiscountAmount(activityAmount);
      int earnPoint = goodsList.stream().mapToInt(MyShoppingCartVo::getEarnSpendingPoints).sum();
      confirmOrderVo.setEarnPoint(earnPoint);
      if(null != paymentMoney && BigDecimal.ZERO.compareTo(paymentMoney) > 0){
         paymentMoney = BigDecimal.ZERO;
      }
      //支付金额,订单金额-订单优惠
      confirmOrderVo.setPayMoney(paymentMoney);
      confirmOrderVo.setResidualPoint(appUser.getAvailablePoint().intValue());
@@ -514,6 +545,7 @@
      UserAddress userAddress = userAddressClient.getDefaultUserAddress(userid).getData();
      if(null != userAddress){
         userAddress.setIdStr(userAddress.getId().toString());
         userAddress.setRecieveAddress(userAddress.getProvince() + userAddress.getCity() + userAddress.getDistrict() + userAddress.getRecieveAddress());
         confirmOrderVo.setUserAddress(userAddress);
      }
      confirmOrderVo.setPaymentType(confirmOrder.getPaymentType());
@@ -539,11 +571,11 @@
                  }
               }else{
                  //部分商品适用
                  BigDecimal goodsMoney = BigDecimal.ZERO;
                   BigDecimal goodsMoney = BigDecimal.ZERO;
                  for (MyShoppingCartVo myShoppingCartVo : goodsList) {
                     Integer goodsId = myShoppingCartVo.getGoodsId();
                     BigDecimal cash = myShoppingCartVo.getCash();
                     if(forGoodIds.contains(goodsId)){
                     if(forGoodIds.contains(String.valueOf(goodsId))){
                        goodsMoney = goodsMoney.add(cash);
                     }
                  }
@@ -709,6 +741,14 @@
               }
            }
         }
         if(goods.getStatus() == 1){
            throw new RuntimeException(goods.getName() + "商品已被下架");
         }
         if(!goods.getCommodityAuthority().contains("-1") && !goods.getCommodityAuthority().contains(appUser.getVipId().toString())){
            throw new RuntimeException("无权限购买" + goods.getName());
         }
         ids.add(id);
      }
@@ -871,7 +911,6 @@
            }
         }
         discount = orderActivityInfo1.getDiscount();
         paymentMoney = paymentMoney.subtract(zyh);
         activityAmount = activityAmount.add(zyh);
      }
@@ -907,6 +946,10 @@
            }
         }
      }
      if(BigDecimal.ZERO.compareTo(paymentMoney) > 0){
         paymentMoney = BigDecimal.ZERO;
      }
      //构建订单明细
      Order order = new Order();
      order.setAppUserId(userid);
@@ -921,7 +964,7 @@
      order.setGoodName(goodName.substring(0, goodName.length() - 1));
      Goods goods = goodsClient.getGoodsById(goodsList.get(0).getGoodsId()).getData();
      order.setOrderType(goods.getType());
      order.setOrderStatus(goods.getType() == 1 ? 3 : 1);
      order.setOrderStatus(goods.getType() == 1 ? 3 : (shoppingCartPayment.getDistributionMode() == 2 ? 1 : 2));
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
      order.setOrderNumber("QJS" + getNumber(3) + sdf.format(new Date()));
      order.setTotalAmount(orderMoney.setScale(2, RoundingMode.HALF_EVEN));
@@ -938,11 +981,12 @@
      }
      if(3 != shoppingCartPayment.getPaymentType() && (fullReductionAmount.compareTo(BigDecimal.ZERO) > 0 ||moneyAmount.compareTo(BigDecimal.ZERO) > 0 || discountAmount.compareTo(BigDecimal.ZERO) > 0)){
         order.setCouponJson(JSON.toJSONString(couponInfoVo));
         order.setUserCouponId(shoppingCartPayment.getUserCouponId());
      }
      if(null != orderActivityInfo1){
         order.setActivityJson(JSON.toJSONString(orderActivityInfo1));
      }
      if(null != shoppingCartPayment.getUserAddressId()){
      if(2 == shoppingCartPayment.getDistributionMode()){
         UserAddress address = userAddressClient.getUserAddressById(shoppingCartPayment.getUserAddressId()).getData();
         order.setExpressAmount(expressFee);
         order.setAddressJson(JSON.toJSONString(address));
@@ -953,6 +997,7 @@
      order.setDelFlag(0);
      order.setCreateTime(LocalDateTime.now());
      order.setExpressPayMethod(shoppingCartPayment.getFreightPaymentType());
      order.setDistributionMode(shoppingCartPayment.getDistributionMode());
      if(2 == shoppingCartPayment.getPaymentType()){
         BigDecimal balance = appUser.getBalance();
@@ -1026,21 +1071,94 @@
      //开始构建支付数据
      //现金支付
      paymentMoney = paymentMoney.add(expressFee).setScale(2, RoundingMode.HALF_EVEN);
      if(BigDecimal.ZERO.compareTo(paymentMoney) > 0){
         paymentMoney = BigDecimal.ZERO;
      }
      if(1 == shoppingCartPayment.getPaymentType()){
         //调起微信支付
         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(), null);
         if(null == uniPayResult || !"100".equals(uniPayResult.getRa_Code())){
            return R.fail(null == uniPayResult ? "支付失败" : uniPayResult.getRb_CodeMsg());
         if(BigDecimal.ZERO.compareTo(paymentMoney) < 0){
            //调起微信支付
            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(), 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(jsonObject.toJSONString());
         }else{
            earnPoint = order.getGetPoint();
            appUser = appUserClient.getAppUserById(order.getAppUserId());
            Integer lavePoint = appUser.getLavePoint();
            paymentMoney = order.getPaymentAmount();
            //构建积分流水记录
            if(earnPoint > 0){
               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.setShopPoint(appUser.getShopPoint() + earnPoint);
               appUser.setLavePoint(appUser.getLavePoint() + earnPoint);
               appUser.setTotalPoint(appUser.getTotalPoint() + earnPoint);
               appUser.setAvailablePoint(appUser.getAvailablePoint() + earnPoint1);
               appUser.setTotalAvailablePoint(appUser.getTotalAvailablePoint() + earnPoint1);
               JSONObject jsonObject = new JSONObject();
               jsonObject.put("shopPoint", earnPoint);
               jsonObject.put("availablePoint", earnPoint1);
               if(null != pointSetting && 1 == pointSetting.getBuyPointGift()){
                  appUser.setTransferablePoint(appUser.getTransferablePoint() + earnPoint1);
                  jsonObject.put("transferablePoint", earnPoint1);
               }
               UserPoint 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());
               userPoint.setExtention(jsonObject.toJSONString());
               userPointClient.saveUserPoint(userPoint);
            }
            appUser.setShopAmount(appUser.getShopAmount().add(paymentMoney).setScale(2, RoundingMode.HALF_EVEN));
            appUser.setLastShopTime(LocalDateTime.now());
            appUserClient.editAppUserById(appUser);
            //变更等级
            appUserClient.vipUpgrade(appUser.getId());
            //修改订支付状态
            order.setPayStatus(2);
            //自提
            if(order.getOrderType() == 1 && StringUtils.isEmpty(order.getAddressJson())){
               order.setOrderStatus(2);
            }
            orderService.updateById(order);
            //处理优惠券
            if(null != order.getUserCouponId()){
               UserCoupon userCoupon = userCouponClient.getUserCoupon(order.getUserCouponId()).getData();
               if(null != userCoupon && null == userCoupon.getUseTime()){
                  userCoupon.setStatus(2);
                  userCoupon.setUseTime(LocalDateTime.now());
                  userCouponClient.editUserCoupon(userCoupon);
               }
            }
            //删除购物车数据
            userid = tokenService.getLoginUserApplet().getUserid();
            List<OrderGood> list1 = orderGoodService.list(new LambdaQueryWrapper<OrderGood>().eq(OrderGood::getOrderId, order.getId()));
            List<Integer> goodsIds1 = list1.stream().map(OrderGood::getGoodsId).collect(Collectors.toList());
            this.remove(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getAppUserId, userid).in(ShoppingCart::getGoodsId, goodsIds1));
         }
         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(jsonObject.toJSONString());
      }
      //账户余额
      BigDecimal redPacketAmount = BigDecimal.ZERO;
@@ -1120,6 +1238,7 @@
         //构建余额明细变动记录
         BalanceChangeRecord balanceChangeRecord = new BalanceChangeRecord();
         balanceChangeRecord.setAppUserId(appUser.getId());
         balanceChangeRecord.setVipId(appUser.getVipId());
         balanceChangeRecord.setOrderId(order.getId());
         balanceChangeRecord.setChangeType(5);
         balanceChangeRecord.setBeforeAmount(balance.add(paymentMoney));
@@ -1136,6 +1255,15 @@
         orderService.updateById(order);
         //删除购物车数据
         this.removeBatchByIds(ids);
         //处理优惠券
         if(null != order.getUserCouponId()){
            UserCoupon userCoupon = userCouponClient.getUserCoupon(order.getUserCouponId()).getData();
            if(null != userCoupon && null == userCoupon.getUseTime()){
               userCoupon.setUseTime(LocalDateTime.now());
               userCouponClient.editUserCoupon(userCoupon);
            }
         }
      }
      //积分支付
      if(3 == shoppingCartPayment.getPaymentType()){
@@ -1230,6 +1358,7 @@
               //构建余额明细变动记录
               BalanceChangeRecord balanceChangeRecord = new BalanceChangeRecord();
               balanceChangeRecord.setAppUserId(appUser.getId());
               balanceChangeRecord.setVipId(appUser.getVipId());
               balanceChangeRecord.setOrderId(order.getId());
               balanceChangeRecord.setChangeType(5);
               balanceChangeRecord.setBeforeAmount(balance.add(expressFee));
@@ -1257,6 +1386,7 @@
            //删除购物车数据
            this.removeBatchByIds(ids);
         }
      }
      //添加账户余额支付明细
@@ -1339,11 +1469,27 @@
         order.setOrderStatus(2);
      }
      orderService.updateById(order);
      //处理优惠券
      if(null != order.getUserCouponId()){
         UserCoupon userCoupon = userCouponClient.getUserCoupon(order.getUserCouponId()).getData();
         if(null != userCoupon && null == userCoupon.getUseTime()){
            userCoupon.setStatus(2);
            userCoupon.setUseTime(LocalDateTime.now());
            userCouponClient.editUserCoupon(userCoupon);
         }
      }
      //删除购物车数据
      Long userid = tokenService.getLoginUserApplet().getUserid();
      Long userid = order.getAppUserId();
      List<OrderGood> list = orderGoodService.list(new LambdaQueryWrapper<OrderGood>().eq(OrderGood::getOrderId, order.getId()));
      List<Integer> goodsIds = list.stream().map(OrderGood::getGoodsId).collect(Collectors.toList());
      this.remove(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getAppUserId, userid).in(ShoppingCart::getGoodsId, goodsIds));
      //商品销量增加
      for (Integer goodsId : goodsIds) {
         goodsClient.editGoodsNum(goodsId, 1);
      }
      return R.ok();
   }