From e0fc8f7e2230b51e3bbe8dea43790cf11b7170e9 Mon Sep 17 00:00:00 2001
From: luodangjia <luodangjia>
Date: 星期一, 04 十一月 2024 15:39:01 +0800
Subject: [PATCH] merge

---
 ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/service/impl/TChargingPileServiceImpl.java |  290 ++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 213 insertions(+), 77 deletions(-)

diff --git a/ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/service/impl/TChargingPileServiceImpl.java b/ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/service/impl/TChargingPileServiceImpl.java
index d5b88bc..447ff59 100644
--- a/ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/service/impl/TChargingPileServiceImpl.java
+++ b/ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/service/impl/TChargingPileServiceImpl.java
@@ -1,14 +1,13 @@
 package com.ruoyi.chargingPile.service.impl;
 
+import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.account.api.feignClient.AppUserCarClient;
 import com.ruoyi.account.api.model.TAppUserCar;
 import com.ruoyi.chargingPile.api.dto.PageChargingPileListDTO;
-import com.ruoyi.chargingPile.api.model.Site;
-import com.ruoyi.chargingPile.api.model.TChargingGun;
-import com.ruoyi.chargingPile.api.model.TChargingPile;
+import com.ruoyi.chargingPile.api.model.*;
 import com.ruoyi.chargingPile.api.query.BatchSetAccountingStrategy;
 import com.ruoyi.chargingPile.api.query.PageChargingPileList;
 import com.ruoyi.chargingPile.api.vo.TChargingGunVO;
@@ -19,16 +18,21 @@
 import com.ruoyi.chargingPile.dto.ChargingGunMonitoring;
 import com.ruoyi.chargingPile.dto.GetChargingGunMonitoring;
 import com.ruoyi.chargingPile.mapper.TChargingPileMapper;
-import com.ruoyi.chargingPile.service.IPartnerService;
-import com.ruoyi.chargingPile.service.ISiteService;
-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.utils.StringUtils;
 import com.ruoyi.common.core.web.domain.AjaxResult;
+import com.ruoyi.common.core.web.domain.BasePojo;
 import com.ruoyi.common.core.web.page.PageInfo;
+import com.ruoyi.common.redis.service.RedisService;
 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.UploadRealTimeMonitoringDataClient;
 import com.ruoyi.integration.api.model.UploadRealTimeMonitoringData;
+import com.ruoyi.integration.api.vo.AddDevice;
+import com.ruoyi.integration.api.vo.AddDeviceResp;
+import com.ruoyi.integration.api.vo.DeleteDeviceResp;
 import com.ruoyi.order.api.feignClient.ChargingOrderAccountingStrategyClient;
 import com.ruoyi.order.api.feignClient.ChargingOrderClient;
 import com.ruoyi.order.api.model.TChargingOrder;
@@ -45,6 +49,7 @@
 import java.math.MathContext;
 import java.math.RoundingMode;
 import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
 import java.util.*;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
@@ -92,6 +97,21 @@
 
 	@Resource
 	private SysUserRoleClient sysUserRoleClient;
+	
+	@Resource
+	private IotInterfaceClient iotInterfaceClient;
+	
+	@Resource
+	private RedisService redisService;
+	
+	@Resource
+	private TFaultMessageService faultMessageService;
+	
+	@Resource
+	private IntegrationClient integrationClient;
+	
+	@Resource
+	private TAccountingStrategyDetailService accountingStrategyDetailService;
 	
 	
 	
@@ -151,6 +171,19 @@
 		if(count > 0){
 			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());
+//			}
+//		}
 		this.save(chargingPile);
 		return AjaxResult.success();
 	}
