Pu Zhibing
2025-05-19 3244b550596e0330031b3f4547356927df83b0ad
ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/controller/TChargingPileController.java
@@ -1,16 +1,19 @@
package com.ruoyi.chargingPile.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.chargingPile.api.model.TChargingGun;
import com.ruoyi.chargingPile.api.model.TChargingPile;
import com.ruoyi.chargingPile.api.model.TFaultMessage;
import com.ruoyi.chargingPile.api.query.TChargingGunQuery;
import com.ruoyi.chargingPile.api.vo.TChargingGunVO;
import com.ruoyi.chargingPile.api.vo.UpdateChargingPileStatusVo;
import com.ruoyi.chargingPile.dto.ChargeMonitoring;
import com.ruoyi.chargingPile.dto.ChargingGunCountMonitoring;
import com.ruoyi.chargingPile.dto.ChargingGunMonitoring;
import com.ruoyi.chargingPile.dto.GetChargingGunMonitoring;
import com.ruoyi.chargingPile.service.TChargingGunService;
import com.ruoyi.chargingPile.service.TChargingPileService;
import com.ruoyi.chargingPile.service.*;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageInfo;
@@ -24,10 +27,19 @@
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.log.enums.OperatorType;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.annotation.Logical;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.integration.api.feignClient.TCECClient;
import com.ruoyi.order.api.feignClient.ChargingOrderClient;
import com.ruoyi.order.api.model.TChargingOrder;
import com.ruoyi.order.api.vo.TChargingOrderVo;
import com.ruoyi.other.api.domain.TVip;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@@ -35,7 +47,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
/**
 * <p>
@@ -51,23 +66,50 @@
public class TChargingPileController {
    private final TChargingPileService chargingPileService;
    private final TChargingGunService chargingGunService;
   @Resource
   private RedisTemplate redisTemplate;
   @Resource
   private TFaultMessageService faultMessageService;
   @Resource
   private ChargingOrderClient chargingOrderClient;
   @Resource
   private TCECClient tcecClient;
    @Autowired
    public TChargingPileController(TChargingPileService chargingPileService) {
    public TChargingPileController(TChargingPileService chargingPileService, TChargingGunService chargingGunService) {
        this.chargingPileService = chargingPileService;
        this.chargingGunService = chargingGunService;
    }
    /**
     * 查询充电桩列表
     */
    @ApiOperation(tags = {"小程序-充电桩"},value = "查询充电桩列表")
    @PostMapping(value = "/list")
    @ApiOperation(tags = {"小程序-充电桩"},value = "通过站点id查询充电桩列表")
    @GetMapping(value = "/list")
    @ApiImplicitParams({
          @ApiImplicitParam(name = "siteId", value = "站点id")
    })
    public AjaxResult<List<TChargingPile>> list(@RequestParam("siteId") Integer siteId) {
        return AjaxResult.ok(chargingPileService.list(Wrappers.lambdaQuery(TChargingPile.class)
                .eq(TChargingPile::getSiteId,siteId)));
    }
   /**
     * 查询充电桩列表
     */
    @ApiOperation(tags = {"通过充电桩id 查询充电枪列表"},value = "通过充电桩id 查询充电枪列表")
    @PostMapping(value = "/pileList")
    @ApiImplicitParams({
          @ApiImplicitParam(name = "pileId", value = "充电桩id")
    })
    public AjaxResult<List<TChargingGun>> pileList(@RequestParam("pileId") Integer pileId) {
        return AjaxResult.ok(chargingGunService.list(Wrappers.lambdaQuery(TChargingGun.class)
                .eq(TChargingGun::getChargingPileId,pileId)));
    }
   /**
     * 管理后台-通过站点id获取充电桩列表 不分页
@@ -78,8 +120,16 @@
        return R.ok(chargingPileService.list(Wrappers.lambdaQuery(TChargingPile.class)
                .eq(TChargingPile::getSiteId,siteId)));
    }
   @PostMapping(value = "/getChargingPileBySiteIds")
   public R<List<TChargingPile>> getChargingPileBySiteIds(@RequestParam("siteIds") List<Integer> siteIds) {
      return R.ok(chargingPileService.list(Wrappers.lambdaQuery(TChargingPile.class)
            .in(!siteIds.isEmpty(),TChargingPile::getSiteId,siteIds)));
   }
   
   
   @RequiresPermissions(value = {"/chargingPile", "/chargingGun"}, logical = Logical.OR)
   @ResponseBody
   @GetMapping("/pageChargingPileList")
   @ApiOperation(value = "获取充电桩列表数据", tags = {"管理后台-充电桩信息"})
@@ -88,8 +138,10 @@
      return AjaxResult.success(list);
   }
   
   @RequiresPermissions(value = {"/chargingPile/add"}, logical = Logical.OR)
   @ResponseBody
   @Log(title = "添加充电桩数据", businessType = BusinessType.INSERT,operatorType = OperatorType.MANAGE)
   @Log(title = "【充电桩信息】添加充电桩数据", businessType = BusinessType.INSERT,operatorType = OperatorType.MANAGE)
   @PostMapping("/addChargingPile")
   @ApiOperation(value = "添加充电桩数据", tags = {"管理后台-充电桩信息"})
   public AjaxResult addChargingPile(@RequestBody TChargingPile chargingPile){
@@ -97,6 +149,7 @@
   }
   
   
   @RequiresPermissions(value = {"/chargingPile/select", "/chargingPile/update"}, logical = Logical.OR)
   @ResponseBody
   @GetMapping("/getChargingPile/{id}")
   @ApiOperation(value = "获取充电桩详情数据", tags = {"管理后台-充电桩信息"})
@@ -106,8 +159,9 @@
   }
   
   
   @RequiresPermissions(value = {"/chargingPile/update"}, logical = Logical.OR)
   @ResponseBody
   @Log(title = "编辑充电桩数据", businessType = BusinessType.UPDATE,operatorType = OperatorType.MANAGE)
   @Log(title = "【充电桩信息】编辑充电桩数据", businessType = BusinessType.UPDATE,operatorType = OperatorType.MANAGE)
   @PostMapping("/editChargingPile")
   @ApiOperation(value = "编辑充电桩数据", tags = {"管理后台-充电桩信息"})
   public AjaxResult editChargingPile(@RequestBody TChargingPile chargingPile){
@@ -116,9 +170,10 @@
   
   
   
   @RequiresPermissions(value = {"/chargingPile/del", "/chargingPile/batch_del"}, logical = Logical.OR)
   @ResponseBody
   @DeleteMapping("/delChargingPile")
   @Log(title = "删除充电桩", businessType = BusinessType.DELETE,operatorType = OperatorType.MANAGE)
   @Log(title = "【充电桩信息】删除充电桩", businessType = BusinessType.DELETE,operatorType = OperatorType.MANAGE)
   @ApiOperation(value = "删除充电桩", tags = {"管理后台-充电桩信息"})
   @ApiImplicitParams({
         @ApiImplicitParam(value = "充电桩id", name = "id", dataTypeClass = Integer.class, required = true)
@@ -128,8 +183,9 @@
   }
   
   
   @RequiresPermissions(value = {"/chargingPile/accountingStrategy"}, logical = Logical.OR)
   @ResponseBody
   @Log(title = "批量设置计费策略", businessType = BusinessType.UPDATE,operatorType = OperatorType.MANAGE)
   @Log(title = "【充电桩信息】批量设置计费策略", businessType = BusinessType.UPDATE,operatorType = OperatorType.MANAGE)
   @PostMapping("/batchSetAccountingStrategy")
   @ApiOperation(value = "批量设置计费策略", tags = {"管理后台-充电桩信息"})
   public AjaxResult batchSetAccountingStrategy(@RequestBody BatchSetAccountingStrategy setAccountingStrategy){
@@ -138,6 +194,7 @@
   }
   
   
   @RequiresPermissions(value = {"/chargingEquipmentMonitoring"}, logical = Logical.OR)
   @ResponseBody
   @GetMapping("/chargeMonitoring/{siteId}")
   @ApiOperation(value = "获取充电设施监控数据", tags = {"管理后台-充电设备监控"})
@@ -146,6 +203,8 @@
      return AjaxResult.success(chargeMonitoring);
   }
   
   @RequiresPermissions(value = {"/chargingEquipmentMonitoring"}, logical = Logical.OR)
   @ResponseBody
   @GetMapping("/getChargingGunCountMonitoring/{siteId}")
   @ApiOperation(value = "获取充电枪各种状态汇总监控数据", tags = {"管理后台-充电设备监控"})
@@ -155,6 +214,7 @@
   }
   
   
   @RequiresPermissions(value = {"/operationMaintenanceAnalysis", "/chargingEquipmentMonitoring"}, logical = Logical.OR)
   @ResponseBody
   @GetMapping("/getChargingGunMonitoring")
   @ApiOperation(value = "获取充电枪监控数据", tags = {"管理后台-充电设备监控"})
@@ -162,8 +222,8 @@
      PageInfo<ChargingGunMonitoring> chargingGunMonitoring = chargingPileService.getChargingGunMonitoring(query);
      return AjaxResult.success(chargingGunMonitoring);
   }
   /**
    * 根据id获取充电桩信息
    * @param id
@@ -171,9 +231,148 @@
    */
   @ResponseBody
   @PostMapping("/getChargingPileById/{id}")
   public R<TChargingPile> getChargingPileById(@PathVariable Integer id){
   public R<TChargingPile> getChargingPileById(@PathVariable("id") Integer id){
      TChargingPile chargingPile = chargingPileService.getById(id);
      return R.ok(chargingPile);
   }
   /**
    * 更新充电桩状态
    */
   @ResponseBody
   @PostMapping("/updateChargingPileStatus")
   public void updateChargingPileStatus(@RequestBody UpdateChargingPileStatusVo vo){
      String pile_code = vo.getPile_code();
      String gun_code = vo.getGun_code();
      //0:正常,1:故障,2=插枪
      Integer status = vo.getStatus();
      Long time = (Long) redisTemplate.opsForHash().get("charging_gun_online", (pile_code + gun_code));
      //小于1分钟才处理数据,防止频繁查询数据
      if(null != time && (System.currentTimeMillis() - time) < 60000){
         return;
      }
      TChargingGun tChargingGun = chargingGunService.getOne(new LambdaQueryWrapper<TChargingGun>().eq(TChargingGun::getFullNumber, (pile_code + gun_code)).eq(TChargingGun::getDelFlag, 0));
      if(null != tChargingGun){
         redisTemplate.opsForHash().put("charging_gun_online", tChargingGun.getFullNumber(), System.currentTimeMillis());
         TChargingPile chargingPile = chargingPileService.getById(tChargingGun.getChargingPileId());
         //枪处于离线或者故障,返回状态为正常,则修改其状态
         if(0 == status && tChargingGun.getStatus() != 2){
            TChargingGun tChargingGun1 = new TChargingGun();
            tChargingGun1.setId(tChargingGun.getId());
            tChargingGun1.setStatus(2);
            chargingGunService.updateById(tChargingGun1);
            new Thread(new Runnable() {
               @Override
               public void run() {
                  //推送状态给三方平台
                  tcecClient.pushChargingGunStatus(tChargingGun.getFullNumber(), tChargingGun1.getStatus());
               }
            }).start();
            if(chargingPile.getStatus() != 1){
               TChargingPile chargingPile1 = new TChargingPile();
               chargingPile1.setId(chargingPile.getId());
               chargingPile1.setStatus(1);
               chargingPileService.updateById(chargingPile1);
            }
            List<TFaultMessage> list = faultMessageService.list(new LambdaQueryWrapper<TFaultMessage>().eq(TFaultMessage::getChargingGunId, tChargingGun.getId())
                  .isNull(TFaultMessage::getEndTime));
            if(!list.isEmpty()){
               list.forEach(s->s.setEndTime(LocalDateTime.now()));
               faultMessageService.updateBatchById(list);
            }
         }
         //枪处于正常,返回状态为故障,则修改其状态
         if(1 == status && tChargingGun.getStatus() != 1 && tChargingGun.getStatus() != 7){
            TChargingGun tChargingGun1 = new TChargingGun();
            tChargingGun1.setId(tChargingGun.getId());
            tChargingGun1.setStatus(7);
            chargingGunService.updateById(tChargingGun1);
            new Thread(new Runnable() {
               @Override
               public void run() {
                  //推送状态给三方平台
                  tcecClient.pushChargingGunStatus(tChargingGun.getFullNumber(), tChargingGun1.getStatus());
               }
            }).start();
            //桩处于正常,桩所属的枪都处于非正常,修改桩状态为异常
            List<TChargingGun> list = chargingGunService.list(new LambdaQueryWrapper<TChargingGun>().eq(TChargingGun::getChargingPileId, chargingPile.getId()).eq(TChargingGun::getDelFlag, 0));
            int size = list.stream().filter(s -> s.getStatus() == 1 || s.getStatus() == 7).collect(Collectors.toList()).size();
            if(chargingPile.getStatus() == 1 && list.size() == size){
               TChargingPile chargingPile1 = new TChargingPile();
               chargingPile1.setId(chargingPile.getId());
               chargingPile1.setStatus(3);
               chargingPileService.updateById(chargingPile1);
            }
            TFaultMessage faultMessage = new TFaultMessage();
            faultMessage.setSiteId(chargingPile.getSiteId());
            faultMessage.setChargingPileId(chargingPile.getId());
            faultMessage.setChargingGunId(tChargingGun.getId());
            faultMessage.setStatus(2);
            faultMessage.setDownTime(LocalDateTime.now());
            faultMessage.setCreateTime(LocalDateTime.now());
            faultMessage.setDelFlag(false);
            faultMessage.setContent("设备故障");
            faultMessageService.save(faultMessage);
         }
         if(2 == status){
            TChargingOrderVo order = new TChargingOrderVo();
            order.setChargingPileId(tChargingGun.getChargingPileId());
            order.setChargingGunId(tChargingGun.getId());
            order.setStatusList(Arrays.asList(1, 2, 3, 4, 5));
            List<TChargingOrder> orders = chargingOrderClient.getChargingOrder(order).getData();
            if(orders.size() > 0){
               TChargingOrder chargingOrder = orders.get(0);
               TChargingGun tChargingGun1 = new TChargingGun();
               if(Arrays.asList(1, 2, 5).contains(chargingOrder.getStatus())){
                  tChargingGun1.setStatus(3);
                  tChargingGun1.setId(tChargingGun.getId());
                  chargingGunService.updateById(tChargingGun1);
               }
               if(Arrays.asList(3).contains(chargingOrder.getStatus())){
                  tChargingGun1.setStatus(4);
                  tChargingGun1.setId(tChargingGun.getId());
                  chargingGunService.updateById(tChargingGun1);
               }
               if(Arrays.asList(4).contains(chargingOrder.getStatus()) && chargingOrder.getEndMode() == 2){
                  tChargingGun1.setStatus(5);
                  tChargingGun1.setId(tChargingGun.getId());
                  chargingGunService.updateById(tChargingGun1);
               }
               new Thread(new Runnable() {
                  @Override
                  public void run() {
                     //推送状态给三方平台
                     tcecClient.pushChargingGunStatus(tChargingGun.getFullNumber(), tChargingGun1.getStatus());
                  }
               }).start();
            }else{
               TChargingGun tChargingGun1 = new TChargingGun();
               tChargingGun1.setId(tChargingGun.getId());
               tChargingGun1.setStatus(3);
               chargingGunService.updateById(tChargingGun1);
               new Thread(new Runnable() {
                  @Override
                  public void run() {
                     //推送状态给三方平台
                     tcecClient.pushChargingGunStatus(tChargingGun.getFullNumber(), tChargingGun1.getStatus());
                  }
               }).start();
            }
            List<TFaultMessage> list = faultMessageService.list(new LambdaQueryWrapper<TFaultMessage>().eq(TFaultMessage::getChargingGunId, tChargingGun.getId())
                  .isNull(TFaultMessage::getEndTime));
            if(!list.isEmpty()){
               list.forEach(s->s.setEndTime(LocalDateTime.now()));
               faultMessageService.updateBatchById(list);
            }
         }
      }
   }
}