xuhy
2024-10-16 5df9beddb0e8f7bca303a50f59b960d3d8f1346a
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TChargingOrderServiceImpl.java
@@ -532,9 +532,9 @@
   @GlobalTransactional(rollbackFor = Exception.class)
   public AjaxResult chargingOrderCallback(Integer paymentType, String out_trade_no, String transaction_id, String attach) {
      TChargingOrder chargingOrder = this.getOne(new LambdaQueryWrapper<TChargingOrder>().eq(TChargingOrder::getCode, out_trade_no));
//      if(chargingOrder.getRechargePaymentStatus() == 2){
//         return AjaxResult.success();
//      }
      if(chargingOrder.getRechargePaymentStatus() == 2){
         return AjaxResult.success();
      }
      chargingOrder.setRechargePaymentStatus(2);
      chargingOrder.setRechargeSerialNumber(transaction_id);
      chargingOrder.setStatus(2);
@@ -911,7 +911,7 @@
   public ChargingDetails getChargingDetails(Integer id) {
      Long userId = tokenService.getLoginUserApplet().getUserId();
      TChargingOrder one = this.getOne(new LambdaQueryWrapper<TChargingOrder>().eq(TChargingOrder::getAppUserId, userId).eq(TChargingOrder::getChargingGunId, id)
            .eq(TChargingOrder::getRechargePaymentStatus, 2).eq(TChargingOrder::getDelFlag, 0).orderByDesc(TChargingOrder::getEndTime).last(" limit 0, 1"));
            .eq(TChargingOrder::getRechargePaymentStatus, 2).eq(TChargingOrder::getDelFlag, 0).orderByDesc(TChargingOrder::getStartTime).last(" limit 0, 1"));
      if(null == one){
         return null;
      }
@@ -1043,8 +1043,6 @@
            editUserTag(chargingOrder);
            //用户推荐奖励
            referralReward(chargingOrder);
            //计算费用,处理退款
            endCharge(chargingOrder);
            // 将枪状态重置为空闲
            chargingGun.setStatus(2);
            chargingGunClient.updateChargingGunById(chargingGun);
@@ -1221,8 +1219,6 @@
         editUserTag(order);
         //用户推荐奖励
         referralReward(order);
         //计算费用,处理退款
         endCharge(order);
         // 将枪状态重置为空闲
         chargingGun.setStatus(2);
         chargingGunClient.updateChargingGunById(chargingGun);
@@ -1230,193 +1226,6 @@
   }
   
   
   /**
    * 手动结束后的费用计算和退款逻辑
    */
   @GlobalTransactional(rollbackFor = Exception.class)
   public void endCharge(TChargingOrder chargingOrder){
      //如果使用优惠券需要判断优惠券是否满足使用条件
      //根据实际的充电金额计算退款金额   退回费用=(原金额/总金额)*(总金额-实际充电金额)
      //退款金额=优惠券金额+剩余充电金额
      List<TChargingOrderAccountingStrategy> list = chargingOrderAccountingStrategyService.list(new LambdaQueryWrapper<TChargingOrderAccountingStrategy>().eq(TChargingOrderAccountingStrategy::getChargingOrderId, chargingOrder.getId()));
      BigDecimal periodElectricPrice_total = BigDecimal.ZERO;
      BigDecimal periodServicePrice_total = BigDecimal.ZERO;
      BigDecimal total = BigDecimal.ZERO;
      for (TChargingOrderAccountingStrategy chargingOrderAccountingStrategy : list) {
         BigDecimal periodElectricPrice = chargingOrderAccountingStrategy.getPeriodElectricPrice();
         BigDecimal periodServicePrice = chargingOrderAccountingStrategy.getPeriodOriginalServicePrice();
         periodElectricPrice_total = periodElectricPrice_total.add(periodElectricPrice);
         periodServicePrice_total = periodServicePrice_total.add(periodServicePrice);
         total = total.add(periodElectricPrice).add(periodServicePrice);
      }
      //原金额
      BigDecimal rechargeAmount = chargingOrder.getRechargeAmount();
      BigDecimal vipDiscountAmount = chargingOrder.getVipDiscountAmount();
      //总金额(充值金额+会员折扣金额)
      BigDecimal decimal = rechargeAmount.add(vipDiscountAmount);
      //退款金额(已经计算了折扣优惠部分)
      BigDecimal refundAmount = rechargeAmount.divide(decimal, new MathContext(4, RoundingMode.HALF_EVEN)).multiply(decimal.subtract(total));
      BigDecimal orderAmount = BigDecimal.valueOf(total.doubleValue());
      BigDecimal payAmount = BigDecimal.valueOf(total.doubleValue());
      //折扣金额
      BigDecimal discountAmount = BigDecimal.ZERO;
      if(null != chargingOrder.getVipDiscount()){
         //服务费折扣
         discountAmount = periodServicePrice_total.multiply((new BigDecimal(10).subtract(chargingOrder.getVipDiscount())).divide(new BigDecimal(10)));
         TAppUser appUser = appUserClient.getUserById(chargingOrder.getAppUserId()).getData();
         if(null != appUser.getVipId()){
            //判断会员是否还有充电优惠次数
            GetAppUserVipDetail getAppUserVipDetail = new GetAppUserVipDetail();
            getAppUserVipDetail.setAppUserId(chargingOrder.getAppUserId());
            getAppUserVipDetail.setVipId(appUser.getVipId());
            TAppUserVipDetail data = appUserVipDetailClient.getAppUserVipDetail(getAppUserVipDetail).getData();
            if(null != data && data.getChargeNum() > 0){
               data.setChargeNum(data.getChargeNum() - 1);
               appUserVipDetailClient.updateAppUserVipDetail(data);
            }
            TVip vip = vipClient.getInfo1(appUser.getVipId()).getData();
            BigDecimal maximumDeduction = vip.getMaximumDeduction();
            //普通会员有最高优惠限制
            if(vip.getType() == 1 && discountAmount.compareTo(maximumDeduction) > 0){
               discountAmount = maximumDeduction;
            }
         }
         payAmount = payAmount.subtract(discountAmount);
      }
      if(chargingOrder.getEndMode() == 2){
         chargingOrder.setEndMode(refundAmount.compareTo(BigDecimal.ZERO) > 0 ? 2 : 3);
      }
      chargingOrder.setStatus(5);
      chargingOrder.setOrderAmount(orderAmount);
      chargingOrder.setVipDiscountAmount(discountAmount);
      //计算优惠券
      if(null != chargingOrder.getAppCouponId()){
         //判断实际充电金额是否满足优惠券使用条件,如果不满足则不适用优惠券。
         TAppCoupon appCoupon = appCouponClient.getAppCouponById(chargingOrder.getAppCouponId()).getData();
         String couponJson = appCoupon.getCouponJson();
         TCoupon tCoupon = JSON.parseObject(couponJson, TCoupon.class);
         Integer preferentialMode = tCoupon.getPreferentialMode();
         if(1 == preferentialMode){
            //满减
            if(payAmount.compareTo(tCoupon.getMeetTheConditions()) >= 0){
               refundAmount = refundAmount.add(tCoupon.getDiscountAmount());
               chargingOrder.setCouponDiscountAmount(tCoupon.getDiscountAmount());
               payAmount = payAmount.subtract(tCoupon.getDiscountAmount());
            }else{
               chargingOrder.setAppCouponId(null);
               chargingOrder.setCouponDiscountAmount(BigDecimal.ZERO);
               appCouponClient.refund(chargingOrder.getAppCouponId().toString());
            }
         }
         if(2 == preferentialMode){
            //抵扣
            if(payAmount.compareTo(tCoupon.getMeetTheConditions()) >= 0){
               //折扣金额
               BigDecimal divide = payAmount.multiply(new BigDecimal(10).subtract(tCoupon.getDiscount())).divide(new BigDecimal(10));
               divide = divide.compareTo(tCoupon.getMaximumDiscountAmount()) > 0 ? tCoupon.getMaximumDiscountAmount() : divide;
               refundAmount = refundAmount.add(divide);
               chargingOrder.setCouponDiscountAmount(divide);
               payAmount = payAmount.subtract(divide);
            }else{
               chargingOrder.setAppCouponId(null);
               chargingOrder.setCouponDiscountAmount(BigDecimal.ZERO);
               appCouponClient.refund(chargingOrder.getAppCouponId().toString());
            }
         }
      }
      chargingOrder.setPaymentAmount(payAmount);
      this.updateById(chargingOrder);
      //添加积分
      TIntegralRule integralRule = integralRuleClient.getSet().getData();
      if(null != integralRule){
         TAppUser appUser = appUserClient.getUserById(chargingOrder.getAppUserId()).getData();
         Integer num1 = JSON.parseObject(integralRule.getChargeCredit()).getInteger("num1");
         Integer integral = payAmount.multiply(new BigDecimal(num1)).intValue();
         if(null != appUser.getVipId()){
            TVip vip = vipClient.getInfo1(appUser.getVipId()).getData();
            Integer doubleIntegration = vip.getDoubleIntegration();
            //双倍积分
            if(1 == doubleIntegration){
               integral *= 2;
            }
         }
         TAppUserIntegralChange appUserIntegralChange = new TAppUserIntegralChange();
         appUserIntegralChange.setAppUserId(appUser.getId());
         appUserIntegralChange.setChangeType(2);
         appUserIntegralChange.setHistoricalIntegral(appUser.getPoints());
         appUser.setPoints(appUser.getPoints() + integral);
         appUserIntegralChange.setCurrentIntegral(appUser.getPoints());
         appUserIntegralChange.setCreateTime(LocalDateTime.now());
         appUserIntegralChange.setOrderCode(chargingOrder.getCode());
         appUserClient.updateAppUser(appUser);
         appUserIntegralChangeClient.addAppUserIntegralChange(appUserIntegralChange);
      }
      //开始构建退款费用
      if(refundAmount.compareTo(BigDecimal.ZERO) > 0){
         Integer rechargePaymentType = chargingOrder.getRechargePaymentType();
         //构建退款明细
         TChargingOrderRefund chargingOrderRefund = new TChargingOrderRefund();
         chargingOrderRefund.setChargingOrderId(chargingOrder.getId());
         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
         chargingOrderRefund.setRefundCode("CDF" + sdf.format(new Date()) + (Double.valueOf(Math.random() * 1000).intValue()));
         chargingOrderRefund.setRefundAmount(refundAmount);
         chargingOrderRefund.setRefundStatus(1);
         chargingOrderRefund.setPayType(rechargePaymentType);
         chargingOrderRefund.setRefundStatus(1);
         chargingOrderRefund.setCode(chargingOrder.getCode());
         chargingOrderRefund.setRefundTitle("充电完成退款");
         chargingOrderRefund.setRefundContent("充电完成退款");
         chargingOrderRefund.setRefundReason("充电完成退款");
         chargingOrderRefund.setRefundRemark("充电完成退款");
         chargingOrderRefund.setRefundTotalAmount(refundAmount);
         chargingOrderRefund.setPayAmount(rechargeAmount);
         if(1 == rechargePaymentType){
            WxPaymentRefundModel model = new WxPaymentRefundModel();
            model.setOut_trade_no(chargingOrder.getCode());
            model.setOut_refund_no(chargingOrderRefund.getRefundCode());
            model.setReason("充电完成退款");
            model.setNotify_url("/payment/wx/refund/notify");
            WxPaymentRefundModel.RefundAmount amount = new WxPaymentRefundModel.RefundAmount();
            amount.setRefund(refundAmount.multiply(new BigDecimal(100)).intValue());
            amount.setTotal(rechargeAmount.multiply(new BigDecimal(100)).intValue());
            amount.setCurrency("CNY");
            model.setAmount(amount);
            R<String> orderR = wxPaymentClient.refundOrderR(model);
            if(200 == orderR.getCode()){
               chargingOrderRefundService.save(chargingOrderRefund);
            }
         }
         if(2 == rechargePaymentType){
            RefundReq dto = new RefundReq();
            dto.setOutTradeNo(chargingOrder.getCode());
            dto.setOutRequestNo(chargingOrderRefund.getCode());
            dto.setRefundAmount(rechargeAmount.toString());
            dto.setRefundReason("充电完成退款");
            RefundResp resp = aliPaymentClient.refund(dto).getData();
            if(null != resp){
               SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-DDTHH:mm:ss+TIMEZONE");
               AjaxResult success = chargingOrderStartupFailureWxRefund(resp.getOutTradeNo(), resp.getTradeNo(), "SUCCESS", sdf1.format(new Date()));
               if(success.isSuccess()){
                  chargingOrderRefundService.save(chargingOrderRefund);
               }
            }
         }
      }
   }