@@ -227,7 +260,6 @@
 	@Override
 	public AjaxResult delChargingPile(Integer[] ids) {
 		//检查是否有关联数据
-		//接口
 		long count = chargingGunService.count(new LambdaQueryWrapper<TChargingGun>().in(TChargingGun::getChargingPileId, Arrays.asList(ids))
 				.eq(TChargingGun::getDelFlag, 0));
 		if(count > 0){
@@ -235,6 +267,12 @@
 		}
 		for (Integer id : ids) {
 			TChargingPile chargingPile = this.getById(id);
+			//调用华为Iot删除设备
+//			if(StringUtils.isNotEmpty(chargingPile.getIotdDeviceId())){
+//				DeleteDeviceResp deviceResp = iotInterfaceClient.deleteDevice(chargingPile.getIotdDeviceId()).getData();
+//				if(null != deviceResp && deviceResp.getHttpStatusCode() == 0){
+//				}
+//			}
 			this.removeById(chargingPile);
 		}
 		return AjaxResult.success();
@@ -249,9 +287,20 @@
 		List<Integer> id = setAccountingStrategy.getId();
 		List<TChargingGun> list = chargingGunService.list(new LambdaQueryWrapper<TChargingGun>().in(TChargingGun::getChargingPileId, id).eq(TChargingGun::getDelFlag, 0));
 		for (TChargingGun tChargingGun : list) {
-			tChargingGun.setAccountingStrategyId(setAccountingStrategy.getAccountingStrategyId());
+			TChargingGun tChargingGun1 = new TChargingGun();
+			tChargingGun1.setId(tChargingGun.getId());
+			tChargingGun1.setAccountingStrategyId(setAccountingStrategy.getAccountingStrategyId());
+			chargingGunService.updateById(tChargingGun1);
 		}
-		chargingGunService.updateBatchById(list);
+		Integer accountingStrategyId = setAccountingStrategy.getAccountingStrategyId();
+		List<TAccountingStrategyDetail> accountingStrategyDetails = accountingStrategyDetailService.list(new LambdaQueryWrapper<TAccountingStrategyDetail>()
+				.eq(TAccountingStrategyDetail::getAccountingStrategyId, accountingStrategyId).orderByAsc(TAccountingStrategyDetail::getStartTime));
+		//下发硬件更新计费模板
+		List<TChargingPile> tChargingPiles = this.listByIds(id);
+		for (TChargingPile chargingPile : tChargingPiles) {
+			integrationClient.setupBillingModel1(chargingPile.getCode(), JSON.toJSONString(accountingStrategyDetails));
+		}
+		
 	}
 
 	@Override
@@ -265,7 +314,23 @@
 		List<TChargingGun> chargingGuns = chargingGunService.list(wrapper);
 		// 查询充电枪信息
 		chargingPileVOS.forEach(item -> {
-			item.setChargingGunList(chargingGuns.stream().filter(gun -> gun.getChargingPileId().equals(item.getId())).collect(Collectors.toList()));
+			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 {
+						chargingGun.setSoc(100);
+					}
+				}
+				if(chargingGun.getStatus().equals(5)){
+					chargingGun.setSoc(100);
+				}
+			}
+			item.setChargingGunList(chargingGunList);
 		});
 		return chargingPileVOS;
 	}
@@ -280,7 +345,7 @@
 	public ChargeMonitoring chargeMonitoring(Integer siteId) {
 		//获取当前登录账户的站点权限数据
 		Set<Integer> siteIds = new HashSet<>();
-		if(null == siteId){
+		if(0 == siteId){
 			SysUser sysUser = sysUserClient.getSysUser(SecurityUtils.getUserId()).getData();
 			Integer roleType = sysUser.getRoleType();
 			Integer objectId = sysUser.getObjectId();
@@ -294,12 +359,20 @@
                     List<Integer> data1 = roleSiteClient.getSiteIds(data2.get(0).getRoleId()).getData();
 					data.addAll(data1);
 					siteIds = new HashSet<>(data);
+				}else{
+					siteIds = null;
 				}
 			}
 		}else{
 			siteIds.add(siteId);
 		}
-		List<Site> sites = siteService.listByIds(siteIds);
+		List<Site> sites = null;
+		if(null == siteIds){
+			sites = siteService.list(new LambdaQueryWrapper<Site>().eq(Site::getDelFlag, 0));
+			siteIds = sites.stream().map(Site::getId).collect(Collectors.toSet());
+		}else{
+			sites = siteService.listByIds(siteIds);
+		}
 		List<TChargingGun> total = chargingGunService.list(new LambdaQueryWrapper<TChargingGun>().in(TChargingGun::getSiteId, siteIds).eq(TChargingGun::getDelFlag, 0));
 		List<TChargingGun> charging = chargingGunService.list(new LambdaQueryWrapper<TChargingGun>().in(TChargingGun::getSiteId, siteIds).eq(TChargingGun::getStatus, 4).eq(TChargingGun::getDelFlag, 0));
 		BigDecimal ratedPower = total.stream().map(TChargingGun::getRatedPower).reduce(BigDecimal.ZERO, BigDecimal::add);
