luodangjia
2024-10-11 bd97f8c5cd9be78abfaaab295319bd7ceef98f11
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TChargingOrderServiceImpl.java
@@ -11,6 +11,7 @@
import com.ruoyi.account.api.vo.GetInviteUser;
import com.ruoyi.chargingPile.api.feignClient.*;
import com.ruoyi.chargingPile.api.model.*;
import com.ruoyi.chargingPile.api.vo.GetChargingGunByCode;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.dto.ChargingOrderGroup;
import com.ruoyi.common.core.dto.ChargingPercentProvinceDto;
@@ -37,6 +38,7 @@
import com.ruoyi.order.api.query.SettlementListQuery;
import com.ruoyi.order.api.query.UploadRealTimeMonitoringDataQuery;
import com.ruoyi.order.api.vo.*;
import com.ruoyi.order.api.vo.PlatformStopChargingReplyVO;
import com.ruoyi.order.dto.*;
import com.ruoyi.order.mapper.TChargingOrderMapper;
import com.ruoyi.order.mapper.TSettlementConfirmMapper;
@@ -189,6 +191,9 @@
   //计数器
   private Map<String, Integer> counter_map = new HashMap<>();
   //计数器
   private Map<String, Integer> boot_failed_map = new HashMap<>();
@@ -449,7 +454,7 @@
      
      //直营站点才可以享受会员折扣
      if(null != appUser.getVipId() && 1 == site.getBusinessCategory()){
         TVip vip = vipClient.getInfo(appUser.getVipId()).getData();
         TVip vip = vipClient.getInfo1(appUser.getVipId()).getData();
         BigDecimal discount = null;
         if(1 == vip.getType()){
            //普通会员折扣使用积分策略上的折扣,且有最高优惠金额
@@ -541,7 +546,7 @@
      preChargeCheck.setSecureConnectionDetection(false);
      preChargeCheck.setStartupSuccess(1);
      String key = "AQJC_" + chargingOrder.getChargingGunId();
      redisService.setCacheObject(key, preChargeCheck);
      redisService.setCacheObject(key, preChargeCheck, 24L, TimeUnit.HOURS);
      
      //根据当前充值的金额和计费模板算出充电的金额
      BigDecimal rechargeAmount = chargingOrder.getRechargeAmount();
@@ -602,7 +607,7 @@
      if(discountAmount.compareTo(BigDecimal.ZERO) >= 0){
         //计算会员最大优惠金额
         if(null != appUser.getVipId()){
            TVip vip = vipClient.getInfo(appUser.getVipId()).getData();
            TVip vip = vipClient.getInfo1(appUser.getVipId()).getData();
            BigDecimal maximumDeduction = vip.getMaximumDeduction();
            //普通会员有最高优惠限制
            if(vip.getType() == 1 && discountAmount.compareTo(maximumDeduction) > 0){
@@ -614,7 +619,7 @@
            getAppUserVipDetail.setAppUserId(chargingOrder.getAppUserId());
            getAppUserVipDetail.setVipId(appUser.getVipId());
            TAppUserVipDetail data = appUserVipDetailClient.getAppUserVipDetail(getAppUserVipDetail).getData();
            if(data.getChargeNum() > 0){
            if(null != data && data.getChargeNum() > 0){
               data.setChargeNum(data.getChargeNum() - 1);
               appUserVipDetailClient.updateAppUserVipDetail(data);
               //会员有充电优惠次数,直接将优惠金额加入到充电费用中增加充电时长
@@ -622,6 +627,9 @@
            }
         }
      }
      electrovalence = electrovalence.setScale(2, RoundingMode.HALF_EVEN);
      discountAmount = discountAmount.setScale(2, RoundingMode.HALF_EVEN);
      chargingOrder.setChargeAmount(electrovalence);
      chargingOrder.setVipDiscountAmount(discountAmount);
      this.updateById(chargingOrder);
@@ -636,72 +644,18 @@
      //使用订单id作为逻辑卡号
      platformStartCharging.setCard_number(chargingOrder.getId().toString());
      platformStartCharging.setAccount_balance(electrovalence);
      System.err.println("-------------------远程调起开始充电请求-------------------");
      System.err.println(platformStartCharging.toString());
      log.error(chargingOrder.getCode() + ":-------------------远程调起开始充电请求-------------------");
      log.error(platformStartCharging.toString());
      sendMessageClient.platformStartCharging(platformStartCharging);
      //异步线程检测远程启动的应答结果。如果失败,则需要全额退款
      String code = chargingOrder.getCode();
      Long id = chargingOrder.getId();
      //执行5分钟的定时任务检测
      ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
      scheduler.scheduleAtFixedRate(()->{
         //获取安全校验
         SecurityDetection securityDetection = securityDetectionClient.getSecurityDetection(chargingOrder.getCode()).getData();
         if(null != securityDetection){
            PreChargeCheck preChargeCheck1 =  redisService.getCacheObject("AQJC_" + chargingOrder.getChargingGunId());
            if(null != preChargeCheck1){
               preChargeCheck1.setElectronicLockLock(preChargeCheck1.getElectronicLockLock());
               preChargeCheck1.setInsulationTesting(true);
               preChargeCheck1.setSecureConnectionDetection(preChargeCheck1.getSecureConnectionDetection());
               preChargeCheck1.setStartupSuccess(1);
               redisService.setCacheObject("AQJC_" + chargingOrder.getChargingGunId(), preChargeCheck1);
            }
         }
         List<PlatformStartChargingReply> data = platformStartChargingReplyClient.getPlatformStartChargingReply(code).getData();
         System.err.println("-------------------开始检查调起充电结果-------------------");
         System.err.println(data.toString());
         if(data.size() != 0){
            PlatformStartChargingReply platformStartChargingReply = data.get(1);
            Integer startup_result = platformStartChargingReply.getStartup_result();
            Integer failure_cause = platformStartChargingReply.getFailure_cause();
            Integer counter = counter_map.get(code);
            PreChargeCheck preChargeCheck1 = redisService.getCacheObject(key);
            //5分钟内还未插枪则取消充电,退回金额。
            if(failure_cause == 5 && (null == counter || counter < 300)){
               counter = (null == counter ? 0 : counter) + 1;
               counter_map.put(code, counter);
               //启动失败
               preChargeCheck1.setStartupSuccess(3);
               preChargeCheck1.setFailureCause(failure_cause);
               redisService.setCacheObject(key, preChargeCheck1);
               return;
            }
            //清除计时器中的无效数据
            counter_map.remove(code);
            TChargingOrder order = this.getOne(new LambdaQueryWrapper<TChargingOrder>().eq(TChargingOrder::getCode, code));
            if(0 == startup_result){
               //启动失败
               preChargeCheck1.setStartupSuccess(3);
               preChargeCheck1.setFailureCause(failure_cause);
               //启动失败后取消订单,退款操作
               refund(code);
               order.setStatus(-1);
               order.setEndMode(0);
            }else{
               //启动成功
               preChargeCheck1.setStartupSuccess(2);
               order.setStatus(3);
               order.setStartTime(LocalDateTime.now());
            }
            this.updateById(order);
            redisService.setCacheObject(key, preChargeCheck1);
            //提前结束定时任务
         if(timingDetection(id)){
            scheduler.shutdown();
         }else{
            log.error("未上传开启充电结果........");
         }
      }, 5, 1, TimeUnit.SECONDS);
      return AjaxResult.success();
@@ -709,10 +663,167 @@
   
   
   /**
    * 定时检测mongodb数据库数据
    * @param id
    * @return
    */
   public boolean timingDetection(Long id){
      TChargingOrder chargingOrder = this.getById(id);
      if(chargingOrder.getStatus() != 2){
         return true;
      }
      String code = chargingOrder.getCode();
      String key = "AQJC_" + chargingOrder.getChargingGunId();
      //获取安全校验
      com.ruoyi.integration.api.model.SecurityDetection securityDetection = securityDetectionClient.getSecurityDetection(chargingOrder.getCode()).getData();
      if(null != securityDetection){
         PreChargeCheck preChargeCheck1 =  redisService.getCacheObject("AQJC_" + chargingOrder.getChargingGunId());
         if(null != preChargeCheck1){
            preChargeCheck1.setElectronicLockLock(preChargeCheck1.getElectronicLockLock());
            preChargeCheck1.setInsulationTesting(true);
            preChargeCheck1.setSecureConnectionDetection(preChargeCheck1.getSecureConnectionDetection());
            preChargeCheck1.setStartupSuccess(1);
            redisService.setCacheObject("AQJC_" + chargingOrder.getChargingGunId(), preChargeCheck1, 24L, TimeUnit.HOURS);
         }
      }
      List<PlatformStartChargingReply> data = platformStartChargingReplyClient.getPlatformStartChargingReply(code).getData();
      log.error(code + ":-------------------开始检查调起充电结果-------------------");
      log.error(data.toString());
      if(data.size() != 0){
         PlatformStartChargingReply platformStartChargingReply = data.get(1);
         Integer startup_result = platformStartChargingReply.getStartup_result();
         Integer failure_cause = platformStartChargingReply.getFailure_cause();
         Integer counter = counter_map.get(code);
         PreChargeCheck preChargeCheck1 = redisService.getCacheObject(key);
         //5分钟内还未插枪则取消充电,退回金额。
         if(failure_cause == 5 && (null == counter || counter < 300)){
            counter = (null == counter ? 0 : counter) + 1;
            counter_map.put(code, counter);
            //启动失败
            preChargeCheck1.setStartupSuccess(3);
            preChargeCheck1.setFailureCause(failure_cause);
            redisService.setCacheObject(key, preChargeCheck1, 24L, TimeUnit.HOURS);
            return false;
         }
         //清除计时器中的无效数据
         counter_map.remove(code);
         TChargingOrder order = this.getOne(new LambdaQueryWrapper<TChargingOrder>().eq(TChargingOrder::getCode, code));
         if(0 == startup_result){
            //启动失败
            preChargeCheck1.setStartupSuccess(3);
            preChargeCheck1.setFailureCause(failure_cause);
            //启动失败后取消订单,退款操作
            refund(code);
            order.setStatus(-1);
            order.setEndMode(0);
         }else{
            //启动成功
            preChargeCheck1.setStartupSuccess(2);
            order.setStatus(3);
            order.setStartTime(LocalDateTime.now());
         }
         this.updateById(order);
         redisService.setCacheObject(key, preChargeCheck1, 24L, TimeUnit.HOURS);
         return true;
      }else{
         log.error(code + ":-------------------未上传开启充电结果-------------------");
         Integer counter = boot_failed_map.get(code);
         PreChargeCheck preChargeCheck1 = redisService.getCacheObject(key);
         //5分钟内未启动成功,退回金额。
         if(null == counter || counter < 300){
            counter = (null == counter ? 0 : counter) + 1;
            boot_failed_map.put(code, counter);
            //启动失败
            preChargeCheck1.setStartupSuccess(3);
            preChargeCheck1.setFailureCause(0);
            redisService.setCacheObject(key, preChargeCheck1, 24L, TimeUnit.HOURS);
            return false;
         }
         //清除计时器中的无效数据
         boot_failed_map.remove(code);
         TChargingOrder order = this.getOne(new LambdaQueryWrapper<TChargingOrder>().eq(TChargingOrder::getCode, code));
         //启动失败
         preChargeCheck1.setStartupSuccess(3);
         preChargeCheck1.setFailureCause(0);
         //启动失败后取消订单,退款操作
         refund(code);
         order.setStatus(-1);
         order.setEndMode(0);
         this.updateById(order);
         redisService.setCacheObject(key, preChargeCheck1, 24L, TimeUnit.HOURS);
         return true;
      }
   }
   /**
    * 修改安全检测数据
    * @param securityDetection
    */
   @Override
   public void securityDetection(SecurityDetectionVO securityDetection){
      GetChargingGunByCode code = new GetChargingGunByCode();
      code.setCharging_pile_code(securityDetection.getCharging_pile_code());
      code.setCharging_gun_code(securityDetection.getCharging_gun_code());
      TChargingGun chargingGun = chargingGunClient.getChargingGunByCode(code).getData();
      PreChargeCheck preChargeCheck1 =  redisService.getCacheObject("AQJC_" + chargingGun.getId());
      if(null != preChargeCheck1){
         preChargeCheck1.setElectronicLockLock(preChargeCheck1.getElectronicLockLock());
         preChargeCheck1.setInsulationTesting(true);
         preChargeCheck1.setSecureConnectionDetection(preChargeCheck1.getSecureConnectionDetection());
         preChargeCheck1.setStartupSuccess(1);
         redisService.setCacheObject("AQJC_" + chargingGun.getId(), preChargeCheck1, 24L, TimeUnit.HOURS);
      }
   }
   /**
    * 启动充电应发
    * @param message
    */
   @Override
   public void startChargeSuccessfully(PlatformStartChargingReplyMessageVO message) {
      Integer startup_result = message.getStartup_result();
      Integer failure_cause = message.getFailure_cause();
      TChargingOrder order = this.getOne(new LambdaQueryWrapper<TChargingOrder>().eq(TChargingOrder::getCode, message.getTransaction_serial_number()));
      if(order.getStatus() != 2){
         return;
      }
      String code = order.getCode();
      PreChargeCheck preChargeCheck1 =  redisService.getCacheObject("AQJC_" + order.getChargingGunId());
      if(null != preChargeCheck1){
         preChargeCheck1.setElectronicLockLock(preChargeCheck1.getElectronicLockLock());
         preChargeCheck1.setInsulationTesting(true);
         preChargeCheck1.setSecureConnectionDetection(preChargeCheck1.getSecureConnectionDetection());
         preChargeCheck1.setStartupSuccess(1);
      }
      if(0 == startup_result){
         //启动失败
         preChargeCheck1.setStartupSuccess(3);
         preChargeCheck1.setFailureCause(failure_cause);
         //启动失败后取消订单,退款操作
         refund(code);
         order.setStatus(-1);
         order.setEndMode(0);
      }else{
         //启动成功
         preChargeCheck1.setStartupSuccess(2);
         order.setStatus(3);
         order.setStartTime(LocalDateTime.now());
      }
      redisService.setCacheObject("AQJC_" + order.getChargingGunId(), preChargeCheck1, 24L, TimeUnit.HOURS);
      this.updateById(order);
   }
   /**
    * 启动失败后的退款,取消订单
    * @param code
    */
   public void refund(String code){
      log.error(code + ":-------------------充电启动失败,执行退款-------------------");
      TChargingOrder chargingOrder = this.getOne(new LambdaQueryWrapper<TChargingOrder>().eq(TChargingOrder::getCode, code));
      if(chargingOrder.getStatus() == 2){
         Integer rechargePaymentType = chargingOrder.getRechargePaymentType();
@@ -847,9 +958,13 @@
   public AjaxResult stopCharging(String id) {
      TChargingOrder chargingOrder = this.getById(id);
      Integer status = chargingOrder.getStatus();
      if(status != 3){
         return AjaxResult.error("还未开始充电");
      }
      if(status == 4 || status == 5){
         return AjaxResult.error("不能重复操作");
      }
      chargingOrder.setEndTime(LocalDateTime.now());
      chargingOrder.setStatus(4);
      chargingOrder.setEndMode(1);
      this.updateById(chargingOrder);
@@ -910,7 +1025,7 @@
            }
         }
      }
      String code1 = chargingOrder.getCode();
      
      //异步线程处理停机
      ExecutorService cachedThreadPool = Executors.newFixedThreadPool(1);
@@ -922,12 +1037,17 @@
         platformStopCharging.setCharging_pile_code(chargingPile.getCode());
         platformStopCharging.setCharging_gun_code(chargingGun.getCode());
         sendMessageClient.platformStopCharging(platformStopCharging);
         log.error(code1 + ":-------------------远程停止充电请求-------------------");
         log.error(platformStopCharging.toString());
         //开始查询停机应答,成功后开始计费费用
         for (int i = 0; i < 60; i++) {
            GetPlatformStopChargingReply query = new GetPlatformStopChargingReply();
            query.setCharging_gun_code(chargingGun.getCode());
            query.setCharging_pile_code(chargingPile.getCode());
            query.setEnd_time(chargingOrder.getEndTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            PlatformStopChargingReply reply = platformStopChargingReplyClient.getPlatformStopChargingReply(query).getData();
            log.error(code1 + ":-------------------查询远程停止充电应答-------------------");
            log.error(reply.toString());
            if(null == reply){
               try {
                  Thread.sleep(1000);
@@ -936,7 +1056,12 @@
               }
               continue;
            }
            TChargingOrder chargingOrder1 = this.getById(id);
            if(chargingOrder1.getStatus() != 3){
               break;
            }
            if(0 == reply.getStop_result()){
               String failure_cause = "";
               switch (reply.getFailure_cause()){
@@ -953,8 +1078,13 @@
                     failure_cause = "其他";
                     break;
               }
               log.error("停机失败:订单号:{},失败原因:{}", chargingOrder.getCode(), failure_cause);
               throw new RuntimeException(failure_cause);
               log.error(code1 + ":停机失败:订单号:{},失败原因:{}", chargingOrder.getCode(), failure_cause);
               try {
                  Thread.sleep(1000);
               } catch (InterruptedException e) {
                  throw new RuntimeException(e);
               }
               continue;
            }
            // 将枪状态重置为空闲
@@ -962,6 +1092,7 @@
            chargingGunClient.updateChargingGunById(chargingGun);
            //计算费用,处理退款
            endCharge(chargingOrder);
            log.error(code1 + ":-------------------远程停止充电请求成功-------------------");
            break;
         }
      });
@@ -979,7 +1110,7 @@
         
         TAppUser appUser1 = appUserClient.getUserById(appUser.getInviteUserId()).getData();
         if(null != appUser1.getVipId()){
            TVip vip = vipClient.getInfo(appUser1.getVipId()).getData();
            TVip vip = vipClient.getInfo1(appUser1.getVipId()).getData();
            Integer doubleIntegration = vip.getDoubleIntegration();
            //双倍积分
            if(1 == doubleIntegration){
@@ -1018,8 +1149,56 @@
      return AjaxResult.success();
   }
   /**
    * 停止充电应答结果处理
    * @param platformStopChargingReply
    */
   @Override
   public void terminateSuccessfulResponse(PlatformStopChargingReplyVO platformStopChargingReply){
      GetChargingGunByCode code = new GetChargingGunByCode();
      code.setCharging_gun_code(platformStopChargingReply.getCharging_gun_code());
      code.setCharging_pile_code(platformStopChargingReply.getCharging_pile_code());
      TChargingGun chargingGun = chargingGunClient.getChargingGunByCode(code).getData();
      TChargingOrder order = this.getOne(new LambdaQueryWrapper<TChargingOrder>().eq(TChargingOrder::getChargingGunId, chargingGun.getId())
            .orderByDesc(TChargingOrder::getEndTime).eq(TChargingOrder::getRechargePaymentStatus, 2).last(" limit 0, 1"));
      if(null != order && order.getStatus() != 3){
         return;
      }
      String code1 = order.getCode();
      if(0 == platformStopChargingReply.getStop_result()){
         String failure_cause = "";
         switch (platformStopChargingReply.getFailure_cause()){
            case 0:
               failure_cause = "无";
               break;
            case 1:
               failure_cause = "设备编号不匹配";
               break;
            case 2:
               failure_cause = "枪未处于充电状态";
               break;
            case 3:
               failure_cause = "其他";
               break;
         }
         log.error(code1 + ":停机失败:订单号:{},失败原因:{}", order.getCode(), failure_cause);
         return;
      }
      // 将枪状态重置为空闲
      chargingGun.setStatus(2);
      chargingGunClient.updateChargingGunById(chargingGun);
      //计算费用,处理退款
      endCharge(order);
   }
   /**
    * 手动结束后的费用计算和退款逻辑
    */
@@ -1061,7 +1240,7 @@
         Integer num1 = JSON.parseObject(integralRule.getChargeCredit()).getInteger("num1");
         Integer integral = payAmount.multiply(new BigDecimal(num1)).intValue();
         if(null != appUser.getVipId()){
            TVip vip = vipClient.getInfo(appUser.getVipId()).getData();
            TVip vip = vipClient.getInfo1(appUser.getVipId()).getData();
            Integer doubleIntegration = vip.getDoubleIntegration();
            //双倍积分
            if(1 == doubleIntegration){
@@ -1791,7 +1970,7 @@
         
         TAppUser appUser1 = appUserClient.getUserById(appUser.getInviteUserId()).getData();
         if(null != appUser1.getVipId()){
            TVip vip = vipClient.getInfo(appUser1.getVipId()).getData();
            TVip vip = vipClient.getInfo1(appUser1.getVipId()).getData();
            Integer doubleIntegration = vip.getDoubleIntegration();
            //双倍积分
            if(1 == doubleIntegration){