Pu Zhibing
2024-10-16 c4664502dfdaffff555b532e65b51a57ac8b29c2
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TChargingOrderServiceImpl.java
@@ -65,6 +65,7 @@
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
@@ -146,9 +147,6 @@
   @Resource
   private AccountingStrategyDetailOrderClient accountingStrategyDetailOrderClient;
   @Resource
   private AccountingStrategyOrderClient accountingStrategyOrderClient;
   @Resource
   private AccountingStrategyClient accountingStrategyClient;
@@ -892,13 +890,11 @@
    * @return
    */
   public AjaxResult chargingOrderStartupFailureWxRefund(String out_refund_no, String refund_id, String tradeState, String success_time){
      if("SUCCESS".equals(tradeState)){
         TChargingOrderRefund one = chargingOrderRefundService.getOne(new LambdaQueryWrapper<TChargingOrderRefund>().eq(TChargingOrderRefund::getRefundCode, out_refund_no));
         one.setRefundSerialNumber(refund_id);
         one.setRefundStatus(2);
         one.setRefundTime(LocalDateTime.now());
         chargingOrderRefundService.updateById(one);
      }
      TChargingOrderRefund one = chargingOrderRefundService.getOne(new LambdaQueryWrapper<TChargingOrderRefund>().eq(TChargingOrderRefund::getRefundCode, out_refund_no));
      one.setRefundSerialNumber(refund_id);
      one.setRefundStatus(2);
      one.setRefundTime(LocalDateTime.now());
      chargingOrderRefundService.updateById(one);
      return AjaxResult.success();
   }
@@ -1843,25 +1839,6 @@
         return;
      }
      
      //先判断总的充电度数是否和总报文消息中的数据相同,不相同等待30s
      UploadRealTimeMonitoringData uploadRealTimeMonitoringData = uploadRealTimeMonitoringDataClient.chargingOrderInfo(chargingOrder.getCode()).getData();
      if(null != uploadRealTimeMonitoringData){
         for (int i = 0; i < 30; i++) {
            List<TChargingOrderAccountingStrategy> list1 = chargingOrderAccountingStrategyService.list(new LambdaQueryWrapper<TChargingOrderAccountingStrategy>().eq(TChargingOrderAccountingStrategy::getChargingOrderId, chargingOrder.getId()));
            BigDecimal reduce = list1.stream().map(TChargingOrderAccountingStrategy::getChargingCapacity).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal charging_degree = uploadRealTimeMonitoringData.getCharging_degree();
            if(charging_degree.compareTo(reduce) != 0){
               try {
                  Thread.sleep(1000);
               } catch (InterruptedException e) {
                  throw new RuntimeException(e);
               }
            }else{
               break;
            }
         }
      }
      //计算用户标签
      editUserTag(chargingOrder);
      //用户推荐奖励
