Browse Source

碳资产分析-趋势

2025年2月27日10:31:29
qingyuan_dev_new
lishuai 11 months ago
parent
commit
0c987f5af0
  1. 14
      modules/qingyuan/src/main/java/com/thing/qingyuan/carbon/dto/QyCarbonAssetDTO.java
  2. 151
      modules/qingyuan/src/main/java/com/thing/qingyuan/carbon/service/impl/QyCarbonAssetServiceImpl.java

14
modules/qingyuan/src/main/java/com/thing/qingyuan/carbon/dto/QyCarbonAssetDTO.java

@ -19,15 +19,21 @@ public class QyCarbonAssetDTO implements Serializable {
@Schema(description = "碳资产标签")
private String tag;
@Schema(description = "单价")
private BigDecimal unitPrice;
private BigDecimal unitPrice= BigDecimal.ZERO;
@Schema(description = "数量")
private BigDecimal total;
private BigDecimal total= BigDecimal.ZERO;
@Schema(description = "价格")
private BigDecimal price;
private BigDecimal price= BigDecimal.ZERO;
@Schema(description = "账户余额")
private BigDecimal balance;
private BigDecimal balance= BigDecimal.ZERO;
@Schema(description = "交易性质(碳资产状态:1.待发 2.已发 3.买入 4.核销 5.卖出) ")
private String state;
@Schema(description = "买入")
private BigDecimal buy= BigDecimal.ZERO;
@Schema(description = "卖出")
private BigDecimal sell= BigDecimal.ZERO;
@Schema(description = "卖出")
private BigDecimal off= BigDecimal.ZERO;
public QyCarbonAssetDTO() {
}

151
modules/qingyuan/src/main/java/com/thing/qingyuan/carbon/service/impl/QyCarbonAssetServiceImpl.java

@ -21,7 +21,6 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
@ -174,139 +173,81 @@ public class QyCarbonAssetServiceImpl implements QyCarbonAssetService {
*/
@Override
public List<QyCarbonAssetDTO> countRecentCarbonAsset(Long startTime, Long endTime) {
List<QyCarbonAssetDTO> resList = Lists.newArrayList();
List<QyCarbonAssetDTO> tempList = Lists.newArrayList();
Boolean sixMonth = isSixMonth(startTime, endTime);
//查询碳配额这段时间的账户总额度
List<QyCarbonAssetDTO> carbonList = qyCarbonQuotaInfoService.getMapper().selectListByQueryAs(
QueryWrapper.create().select(sum(QY_CARBON_QUOTA_INFO_ENTITY.TOTAL).as("total")
QueryWrapper.create().select(QY_CARBON_QUOTA_INFO_ENTITY.TOTAL
, QY_CARBON_QUOTA_INFO_ENTITY.STATE, QY_CARBON_QUOTA_INFO_ENTITY.ISSUE_DATE)
.ge(QyCarbonQuotaInfoEntity::getIssueDate, startTime)
.lt(QyCarbonQuotaInfoEntity::getIssueDate, endTime)
.in(QyCarbonQuotaInfoEntity::getState, "2", "3", "4", "5")
.groupBy(QY_CARBON_QUOTA_INFO_ENTITY.STATE, QY_CARBON_QUOTA_INFO_ENTITY.ISSUE_DATE)
.orderBy(QY_CARBON_QUOTA_INFO_ENTITY.ISSUE_DATE, true)
, QyCarbonAssetDTO.class);
if(CollectionUtils.isNotEmpty(carbonList)){
tempList.addAll(carbonList);
}
//查询CCER这段时间的账户总额度
List<QyCarbonAssetDTO> ccerInfoList = qyCcerInfoService.getMapper().selectListByQueryAs(
QueryWrapper.create().select(sum(QY_CCER_INFO_ENTITY.TOTAL).as("total")
QueryWrapper.create().select(QY_CCER_INFO_ENTITY.TOTAL
, QY_CCER_INFO_ENTITY.STATE, QY_CCER_INFO_ENTITY.ISSUE_DATE)
.ge(QyCcerInfoEntity::getIssueDate, startTime)
.lt(QyCcerInfoEntity::getIssueDate, endTime)
.in(QyCcerInfoEntity::getState, "2", "3", "4", "5")
.groupBy(QY_CCER_INFO_ENTITY.STATE, QY_CCER_INFO_ENTITY.ISSUE_DATE)
.orderBy(QY_CCER_INFO_ENTITY.ISSUE_DATE, true)
, QyCarbonAssetDTO.class);
if(CollectionUtils.isNotEmpty(ccerInfoList)){
tempList.addAll(ccerInfoList);
}
//查询绿证这段时间的账户总额度
List<QyCarbonAssetDTO> greenList = qyGreenCertificateInfoService.getMapper().selectListByQueryAs(
QueryWrapper.create().select(sum(QY_GREEN_CERTIFICATE_INFO_ENTITY.TOTAL).as("total")
QueryWrapper.create().select(QY_GREEN_CERTIFICATE_INFO_ENTITY.TOTAL
, QY_GREEN_CERTIFICATE_INFO_ENTITY.STATE, QY_GREEN_CERTIFICATE_INFO_ENTITY.ISSUE_DATE)
.ge(QyGreenCertificateInfoEntity::getIssueDate, startTime)
.lt(QyGreenCertificateInfoEntity::getIssueDate, endTime)
.in(QyGreenCertificateInfoEntity::getState, "2", "3", "4", "5")
.groupBy(QY_GREEN_CERTIFICATE_INFO_ENTITY.STATE, QY_GREEN_CERTIFICATE_INFO_ENTITY.ISSUE_DATE)
.orderBy(QY_GREEN_CERTIFICATE_INFO_ENTITY.ISSUE_DATE, true)
, QyCarbonAssetDTO.class);
resList.addAll(carbonList);
resList.addAll(ccerInfoList);
resList.addAll(greenList);
// 计算每个账户的总余额
BigDecimal totalCarbonBalance = BigDecimal.ZERO;
BigDecimal totalCcerBalance = BigDecimal.ZERO;
BigDecimal totalGreenBalance = BigDecimal.ZERO;
// 计算碳配额的余额
BigDecimal carbonTotal = carbonList.stream()
.filter(s -> "2".equals(s.getState()) || "3".equals(s.getState())) // 状态为 2 3
.map(QyCarbonAssetDTO::getTotal)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal carbonSell = carbonList.stream()
.filter(s -> "4".equals(s.getState()) || "5".equals(s.getState())) // 状态为 4 5
.map(QyCarbonAssetDTO::getTotal)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal carbonBalance = carbonTotal.subtract(carbonSell);
totalCarbonBalance = totalCarbonBalance.add(carbonBalance);
// 计算CCER的余额
BigDecimal ccerTotal = ccerInfoList.stream()
.filter(s -> "2".equals(s.getState()) || "3".equals(s.getState())) // 状态为 2 3
.map(QyCarbonAssetDTO::getTotal)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal ccerSell = ccerInfoList.stream()
.filter(s -> "4".equals(s.getState()) || "5".equals(s.getState())) // 状态为 4 5
.map(QyCarbonAssetDTO::getTotal)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal ccerBalance = ccerTotal.subtract(ccerSell);
totalCcerBalance = totalCcerBalance.add(ccerBalance);
// 计算绿证的余额
BigDecimal greenTotal = greenList.stream()
.filter(s -> "2".equals(s.getState()) || "3".equals(s.getState())) // 状态为 2 3
.map(QyCarbonAssetDTO::getTotal)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal greenSell = greenList.stream()
.filter(s -> "4".equals(s.getState()) || "5".equals(s.getState())) // 状态为 4 5
.map(QyCarbonAssetDTO::getTotal)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal greenBalance = greenTotal.subtract(greenSell);
totalGreenBalance = totalGreenBalance.add(greenBalance);
// 计算所有账户的总余额
BigDecimal totalBalance = totalCarbonBalance.add(totalCcerBalance).add(totalGreenBalance);
System.out.println("Total Balance : " + totalBalance);
if(CollectionUtils.isNotEmpty(greenList)){
tempList.addAll(greenList);
}
if(CollectionUtils.isEmpty(tempList)){
return new ArrayList<>();
}
List<QyCarbonAssetDTO> resList = Lists.newArrayList();
Map<Long, List<QyCarbonAssetDTO>> collect ;
//不超过6个月的查询每天的超过的查询每个月的
if(!sixMonth){
Map<String, Map<Long, BigDecimal>> grouped = resList.stream()
.collect(Collectors.groupingBy(
QyCarbonAssetDTO::getState, // state 分组
Collectors.groupingBy(
QyCarbonAssetDTO::getIssueDate, // issueDate 分组
Collectors.reducing(BigDecimal.ZERO, QyCarbonAssetDTO::getTotal, BigDecimal::add) // total 的和
)
));
resList.clear();
grouped.forEach((state, dateMap) -> {
dateMap.forEach((date, total) -> {
resList.add(new QyCarbonAssetDTO(date, total,state,totalBalance));
});
});
collect = tempList.stream().collect(Collectors.groupingBy(QyCarbonAssetDTO::getIssueDate));
}else {
// 创建一个SimpleDateFormat来将时间戳转换为年月按月分组
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
// state 月维度issueDate转换为年月分组并计算每组的 total
Map<String, Map<String, BigDecimal>> grouped = resList.stream()
.collect(Collectors.groupingBy(
QyCarbonAssetDTO::getState, // state 分组
Collectors.groupingBy(
dto -> sdf.format(new Date(dto.getIssueDate())), // 将issueDate转换为 "yyyy-MM" 格式
Collectors.reducing(BigDecimal.ZERO, QyCarbonAssetDTO::getTotal, BigDecimal::add) // 计算每组的 total
)
));
// 将结果重新转换成 List<QyCarbonAssetDTO>
resList.clear();
grouped.forEach((state, dateMap) -> {
dateMap.forEach((date, total) -> {
// 将分组后的数据重新封装成 QyCarbonAssetDTO月份转回为时间戳
try {
Date dateObj = sdf.parse(date);
long issueDate = dateObj.getTime();
resList.add(new QyCarbonAssetDTO(issueDate, total,state,totalBalance));
} catch (Exception e) {
log.error("日期转换错误",e);
}
});
});
// 分组按年月进行分组时间戳转换为年月13位
collect = tempList.stream().collect(Collectors.groupingBy(dto -> {
// 获取13位时间戳
long timestamp = dto.getIssueDate();
// 转换为 LocalDate 对象
LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDate();
// 获取年月的13位时间戳年和月
LocalDate firstDayOfMonth = localDate.withDayOfMonth(1); // 获取当前月份的第一天
// 返回年月对应的13位时间戳
return firstDayOfMonth.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
}));
}
collect.forEach((k,v)->{
QyCarbonAssetDTO temp = new QyCarbonAssetDTO();
//1.待发 2.已发 3.买入 4.核销 5.卖出
BigDecimal total = v.stream().filter(e -> "2".equals(e.getState())).map(QyCarbonAssetDTO::getTotal).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal buy = v.stream().filter(e -> "3".equals(e.getState())).map(QyCarbonAssetDTO::getTotal).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal off = v.stream().filter(e -> "4".equals(e.getState())).map(QyCarbonAssetDTO::getTotal).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal sell = v.stream().filter(e -> "5".equals(e.getState())).map(QyCarbonAssetDTO::getTotal).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal balance = total.add(buy).subtract(sell).subtract(off);
temp.setIssueDate(k);
temp.setBuy(buy);
temp.setOff(off);
temp.setSell(sell);
temp.setBalance(balance);
temp.setTotal(total);
resList.add(temp);
});
// issueDate 升序排序
resList.sort(Comparator.comparingLong(QyCarbonAssetDTO::getIssueDate));
return resList;

Loading…
Cancel
Save