8 changed files with 629 additions and 9 deletions
-
8common/core/src/main/java/com/thing/common/core/enumeration/AttributeTypeEnum.java
-
6modules/report-analysis/src/main/java/com/thing/carbon/energyrepory/dto/PeakValleyDosageReq.java
-
43modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/controller/PeakValleyController.java
-
17modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/dto/EnergyValueAndPrice.java
-
308modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/excel/PricePeakValleyDosageUsageAndCostExcel.java
-
114modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/excel/PricePeakValleyUsageAndCostExcel.java
-
2modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/service/PeakValleyService.java
-
140modules/report-analysis/src/main/java/com/thing/carbon/peakvalley/service/impl/PeakValleyServiceImpl.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; |
|||
} |
|||
@ -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(); |
|||
} |
|||
} |
|||
@ -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; |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue