Pu Zhibing
2025-05-19 3244b550596e0330031b3f4547356927df83b0ad
ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/service/impl/TChargingPileServiceImpl.java
@@ -26,10 +26,7 @@
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.integration.api.feignClient.IntegrationClient;
import com.ruoyi.integration.api.feignClient.IotInterfaceClient;
import com.ruoyi.integration.api.feignClient.SendMessageClient;
import com.ruoyi.integration.api.feignClient.UploadRealTimeMonitoringDataClient;
import com.ruoyi.integration.api.feignClient.*;
import com.ruoyi.integration.api.model.QrCodeDelivery;
import com.ruoyi.integration.api.model.UploadRealTimeMonitoringData;
import com.ruoyi.integration.api.vo.AddDevice;
@@ -45,6 +42,7 @@
import com.ruoyi.system.api.feignClient.SysUserClient;
import com.ruoyi.system.api.feignClient.SysUserRoleClient;
import com.ruoyi.system.api.model.SysUserRoleVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@@ -69,6 +67,9 @@
   
   @Resource
   private TChargingGunService chargingGunService;
   @Resource
   private TCECClient tcecClient;
   
   @Resource
   private SysUserClient sysUserClient;
@@ -104,7 +105,7 @@
   private IotInterfaceClient iotInterfaceClient;
   
   @Resource
   private RedisService redisService;
   private RedisTemplate redisTemplate;
   
   @Resource
   private TFaultMessageService faultMessageService;
@@ -182,22 +183,29 @@
         return AjaxResult.error("设备编号已存在");
      }
      //调用华为Iot创建设备
      AddDevice addDevice = new AddDevice();
      addDevice.setProductId("66da68d21837002b28b34ec0");
      addDevice.setNodeId(chargingPile.getCode());
      addDevice.setDeviceName(chargingPile.getName());
      addDevice.setDescription(chargingPile.getNumber().toString());
      AddDeviceResp deviceResp = iotInterfaceClient.addDevice(addDevice).getData();
      if(null != deviceResp){
         int httpStatusCode = deviceResp.getHttpStatusCode();
         if(httpStatusCode == 201){
            chargingPile.setIotdDeviceId(deviceResp.getDeviceId());
      ShowDeviceResp showDeviceResp = iotInterfaceClient.showDeviceRequest(chargingPile.getCode()).getData();
      if(null == showDeviceResp){
         //调用华为Iot创建设备
         AddDevice addDevice = new AddDevice();
         addDevice.setProductId("66da68d21837002b28b34ec0");
         addDevice.setNodeId(chargingPile.getCode());
         addDevice.setDeviceName(chargingPile.getName());
         addDevice.setDescription(chargingPile.getNumber().toString());
         AddDeviceResp deviceResp = iotInterfaceClient.addDevice(addDevice).getData();
         if(null != deviceResp){
            int httpStatusCode = deviceResp.getHttpStatusCode();
            if(httpStatusCode == 201){
               chargingPile.setIotdDeviceId(deviceResp.getDeviceId());
            }else{
               log.error("华为创建设备失败" + JSON.toJSONString(deviceResp));
               return AjaxResult.error("华为创建设备失败");
            }
         }else{
            log.error("华为创建设备失败" + JSON.toJSONString(deviceResp));
            return AjaxResult.error("华为创建设备失败");
         }
      }else{
         return AjaxResult.error("华为创建设备失败");
         chargingPile.setIotdDeviceId(showDeviceResp.getDeviceId());
      }
      this.save(chargingPile);
      return AjaxResult.success();
@@ -302,6 +310,7 @@
         TChargingGun chargingGun1 = new TChargingGun();
         chargingGun1.setId(chargingGun.getId());
         chargingGun1.setFullNumber(chargingPile.getCode() + chargingGun.getCode());
         chargingGun1.setSiteId(chargingPile.getSiteId());
         chargingGunService.updateById(chargingGun1);
         
         //下发硬件充电二维码
@@ -315,6 +324,7 @@
         qrCodeDelivery.setQr_code(code_prefix + chargingPile.getCode() + chargingGun.getCode());
         sendMessageClient.qrCodeDelivery(qrCodeDelivery);
      }
      tcecClient.superviseNotificationStationInfo(chargingPile.getSiteId());
      return AjaxResult.success();
   }
   