@@ -321,9 +394,9 @@
 		}
 
 		List<String> dates = new ArrayList<>();
-		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd");
-		Calendar calendar = Calendar.getInstance();
+		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 		for (int i = 6; i >= 0; i--) {
+			Calendar calendar = Calendar.getInstance();
 			calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - i);
 			dates.add(sdf.format(calendar.getTime()));
 		}
@@ -342,7 +415,7 @@
 			if(list.size() == 0){
 				value2.add(0D);
 			}else{
-				Double datum1 = new BigDecimal(datum).divide(new BigDecimal(list.size()).multiply(v).multiply(new BigDecimal(24))).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
+				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();
 				value2.add(datum1);
 			}
 		}
@@ -364,7 +437,7 @@
 	public ChargingGunCountMonitoring getChargingGunCountMonitoring(Integer siteId) {
 		//获取当前登录账户的站点权限数据
 		Set<Integer> siteIds = new HashSet<>();
-		if(null == siteId){
+		if(0 == siteId){
 			SysUser sysUser = sysUserClient.getSysUser(SecurityUtils.getUserId()).getData();
 			Integer roleType = sysUser.getRoleType();
 			Integer objectId = sysUser.getObjectId();
@@ -378,6 +451,9 @@
                     List<Integer> data1 = roleSiteClient.getSiteIds(data2.get(0).getRoleId()).getData();
 					data.addAll(data1);
 					siteIds = new HashSet<>(data);
+				}else{
+					List<Site> list = siteService.list(new LambdaQueryWrapper<Site>().eq(Site::getDelFlag, 0));
+					siteIds = list.stream().map(Site::getId).collect(Collectors.toSet());
 				}
 			}
 		}else{
@@ -391,7 +467,7 @@
 		//充电中
 		long charging = list.stream().filter(s -> s.getStatus().equals(4)).count();
 		//离网
-		long offline = list.stream().filter(s -> s.getStatus().equals(0)).count();
+		long offline = list.stream().filter(s -> s.getStatus().equals(1)).count();
 		//故障
 		long breakdown = list.stream().filter(s -> s.getStatus().equals(7)).count();
 		ChargingGunCountMonitoring chargingGunCountMonitoring = new ChargingGunCountMonitoring();
@@ -412,20 +488,24 @@
 	public PageInfo<ChargingGunMonitoring> getChargingGunMonitoring(GetChargingGunMonitoring query) {
 		//获取当前登录账户的站点权限数据
 		Set<Integer> siteIds = new HashSet<>();
-		if(null == query.getSiteId()){
+		if(0 == query.getSiteId()){
 			SysUser sysUser = sysUserClient.getSysUser(SecurityUtils.getUserId()).getData();
+			R<Integer> admin = sysUserClient.isAdmin(SecurityUtils.getUserId());
 			Integer roleType = sysUser.getRoleType();
 			Integer objectId = sysUser.getObjectId();
 			if(2 == roleType){
 				siteIds = partnerService.authSite(objectId, SiteMenu.SITE_LIST);
 			}else{
 				//非管理员需要根据角色和用户配置查询允许的站点数据
-				if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
+				if(admin.getData()!=1){
 					List<Integer> data = userSiteClient.getSiteIds(sysUser.getUserId()).getData();
                     List<SysUserRoleVo> data2 = sysUserRoleClient.getRoleByUserId(sysUser.getUserId()).getData();
                     List<Integer> data1 = roleSiteClient.getSiteIds(data2.get(0).getRoleId()).getData();
 					data.addAll(data1);
 					siteIds = new HashSet<>(data);
+				}else{
+					List<Site> list = siteService.list(new LambdaQueryWrapper<Site>().eq(Site::getDelFlag, 0));
+					siteIds = list.stream().map(Site::getId).collect(Collectors.toSet());
 				}
 			}
 		}else{
@@ -434,8 +514,8 @@
 		
 		if(null != query.getStatus() && 0 < query.getStatus().size()){
 			if(query.getStatus().contains(3)){
+				query.getStatus().add(5);
 				query.getStatus().add(6);
-				query.getStatus().add(7);
 			}
 		}
 		PageInfo<ChargingGunMonitoring> pageInfo = new PageInfo<>(query.getPageCurr(), query.getPageSize());
@@ -445,67 +525,123 @@
 			if(status == 5 || status == 6){
 				gunMonitoring.setStatus(3);
 			}
-			if(status == 4){
+			if(status == 4 || status == 7){
 				Integer id = gunMonitoring.getId();
-				//根据订单数据查询车牌号
 				TChargingOrder chargingOrder = chargingOrderClient.getOrderDetailByGunId(id).getData();
-				Long appUserCarId = chargingOrder.getAppUserCarId();
-				if(null != appUserCarId){
-					TAppUserCar tAppUserCar = appUserCarClient.getCarByIds(Arrays.asList(appUserCarId)).getData().get(0);
-					gunMonitoring.setLicensePlate(tAppUserCar.getLicensePlate());
-				}
-				
-				UploadRealTimeMonitoringData data = uploadRealTimeMonitoringDataClient.chargingOrderInfo(chargingOrder.getCode()).getData();
-				gunMonitoring.setChargingRatio(BigDecimal.valueOf(data.getSoc()));
-				gunMonitoring.setElectricQuantity(data.getCharging_degree().toString());
-				gunMonitoring.setSoc(data.getSoc().toString());
-				gunMonitoring.setElectricCurrent(data.getOutput_current().toString());
-				gunMonitoring.setVoltage(data.getOutput_voltage().toString());
-				switch (data.getHardware_fault()){
-					case 1:
-						gunMonitoring.setFaultCause("急停按钮动作故障");
-						break;
-					case 2:
-						gunMonitoring.setFaultCause("无可用整流模块");
-						break;
-					case 3:
-						gunMonitoring.setFaultCause("出风口温度过高");
-						break;
-					case 4:
-						gunMonitoring.setFaultCause("交流防雷故障");
-						break;
-					case 5:
-						gunMonitoring.setFaultCause("交直流模块 DC20 通信中断");
-						break;
-					case 6:
-						gunMonitoring.setFaultCause("绝缘检测模块 FC08 通信中断");
-						break;
-					case 7:
-						gunMonitoring.setFaultCause("电度表通信中断");
-						break;
-					case 8:
-						gunMonitoring.setFaultCause("读卡器通信中断");
-						break;
-					case 9:
-						gunMonitoring.setFaultCause("RC10 通信中断");
-						break;
-					case 10:
-						gunMonitoring.setFaultCause("风扇调速板故障");
-						break;
-					case 11:
-						gunMonitoring.setFaultCause("直流熔断器故障");
-						break;
-					case 12:
-						gunMonitoring.setFaultCause("高压接触器故障");
-						break;
-					case 13:
-						gunMonitoring.setFaultCause("门打开");
-						break;
+				if(null != chargingOrder){
+					Long appUserCarId = chargingOrder.getAppUserCarId();
+					if(null != appUserCarId){
+						TAppUserCar tAppUserCar = appUserCarClient.getCarByIds(Arrays.asList(appUserCarId)).getData().get(0);
+						gunMonitoring.setLicensePlate(tAppUserCar.getLicensePlate());
+					}
+
+					UploadRealTimeMonitoringData data = uploadRealTimeMonitoringDataClient.chargingOrderInfo(chargingOrder.getCode()).getData();
+					gunMonitoring.setChargingRatio(BigDecimal.valueOf(data.getSoc()));
+					gunMonitoring.setElectricQuantity(data.getCharging_degree().toString());
+					gunMonitoring.setSoc(data.getSoc().toString());
+					gunMonitoring.setElectricCurrent(data.getOutput_current().toString());
+					gunMonitoring.setVoltage(data.getOutput_voltage().toString());
+					switch (data.getHardware_fault()){
+						case 1:
+							gunMonitoring.setFaultCause("急停按钮动作故障");
+							break;
+						case 2:
+							gunMonitoring.setFaultCause("无可用整流模块");
+							break;
+						case 3:
+							gunMonitoring.setFaultCause("出风口温度过高");
+							break;
+						case 4:
+							gunMonitoring.setFaultCause("交流防雷故障");
+							break;
+						case 5:
+							gunMonitoring.setFaultCause("交直流模块 DC20 通信中断");
+							break;
+						case 6:
+							gunMonitoring.setFaultCause("绝缘检测模块 FC08 通信中断");
+							break;
+						case 7:
+							gunMonitoring.setFaultCause("电度表通信中断");
+							break;
+						case 8:
+							gunMonitoring.setFaultCause("读卡器通信中断");
+							break;
+						case 9:
+							gunMonitoring.setFaultCause("RC10 通信中断");
+							break;
+						case 10:
+							gunMonitoring.setFaultCause("风扇调速板故障");
+							break;
+						case 11:
+							gunMonitoring.setFaultCause("直流熔断器故障");
+							break;
+						case 12:
+							gunMonitoring.setFaultCause("高压接触器故障");
+							break;
+						case 13:
+							gunMonitoring.setFaultCause("门打开");
+							break;
+					}
 				}
 			}
-			
-			
+			//test
+//			if (status==1||status==7){
+//				TFaultMessage one = faultMessageService.lambdaQuery().eq(TFaultMessage::getChargingGunId, gunMonitoring.getId()).orderByDesc(TFaultMessage::getCreateTime).last("limit 1").one();
+//				if (one!=null) {
+//					gunMonitoring.setFaultCause(one.getContent());
+//				}
+//			}
 		}
 		return pageInfo.setRecords(chargingGunMonitoring);
 	}
+	
+	
+	/**
+	 * 定时任务修改充电桩状态
+	 */
+	@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));
+		}
+		for (TChargingGun chargingGun : list) {
+			Long time = (Long) charging_gun_online.get(chargingGun.getFullNumber());
+			if(null != time && System.currentTimeMillis() - time > 60000){
+				TChargingGun chargingGun1 = new TChargingGun();
+				chargingGun1.setId(chargingGun.getId());
+				chargingGun1.setStatus(1);
+				chargingGunService.updateById(chargingGun1);
+				
+				//桩处于正常,桩所属的枪都处于非正常,修改桩状态为异常
+				TChargingPile chargingPile = this.getById(chargingGun.getChargingPileId());
+				List<TChargingGun> list1 = chargingGunService.list(new LambdaQueryWrapper<TChargingGun>().eq(TChargingGun::getChargingPileId, chargingPile.getId()).eq(TChargingGun::getDelFlag, 0));
+				int size = list1.stream().filter(s -> s.getStatus() == 1 || s.getStatus() == 7).collect(Collectors.toList()).size();
+				if(chargingPile.getStatus() == 1 && list1.size() == size){
+					TChargingPile chargingPile1 = new TChargingPile();
+					chargingPile1.setId(chargingGun.getChargingPileId());
+					chargingPile1.setStatus(2);
+					this.updateById(chargingPile1);
+				}
+				
+				//添加记录
+				TFaultMessage faultMessage = faultMessageService.getOne(new LambdaQueryWrapper<TFaultMessage>().eq(TFaultMessage::getChargingPileId, chargingGun.getChargingPileId())
+						.eq(TFaultMessage::getChargingGunId, chargingGun.getId()).eq(TFaultMessage::getStatus, 1).eq(TFaultMessage::getDelFlag, 0).isNull(TFaultMessage::getEndTime));
+				if(null == faultMessage){
+					faultMessage = new TFaultMessage();
+					faultMessage.setSiteId(chargingGun.getSiteId());
+					faultMessage.setChargingPileId(chargingGun.getChargingPileId());
+					faultMessage.setChargingGunId(chargingGun.getId());
+					faultMessage.setStatus(1);
+					faultMessage.setDownTime(LocalDateTime.now());
+					faultMessage.setCreateTime(LocalDateTime.now());
+					faultMessage.setDelFlag(false);
+					faultMessage.setContent("设备离线");
+					faultMessageService.save(faultMessage);
+				}
+			}
+		}
+	}
 }

--
Gitblit v1.7.1