Browse Source

1、中央空调能耗管理:同比环比接口修改;

dev
mh 2 months ago
parent
commit
d9c34ac040
  1. 2
      mh-admin/src/main/java/com/mh/web/controller/energy/SysEnergyConsumptionController.java
  2. 152
      mh-system/src/main/java/com/mh/system/mapper/energy/EnergyMapper.java
  3. 204
      mh-system/src/main/java/com/mh/system/service/energy/impl/EnergyServiceImpl.java
  4. 794
      mh-system/src/main/resources/mapper/system/EnergyMapper.xml

2
mh-admin/src/main/java/com/mh/web/controller/energy/SysEnergyConsumptionController.java

@ -26,7 +26,7 @@ public class SysEnergyConsumptionController extends BaseController {
private IEnergyService energyService;
/**
* 获取系统能耗分析
* 获取系统瞬时能耗分析
* @param vo
* @return
*/

152
mh-system/src/main/java/com/mh/system/mapper/energy/EnergyMapper.java

@ -26,35 +26,95 @@ public interface EnergyMapper {
* @return
*/
@Select("<script>" +
"select sum(calc_value) as curValue,device_type as deviceType,convert(varchar(${len}), cur_time, 120) as timeStr from ${lastTableName} dt " +
" where cur_time >= concat(#{startTime},' 00:00:00') and cur_time &lt;= concat(#{endTime},' 23:59:59') " +
"<if test='paramType != null and paramType != \"\"'>" +
" and device_type = #{paramType}" +
"</if>" +
" and exists ( " +
"select " +
" 1 " +
" sum(dm.calc_value) as curValue, " +
" CASE dm.device_type " +
" WHEN '5' THEN 'meter' " +
" WHEN '6' THEN 'cloud' " +
" ELSE 'Unknown' " +
" END as deviceType," +
" TO_CHAR(dm.cur_time, " +
" <choose>" +
" <when test='len == 4'>'YYYY'</when>" +
" <when test='len == 7'>'YYYY-MM'</when>" +
" <when test='len == 10'>'YYYY-MM-DD'</when>" +
" <when test='len == 13'>'YYYY-MM-DD HH24'</when>" +
" <when test='len == 16'>'YYYY-MM-DD HH24:MI'</when>" +
" <otherwise>'YYYY-MM-DD HH24:MI:SS'</otherwise>" +
" </choose>" +
" ) AS timeStr " +
"from " +
" device_manage " +
" ${lastTableName} dm " +
"where " +
" device_num = dt.device_num and grade &lt; 50 and grade >=40 " +
" dm.cur_time &gt;= #{startTime}::timestamp " +
" AND dm.cur_time &lt;= #{endTime}::timestamp " +
" and device_num in ( " +
" select " +
" device_num " +
" from " +
" collection_params_manage cpm " +
" join device_ledger dl " +
"on " +
" cpm.device_ledger_id = dl.id " +
" and dl.device_type in ('5', '6') and cpm.grade = 40 " +
") " +
"group by " +
" device_type, " +
" TO_CHAR(dm.cur_time, " +
" <choose>" +
" <when test='len == 4'>'YYYY'</when>" +
" <when test='len == 7'>'YYYY-MM'</when>" +
" <when test='len == 10'>'YYYY-MM-DD'</when>" +
" <when test='len == 13'>'YYYY-MM-DD HH24'</when>" +
" <when test='len == 16'>'YYYY-MM-DD HH24:MI'</when>" +
" <otherwise>'YYYY-MM-DD HH24:MI:SS'</otherwise>" +
" </choose>" +
" ) " +
" group by device_type,convert(varchar(${len}), cur_time, 120) " +
"union all " +
"select sum(calc_value) as curValue,device_type as deviceType,convert(varchar(${len}), cur_time, 120) as timeStr from ${curTableName} dy " +
" where cur_time >= concat(#{startTime},' 00:00:00') and cur_time &lt;= concat(#{endTime},' 23:59:59') " +
"<if test='paramType != null and paramType != \"\"'>" +
" and device_type = #{paramType}" +
"</if>" +
" and exists ( " +
"select " +
" 1 " +
" sum(dm.calc_value) as curValue, " +
" CASE dm.device_type " +
" WHEN '5' THEN 'meter' " +
" WHEN '6' THEN 'cloud' " +
" ELSE 'Unknown' " +
" END as deviceType," +
" TO_CHAR(dm.cur_time, " +
" <choose>" +
" <when test='len == 4'>'YYYY'</when>" +
" <when test='len == 7'>'YYYY-MM'</when>" +
" <when test='len == 10'>'YYYY-MM-DD'</when>" +
" <when test='len == 13'>'YYYY-MM-DD HH24'</when>" +
" <when test='len == 16'>'YYYY-MM-DD HH24:MI'</when>" +
" <otherwise>'YYYY-MM-DD HH24:MI:SS'</otherwise>" +
" </choose>" +
" ) AS timeStr " +
"from " +
" device_manage " +
" ${curTableName} dm " +
"where " +
" device_num = dy.device_num and grade &lt; 50 and grade >=40 " +
" dm.cur_time &gt;= #{startTime}::timestamp " +
" AND dm.cur_time &lt;= #{endTime}::timestamp " +
" and device_num in ( " +
" select " +
" device_num " +
" from " +
" collection_params_manage cpm " +
" join device_ledger dl " +
"on " +
" cpm.device_ledger_id = dl.id " +
" and dl.device_type in ('5', '6') and cpm.grade = 40 " +
") " +
"group by " +
" device_type, " +
" TO_CHAR(dm.cur_time, " +
" <choose>" +
" <when test='len == 4'>'YYYY'</when>" +
" <when test='len == 7'>'YYYY-MM'</when>" +
" <when test='len == 10'>'YYYY-MM-DD'</when>" +
" <when test='len == 13'>'YYYY-MM-DD HH24'</when>" +
" <when test='len == 16'>'YYYY-MM-DD HH24:MI'</when>" +
" <otherwise>'YYYY-MM-DD HH24:MI:SS'</otherwise>" +
" </choose>" +
" ) " +
" group by device_type,convert(varchar(${len}), cur_time, 120)" +
"</script>")
List<ConsumptionAnalyze> queryManyTable(@Param("startTime") String startTime,
@Param("endTime") String endTime,
@ -72,20 +132,52 @@ public interface EnergyMapper {
* @return
*/
@Select("<script>" +
"select sum(calc_value) as curValue,device_type as deviceType,convert(varchar(${len}), cur_time, 120) as timeStr from ${lastTableName} dy " +
" where cur_time >= #{startTime} and cur_time &lt;= #{endTime} " +
"<if test='paramType != null and paramType != \"\"'>" +
" and device_type = #{paramType}" +
"</if>" +
" and exists ( " +
"select " +
" 1 " +
" sum(dm.calc_value) as curValue, " +
" CASE dm.device_type " +
" WHEN '5' THEN 'meter' " +
" WHEN '6' THEN 'cloud' " +
" ELSE 'Unknown' " +
" END as deviceType," +
" TO_CHAR(dm.cur_time, " +
" <choose>" +
" <when test='len == 4'>'YYYY'</when>" +
" <when test='len == 7'>'YYYY-MM'</when>" +
" <when test='len == 10'>'YYYY-MM-DD'</when>" +
" <when test='len == 13'>'YYYY-MM-DD HH24'</when>" +
" <when test='len == 16'>'YYYY-MM-DD HH24:MI'</when>" +
" <otherwise>'YYYY-MM-DD HH24:MI:SS'</otherwise>" +
" </choose>" +
" ) AS timeStr " +
"from " +
" device_manage " +
" ${lastTableName} dm " +
"where " +
" device_num = dy.device_num and grade &lt; 50 and grade >=40 " +
" dm.cur_time &gt;= #{startTime}::timestamp " +
" AND dm.cur_time &lt;= #{endTime}::timestamp " +
" and device_num in ( " +
" select " +
" device_num " +
" from " +
" collection_params_manage cpm " +
" join device_ledger dl " +
"on " +
" cpm.device_ledger_id = dl.id " +
" and dl.device_type in ('5', '6') and cpm.grade = 40 " +
") " +
"group by " +
" device_type, " +
" TO_CHAR(dm.cur_time, " +
" <choose>" +
" <when test='len == 4'>'YYYY'</when>" +
" <when test='len == 7'>'YYYY-MM'</when>" +
" <when test='len == 10'>'YYYY-MM-DD'</when>" +
" <when test='len == 13'>'YYYY-MM-DD HH24'</when>" +
" <when test='len == 16'>'YYYY-MM-DD HH24:MI'</when>" +
" <otherwise>'YYYY-MM-DD HH24:MI:SS'</otherwise>" +
" </choose>" +
" ) " +
" group by device_type,convert(varchar(${len}), cur_time, 120)" +
"order by " +
" timeStr " +
"</script>")
List<ConsumptionAnalyze> queryOneTable(@Param("startTime") String startTime,
@Param("endTime") String endTime,

204
mh-system/src/main/java/com/mh/system/service/energy/impl/EnergyServiceImpl.java

@ -13,6 +13,8 @@ import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
@ -157,7 +159,7 @@ public class EnergyServiceImpl implements IEnergyService {
consumptionAnalyzeEntities = energyMapper.queryManyTable(vo.getStartTime(), vo.getEndTime(), lastTableName.get(), curTableName.get(), getTimeLen(vo.getTimeType()), null);
}
}
if (null == consumptionAnalyzeEntities || consumptionAnalyzeEntities.size() == 0) {
if (null == consumptionAnalyzeEntities || consumptionAnalyzeEntities.isEmpty()) {
latch.countDown();
return null;
}
@ -232,24 +234,9 @@ public class EnergyServiceImpl implements IEnergyService {
.collect(Collectors.groupingBy(ConsumptionAnalyze::getDeviceType, HashMap::new, Collectors
.collectingAndThen(Collectors.toList(),
list -> list.stream().sorted(Comparator.comparing(ConsumptionAnalyze::getTimeStr)).collect(Collectors.toList()))));
// List<ConsumptionAnalyzeEntity> meterLineData = new ArrayList<>();
// List<ConsumptionAnalyzeEntity> coldLineData = new ArrayList<>();
// List<ConsumptionAnalyzeEntity> copLineData = new ArrayList<>();
List<ConsumptionAnalyze> efrColdLineData = new ArrayList<>();
List<ConsumptionAnalyze> efrMeterLineData = new ArrayList<>();
for (Map.Entry<String, List<ConsumptionAnalyze>> nmap : collectLine.entrySet()) {
// // 获取电表的值
// if (nmap.getKey().equalsIgnoreCase("meter")) {
// meterLineData = nmap.getValue();
// }
// // 获取冷量计的值
// if (nmap.getKey().equalsIgnoreCase("cloud")) {
// coldLineData = nmap.getValue();
// }
// // 获取cop的值
// if (nmap.getKey().equalsIgnoreCase("cop")) {
// copLineData = nmap.getValue();
// }
// 获取瞬时冷量计的值
if (nmap.getKey().equalsIgnoreCase("efrCloud")) {
efrColdLineData = nmap.getValue();
@ -262,12 +249,7 @@ public class EnergyServiceImpl implements IEnergyService {
String[] lineTimeStrArr = efrMeterLineData.stream()
.map(ConsumptionAnalyze::getTimeStr)
.toArray(String[]::new);
// String[] lineMeterArr = meterLineData.stream()
// .map(ConsumptionAnalyzeEntity::getCurValue)
// .toArray(String[]::new);
String[] lineInstantaneousMeterArr = getArr(efrMeterLineData, lineTimeStrArr);
// String[] lineColdArr = getArr(coldLineData, lineTimeStrArr);
// String[] lineCopArr = getArr(copLineData, lineTimeStrArr);
String[] lineEfrColdArr = getArr(efrColdLineData, lineTimeStrArr);
String[] lineCopArr = new String[efrMeterLineData.size()];
// 计算瞬时COP=制冷量/耗电量
@ -285,8 +267,6 @@ public class EnergyServiceImpl implements IEnergyService {
// 折现图数据
Map<String, Object> map = new HashMap<>();
map.put("lineTimeStrArr", lineTimeStrArr);
// map.put("lineMeterArr", lineMeterArr);
// map.put("lineColdArr", lineColdArr);
map.put("lineCopArr", lineCopArr);
map.put("lineInstantaneousColdArr", lineEfrColdArr);
map.put("lineInstantaneousMeterArr", lineInstantaneousMeterArr);
@ -370,9 +350,10 @@ public class EnergyServiceImpl implements IEnergyService {
String endTime = vo.getEndTime();
String deviceType = vo.getParamType();
if ("cop".equalsIgnoreCase(vo.getParamType())) {
lastTableName = "data_min_cop";
curTableName = "data_min_cop";
deviceType = "system";
return calcCopYoyData(vo);
// lastTableName = "data_min_cop";
// curTableName = "data_min_cop";
// deviceType = "system";
} else {
lastTableName = lastTableName+startTime.substring(0,4);
curTableName = curTableName+endTime.substring(0,4);
@ -408,6 +389,146 @@ public class EnergyServiceImpl implements IEnergyService {
return getConsumptionDTOHttpResult(consumptionAnalyzeEntities, "yoy");
}
private AjaxResult calcCopYoyData(EnergyConsumptionVO vo) {
// 获取冷量读数
vo.setParamType("cloud");
AjaxResult cloudData = yoy(vo);
EnergyConsumptionDTO cloudEnergyData = (EnergyConsumptionDTO) cloudData.get("data");
if (cloudEnergyData == null) {
return AjaxResult.error();
}
// 获取电量读数
vo.setParamType("meter");
AjaxResult meterData = yoy(vo);
EnergyConsumptionDTO meterEnergyData = (EnergyConsumptionDTO) meterData.get("data");;
if (meterEnergyData == null) {
return AjaxResult.error();
}
// 计算COP值
return getEnergyConsumptionDTOHttpResult(meterEnergyData, cloudEnergyData);
}
private static AjaxResult getEnergyConsumptionDTOHttpResult(EnergyConsumptionDTO meterEnergyData, EnergyConsumptionDTO cloudEnergyData) {
// 计算COP值
EnergyConsumptionDTO energyConsumptionDTO = new EnergyConsumptionDTO();
//
// 获取冷量数据
List<Map<String, Object>> cloudListData = cloudEnergyData.getData();
String[] cloudCurData = new String[cloudListData.size()];
String[] cloudLastData = new String[cloudListData.size()];
for (Map<String, Object> cloudListDatum : cloudListData) {
if (cloudListDatum.get("curValue") != null) {
cloudCurData = (String[]) cloudListDatum.get("curValue");
if (cloudCurData == null) {
cloudCurData = new String[cloudListData.size()];
}
}
if (cloudListDatum.get("lastValue") != null) {
cloudLastData = (String[]) cloudListDatum.get("curValue");
if (cloudLastData == null) {
cloudLastData = new String[cloudListData.size()];
}
}
}
// 获取电量数据
List<Map<String, Object>> meterListData = meterEnergyData.getData();
String[] meterCurData = new String[meterListData.size()];
String[] meterLastData = new String[meterListData.size()];
for (Map<String, Object> meterListDatum : meterListData) {
if (meterListDatum.get("curValue") != null) {
meterCurData = (String[]) meterListDatum.get("curValue");
if (meterCurData == null) {
meterCurData = new String[meterListData.size()];
}
}
if (meterListDatum.get("lastValue") != null) {
meterLastData = (String[]) meterListDatum.get("curValue");
if (meterLastData == null) {
meterLastData = new String[meterListData.size()];
}
}
}
// 初始化数据参数,当其中某个参数为空时,初始化数据为0
String[] timeA = cloudEnergyData.getTimes();
String[] timeB = meterEnergyData.getTimes();
List<String> timeAList = new ArrayList<>(Arrays.asList(timeA));
List<String> cloudCurDataList = new ArrayList<>(Arrays.asList(cloudCurData));
List<String> cloudLastDataList = new ArrayList<>(Arrays.asList(cloudLastData));
List<String> timeBList = new ArrayList<>(Arrays.asList(timeB));
List<String> meterCurDataList = new ArrayList<>(Arrays.asList(meterCurData));
List<String> meterLastDataList = new ArrayList<>(Arrays.asList(meterLastData));
if (timeAList.size() < timeBList.size()) {
for (String time : timeBList) {
if (!timeAList.contains(time)) {
timeAList.add(time);
cloudCurDataList.add("0");
}
}
meterEnergyData.setTimes(timeAList.toArray(new String[0]));
cloudCurData = cloudCurDataList.toArray(new String[0]);
cloudLastData = cloudLastDataList.toArray(new String[0]);
} else if (timeBList.size() < timeAList.size()) {
for (String time : timeAList) {
if (!timeBList.contains(time)) {
timeBList.add(time);
meterCurDataList.add("0");
meterLastDataList.add("0");
}
}
meterEnergyData.setTimes(timeBList.toArray(new String[0]));
meterCurData = meterCurDataList.toArray(new String[0]);
meterLastData = meterLastDataList.toArray(new String[0]);
}
energyConsumptionDTO.setTitles(meterEnergyData.getTitles());
energyConsumptionDTO.setTimes(meterEnergyData.getTimes());
energyConsumptionDTO.setLineTimes(meterEnergyData.getLineTimes());
// 直接两个相除,保留两位小数
if (cloudCurData.length == meterCurData.length) {
String[] copMap = new String[meterCurData.length];
String[] copLastMap = new String[meterCurData.length];
String[] yoyMap = new String[meterCurData.length];
for (int i = 0; i < meterCurData.length; i++) {
String curValue = StringUtils.isBlank(cloudCurData[i]) ? "0" : cloudCurData[i];
String lastValue = cloudLastData == null || StringUtils.isBlank(cloudLastData[i]) ? "0" : cloudLastData[i];
String curMeterValue = StringUtils.isBlank(meterCurData[i]) ? "0" : meterCurData[i];
String lastMeterValue = meterLastData == null || StringUtils.isBlank(meterLastData[i]) ? "0" : meterLastData[i];
String curCop = "0.00";
if (StringUtils.isBlank(curMeterValue) || new BigDecimal(curMeterValue).compareTo(BigDecimal.ZERO) == 0) {
curCop = "0.00";
} else {
curCop = new BigDecimal(curValue).divide(new BigDecimal(curMeterValue), 2, RoundingMode.HALF_UP).toString();
}
String lastCop = "0.00";
if (StringUtils.isBlank(lastMeterValue) || new BigDecimal(lastMeterValue).compareTo(BigDecimal.ZERO) == 0) {
lastCop = "0.00";
} else {
lastCop = new BigDecimal(lastValue).divide(new BigDecimal(lastMeterValue), 2, RoundingMode.HALF_UP).toString();
}
// 同比操作
String yoy = "0.00%";
if (new BigDecimal(lastCop).compareTo(BigDecimal.ZERO) == 0) {
yoy = "0.00%";
} else {
yoy = new BigDecimal(curCop).subtract(new BigDecimal(lastCop)).divide(new BigDecimal(lastCop), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).toString() + "%";
}
copMap[i] = curCop;
copLastMap[i] = lastCop;
yoyMap[i] = yoy;
}
energyConsumptionDTO.setData(Arrays.asList(
new HashMap<String, Object>() {{
put("curValue", copMap);
put("lastValue", copLastMap);
put("yoy", yoyMap);
}}
));
}
return AjaxResult.success(energyConsumptionDTO);
}
@Override
public AjaxResult mom(EnergyConsumptionVO vo) {
List<ConsumptionAnalyze> consumptionAnalyzeEntities = null;
@ -420,8 +541,9 @@ public class EnergyServiceImpl implements IEnergyService {
String tableName = "data_"+vo.getTimeType();
// 计算COP值(后期有一个表存储)
if (!StringUtils.isBlank(vo.getParamType()) && vo.getParamType().equalsIgnoreCase("cop")) {
tableName = "data_min_cop";
deviceType = "system";
// tableName = "data_min_cop";
// deviceType = "system";
return calcCopMomData(vo);
}
consumptionAnalyzeEntities = energyMapper.queryMonthAndYearMom(vo.getStartTime(), vo.getEndTime(), startTime, endTime, tableName, getTimeLen(vo.getTimeType()), deviceType, vo.getTimeType());
} else {
@ -430,9 +552,10 @@ public class EnergyServiceImpl implements IEnergyService {
String tableName2 = "data_"+vo.getTimeType()+vo.getEndTime().substring(0, 4);
// 计算COP值(后期有一个表存储)
if (!StringUtils.isBlank(vo.getParamType()) && vo.getParamType().equalsIgnoreCase("cop")) {
tableName1 = "data_min_cop";
tableName2 = "data_min_cop";
deviceType = "system";
// tableName1 = "data_min_cop";
// tableName2 = "data_min_cop";
// deviceType = "system";
return calcCopMomData(vo);
}
if (tableName1.equalsIgnoreCase(tableName2)) {
// 单表查询
@ -445,6 +568,25 @@ public class EnergyServiceImpl implements IEnergyService {
return getConsumptionDTOHttpResult(consumptionAnalyzeEntities, "mom");
}
private AjaxResult calcCopMomData(EnergyConsumptionVO vo) {
// 获取冷量读数
vo.setParamType("cloud");
AjaxResult cloudData = mom(vo);
EnergyConsumptionDTO cloudEnergyData = (EnergyConsumptionDTO) cloudData.get("data");
if (cloudEnergyData == null) {
return AjaxResult.error();
}
// 获取电量读数
vo.setParamType("meter");
AjaxResult meterData = mom(vo);
EnergyConsumptionDTO meterEnergyData = (EnergyConsumptionDTO) meterData.get("data");
if (meterEnergyData == null) {
return AjaxResult.error();
}
return getEnergyConsumptionDTOHttpResult(meterEnergyData, cloudEnergyData);
}
@Override
public AjaxResult yoyMom(EnergyConsumptionVO vo) {
// 多线程分别求出yoy和mom

794
mh-system/src/main/resources/mapper/system/EnergyMapper.xml

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save