@@ -378,33 +388,47 @@
   @Override
   public List<TChargingPileVO> getChargingGunList(Integer siteId,Integer type) {
      List<TChargingPileVO> chargingPileVOS = this.baseMapper.getChargingGunList(siteId);
      LambdaQueryWrapper<TChargingGun> wrapper = new LambdaQueryWrapper<>();
      if(Objects.nonNull(type)){
         wrapper.eq(TChargingGun::getChargeMode, type);
      }
      wrapper.eq(TChargingGun::getSiteId, siteId);
      List<TChargingGun> chargingGuns = chargingGunService.list(wrapper);
      // 查询充电枪信息
      chargingPileVOS.forEach(item -> {
         List<TChargingGun> chargingGunList = chargingGuns.stream().filter(gun -> gun.getChargingPileId().equals(item.getId())).collect(Collectors.toList());
         for (TChargingGun chargingGun : chargingGunList) {
            if(chargingGun.getStatus().equals(4)){
               // 查询正在充电的单子的实时记录
               TChargingOrder chargingOrder = chargingOrderClient.getOrderDetailByGunId(chargingGun.getId()).getData();
               if(Objects.nonNull(chargingOrder)){
                  UploadRealTimeMonitoringData uploadRealTimeMonitoringData = uploadRealTimeMonitoringDataClient.chargingOrderInfo(chargingOrder.getCode()).getData();
                  chargingGun.setSoc(uploadRealTimeMonitoringData.getSoc());
               }else {
      Set<Integer> collect = chargingGuns.stream().map(TChargingGun::getChargingPileId).collect(Collectors.toSet());
      List<TChargingPileVO> chargingPileVOS = new ArrayList<>();
      if(collect.size() > 0){
         List<TChargingPile> tChargingPiles = this.baseMapper.selectBatchIds(collect);
         for (TChargingPile tChargingPile : tChargingPiles) {
            TChargingPileVO vo = new TChargingPileVO();
            List<TChargingGun> chargingGunList = chargingGuns.stream().filter(gun -> gun.getChargingPileId().equals(tChargingPile.getId())).collect(Collectors.toList());
            for (TChargingGun chargingGun : chargingGunList) {
               if(chargingGun.getStatus().equals(4)){
                  // 查询正在充电的单子的实时记录
                  TChargingOrder chargingOrder = chargingOrderClient.getOrderDetailByGunId(chargingGun.getId()).getData();
                  if(Objects.nonNull(chargingOrder)){
                     UploadRealTimeMonitoringData uploadRealTimeMonitoringData = uploadRealTimeMonitoringDataClient.chargingOrderInfo(chargingOrder.getCode()).getData();
                     if(null != uploadRealTimeMonitoringData){
                        chargingGun.setSoc(uploadRealTimeMonitoringData.getSoc());
                     }else{
                        chargingGun.setSoc(0);
                     }
                  }else {
                     chargingGun.setSoc(100);
                  }
               }
               if(chargingGun.getStatus().equals(5)){
                  chargingGun.setSoc(100);
               }
            }
            if(chargingGun.getStatus().equals(5)){
               chargingGun.setSoc(100);
            }
            vo.setChargingGunList(chargingGunList);
            vo.setId(tChargingPile.getId());
            vo.setCode(tChargingPile.getCode());
            vo.setName(tChargingPile.getName());
            vo.setNumber(tChargingPile.getNumber());
            chargingPileVOS.add(vo);
         }
         item.setChargingGunList(chargingGunList);
      });
      }
      return chargingPileVOS;
   }
   
@@ -479,16 +503,18 @@
      chargeTrend.put("value", value1);
      chargeMonitoring.setChargeTrend(chargeTrend);
      
      //每日利用率=当日充电度数/(总桩数量*桩总功率*24小时)
      //每日利用率=当日充电度数/(桩总功率*24小时)
      List<TChargingPile> list = this.list(new LambdaQueryWrapper<TChargingPile>().in(TChargingPile::getSiteId, siteIds).eq(TChargingPile::getDelFlag, 0));
      BigDecimal v = list.stream().map(TChargingPile::getRatedPower).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(4, RoundingMode.HALF_EVEN);
      List<Double> data = chargingOrderAccountingStrategyClient.getDailyChargingDegree(6, siteIds).getData();
      List<Double> value2 = new ArrayList<>();
      BigDecimal multiply = v.multiply(new BigDecimal(24));
      for (Double datum : data) {
         if(list.size() == 0){
            value2.add(0D);
         }else{
            Double datum1 = new BigDecimal(datum).divide(new BigDecimal(list.size()).multiply(v).multiply(new BigDecimal(24)), new MathContext(4, RoundingMode.HALF_EVEN)).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
            Double datum1 = new BigDecimal(datum).divide(multiply, new MathContext(4, RoundingMode.HALF_EVEN))
                  .multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
            value2.add(datum1);
         }
      }
@@ -606,6 +632,8 @@
               if(null != appUserCarId){
                  TAppUserCar tAppUserCar = appUserCarClient.getCarByIds(Arrays.asList(appUserCarId)).getData().get(0);
                  gunMonitoring.setLicensePlate(tAppUserCar.getLicensePlate());
               }else{
                  gunMonitoring.setLicensePlate(chargingOrder.getPlateNum());
               }
               UploadRealTimeMonitoringData data = uploadRealTimeMonitoringDataClient.chargingOrderInfo(chargingOrder.getCode()).getData();
@@ -674,19 +702,22 @@
    */
   @Override
   public void updateStatus() {
      Map<String, Object> charging_gun_online = redisService.getCacheMap("charging_gun_online");
      Set<String> keySet = charging_gun_online.keySet();
      List<TChargingGun> list = new ArrayList<>();
      if(keySet.size() > 0){
         list = chargingGunService.list(new LambdaQueryWrapper<TChargingGun>().eq(TChargingGun::getDelFlag, 0).in(TChargingGun::getFullNumber, keySet));
      }
      List<TChargingGun> list = chargingGunService.list(new LambdaQueryWrapper<TChargingGun>().eq(TChargingGun::getDelFlag, 0));
      for (TChargingGun chargingGun : list) {
         Long time = (Long) charging_gun_online.get(chargingGun.getFullNumber());
         if(null != time && System.currentTimeMillis() - time > 60000){
         Long time = (Long) redisTemplate.opsForHash().get("charging_gun_online", chargingGun.getFullNumber());
         //判断设备是否真的离线3分钟(2分钟+代码中的60秒间隔处理存储)
         if(null != time && System.currentTimeMillis() - time > 180000){
            TChargingGun chargingGun1 = new TChargingGun();
            chargingGun1.setId(chargingGun.getId());
            chargingGun1.setStatus(1);
            chargingGunService.updateById(chargingGun1);
            new Thread(new Runnable() {
               @Override
               public void run() {
                  //推送状态给三方平台
                  tcecClient.pushChargingGunStatus(chargingGun1.getFullNumber(), chargingGun1.getStatus());
               }
            }).start();
            
            //桩处于正常,桩所属的枪都处于非正常,修改桩状态为异常
            TChargingPile chargingPile = this.getById(chargingGun.getChargingPileId());
@@ -727,7 +758,6 @@
                  siteName = siteName.substring(0,10);
               }
               chargingPileNotificationService.saveData(4,faultMessage.getSiteId(),faultMessage.getChargingPileId(),site.getPhone(),"检测到"+siteName+"..."+chargingPile.getNumber()+"号桩设备离线,请及时查看处理!");
            }
         }
      }