代码:
public List<AreaToGrossVo> manage(Integer year, Integer month) {
//找出特性为7的出水表
List<MeterToNetworkVo> meterAndNetwork = commonDAO.getMeterAndNetwork();
//获取所有的区域
List<Integer> areaIds = meterAndNetwork.stream().map(MeterToNetworkVo::getArea_ID).distinct().collect(Collectors.toList());
List<AreaToGrossVo> areaToGrossVos = new ArrayList<>();
DecimalFormat df = new DecimalFormat("#0.00");
for (Integer areaId : areaIds) {
// AreaToGrossVo areaToGross = getAreaUseStyleData(areaId, year, month);
//获取教学类型的水表列表
List<Integer> teachMeters = meterAndNetwork.stream().filter(x -> "1".equals(x.getTexingValue()) && x.getArea_ID().equals(areaId)).map(MeterToNetworkVo::getMeter_ID).collect(Collectors.toList());
//获取公共类型的水表列表
List<Integer> commonMeters = meterAndNetwork.stream().filter(x -> "2".equals(x.getTexingValue()) && x.getArea_ID().equals(areaId)).map(MeterToNetworkVo::getMeter_ID).collect(Collectors.toList());
//获取生活类型的水表列表
List<Integer> lifeMeters = meterAndNetwork.stream().filter(x -> "2".equals(x.getTexingValue()) && x.getArea_ID().equals(areaId)).map(MeterToNetworkVo::getMeter_ID).collect(Collectors.toList());
//获取商业类型的水表列表
List<Integer> businessMeters = meterAndNetwork.stream().filter(x -> "4".equals(x.getTexingValue()) && x.getArea_ID().equals(areaId)).map(MeterToNetworkVo::getMeter_ID).collect(Collectors.toList());
AreaToGrossVo areaToGross = new AreaToGrossVo();
areaToGross.setAreaID(areaId);
// LambdaQueryWrapper<Area> areaLambdaQueryWrapper = new LambdaQueryWrapper<Area>().eq(Area::getAreaID,areaId);
Area area = areaDAO.selectById(areaId);
// Area area = areaDAO.selectOne(areaLambdaQueryWrapper);
areaToGross.setAreaName(area.getAreaName());
if (teachMeters.isEmpty()) {
areaToGross.setTeachValue("0.0");
} else {
Double allTeachMeterSum = commonDAO.getGrossByArea(year, month, teachMeters);
areaToGross.setTeachValue(df.format(allTeachMeterSum));
}
if (commonMeters.isEmpty()) {
areaToGross.setCommonValue("0.0");
} else {
Double allCommonMeterSum = commonDAO.getGrossByArea(year, month, teachMeters);
areaToGross.setCommonValue(df.format(allCommonMeterSum));
}
if (lifeMeters.isEmpty()) {
areaToGross.setLifeValue("0.0");
} else {
Double allLifeMeterSum = commonDAO.getGrossByArea(year, month, teachMeters);
areaToGross.setLifeValue(df.format(allLifeMeterSum));
}
if (businessMeters.isEmpty()) {
areaToGross.setBusinessValue("0.0");
} else {
Double allBusinessMeterSum = commonDAO.getGrossByArea(year, month, teachMeters);
areaToGross.setBusinessValue(df.format(allBusinessMeterSum));
}
areaToGrossVos.add(areaToGross);
}
return areaToGrossVos;
}
第二种改为方法体
public AreaToGrossVo getAreaUseStyleData(Integer areaId, Integer year, Integer month) {
//找出特性为7的出水表
List<MeterToNetworkVo> meterAndNetwork = commonDAO.getMeterAndNetwork();
DecimalFormat df = new DecimalFormat("#0.00");
//获取教学类型的水表列表
List<Integer> teachMeters = meterAndNetwork.stream().filter(x -> "1".equals(x.getTexingValue()) && x.getArea_ID().equals(areaId)).map(MeterToNetworkVo::getMeter_ID).collect(Collectors.toList());
//获取公共类型的水表列表
List<Integer> commonMeters = meterAndNetwork.stream().filter(x -> "2".equals(x.getTexingValue()) && x.getArea_ID().equals(areaId)).map(MeterToNetworkVo::getMeter_ID).collect(Collectors.toList());
//获取生活类型的水表列表
List<Integer> lifeMeters = meterAndNetwork.stream().filter(x -> "2".equals(x.getTexingValue()) && x.getArea_ID().equals(areaId)).map(MeterToNetworkVo::getMeter_ID).collect(Collectors.toList());
//获取商业类型的水表列表
List<Integer> businessMeters = meterAndNetwork.stream().filter(x -> "4".equals(x.getTexingValue()) && x.getArea_ID().equals(areaId)).map(MeterToNetworkVo::getMeter_ID).collect(Collectors.toList());
AreaToGrossVo areaToGross = new AreaToGrossVo();
areaToGross.setAreaID(areaId);
Area area = areaDAO.selectById(areaId);
areaToGross.setAreaName(area.getAreaName());
if (teachMeters.isEmpty()) {
areaToGross.setTeachValue("0.0");
} else {
Double allTeachMeterSum = commonDAO.getGrossByArea(year, month, teachMeters);
areaToGross.setTeachValue(df.format(allTeachMeterSum));
}
if (commonMeters.isEmpty()) {
areaToGross.setCommonValue("0.0");
} else {
Double allCommonMeterSum = commonDAO.getGrossByArea(year, month, teachMeters);
areaToGross.setCommonValue(df.format(allCommonMeterSum));
}
if (lifeMeters.isEmpty()) {
areaToGross.setLifeValue("0.0");
} else {
Double allLifeMeterSum = commonDAO.getGrossByArea(year, month, teachMeters);
areaToGross.setLifeValue(df.format(allLifeMeterSum));
}
if (businessMeters.isEmpty()) {
areaToGross.setBusinessValue("0.0");
} else {
Double allBusinessMeterSum = commonDAO.getGrossByArea(year, month, teachMeters);
areaToGross.setBusinessValue(df.format(allBusinessMeterSum));
}
return areaToGross;
}
/**
* 查询一段时间的用水数据(最小单位为月)
*
* @param startYear 起始时间段年
* @param startMonth 起始时间段月
* @param endYear 终止时间段年
* @param endMonth 终止时间段月
* @return
*/
public List<AreaToGrossVo> manageByRangeTime(Integer startYear, Integer startMonth, Integer endYear, Integer endMonth) {
//找出特性为7的出水表
List<MeterToNetworkVo> meterAndNetwork = commonDAO.getMeterAndNetwork();
//获取所有的区域
List<Integer> areaIds = meterAndNetwork.stream().map(MeterToNetworkVo::getArea_ID).distinct().collect(Collectors.toList());
List<AreaToGrossVo> areaToGrossVos = new ArrayList<>();
DecimalFormat df = new DecimalFormat("#0.00");
Integer startDate = startYear * 100 + startMonth;
Integer endDate = endYear * 100 + endMonth;
for (Integer areaId : areaIds) {
//获取教学类型的水表列表
List<Integer> teachMeters = meterAndNetwork.stream().filter(x -> "1".equals(x.getTexingValue()) && x.getArea_ID().equals(areaId)).map(MeterToNetworkVo::getMeter_ID).collect(Collectors.toList());
//获取公共类型的水表列表
List<Integer> commonMeters = meterAndNetwork.stream().filter(x -> "2".equals(x.getTexingValue()) && x.getArea_ID().equals(areaId)).map(MeterToNetworkVo::getMeter_ID).collect(Collectors.toList());
//获取生活类型的水表列表
List<Integer> lifeMeters = meterAndNetwork.stream().filter(x -> "2".equals(x.getTexingValue()) && x.getArea_ID().equals(areaId)).map(MeterToNetworkVo::getMeter_ID).collect(Collectors.toList());
//获取商业类型的水表列表
List<Integer> businessMeters = meterAndNetwork.stream().filter(x -> "4".equals(x.getTexingValue()) && x.getArea_ID().equals(areaId)).map(MeterToNetworkVo::getMeter_ID).collect(Collectors.toList());
AreaToGrossVo areaToGross = new AreaToGrossVo();
areaToGross.setAreaID(areaId);
// LambdaQueryWrapper<Area> areaLambdaQueryWrapper = new LambdaQueryWrapper<Area>().eq(Area::getAreaID,areaId);
Area area = areaDAO.selectById(areaId);
// Area area = areaDAO.selectOne(areaLambdaQueryWrapper);
areaToGross.setAreaName(area.getAreaName());
if (teachMeters.isEmpty()) {
areaToGross.setTeachValue("0.0");
} else {
Double allTeachMeterSum = commonDAO.getGrossByAreaAndRangeTime(startDate, endDate, teachMeters);
areaToGross.setTeachValue(df.format(allTeachMeterSum));
}
if (commonMeters.isEmpty()) {
areaToGross.setCommonValue("0.0");
} else {
Double allCommonMeterSum = commonDAO.getGrossByArea(startDate, endDate, teachMeters);
areaToGross.setCommonValue(df.format(allCommonMeterSum));
}
if (lifeMeters.isEmpty()) {
areaToGross.setLifeValue("0.0");
} else {
Double allLifeMeterSum = commonDAO.getGrossByArea(startDate, endDate, teachMeters);
areaToGross.setLifeValue(df.format(allLifeMeterSum));
}
if (businessMeters.isEmpty()) {
areaToGross.setBusinessValue("0.0");
} else {
Double allBusinessMeterSum = commonDAO.getGrossByArea(startDate, endDate, teachMeters);
areaToGross.setBusinessValue(df.format(allBusinessMeterSum));
}
areaToGrossVos.add(areaToGross);
}
return areaToGrossVos;
}
原来代码改为1行:
AreaToGrossVo areaToGross = getAreaUseStyleData(areaId, year, month);
前者执行速度:
后者执行速度:
因此我推断调用方法栈的会更耗时,因为调用一个方法要去查询方法栈耗时,但是这种做法可以减少代码的冗余量,有利有弊