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