|
|
|
@ -1,24 +1,30 @@
|
|
|
|
|
package com.op.energy.record.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
|
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
|
|
|
|
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
|
|
|
|
import com.op.common.core.text.Convert;
|
|
|
|
|
import com.op.common.core.utils.DateUtils;
|
|
|
|
|
import com.op.common.core.utils.StringUtils;
|
|
|
|
|
import com.op.common.core.web.domain.AjaxResult;
|
|
|
|
|
import com.op.energy.base.domain.BaseMonitorInfo;
|
|
|
|
|
import com.op.energy.base.mapper.BaseMonitorInfo1Mapper;
|
|
|
|
|
import com.op.energy.base.service.IBaseMonitorInfoService;
|
|
|
|
|
import com.op.energy.record.domain.RecordDnbInstant;
|
|
|
|
|
import com.op.energy.record.mapper.RecordDnbInstantMapper;
|
|
|
|
|
import com.op.energy.record.service.IRecordDnbInstantService;
|
|
|
|
|
import com.op.energy.report.domain.ReportPointDnb;
|
|
|
|
|
import com.op.energy.report.mapper.ReportPointDnbMapper;
|
|
|
|
|
import com.op.system.api.RemoteOpenService;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -35,6 +41,15 @@ public class RecordDnbInstantServiceImpl implements IRecordDnbInstantService {
|
|
|
|
|
@Autowired
|
|
|
|
|
private IBaseMonitorInfoService baseMonitorInfoService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RemoteOpenService remoteOpenService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ReportPointDnbMapper reportPointDnbMapper;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private BaseMonitorInfo1Mapper baseMonitorInfoMapper;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询电实时数据
|
|
|
|
|
*
|
|
|
|
@ -232,5 +247,212 @@ public class RecordDnbInstantServiceImpl implements IRecordDnbInstantService {
|
|
|
|
|
return recordDnbInstantMapper.selectRecordDnbInstantList(dnbInstant);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void getNhEnergyDetail() {
|
|
|
|
|
Map<String,String> params = new HashMap<>();
|
|
|
|
|
params.put("dateType","mi15");
|
|
|
|
|
//获取当前时间的前后5分钟
|
|
|
|
|
Date nowTime = DateUtils.getNowDate();
|
|
|
|
|
String startTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.addMinutes(nowTime,-20));
|
|
|
|
|
String endTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.addMinutes(nowTime,-10));
|
|
|
|
|
params.put("startTime",startTime);
|
|
|
|
|
params.put("endTime",endTime);
|
|
|
|
|
// params.put("startTime","2025-07-30 13:40:00");
|
|
|
|
|
// params.put("endTime","2025-07-30 13:50:00");
|
|
|
|
|
params.put("valueType","SJZ");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 电压/电流
|
|
|
|
|
* **/
|
|
|
|
|
AjaxResult voltageRes = remoteOpenService.getVoltageCurrent(params);
|
|
|
|
|
List<RecordDnbInstant> list = new ArrayList<>();
|
|
|
|
|
if(voltageRes.isSuccess()){
|
|
|
|
|
JSONObject resData = JSONObject.parseObject(JSON.toJSONString(voltageRes.get(AjaxResult.DATA_TAG)));
|
|
|
|
|
JSONArray array = JSONArray.parseArray(resData.get("Data").toString());
|
|
|
|
|
for (int i = 0; i < array.size(); i++) {
|
|
|
|
|
RecordDnbInstant instant = new RecordDnbInstant();
|
|
|
|
|
instant.setVA(getSafeBigDecimal(array.getJSONObject(i), "ADY"));
|
|
|
|
|
instant.setVB(getSafeBigDecimal(array.getJSONObject(i), "BDY"));
|
|
|
|
|
instant.setVC(getSafeBigDecimal(array.getJSONObject(i), "CDY"));
|
|
|
|
|
instant.setIA(getSafeBigDecimal(array.getJSONObject(i), "ADL"));
|
|
|
|
|
instant.setIB(getSafeBigDecimal(array.getJSONObject(i), "BDL"));
|
|
|
|
|
instant.setIC(getSafeBigDecimal(array.getJSONObject(i), "CDL"));
|
|
|
|
|
instant.setMonitorId(array.getJSONObject(i).getString("Address"));
|
|
|
|
|
instant.setRecordTime(nowTime);
|
|
|
|
|
instant.setCollectTime(DateUtils.dateTime("yyyy-MM-dd HH:mm", array.getJSONObject(i).getString("ReadingDate")));
|
|
|
|
|
list.add(instant);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功率
|
|
|
|
|
* **/
|
|
|
|
|
AjaxResult electricPowerRes = remoteOpenService.getElectricPower(params);
|
|
|
|
|
if(electricPowerRes.isSuccess()){
|
|
|
|
|
JSONObject resData = JSONObject.parseObject(JSON.toJSONString(electricPowerRes.get(AjaxResult.DATA_TAG)));
|
|
|
|
|
JSONArray array = JSONArray.parseArray(resData.get("Data").toString());
|
|
|
|
|
for (int i = 0; i < array.size(); i++) {
|
|
|
|
|
for (RecordDnbInstant item : list) {
|
|
|
|
|
String address = array.getJSONObject(i).getString("Address");
|
|
|
|
|
if (address.equals(item.getMonitorId())) {
|
|
|
|
|
item.setGlys(getSafeBigDecimal(array.getJSONObject(i), "GLYS"));
|
|
|
|
|
item.setZxyg(getSafeBigDecimal(array.getJSONObject(i), "YGGL"));
|
|
|
|
|
item.setActivePower(getSafeBigDecimal(array.getJSONObject(i), "YGGL"));
|
|
|
|
|
item.setReactivePower(getSafeBigDecimal(array.getJSONObject(i), "WGGL"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 电量
|
|
|
|
|
* **/
|
|
|
|
|
AjaxResult electricQuantityRes= remoteOpenService.getElectricQuantity(params);
|
|
|
|
|
if(electricQuantityRes.isSuccess()){
|
|
|
|
|
JSONObject resData = JSONObject.parseObject(JSON.toJSONString(electricQuantityRes.get(AjaxResult.DATA_TAG)));
|
|
|
|
|
JSONArray array = JSONArray.parseArray(resData.get("Data").toString());
|
|
|
|
|
for (int i = 0; i < array.size(); i++) {
|
|
|
|
|
for (RecordDnbInstant item : list) {
|
|
|
|
|
String address = array.getJSONObject(i).getString("Address");
|
|
|
|
|
if (address.equals(item.getMonitorId())) {
|
|
|
|
|
item.setReactivePower(getSafeBigDecimal(array.getJSONObject(i), "ZYGDN"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DynamicDataSourceContextHolder.push("ds_1000");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
for (RecordDnbInstant item : list) {
|
|
|
|
|
BaseMonitorInfo temp = new BaseMonitorInfo();
|
|
|
|
|
temp.setAddressCode(item.getMonitorId());
|
|
|
|
|
List<BaseMonitorInfo> BaseList = baseMonitorInfoMapper.selectBaseMonitorInfoList(temp);
|
|
|
|
|
for (BaseMonitorInfo base : BaseList) {
|
|
|
|
|
item.setMonitorId(base.getMonitorId());
|
|
|
|
|
recordDnbInstantMapper.insertRecordDnbInstant(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}finally {
|
|
|
|
|
DynamicDataSourceContextHolder.poll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void getNhEnergy() {
|
|
|
|
|
Map<String,String> params = new HashMap<>();
|
|
|
|
|
params.put("dateType","mi15");
|
|
|
|
|
//获取当前时间的前后5分钟
|
|
|
|
|
Date nowDate = DateUtils.getNowDate();
|
|
|
|
|
//取整点数据
|
|
|
|
|
String nowDateStr = DateUtils.parseDateToStr("yyyy-MM-dd HH",nowDate)+":00:00";
|
|
|
|
|
String startTime = DateUtils.parseDateToStr("yyyy-MM-dd HH",DateUtils.addHours(nowDate,-1))+":00:00";
|
|
|
|
|
params.put("startTime",startTime);
|
|
|
|
|
params.put("endTime",nowDateStr);
|
|
|
|
|
params.put("valueType","SJZ");
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 电量
|
|
|
|
|
* **/
|
|
|
|
|
List<ReportPointDnb> list = new ArrayList<>();
|
|
|
|
|
AjaxResult electricQuantityRes= remoteOpenService.getElectricQuantity(params);
|
|
|
|
|
if(electricQuantityRes.isSuccess()){
|
|
|
|
|
JSONObject resData = JSONObject.parseObject(JSON.toJSONString(electricQuantityRes.get(AjaxResult.DATA_TAG)));
|
|
|
|
|
JSONArray array = JSONArray.parseArray(resData.get("Data").toString());
|
|
|
|
|
//根据Address分类
|
|
|
|
|
List<String> addressList = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < array.size(); i++) {
|
|
|
|
|
addressList.add(array.getJSONObject(i).getString("Address"));
|
|
|
|
|
}
|
|
|
|
|
//addressList去重
|
|
|
|
|
addressList = addressList.stream().distinct().collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
DynamicDataSourceContextHolder.push("ds_1000");
|
|
|
|
|
try {
|
|
|
|
|
for (String address : addressList) {
|
|
|
|
|
ReportPointDnb dnb = new ReportPointDnb();
|
|
|
|
|
BigDecimal electricQuantity = BigDecimal.ZERO;
|
|
|
|
|
for (int i = 0; i < array.size(); i++) {
|
|
|
|
|
if (address.equals(array.getJSONObject(i).getString("Address"))) {
|
|
|
|
|
electricQuantity = electricQuantity.add(getSafeBigDecimal(array.getJSONObject(i), "ZYGDN"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
dnb.setMeterValue(array.getJSONObject(0).getBigDecimal("ZYGDNSZ"));
|
|
|
|
|
dnb.setExpend(electricQuantity);
|
|
|
|
|
dnb.setMonitorId(address);
|
|
|
|
|
dnb.setBeginTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,startTime));
|
|
|
|
|
dnb.setEndTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,nowDateStr));
|
|
|
|
|
dnb.setRecordTime(new Date());
|
|
|
|
|
dnb.setPointTime(removeLeadingZeros(DateUtils.parseDateToStr("HH",DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,startTime))));
|
|
|
|
|
BaseMonitorInfo temp = new BaseMonitorInfo();
|
|
|
|
|
temp.setAddressCode(dnb.getMonitorId());
|
|
|
|
|
List<BaseMonitorInfo> BaseList = baseMonitorInfoMapper.selectBaseMonitorInfoList(temp);
|
|
|
|
|
for (BaseMonitorInfo base : BaseList) {
|
|
|
|
|
dnb.setMonitorId(base.getMonitorId());
|
|
|
|
|
reportPointDnbMapper.insertReportPointDnb(dnb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}finally {
|
|
|
|
|
DynamicDataSourceContextHolder.poll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 移除前面的0
|
|
|
|
|
* **/
|
|
|
|
|
public static String removeLeadingZeros(String str) {
|
|
|
|
|
if (str.isEmpty()){
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
int index = 0;
|
|
|
|
|
while (index < str.length() && str.charAt(index) == '0') {
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
// 处理全零情况(如"0000"返回"0")
|
|
|
|
|
return index == str.length() ? "0" : str.substring(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static BigDecimal getSafeBigDecimal(JSONObject jsonObject, String key) {
|
|
|
|
|
BigDecimal defaultValue = BigDecimal.ZERO;
|
|
|
|
|
// 处理字段不存在的情况
|
|
|
|
|
if (!jsonObject.containsKey(key)) {
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Object value = jsonObject.get(key);
|
|
|
|
|
// 处理值为null的情况
|
|
|
|
|
if (value == null) {
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理各种可能的数值类型
|
|
|
|
|
try {
|
|
|
|
|
if (value instanceof BigDecimal) {
|
|
|
|
|
return (BigDecimal) value;
|
|
|
|
|
} else if (value instanceof Number) {
|
|
|
|
|
// 处理整数、浮点数等数字类型
|
|
|
|
|
return new BigDecimal(value.toString());
|
|
|
|
|
} else if (value instanceof String) {
|
|
|
|
|
// 处理字符串类型的数字
|
|
|
|
|
String strValue = (String) value;
|
|
|
|
|
if (strValue.trim().isEmpty()) {
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
return new BigDecimal(strValue);
|
|
|
|
|
} else {
|
|
|
|
|
// 不支持的类型
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
// 转换失败时返回默认值
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|