1
0

feat: 增加财务报表近5年图表展示

This commit is contained in:
2025-09-12 17:46:56 +08:00
parent 338554c523
commit cfc71f83a1
4 changed files with 254 additions and 10 deletions

View File

@@ -1,11 +1,14 @@
package com.lanyuanxiaoyao.leopard.server.controller;
import cn.hutool.core.bean.BeanUtil;
import com.lanyuanxiaoyao.leopard.core.entity.Stock;
import com.lanyuanxiaoyao.leopard.server.helper.NumberHelper;
import com.lanyuanxiaoyao.leopard.server.service.StockService;
import com.lanyuanxiaoyao.service.template.controller.GlobalResponse;
import com.lanyuanxiaoyao.service.template.controller.SimpleControllerSupport;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -44,7 +47,11 @@ public class StockController extends SimpleControllerSupport<Stock, Void, StockC
balanceSheet
.map(bs -> new BalanceSheetItem(
NumberHelper.formatFinanceDouble(bs.getTotalAssets()),
NumberHelper.formatFinanceDouble(bs.getTotalLiabilities())
NumberHelper.formatFinanceDouble(bs.getTotalCurrentAssets()),
NumberHelper.formatFinanceDouble(bs.getTotalNonCurrentAssets()),
NumberHelper.formatFinanceDouble(bs.getTotalLiabilities()),
NumberHelper.formatFinanceDouble(bs.getTotalCurrentLiabilities()),
NumberHelper.formatFinanceDouble(bs.getTotalNonCurrentLiabilities())
))
.orElse(new BalanceSheetItem()),
income
@@ -62,6 +69,24 @@ public class StockController extends SimpleControllerSupport<Stock, Void, StockC
));
}
private GlobalResponse<Map<String, Object>> convertFinanceChartData(List<?> data, String field) {
return GlobalResponse.responseDetailData(
data.stream()
.map(item -> BeanUtil.getFieldValue(item, field))
.toList()
);
}
@GetMapping("finance/{id}/{type}/{field}")
public GlobalResponse<Map<String, Object>> financeCharts(@PathVariable("id") Long id, @PathVariable("type") String type, @PathVariable("field") String field) {
return switch (type) {
case "balanceSheet" -> convertFinanceChartData(stockService.findBalanceSheetRecent(id, 5), field);
case "income" -> convertFinanceChartData(stockService.findIncomeRecent(id, 5), field);
case "cashflow" -> convertFinanceChartData(stockService.findCashFlowRecent(id, 5), field);
default -> throw new IllegalStateException("Unexpected value: " + type);
};
}
@Override
protected Function<Void, Stock> saveItemMapper() {
throw new UnsupportedOperationException();
@@ -111,10 +136,18 @@ public class StockController extends SimpleControllerSupport<Stock, Void, StockC
public record BalanceSheetItem(
String totalAssets,
String totalLiabilities
String totalCurrentAssets,
String totalNonCurrentAssets,
String totalLiabilities,
String totalCurrentLiabilities,
String totalNonCurrentLiabilities
) {
public BalanceSheetItem() {
this(
NumberHelper.FINANCE_NULL_DOUBLE,
NumberHelper.FINANCE_NULL_DOUBLE,
NumberHelper.FINANCE_NULL_DOUBLE,
NumberHelper.FINANCE_NULL_DOUBLE,
NumberHelper.FINANCE_NULL_DOUBLE,
NumberHelper.FINANCE_NULL_DOUBLE
);

View File

@@ -1,5 +1,6 @@
package com.lanyuanxiaoyao.leopard.server.helper;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import java.util.function.Function;
@@ -15,7 +16,7 @@ public class NumberHelper {
if (ObjectUtil.isNull(value)) {
return FINANCE_NULL_DOUBLE;
}
return value.toString();
return NumberUtil.decimalFormat("#.##", value);
}
public static Double parseDouble(String value) {

View File

@@ -1,8 +1,11 @@
package com.lanyuanxiaoyao.leopard.server.service;
import com.lanyuanxiaoyao.leopard.core.entity.BalanceSheet;
import com.lanyuanxiaoyao.leopard.core.entity.BalanceSheet_;
import com.lanyuanxiaoyao.leopard.core.entity.CashFlow;
import com.lanyuanxiaoyao.leopard.core.entity.CashFlow_;
import com.lanyuanxiaoyao.leopard.core.entity.Income;
import com.lanyuanxiaoyao.leopard.core.entity.Income_;
import com.lanyuanxiaoyao.leopard.core.entity.QBalanceSheet;
import com.lanyuanxiaoyao.leopard.core.entity.QCashFlow;
import com.lanyuanxiaoyao.leopard.core.entity.QIncome;
@@ -12,8 +15,11 @@ import com.lanyuanxiaoyao.leopard.core.repository.CashFlowRepository;
import com.lanyuanxiaoyao.leopard.core.repository.IncomeRepository;
import com.lanyuanxiaoyao.leopard.core.repository.StockRepository;
import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
/**
@@ -56,4 +62,31 @@ public class StockService extends SimpleServiceSupport<Stock> {
.and(QCashFlow.cashFlow.stock.id.eq(stockId))
);
}
public List<BalanceSheet> findBalanceSheetRecent(Long stockId, int years) {
var current = LocalDate.now();
return balanceSheetRepository.findAll(
QBalanceSheet.balanceSheet.stock.id.eq(stockId)
.and(QBalanceSheet.balanceSheet.year.between(current.minusYears(years).getYear(), current.getYear())),
Sort.by(Sort.Direction.ASC, BalanceSheet_.YEAR)
);
}
public List<Income> findIncomeRecent(Long stockId, int years) {
var current = LocalDate.now();
return incomeRepository.findAll(
QIncome.income.stock.id.eq(stockId)
.and(QIncome.income.year.between(current.minusYears(years).getYear(), current.getYear())),
Sort.by(Sort.Direction.ASC, Income_.YEAR)
);
}
public List<CashFlow> findCashFlowRecent(Long stockId, int years) {
var current = LocalDate.now();
return cashFlowRepository.findAll(
QCashFlow.cashFlow.stock.id.eq(stockId)
.and(QCashFlow.cashFlow.year.between(current.minusYears(years).getYear(), current.getYear())),
Sort.by(Sort.Direction.ASC, CashFlow_.YEAR)
);
}
}