@@ -1870,17 +1847,68 @@
      //如果使用优惠券需要判断优惠券是否满足使用条件
      //根据实际的充电金额计算退款金额   退回费用=(原金额/总金额)*(总金额-实际充电金额)
      //退款金额=优惠券金额+剩余充电金额
      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);
      //获取订单的计费策略
      List<AccountingStrategyDetailOrder> accountingStrategyDetailOrderList = accountingStrategyDetailOrderClient.getAllAccountingStrategyDetailOrder(chargingOrder.getId()).getData();
      chargingOrderAccountingStrategyService.remove(new LambdaQueryWrapper<TChargingOrderAccountingStrategy>().eq(TChargingOrderAccountingStrategy::getChargingOrderId, chargingOrder.getId()));
      for (int i = 0; i < 12; i++) {
         Class<? extends TransactionRecordMessageVO> clazz = vo.getClass();
         try {
            String time = (String) clazz.getMethod("getTime" + (i + 1)).invoke(vo);
            if(StringUtils.hasLength(time)){
               //充电度数
               Object invoke = clazz.getMethod("getSharp_peak_charge" + (i + 1)).invoke(vo);
               BigDecimal Sharp_peak_charge = new BigDecimal(invoke.toString());
               //充电金额
               Object invoke1 = clazz.getMethod("getSharp_peak_amount" + (i + 1)).invoke(vo);
               BigDecimal sharp_peak_amount = new BigDecimal(invoke1.toString());
               String[] split = time.split("~");
               AccountingStrategyDetailOrder strategyDetail = accountingStrategyDetailOrderList.stream().filter(s -> s.getStartTime().equals(split[1]) && s.getEndTime().equals(split[2])).findFirst().get();
               TChargingOrderAccountingStrategy chargingOrderAccountingStrategy = new TChargingOrderAccountingStrategy();
               chargingOrderAccountingStrategy.setChargingOrderId(chargingOrder.getId());
               chargingOrderAccountingStrategy.setAccountingStrategyDetailId(strategyDetail.getId());
               chargingOrderAccountingStrategy.setType(strategyDetail.getType());
               chargingOrderAccountingStrategy.setStartTime(chargingOrder.getStartTime().format(DateTimeFormatter.ofPattern("HH:mm")));
               chargingOrderAccountingStrategy.setEndTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm")));
               chargingOrderAccountingStrategy.setElectrovalence(strategyDetail.getElectrovalence());
               chargingOrderAccountingStrategy.setServiceCharge(strategyDetail.getServiceCharge());
               chargingOrderAccountingStrategy.setCostServiceCharge(strategyDetail.getCostServiceCharge());
               //已充电总度数
               BigDecimal electrovalenc = strategyDetail.getElectrovalence().multiply(Sharp_peak_charge);
               BigDecimal originalServicePrice = strategyDetail.getServiceCharge().multiply(Sharp_peak_charge);
               BigDecimal serviceCharge = originalServicePrice;
               //计算优惠金额
               if(null != chargingOrder.getVipDiscount()){
                  serviceCharge = serviceCharge.multiply(chargingOrder.getVipDiscount().divide(new BigDecimal(10)));
               }
               chargingOrderAccountingStrategy.setChargingCapacity(Sharp_peak_charge);
               chargingOrderAccountingStrategy.setPeriodElectricPrice(electrovalenc);
               chargingOrderAccountingStrategy.setPeriodServicePrice(serviceCharge);
               chargingOrderAccountingStrategy.setPeriodOriginalServicePrice(originalServicePrice);
               chargingOrderAccountingStrategy.setCreateTime(LocalDateTime.now());
               chargingOrderAccountingStrategyService.save(chargingOrderAccountingStrategy);
               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);
            }
         } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
         } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
         } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
         }
      }
      //原金额
      BigDecimal rechargeAmount = chargingOrder.getRechargeAmount();
      BigDecimal vipDiscountAmount = chargingOrder.getVipDiscountAmount();
@@ -1927,6 +1955,7 @@
      if(null == chargingOrder.getEndMode()){
         order.setEndMode(1);
      }
      order.setResidualAmount(rechargeAmount.subtract(total));
      order.setEndTime(LocalDateTime.now());
      order.setStatus(5);
      order.setOrderAmount(orderAmount);
@@ -2192,10 +2221,10 @@
               RefundResp resp = aliPaymentClient.refund(dto).getData();
               if(null != resp){
                  SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-DDTHH:mm:ss+TIMEZONE");
                  tChargingOrder.setRefundStatus(2);
                  tChargingOrder.setRefundAmount((tChargingOrder.getRefundAmount()==null? BigDecimal.valueOf(0) :tChargingOrder.getRefundAmount()).add(payOrderQueryDto.getRefundAmount()));
                  chargingOrderRefund.setRefundStatus(2);
                  chargingOrderRefund.setRefundAmount((tChargingOrder.getRefundAmount()==null? BigDecimal.valueOf(0) :tChargingOrder.getRefundAmount()).add(payOrderQueryDto.getRefundAmount()));
                  this.baseMapper.updateById(tChargingOrder);
                     chargingOrderRefundService.save(chargingOrderRefund);
                  chargingOrderRefundService.save(chargingOrderRefund);
               }
            }