这里假设前台发来三个查询条件,分别为姓名、日期、结果,让后台根据查询条件返回符合要求的结果。条件可能为空,选出满足所有非空条件的对象。若条件全为空,则返回所有对象。
这里用一个list表明所有对象存在的列表,targetlist为返回对象所在的列表。 因为条件可能为空,不能简单地比较每个对象的对应值与查询条件是否相等,且要求符合所有非空条件,比较容易想到的是对三个查询条件进行排列组合再筛选,但这样代码量大也不易读懂。
可以套一个三重循环来解决。
String targetName = (String)params.get("name") == null ? "" : (String)params.get("name");
String targetSignResult = (String)params.get("result") == null ? "" : (String)params.get("result");
Date targetSignTime = (Date)params.get("date");
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
List<TrainSignTotalDTO> targetList = new ArrayList<>();
for(TrainSignTotalDTO eachTotal : list){
String eachName = eachTotal.getStaffName();
Date eachSignTime = eachTotal.getRecentSignDate();
String eachResult = (fmt.format(eachSignTime).equals(fmt.format(nowTime))?"是":"否");
if (targetName.equals("") || targetName.equals(eachName)){
if (targetSignResult.equals("") || targetSignResult.equals(eachResult)){
if (targetSignTime == null || targetSignTime.equals(eachSignTime)){
targetList.add(eachTotal);
}
}
}
}
return new targetList;