@@ -2017,8 +1826,6 @@
      editUserTag(chargingOrder);
      //用户推荐奖励
      referralReward(chargingOrder);
      //计算费用,处理退款
      endCharge(chargingOrder);
   }
   /**
@@ -2029,7 +1836,194 @@
   public void excelEndCharge(String orderCode) {
      endCharge(orderCode, 0);
   }
   /**
    * 停止充电返回账单后计算费用
    * @param vo
    */
   @Override
   @GlobalTransactional(rollbackFor = Exception.class)
   public void endChargeBillingCharge(TransactionRecordMessageVO vo) {
      //如果使用优惠券需要判断优惠券是否满足使用条件
      //根据实际的充电金额计算退款金额   退回费用=(原金额/总金额)*(总金额-实际充电金额)
      //退款金额=优惠券金额+剩余充电金额
      TChargingOrder chargingOrder = this.getOne(new LambdaQueryWrapper<TChargingOrder>().eq(TChargingOrder::getCode, vo.getTransaction_serial_number()));
      List<TChargingOrderAccountingStrategy> list = chargingOrderAccountingStrategyService.list(new LambdaQueryWrapper<TChargingOrderAccountingStrategy>().eq(TChargingOrderAccountingStrategy::getChargingOrderId, chargingOrder.getId()));
      BigDecimal periodElectricPrice_total = BigDecimal.ZERO;
      BigDecimal periodServicePrice_total = BigDecimal.ZERO;
      BigDecimal total = BigDecimal.ZERO;
      for (TChargingOrderAccountingStrategy chargingOrderAccountingStrategy : list) {
         BigDecimal periodElectricPrice = chargingOrderAccountingStrategy.getPeriodElectricPrice();
         BigDecimal periodServicePrice = chargingOrderAccountingStrategy.getPeriodOriginalServicePrice();
         periodElectricPrice_total = periodElectricPrice_total.add(periodElectricPrice);
         periodServicePrice_total = periodServicePrice_total.add(periodServicePrice);
         total = total.add(periodElectricPrice).add(periodServicePrice);
      }
      //原金额
      BigDecimal rechargeAmount = chargingOrder.getRechargeAmount();
      BigDecimal vipDiscountAmount = chargingOrder.getVipDiscountAmount();
      //总金额(充值金额+会员折扣金额)
      BigDecimal decimal = rechargeAmount.add(vipDiscountAmount);
      //退款金额(已经计算了折扣优惠部分)
      BigDecimal refundAmount = rechargeAmount.divide(decimal, new MathContext(4, RoundingMode.HALF_EVEN)).multiply(decimal.subtract(total));
      BigDecimal orderAmount = BigDecimal.valueOf(total.doubleValue());
      BigDecimal payAmount = BigDecimal.valueOf(total.doubleValue());
      //折扣金额
      BigDecimal discountAmount = BigDecimal.ZERO;
      if(null != chargingOrder.getVipDiscount()){
         //服务费折扣
         discountAmount = periodServicePrice_total.multiply((new BigDecimal(10).subtract(chargingOrder.getVipDiscount())).divide(new BigDecimal(10)));
         TAppUser appUser = appUserClient.getUserById(chargingOrder.getAppUserId()).getData();
         if(null != appUser.getVipId()){
            //判断会员是否还有充电优惠次数
            GetAppUserVipDetail getAppUserVipDetail = new GetAppUserVipDetail();
            getAppUserVipDetail.setAppUserId(chargingOrder.getAppUserId());
            getAppUserVipDetail.setVipId(appUser.getVipId());
            TAppUserVipDetail data = appUserVipDetailClient.getAppUserVipDetail(getAppUserVipDetail).getData();
            if(null != data && data.getChargeNum() > 0){
               data.setChargeNum(data.getChargeNum() - 1);
               appUserVipDetailClient.updateAppUserVipDetail(data);
            }
            TVip vip = vipClient.getInfo1(appUser.getVipId()).getData();
            BigDecimal maximumDeduction = vip.getMaximumDeduction();
            //普通会员有最高优惠限制
            if(vip.getType() == 1 && discountAmount.compareTo(maximumDeduction) > 0){
               discountAmount = maximumDeduction;
            }
         }
         payAmount = payAmount.subtract(discountAmount);
      }
      if(chargingOrder.getEndMode() == 2){
         chargingOrder.setEndMode(refundAmount.compareTo(BigDecimal.ZERO) > 0 ? 2 : 3);
      }
      chargingOrder.setStatus(5);
      chargingOrder.setOrderAmount(orderAmount);
      chargingOrder.setVipDiscountAmount(discountAmount);
      //计算优惠券
      if(null != chargingOrder.getAppCouponId()){
         //判断实际充电金额是否满足优惠券使用条件,如果不满足则不适用优惠券。
         TAppCoupon appCoupon = appCouponClient.getAppCouponById(chargingOrder.getAppCouponId()).getData();
         String couponJson = appCoupon.getCouponJson();
         TCoupon tCoupon = JSON.parseObject(couponJson, TCoupon.class);
         Integer preferentialMode = tCoupon.getPreferentialMode();
         if(1 == preferentialMode){
            //满减
            if(payAmount.compareTo(tCoupon.getMeetTheConditions()) >= 0){
               refundAmount = refundAmount.add(tCoupon.getDiscountAmount());
               chargingOrder.setCouponDiscountAmount(tCoupon.getDiscountAmount());
               payAmount = payAmount.subtract(tCoupon.getDiscountAmount());
            }else{
               chargingOrder.setAppCouponId(null);
               chargingOrder.setCouponDiscountAmount(BigDecimal.ZERO);
               appCouponClient.refund(chargingOrder.getAppCouponId().toString());
            }
         }
         if(2 == preferentialMode){
            //抵扣
            if(payAmount.compareTo(tCoupon.getMeetTheConditions()) >= 0){
               //折扣金额
               BigDecimal divide = payAmount.multiply(new BigDecimal(10).subtract(tCoupon.getDiscount())).divide(new BigDecimal(10));
               divide = divide.compareTo(tCoupon.getMaximumDiscountAmount()) > 0 ? tCoupon.getMaximumDiscountAmount() : divide;
               refundAmount = refundAmount.add(divide);
               chargingOrder.setCouponDiscountAmount(divide);
               payAmount = payAmount.subtract(divide);
            }else{
               chargingOrder.setAppCouponId(null);
               chargingOrder.setCouponDiscountAmount(BigDecimal.ZERO);
               appCouponClient.refund(chargingOrder.getAppCouponId().toString());
            }
         }
      }
      chargingOrder.setPaymentAmount(payAmount);
      this.updateById(chargingOrder);
      //添加积分
      TIntegralRule integralRule = integralRuleClient.getSet().getData();
      if(null != integralRule){
         TAppUser appUser = appUserClient.getUserById(chargingOrder.getAppUserId()).getData();
         Integer num1 = JSON.parseObject(integralRule.getChargeCredit()).getInteger("num1");
         Integer integral = payAmount.multiply(new BigDecimal(num1)).intValue();
         if(null != appUser.getVipId()){
            TVip vip = vipClient.getInfo1(appUser.getVipId()).getData();
            Integer doubleIntegration = vip.getDoubleIntegration();
            //双倍积分
            if(1 == doubleIntegration){
               integral *= 2;
            }
         }
         TAppUserIntegralChange appUserIntegralChange = new TAppUserIntegralChange();
         appUserIntegralChange.setAppUserId(appUser.getId());
         appUserIntegralChange.setChangeType(2);
         appUserIntegralChange.setHistoricalIntegral(appUser.getPoints());
         appUser.setPoints(appUser.getPoints() + integral);
         appUserIntegralChange.setCurrentIntegral(appUser.getPoints());
         appUserIntegralChange.setCreateTime(LocalDateTime.now());
         appUserIntegralChange.setOrderCode(chargingOrder.getCode());
         appUserClient.updateAppUser(appUser);
         appUserIntegralChangeClient.addAppUserIntegralChange(appUserIntegralChange);
      }
      //开始构建退款费用
      if(refundAmount.compareTo(BigDecimal.ZERO) > 0){
         Integer rechargePaymentType = chargingOrder.getRechargePaymentType();
         //构建退款明细
         TChargingOrderRefund chargingOrderRefund = new TChargingOrderRefund();
         chargingOrderRefund.setChargingOrderId(chargingOrder.getId());
         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
         chargingOrderRefund.setRefundCode("CDF" + sdf.format(new Date()) + (Double.valueOf(Math.random() * 1000).intValue()));
         chargingOrderRefund.setRefundAmount(refundAmount);
         chargingOrderRefund.setRefundStatus(1);
         chargingOrderRefund.setPayType(rechargePaymentType);
         chargingOrderRefund.setRefundStatus(1);
         chargingOrderRefund.setCode(chargingOrder.getCode());
         chargingOrderRefund.setRefundTitle("充电完成退款");
         chargingOrderRefund.setRefundContent("充电完成退款");
         chargingOrderRefund.setRefundReason("充电完成退款");
         chargingOrderRefund.setRefundRemark("充电完成退款");
         chargingOrderRefund.setRefundTotalAmount(refundAmount);
         chargingOrderRefund.setPayAmount(rechargeAmount);
         if(1 == rechargePaymentType){
            WxPaymentRefundModel model = new WxPaymentRefundModel();
            model.setOut_trade_no(chargingOrder.getCode());
            model.setOut_refund_no(chargingOrderRefund.getRefundCode());
            model.setReason("充电完成退款");
            model.setNotify_url("/payment/wx/refund/notify");
            WxPaymentRefundModel.RefundAmount amount = new WxPaymentRefundModel.RefundAmount();
            amount.setRefund(refundAmount.multiply(new BigDecimal(100)).intValue());
            amount.setTotal(rechargeAmount.multiply(new BigDecimal(100)).intValue());
            amount.setCurrency("CNY");
            model.setAmount(amount);
            R<String> orderR = wxPaymentClient.refundOrderR(model);
            if(200 == orderR.getCode()){
               chargingOrderRefundService.save(chargingOrderRefund);
            }
         }
         if(2 == rechargePaymentType){
            RefundReq dto = new RefundReq();
            dto.setOutTradeNo(chargingOrder.getCode());
            dto.setOutRequestNo(chargingOrderRefund.getCode());
            dto.setRefundAmount(rechargeAmount.toString());
            dto.setRefundReason("充电完成退款");
            RefundResp resp = aliPaymentClient.refund(dto).getData();
            if(null != resp){
               SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-DDTHH:mm:ss+TIMEZONE");
               AjaxResult success = chargingOrderStartupFailureWxRefund(resp.getOutTradeNo(), resp.getTradeNo(), "SUCCESS", sdf1.format(new Date()));
               if(success.isSuccess()){
                  chargingOrderRefundService.save(chargingOrderRefund);
               }
            }
         }
      }
   }
   @Override
   public List<Map<String, Object>> getByDate(List<Long> chargingOrderIds) {
      return this.baseMapper.getByDate(chargingOrderIds);