Browse Source

设备管理-202409025

thing_master
xiezw 1 year ago
parent
commit
424041a36e
  1. 8
      common/core/src/main/java/com/thing/common/core/enumeration/AttributeTypeEnum.java
  2. 6
      modules/report-analysis/src/main/java/com/thing/carbon/energyrepory/dto/PeakValleyDosageReq.java
  3. 43
      modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/controller/PeakValleyController.java
  4. 17
      modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/dto/EnergyValueAndPrice.java
  5. 308
      modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/excel/PricePeakValleyDosageUsageAndCostExcel.java
  6. 114
      modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/excel/PricePeakValleyUsageAndCostExcel.java
  7. 2
      modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/service/PeakValleyService.java
  8. 140
      modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/service/impl/PeakValleyServiceImpl.java

8
common/core/src/main/java/com/thing/common/core/enumeration/AttributeTypeEnum.java

@ -43,6 +43,14 @@ public enum AttributeTypeEnum {
return attribute.replaceAll(optional.get().name(), "")+attributeType;
}
public static List<LocalDateTime> getTimeRangeByHh(LocalDateTime startTime, LocalDateTime endTime) {
LocalDateTime beginTimeOfHour = LocalDateTime.of(startTime.toLocalDate(), LocalTime.MIN);
LocalDateTime endTimeOfHour = endTime.withMinute(0).withSecond(0).withNano(0);;
return Stream.iterate(beginTimeOfHour, d -> d.plusHours(1))
.limit(ChronoUnit.HOURS.between(beginTimeOfHour, endTimeOfHour) + 1)
.collect(Collectors.toList());
}
/**
* 获取给定时间所在的被指定范围类型的时间区间

6
modules/report-analysis/src/main/java/com/thing/carbon/energyrepory/dto/PeakValleyDosageReq.java

@ -33,6 +33,9 @@ public class PeakValleyDosageReq {
@Schema(description = "用量 属性值")
private BigDecimal dosage;
@Schema(description = "用量占比--用量和费用报表用")
private BigDecimal dosageRatio;
@Schema(description = "时间")
private Long ts;
@ -42,6 +45,9 @@ public class PeakValleyDosageReq {
@Schema(description = "用量的总价")
private BigDecimal totalPrice;
@Schema(description = "用量的总价占比--用量和费用报表用")
private BigDecimal totalPriceRatio;
@Schema(description = "年份")
private Integer year;
@Schema(description = "月份")

43
modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/controller/PeakValleyController.java

@ -6,10 +6,7 @@ import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.hutool.core.util.ObjectUtil;
import com.thing.carbon.energyrepory.dto.PeakValleyDosageReq;
import com.thing.carbon.peakvalley.dto.*;
import com.thing.carbon.peakvalley.excel.PeakValleyDosageVerticalExcel;
import com.thing.carbon.peakvalley.excel.PeakValleyExcel;
import com.thing.carbon.peakvalley.excel.PeakValleyPriceVerticalExcel;
import com.thing.carbon.peakvalley.excel.PricePeakValleyExcel;
import com.thing.carbon.peakvalley.excel.*;
import com.thing.carbon.peakvalley.service.PeakValleyService;
import com.thing.common.core.utils.excel.ExcelUtils;
import com.thing.common.core.utils.export.ExcelEntityGenerator;
@ -23,6 +20,7 @@ import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -129,8 +127,6 @@ public class PeakValleyController {
// 导出
ExcelUtils.downLoadExcel("峰平谷尖用能报表.xls", response, workbook);
}
}
@PostMapping("price/peakValleyExport")
@ -173,12 +169,41 @@ public class PeakValleyController {
// 导出
ExcelUtils.downLoadExcel("峰平谷尖成本报表.xls", response, workbook);
}
}
@PostMapping("peakValleyReport/usageAndCost")
@Operation(summary="尖峰谷平用量和费用报表")
public Result<Map<String,List<PeakValleyReportReq>>> peakValleyReportUsageAndCost(@RequestBody PeakValleyParam param){
Map<String,List<PeakValleyReportReq>> result = peakValleyService.peakValleyReportUsageAndCost(param);
return new Result<Map<String,List<PeakValleyReportReq>>>().ok(result);
}
@PostMapping("peakValleyUsageAndCostExport")
@Operation(summary ="尖峰谷平用量和费用查询导出")
public void peakValleyUsageAndCostExport(@RequestBody PeakValleyExportParam param, HttpServletResponse response){
List<PeakValleyReportReq> exportInfO = new ArrayList<>();
param.getThingParams().forEach(temp->{
Map<String,List<PeakValleyReportReq>> result = peakValleyService.peakValleyReportUsageAndCost(PeakValleyParam.builder(param,temp));
if(ObjectUtil.isNotEmpty(result)){
result.keySet().forEach(info->{
if(ObjectUtil.isNotEmpty(result.get(info))){
exportInfO.addAll(result.get(info));
}
});
}
});
// 数据类型转换
List<PricePeakValleyUsageAndCostExcel> excelDataList = PricePeakValleyUsageAndCostExcel.createBeans(exportInfO, param.getDateType());
// 生成表头 & 生成数据集合
List<ExcelExportEntity> excelExportEntities = ExcelEntityGenerator.generateExportEntity(new ArrayList<>(excelDataList));
List<Map<String, Object>> dataCollection = ExcelEntityGenerator.generateDataCollection(new ArrayList<>(excelDataList));
// 计算合并区域
List<CellRangeAddress> regions = PricePeakValleyUsageAndCostExcel.calculateMergeRegion(excelDataList);
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(null, "峰平谷尖用量和费用报表"), excelExportEntities, dataCollection);
regions.forEach(workbook.getSheetAt(0)::addMergedRegion);
// 导出
ExcelUtils.downLoadExcel("峰平谷尖用量和费用报表.xls", response, workbook);
}
}

17
modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/dto/EnergyValueAndPrice.java

@ -0,0 +1,17 @@
package com.thing.carbon.peakvalley.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.math.BigDecimal;
@Data
public class EnergyValueAndPrice {
@Schema(description = "总用量")
private BigDecimal totalUsage;
@Schema(description = "总单价")
private BigDecimal totalPrice;
}

308
modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/excel/PricePeakValleyDosageUsageAndCostExcel.java

@ -0,0 +1,308 @@
package com.thing.carbon.peakvalley.excel;
import com.thing.carbon.energyrepory.dto.PeakValleyDosageReq;
import com.thing.common.core.annotation.CustomExcel;
import com.thing.common.core.enumeration.AttributeTypeEnum;
import com.thing.common.core.utils.ConvertUtils;
import com.thing.common.core.utils.DateTimeUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PricePeakValleyDosageUsageAndCostExcel {
private static final Map<Integer, String> weekMap = new HashMap<>();
static {
weekMap.put(1, "周一");
weekMap.put(2, "周二");
weekMap.put(3, "周三");
weekMap.put(4, "周四");
weekMap.put(5, "周五");
weekMap.put(6, "周六");
weekMap.put(7, "周日");
}
private String dateType;
private Long ts;
@Getter
private BigDecimal val1;
@CustomExcel(keyGenerator = "generateKey1", columnNameGenerator = "getTitle1", width = 12, orderGenerator = "getOrder1", groupGenerator = "getGroupName1")
private String val1Export="--";
@Getter
private BigDecimal val2;
@CustomExcel(keyGenerator = "generateKey2",columnNameGenerator = "getTitle2", width = 12, orderGenerator = "getOrder2", groupGenerator = "getGroupName1")
private String val2Export="--";
@Getter
private BigDecimal val3;
@CustomExcel(keyGenerator = "generateKey3",columnNameGenerator = "getTitle3", width = 12, orderGenerator = "getOrder3", groupGenerator = "getGroupName1")
private String val3Export="--";
private Integer hh;
private Integer dd;
@Schema(description = "年份")
private Integer year;
@Schema(description = "月份")
private Integer month;
@Schema(description = "当年第几周")
private Integer weekOfYear;
@Schema(description = "当月第几天")
private Integer dateOfMonth;
@Schema(description = "当周周几")
private Integer dateOfWeek;
@Schema(description = "当天几点")
private Integer hourOfDay;
@Schema(description = "数据标签 weekTotal周总计,item用量")
private String label;
public static PricePeakValleyDosageUsageAndCostExcel crateBean(PeakValleyDosageReq dosageReq, String dateType){
PricePeakValleyDosageUsageAndCostExcel excel = ConvertUtils.convertWithTypeAdapt(dosageReq, PricePeakValleyDosageUsageAndCostExcel.class);
if(dosageReq.getTs()!=null){
LocalDateTime dateTime = DateTimeUtils.parseDateTime(dosageReq.getTs());
int hh = dateTime.getHour();
int dd = dateTime.getDayOfMonth();
excel.setTs(dosageReq.getTs());
excel.setHh(hh);
excel.setDd(dd);
}
excel.setVal1(dosageReq.getDosage());
excel.setVal2(dosageReq.getTotalPrice());
excel.setVal3(dosageReq.getDosageRatio());
excel.setVal1Export(Objects.nonNull(excel.getVal1()) ? excel.getVal1().toString() : "--");
excel.setVal2Export(Objects.nonNull(excel.getVal2()) ? excel.getVal2().toString() : "--");
excel.setVal3Export(Objects.nonNull(excel.getVal3()) ? excel.getVal3().toString() : "--");
excel.setDateType(dateType);
return excel;
}
public String generateKey1() {
AttributeTypeEnum attributeTypeEnum = AttributeTypeEnum.match(dateType);
switch (attributeTypeEnum) {
case am:
case hh:
return hh+"时"+"用量";
case dd:
return dd+"日"+"用量";
case week:
StringBuilder stringBuilder = new StringBuilder();
if (Objects.equals(label, "item")) {
stringBuilder
.append(addZeroPrefix(month))
.append("-")
.append(addZeroPrefix(dateOfMonth))
.append(" ")
.append(weekMap.get(dateOfWeek));
} else if (Objects.equals(label, "weekTotal")) {
stringBuilder.append(weekOfYear).append("周 总计");
}
return stringBuilder.toString();
case mm:
return month+"月"+"用量";
default:
}
return null;
}
public String generateKey2() {
AttributeTypeEnum attributeTypeEnum = AttributeTypeEnum.match(dateType);
switch (attributeTypeEnum) {
case am:
case hh:
return hh+"时"+"费用";
case dd:
return dd+"日"+"费用";
case week:
StringBuilder stringBuilder = new StringBuilder();
if (Objects.equals(label, "item")) {
stringBuilder
.append(addZeroPrefix(month))
.append("-")
.append(addZeroPrefix(dateOfMonth))
.append(" ")
.append(weekMap.get(dateOfWeek));
} else if (Objects.equals(label, "weekTotal")) {
stringBuilder.append(weekOfYear).append("周 总计");
}
return stringBuilder.toString();
case mm:
return month+"月"+"费用";
default:
}
return null;
}
public String generateKey3() {
AttributeTypeEnum attributeTypeEnum = AttributeTypeEnum.match(dateType);
switch (attributeTypeEnum) {
case am:
case hh:
return hh+"时"+"用量占比";
case dd:
return dd+"日"+"用量占比";
case week:
StringBuilder stringBuilder = new StringBuilder();
if (Objects.equals(label, "item")) {
stringBuilder
.append(addZeroPrefix(month))
.append("-")
.append(addZeroPrefix(dateOfMonth))
.append(" ")
.append(weekMap.get(dateOfWeek));
} else if (Objects.equals(label, "weekTotal")) {
stringBuilder.append(weekOfYear).append("周 总计");
}
return stringBuilder.toString();
case mm:
return month+"月"+"用量占比";
default:
}
return null;
}
private String getTitle1(){
return "用量";
}
private String getTitle2(){
return "费用";
}
private String getTitle3(){
return "用量占比";
}
private String getTitle(){
AttributeTypeEnum attributeTypeEnum = AttributeTypeEnum.match(dateType);
switch (attributeTypeEnum) {
case am:
case hh:
return hh+"时";
case dd:
return dd+"日";
case week:
StringBuilder stringBuilder = new StringBuilder();
if (Objects.equals(label, "item")) {
stringBuilder
.append(addZeroPrefix(month))
.append("-")
.append(addZeroPrefix(dateOfMonth))
.append(" ")
.append(weekMap.get(dateOfWeek));
} else if (Objects.equals(label, "weekTotal")) {
stringBuilder.append(weekOfYear).append("周 总计");
}
return stringBuilder.toString();
case mm:
return month+"月";
default:
}
return null;
}
private String getGroupName1(){
AttributeTypeEnum attributeTypeEnum = AttributeTypeEnum.match(dateType);
switch (attributeTypeEnum) {
case am:
case hh:
return hh+"时";
case dd:
return dd+"日";
case week:
StringBuilder stringBuilder = new StringBuilder();
if (Objects.equals(label, "item")) {
stringBuilder
.append(addZeroPrefix(month))
.append("-")
.append(addZeroPrefix(dateOfMonth))
.append(" ")
.append(weekMap.get(dateOfWeek));
} else if (Objects.equals(label, "weekTotal")) {
stringBuilder.append(weekOfYear).append("周 总计");
}
return stringBuilder.toString();
case mm:
return month+"月";
default:
}
return null;
}
private String getGroupName(){
AttributeTypeEnum attributeTypeEnum = AttributeTypeEnum.match(dateType);
switch (attributeTypeEnum) {
case am:
case hh:
return year+"年"+month+"月"+dd+"日";
case week:
return "第"+weekOfYear+"周";
case dd:
return year+"年"+month+"月";
case mm:
return year+"年";
default:
}
return null;
}
private int getOrder1(){
return getOrder();
}
private int getOrder2(){
return getOrder()+1;
}
private int getOrder3(){
return getOrder()+2;
}
private int getOrder(){
int orderNum = 8;
AttributeTypeEnum attributeTypeEnum = AttributeTypeEnum.match(dateType);
switch (attributeTypeEnum) {
case am:
case hh:
orderNum+=(24-hh);
break;
case week:
// 算法可以自定义这里只是从数学上给出一个粗浅的计算方法
if (Objects.equals(label, "item")) {
orderNum += (((52 - weekOfYear) << 3) + (8 - dateOfWeek));
} else if (Objects.equals(label, "weekTotal")) {
orderNum += (52 - weekOfYear) << 3;
}
break;
case dd:
orderNum+=(31-dd);
break;
case mm:
orderNum+=(12-month);
break;
default:
}
return orderNum;
}
private String addZeroPrefix(Integer num) {
return (num < 10 && num > 0) ? "0" + num : num.toString();
}
}

114
modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/excel/PricePeakValleyUsageAndCostExcel.java

@ -0,0 +1,114 @@
package com.thing.carbon.peakvalley.excel;
import com.thing.carbon.peakvalley.dto.PeakValleyReportReq;
import com.thing.common.core.annotation.CustomExcel;
import com.thing.common.core.annotation.CustomExcelCollection;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.poi.ss.util.CellRangeAddress;
import java.util.*;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PricePeakValleyUsageAndCostExcel {
@CustomExcel(name = "结构", width = 25)
private String thingName;
@CustomExcel(name = "能源品种",orderNum=1)
private String baseName;
@CustomExcel(name = "类型",orderNum=2)
private String attrName;
@CustomExcel(name = "单价",orderNum=3)
private String uintPrice;
@CustomExcel(name = "总量",orderNum=4)
private String allDosage;
@CustomExcel(name = "总价",orderNum=5)
private String totalPrice;
@CustomExcel(name = "用量占比",orderNum=6)
private String realityRatio;
@CustomExcelCollection
private List<PricePeakValleyDosageUsageAndCostExcel> peakValleyDosageReqs;
public static List<PricePeakValleyUsageAndCostExcel> createBeans(List<PeakValleyReportReq> reqs, String dataType){
List<PricePeakValleyUsageAndCostExcel> result = new ArrayList<>();
reqs.forEach(temp->{
PricePeakValleyUsageAndCostExcel excelBean = new PricePeakValleyUsageAndCostExcel();
excelBean.setThingName(temp.getThingName());
excelBean.setBaseName(temp.getBaseName()+"("+temp.getBaseUnit()+")");
excelBean.setAllDosage(Objects.nonNull(temp.getAllDosage()) ? temp.getAllDosage().toString() : "--");
excelBean.setUintPrice(Objects.nonNull(temp.getUintPrice()) ? temp.getUintPrice()+"" : "--");
excelBean.setTotalPrice(Objects.nonNull(temp.getTotalPrice()) ? temp.getTotalPrice()+"" : "--");
excelBean.setAttrName(temp.getAttrName());
excelBean.setRealityRatio(Objects.nonNull(temp.getRealityRatio()) ? temp.getRealityRatio()+"" : "--");
List<PricePeakValleyDosageUsageAndCostExcel> dosageExcels = new ArrayList<>();
if(temp.getPeakValleyDosageReqs()!=null){
temp.getPeakValleyDosageReqs().forEach(info->{
//特殊处理只要当前时间之前的数据
if(info.getTs()==null||info.getTs()<=System.currentTimeMillis()){
PricePeakValleyDosageUsageAndCostExcel dosageExcel = PricePeakValleyDosageUsageAndCostExcel.crateBean(info,dataType);
dosageExcels.add(dosageExcel);
}
});
}
excelBean.setPeakValleyDosageReqs(dosageExcels);
result.add(excelBean);
});
return result;
}
@SuppressWarnings({"unchecked"})
public static List<CellRangeAddress> calculateMergeRegion(List<PricePeakValleyUsageAndCostExcel> excelDataList) {
List<CellRangeAddress> mergeRegionList = new ArrayList<>();
Map<String, Pair<Integer, Integer>> thingCountMap = new HashMap<>();
Map<String, Pair<Integer, Integer>> thingEnergyCountMap = new HashMap<>();
int beginRowOfThing =1 ;
int beginRowOfEnergy =1;
for (PricePeakValleyUsageAndCostExcel currentData : excelDataList) {
if (!thingCountMap.containsKey(currentData.getThingName())) {
thingCountMap.put(
currentData.getThingName(),
MutablePair.of(++beginRowOfThing, beginRowOfThing));
} else {
Pair<Integer, Integer> thingCountPair =
thingCountMap.get(currentData.getThingName());
thingCountPair.setValue(++beginRowOfThing);
}
String energyKey = currentData.getThingName() + "_" + currentData.getBaseName();
if (!thingEnergyCountMap.containsKey(energyKey)) {
thingEnergyCountMap.put(
energyKey, MutablePair.of(++beginRowOfEnergy, beginRowOfEnergy));
} else {
Pair<Integer, Integer> energyCountPair = thingEnergyCountMap.get(energyKey);
energyCountPair.setValue(++beginRowOfEnergy);
}
}
thingCountMap.forEach(
(k, pair) -> {
if (Objects.equals(pair.getLeft(), pair.getRight())) {
return;
}
mergeRegionList.add(
new CellRangeAddress(pair.getLeft(), pair.getRight(), 0, 0));
});
thingEnergyCountMap.forEach(
(k, pair) -> {
if (Objects.equals(pair.getLeft(), pair.getRight())) {
return;
}
mergeRegionList.add(
new CellRangeAddress(pair.getLeft(), pair.getRight(), 1, 1));
});
return mergeRegionList;
}
}

2
modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/service/PeakValleyService.java

@ -26,4 +26,6 @@ public interface PeakValleyService {
List<CarbonEnergyDictRelationEntity> queryEnergyList();
Map<String, List<PeakValleyReportReq>> peakValleyReportUsageAndCost(PeakValleyParam param);
}

140
modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/service/impl/PeakValleyServiceImpl.java

@ -323,6 +323,9 @@ public class PeakValleyServiceImpl implements PeakValleyService {
}else {
if(ObjectUtil.isNotEmpty(param.getStartTime())){
timeRange= AttributeTypeEnum.getTimeRange(DateTimeUtils.parseDateTime(param.getStartTime()),(DateTimeUtils.parseDateTime(param.getEndTime())));
if (param.getDateType().equals("hh")){
timeRange= AttributeTypeEnum.getTimeRangeByHh(DateTimeUtils.parseDateTime(param.getStartTime()),(DateTimeUtils.parseDateTime(param.getEndTime())));
}
}else {
AttributeTypeEnum attributeTypeEnum = AttributeTypeEnum.match(param.getDateType());
timeRange = attributeTypeEnum.getTimeRange(param.getBeginTime());
@ -450,6 +453,7 @@ public class PeakValleyServiceImpl implements PeakValleyService {
energyTimeLabelData.forEach(temp->{
List<PeakValleyDosageReq> infoList = groupList.get(temp.getTs());
PeakValleyDosageReq req = new PeakValleyDosageReq();
req.setThingCode(dosageReq.getThingCode());
req.setUint(dosageReq.getUint());
req.setTs(temp.getTs());
req.setBaseCode(dosageReq.getBaseCode());
@ -713,7 +717,143 @@ public class PeakValleyServiceImpl implements PeakValleyService {
return energys;
}
@Override
public Map<String, List<PeakValleyReportReq>> peakValleyReportUsageAndCost(PeakValleyParam param) {
Map<String,List<PeakValleyReportReq>> result = new HashMap<>();
List<String> keys = List.of(param.getKeys().split(","));
keys.forEach(temp->{
param.setKey(temp);
List<PeakValleyReportReq> info = this.peakValleyReportByKeyNew(param);
result.put(temp,info);
});
return result;
}
public List<PeakValleyReportReq> peakValleyReportByKeyNew(PeakValleyParam param){
String dataType = param.getDateType();
List<PeakValleyReportReq> result = new ArrayList<>();
PeakValleyAnalysisReq peakValleyAnalysisReq = this.analysis(param);
if(ObjectUtil.isEmpty(peakValleyAnalysisReq)){
return null;
}
List<LocalDateTime> timeRange;
if(dataType.equals("week")||dataType.equals("any")){
if(ObjectUtil.isNotEmpty(param.getStartTime())){
timeRange= AttributeTypeEnum.getTimeRange(DateTimeUtils.parseDateTime(param.getStartTime()),(DateTimeUtils.parseDateTime(param.getEndTime())));
}else {
param.setDateType("week");
timeRange= AttributeTypeEnum.getTimeRange(param.getBeginTime().minusDays(30),param.getBeginTime());
}
}else {
if(ObjectUtil.isNotEmpty(param.getStartTime())){
timeRange= AttributeTypeEnum.getTimeRange(DateTimeUtils.parseDateTime(param.getStartTime()),(DateTimeUtils.parseDateTime(param.getEndTime())));
if (param.getDateType().equals("hh")){
timeRange= AttributeTypeEnum.getTimeRangeByHh(DateTimeUtils.parseDateTime(param.getStartTime()),(DateTimeUtils.parseDateTime(param.getEndTime())));
}
}else {
AttributeTypeEnum attributeTypeEnum = AttributeTypeEnum.match(param.getDateType());
timeRange = attributeTypeEnum.getTimeRange(param.getBeginTime());
}
}
List<EnergyTimeLabelData> energyTimeLabelData = this.generateEmptyData(timeRange, param.getDateType());
Map<String, List<PeakValleyDosageReq>> groupList = peakValleyAnalysisReq.getPeakValleyDosageReqs().stream()
.collect(Collectors.groupingBy(PeakValleyDosageReq::getAttrCode));
Map<String,EnergyValueAndPrice> usageAndCostMap = new HashMap<>();
peakValleyAnalysisReq.getPriceStatisticsInfos().forEach(temp->{
PeakValleyReportReq req = new PeakValleyReportReq();
req.setThingCode(param.getThingCode());
req.setThingName(param.getThingName());
req.setBaseCode(param.getKey());
req.setBaseName(temp.getBaseName());
req.setAttrCode(temp.getAttrCode());
req.setAttrName(temp.getAttrName());
req.setAllDosage(temp.getAllDosage());
req.setRealityRatio(temp.getAllDosageRatio());
req.setStandardRatio(temp.getStandardRatio());
req.setTotalPrice(temp.getAllTotalPrice());
req.setTotalPriceRatio(temp.getPriceRatio());
req.setSort(temp.getSort());
req.setBaseUnit(temp.getUnit());
req.setUintPrice(temp.getUnitPrice());
if(!dataType.equals("any")){
List<PeakValleyDosageReq> dosageReqs = groupList.get(req.getAttrCode());
if(ObjectUtil.isNotEmpty(dosageReqs)){
Map<Long, List<PeakValleyDosageReq>> tsDosageList = dosageReqs.stream()
.collect(Collectors.groupingBy(PeakValleyDosageReq::getTs));
energyTimeLabelData.forEach(ts->{
if(ObjectUtil.isEmpty(tsDosageList.get(ts.getTs()))){
PeakValleyDosageReq pvBean = new PeakValleyDosageReq();
pvBean.setTs(ts.getTs());
pvBean.setBaseCode(req.getBaseCode());
pvBean.setBaseName(req.getBaseName());
pvBean.setAttrCode(req.getAttrCode());
pvBean.setAttrName(req.getAttrName());
dosageReqs.add(pvBean);
}
});
dosageReqs.sort(Comparator.comparingLong(PeakValleyDosageReq::getTs).reversed());
req.setPeakValleyDosageReqs(dosageReqs);
}
if(req.getSort()==99){
List<PeakValleyDosageReq> peakValleyDosageReqs = this.total(peakValleyAnalysisReq.getPeakValleyDosageReqs(),energyTimeLabelData);
//记录一下每个设备总的用量和总价
if (CollectionUtil.isNotEmpty(peakValleyDosageReqs)){
for (PeakValleyDosageReq dosageReq:peakValleyDosageReqs){
EnergyValueAndPrice energyValueAndPrice = new EnergyValueAndPrice();
energyValueAndPrice.setTotalUsage(dosageReq.getDosage());
energyValueAndPrice.setTotalPrice(dosageReq.getTotalPrice());
usageAndCostMap.put(dosageReq.getThingCode()+"-"+dosageReq.getTs(),energyValueAndPrice);
}
}
peakValleyDosageReqs.sort(Comparator.comparingLong(PeakValleyDosageReq::getTs).reversed());
req.setPeakValleyDosageReqs(peakValleyDosageReqs);
}
}
result.add(req);
result.sort(Comparator.comparing(PeakValleyReportReq::getSort));
});
//处理打标
result.forEach(info->{
info.getPeakValleyDosageReqs().forEach(t->{
LocalDateTime time = DateTimeUtils.parseDateTime(t.getTs());
//日期打标
t.setYear(time.getYear());
t.setMonth(time.getMonthValue());
t.setWeekOfYear(time.get(WeekFields.ISO.weekOfYear()));
t.setDateOfMonth(time.getDayOfMonth());
t.setDateOfWeek(time.getDayOfWeek().getValue());
t.setHourOfDay(time.getHour());
t.setLabel("item");
BigDecimal totalUsage =null;
BigDecimal totalCost =null;
if (usageAndCostMap!=null){
EnergyValueAndPrice energyValueAndPrice = usageAndCostMap.get(t.getThingCode()+"-"+t.getTs());
if (energyValueAndPrice!=null){
totalUsage = energyValueAndPrice.getTotalUsage();
totalCost = energyValueAndPrice.getTotalPrice();
}
}
if (t.getDosage()!=null &&totalUsage!=null && totalUsage.compareTo(BigDecimal.ZERO)!=0 ){
t.setDosageRatio(t.getDosage().divide(totalUsage,2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
}
if (t.getTotalPrice()!=null &&totalCost!=null && totalCost.compareTo(BigDecimal.ZERO)!=0 ){
t.setTotalPriceRatio(t.getTotalPrice().divide(totalCost,2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
}
});
if(dataType.equals("week")){
//按tWeekOfYear分组获取每周得总计数据
Map<Integer, List<PeakValleyDosageReq>> weekDataMap =
info.getPeakValleyDosageReqs().stream()
.filter(item -> Objects.equals(item.getLabel(), "item"))
.collect(Collectors.groupingBy(PeakValleyDosageReq::getWeekOfYear));
info.getPeakValleyDosageReqs().addAll(summary(weekDataMap));
System.out.println(info.getPeakValleyDosageReqs().size());
}
});
return result;
}
/**

Loading…
Cancel
Save