fix: 查询性能优化。

This commit is contained in:
tianyongbao
2025-11-28 12:18:17 +08:00
parent ff9cc6f473
commit 26575b3060

View File

@@ -2137,136 +2137,119 @@ public class StatisticAnalysisImpl implements IStatisticAnalysisService {
List<AccountsDealRecordVo> queryList =new ArrayList<>(); List<AccountsDealRecordVo> queryList =new ArrayList<>();
if("1".equals(analysisDto.getType())){ if("1".equals(analysisDto.getType())){
//日列表 //日列表 - 优化版本使用Map避免嵌套循环预先格式化日期
List <String> staticsTimeList=new ArrayList<>(); Map<String, Double> amountMap = new LinkedHashMap<>();
for (AccountsDealRecordVo vo:accountsDealRecordVoList Map<String, StringBuilder> detailsMap = new LinkedHashMap<>();
) {
if (null != vo.getDealCategory()) { for (AccountsDealRecordVo vo:accountsDealRecordVoList) {
// 预先格式化日期,避免重复格式化
String dayString=dateFormat.format(vo.getCreateTime());
String timeString=dateFormatSecond.format(vo.getCreateTime());
// 获取字典标签(仅在需要时)
if (null != vo.getDealCategory() && !analysisDto.getDataType().equals("5")) {
vo.setDealCategoryName(DictUtils.getDictLabel("deal_category", vo.getDealCategory().toString())); vo.setDealCategoryName(DictUtils.getDictLabel("deal_category", vo.getDealCategory().toString()));
} }
String dayString=dateFormat.format(vo.getCreateTime());
// 初始化Map
if(!staticsTimeList.contains(dayString)){ amountMap.putIfAbsent(dayString, 0.0);
staticsTimeList.add(dayString); detailsMap.putIfAbsent(dayString, new StringBuilder());
String remark="";
if(vo.getRemark()!=null&&StringUtils.isNotEmpty(vo.getRemark())){
remark=",备注:"+vo.getRemark();
} }
}
if(vo.getDealType().equals("1")){
for (String staticsTime :staticsTimeList if(analysisDto.getDataType().equals("5")){
) { if(vo.getDealCategory().equals("2")){
double actualCreditBillDay=0; amountMap.put(dayString, amountMap.get(dayString) + vo.getAmount());
String details=""; detailsMap.get(dayString).append(vo.getAccountName()).append("").append(timeString)
for (AccountsDealRecordVo vo:accountsDealRecordVoList .append("交易盈利").append(vo.getAmount()).append(remark).append("<br/>");
) { }
String dayString=dateFormat.format(vo.getCreateTime()); } else {
amountMap.put(dayString, amountMap.get(dayString) + vo.getAmount());
if(staticsTime.equals(dayString)){ detailsMap.get(dayString).append(vo.getAccountName()).append("").append(timeString)
String remark=""; .append("因交易类别【").append(vo.getDealCategoryName()).append("】收入")
if(vo.getRemark()!=null&&StringUtils.isNotEmpty(vo.getRemark())){ .append(vo.getAmount()).append(remark).append("<br/>");
remark+=",备注:"+vo.getRemark(); }
}
if(vo.getDealType().equals("2")){
if(analysisDto.getDataType().equals("5")){
if(vo.getDealCategory().equals("2")){
amountMap.put(dayString, amountMap.get(dayString) - vo.getAmount());
detailsMap.get(dayString).append(vo.getAccountName()).append("").append(timeString)
.append("交易亏损").append(vo.getAmount()).append(remark).append("<br/>");
} }
if(vo.getDealType().equals("1")){ } else {
if(analysisDto.getDataType().equals("5")){ amountMap.put(dayString, amountMap.get(dayString) - vo.getAmount());
if(vo.getDealCategory().equals("2")){ detailsMap.get(dayString).append(vo.getAccountName()).append("").append(timeString)
actualCreditBillDay+=vo.getAmount(); .append("因交易类别【").append(vo.getDealCategoryName()).append("】支出")
details+=vo.getAccountName()+""+dateFormatSecond.format(vo.getCreateTime())+"交易盈利"+vo.getAmount()+remark+"<br/>"; .append(vo.getAmount()).append(remark).append("<br/>");
}
} else {
actualCreditBillDay+=vo.getAmount();
details+=vo.getAccountName()+""+dateFormatSecond.format(vo.getCreateTime())+"因交易类别【"+vo.getDealCategoryName()+"】收入"+vo.getAmount()+remark+"<br/>";
}
}
if(vo.getDealType().equals("2")){
if(analysisDto.getDataType().equals("5")){
if(vo.getDealCategory().equals("2")){
actualCreditBillDay-=vo.getAmount();
details+=vo.getAccountName()+""+dateFormatSecond.format(vo.getCreateTime())+"交易亏损"+vo.getAmount()+remark+"<br/>";
}
} else {
actualCreditBillDay-=vo.getAmount();
details+=vo.getAccountName()+""+dateFormatSecond.format(vo.getCreateTime())+"因交易类别【"+vo.getDealCategoryName()+"】支出"+vo.getAmount()+remark+"<br/>";
}
}
} }
} }
}
// 转换为结果列表
for (Map.Entry<String, Double> entry : amountMap.entrySet()) {
AccountsDealRecordVo analysisVo=new AccountsDealRecordVo(); AccountsDealRecordVo analysisVo=new AccountsDealRecordVo();
analysisVo.setAmount(actualCreditBillDay); analysisVo.setAmount(entry.getValue());
analysisVo.setRemark(staticsTime); analysisVo.setRemark(entry.getKey());
analysisVo.setName(details); analysisVo.setName(detailsMap.get(entry.getKey()).toString());
queryList.add(analysisVo); queryList.add(analysisVo);
} }
//年查询
} }
else if("2".equals(analysisDto.getType())){ else if("2".equals(analysisDto.getType())){
//月列表 //月列表 - 优化版本使用Map避免嵌套循环
List <String> staticsTimeList=new ArrayList<>(); Map<String, Double> monthAmountMap = new LinkedHashMap<>();
for (AccountsDealRecordVo vo:accountsDealRecordVoList
) { for (AccountsDealRecordVo vo:accountsDealRecordVoList) {
// 预先格式化并截取月份,避免重复操作
String monthString=dateFormat.format(vo.getCreateTime()).substring(0,7); String monthString=dateFormat.format(vo.getCreateTime()).substring(0,7);
if(!staticsTimeList.contains(monthString)){ monthAmountMap.putIfAbsent(monthString, 0.0);
staticsTimeList.add(monthString);
if(vo.getDealType().equals("1")){
monthAmountMap.put(monthString, monthAmountMap.get(monthString) + vo.getAmount());
}
if(vo.getDealType().equals("2")){
monthAmountMap.put(monthString, monthAmountMap.get(monthString) - vo.getAmount());
} }
} }
for (String staticsTime :staticsTimeList // 转换为结果列表
) { for (Map.Entry<String, Double> entry : monthAmountMap.entrySet()) {
double actualCreditBillMonth=0;
for (AccountsDealRecordVo vo:accountsDealRecordVoList
) {
String monthString=dateFormat.format(vo.getCreateTime()).substring(0,7);
if(staticsTime.equals(monthString)){
if(vo.getDealType().equals("1")){
actualCreditBillMonth+=vo.getAmount();
}
if(vo.getDealType().equals("2")){
actualCreditBillMonth-=vo.getAmount();
}
}
}
AccountsDealRecordVo analysisVo=new AccountsDealRecordVo(); AccountsDealRecordVo analysisVo=new AccountsDealRecordVo();
analysisVo.setAmount(actualCreditBillMonth); analysisVo.setAmount(entry.getValue());
analysisVo.setRemark(staticsTime); analysisVo.setRemark(entry.getKey());
queryList.add(analysisVo); queryList.add(analysisVo);
} }
//年查询
}else if("3".equals(analysisDto.getType())){ }else if("3".equals(analysisDto.getType())){
List <String> staticsTimeList=new ArrayList<>(); //年列表 - 优化版本使用Map避免嵌套循环
for (AccountsDealRecordVo vo:accountsDealRecordVoList Map<String, Double> yearAmountMap = new LinkedHashMap<>();
) {
for (AccountsDealRecordVo vo:accountsDealRecordVoList) {
// 预先格式化并截取年份,避免重复操作
String yearString=dateFormat.format(vo.getCreateTime()).substring(0,4); String yearString=dateFormat.format(vo.getCreateTime()).substring(0,4);
if(!staticsTimeList.contains(yearString)){ yearAmountMap.putIfAbsent(yearString, 0.0);
staticsTimeList.add(yearString);
if(vo.getDealType().equals("1")){
yearAmountMap.put(yearString, yearAmountMap.get(yearString) + vo.getAmount());
}
if(vo.getDealType().equals("2")){
yearAmountMap.put(yearString, yearAmountMap.get(yearString) - vo.getAmount());
} }
} }
for (String staticsTime :staticsTimeList // 转换为结果列表
) { for (Map.Entry<String, Double> entry : yearAmountMap.entrySet()) {
double actualCreditBillYear=0;
for (AccountsDealRecordVo vo:accountsDealRecordVoList
) {
String yearString=dateFormat.format(vo.getCreateTime()).substring(0,4);
if(staticsTime.equals(yearString)){
if(vo.getDealType().equals("1")){
actualCreditBillYear+=vo.getAmount();
}
if(vo.getDealType().equals("2")){
actualCreditBillYear-=vo.getAmount();
}
}
}
AccountsDealRecordVo analysisVo=new AccountsDealRecordVo(); AccountsDealRecordVo analysisVo=new AccountsDealRecordVo();
analysisVo.setAmount(actualCreditBillYear); analysisVo.setAmount(entry.getValue());
analysisVo.setRemark(staticsTime); analysisVo.setRemark(entry.getKey());
queryList.add(analysisVo); queryList.add(analysisVo);
} }
} }
@@ -2639,58 +2622,36 @@ public class StatisticAnalysisImpl implements IStatisticAnalysisService {
//年查询 //年查询
} }
else if("2".equals(analysisDto.getType())){ else if("2".equals(analysisDto.getType())){
//月列表 //月列表 - 优化版本使用Map避免嵌套循环
List <String> staticsTimeList=new ArrayList<>(); Map<String, Double> monthAmountMap = new LinkedHashMap<>();
for (AccountsDealRecordVo vo:accountsDateList
) { for (AccountsDealRecordVo vo:accountsDateList) {
String monthString=vo.getDateStr().substring(0,7); String monthString=vo.getDateStr().substring(0,7);
if(!staticsTimeList.contains(monthString)){ monthAmountMap.put(monthString, monthAmountMap.getOrDefault(monthString, 0.0) + vo.getAmount());
staticsTimeList.add(monthString);
}
} }
for (String staticsTime :staticsTimeList
) { for (Map.Entry<String, Double> entry : monthAmountMap.entrySet()) {
double actualCreditBillMonth=0;
for (AccountsDealRecordVo vo:accountsDateList
) {
String monthString=vo.getDateStr().substring(0,7);
if(staticsTime.equals(monthString)){
actualCreditBillMonth+=vo.getAmount();
}
}
AccountsDealRecordVo analysisVo=new AccountsDealRecordVo(); AccountsDealRecordVo analysisVo=new AccountsDealRecordVo();
analysisVo.setAmount(actualCreditBillMonth); analysisVo.setAmount(entry.getValue());
analysisVo.setRemark(staticsTime); analysisVo.setRemark(entry.getKey());
queryList.add(analysisVo); queryList.add(analysisVo);
} }
//年查询 //年查询
}else if("3".equals(analysisDto.getType())){ }else if("3".equals(analysisDto.getType())){
List <String> staticsTimeList=new ArrayList<>(); //年列表 - 优化版本使用Map避免嵌套循环
for (AccountsDealRecordVo vo:accountsDateList Map<String, Double> yearAmountMap = new LinkedHashMap<>();
) {
for (AccountsDealRecordVo vo:accountsDateList) {
String yearString=vo.getDateStr().substring(0,4); String yearString=vo.getDateStr().substring(0,4);
yearAmountMap.put(yearString, yearAmountMap.getOrDefault(yearString, 0.0) + vo.getAmount());
if(!staticsTimeList.contains(yearString)){
staticsTimeList.add(yearString);
}
} }
for (String staticsTime :staticsTimeList for (Map.Entry<String, Double> entry : yearAmountMap.entrySet()) {
) {
double actualCreditBillYear=0;
for (AccountsDealRecordVo vo:accountsDateList
) {
String yearString=vo.getDateStr().substring(0,4);
if(staticsTime.equals(yearString)){
actualCreditBillYear+=vo.getAmount();
}
}
AccountsDealRecordVo analysisVo=new AccountsDealRecordVo(); AccountsDealRecordVo analysisVo=new AccountsDealRecordVo();
analysisVo.setAmount(actualCreditBillYear); analysisVo.setAmount(entry.getValue());
analysisVo.setRemark(staticsTime); analysisVo.setRemark(entry.getKey());
queryList.add(analysisVo); queryList.add(analysisVo);
} }
} }
@@ -2905,45 +2866,34 @@ public class StatisticAnalysisImpl implements IStatisticAnalysisService {
dealDto.setType("5"); dealDto.setType("5");
dealDto.setDealCategory("2"); dealDto.setDealCategory("2");
List<AccountsDealRecordVo> accountsDealRecordVoList=accountsDealRecordMapper.selectAccountsDealRecordList(dealDto); List<AccountsDealRecordVo> accountsDealRecordVoList=accountsDealRecordMapper.selectAccountsDealRecordList(dealDto);
//日列表 //日列表 - 优化版本使用Map避免嵌套循环
List <String> staticsTimeList=new ArrayList<>(); Map<String, Double> investAmountMap = new LinkedHashMap<>();
for (AccountsDealRecordVo vo:accountsDealRecordVoList
) { for (AccountsDealRecordVo vo:accountsDealRecordVoList) {
String dayString=sdf.format(vo.getCreateTime()); String dayString=sdf.format(vo.getCreateTime());
if(!staticsTimeList.contains(dayString)){ investAmountMap.putIfAbsent(dayString, 0.0);
staticsTimeList.add(dayString);
if(vo.getDealType().equals("1")){
if(vo.getDealCategory().equals("2")){
investAmountMap.put(dayString, investAmountMap.get(dayString) + vo.getAmount());
}
}
if(vo.getDealType().equals("2")){
if(vo.getDealCategory().equals("2")){
investAmountMap.put(dayString, investAmountMap.get(dayString) - vo.getAmount());
}
} }
} }
for (String staticsTime :staticsTimeList
) { for (Map.Entry<String, Double> entry : investAmountMap.entrySet()) {
double investAmount=0; AccountCalendarVo billDate = new AccountCalendarVo();
for (AccountsDealRecordVo vo:accountsDealRecordVoList billDate.setStart(entry.getKey());
) { billDate.setTitle("投资收益:"+decimalFormat.format(entry.getValue()));
String dayString=sdf.format(vo.getCreateTime()); billDate.setType(6);
billDate.setColor("purple");
if(staticsTime.equals(dayString)){ list.add(billDate);
if(vo.getDealType().equals("1")){ }
if(vo.getDealCategory().equals("2")){
investAmount+=vo.getAmount();
}
}
if(vo.getDealType().equals("2")){
if(vo.getDealCategory().equals("2")){
investAmount-=vo.getAmount();
}
}
}
}
AccountCalendarVo billDate = new AccountCalendarVo();
billDate.setStart(staticsTime);
billDate.setTitle("投资收益:"+decimalFormat.format(investAmount));
billDate.setType(6);
billDate.setColor("purple");
list.add(billDate);
}
AccountsTransferRecordDto posDto=new AccountsTransferRecordDto(); AccountsTransferRecordDto posDto=new AccountsTransferRecordDto();
//获取数据 //获取数据
@@ -2951,32 +2901,18 @@ public class StatisticAnalysisImpl implements IStatisticAnalysisService {
posDto.setStartTime(analysisDto.getStartTime()); posDto.setStartTime(analysisDto.getStartTime());
posDto.setType("1"); posDto.setType("1");
List<AccountsTransferRecordVo> transferList=accountsTransferRecordMapper.selectAccountsTransferRecordList(posDto); List<AccountsTransferRecordVo> transferList=accountsTransferRecordMapper.selectAccountsTransferRecordList(posDto);
//日列表 //日列表 - 优化版本使用Map避免嵌套循环
List <String> posTimeList=new ArrayList<>(); Map<String, Double> posAmountMap = new LinkedHashMap<>();
for (AccountsTransferRecordVo vo:transferList
) { for (AccountsTransferRecordVo vo:transferList) {
String dayString=sdf.format(vo.getCreateTime()); String dayString=sdf.format(vo.getCreateTime());
posAmountMap.put(dayString, posAmountMap.getOrDefault(dayString, 0.0) + vo.getAmount());
if(!posTimeList.contains(dayString)){
posTimeList.add(dayString);
}
} }
for (String staticsTime :posTimeList for (Map.Entry<String, Double> entry : posAmountMap.entrySet()) {
) {
double posAmount=0;
for (AccountsTransferRecordVo vo:transferList
) {
String dayString=sdf.format(vo.getCreateTime());
if(staticsTime.equals(dayString)){
posAmount+=vo.getAmount();
}
}
AccountCalendarVo billDate = new AccountCalendarVo(); AccountCalendarVo billDate = new AccountCalendarVo();
billDate.setStart(staticsTime); billDate.setStart(entry.getKey());
billDate.setTitle("POS机刷卡:"+decimalFormat.format(posAmount)); billDate.setTitle("POS机刷卡:"+decimalFormat.format(entry.getValue()));
billDate.setType(7); billDate.setType(7);
list.add(billDate); list.add(billDate);
} }