最近公司项目做了一个功能模块里面有个需求,是要求做一个时间筛选的操作,包括今天,昨天,本周,近七天,半月,上月,自定义这个选项,刚刚开始写的代码比较low逼,都是写在控制器controller里面,具体代码如下:
if("0".equals(searchDate)){//今天
bookingManageList =bookingManageService.getBoookingManageList(status,todayStart,todayEnd, null, keyWord, pageNum, pageSize,storeId,drawer);
totalAmount = bookingManageService.getTotalAmount(status,storeId,todayStart,todayEnd,null,keyWord,drawer);//获取总计金额
count =bookingManageService.getCount(status,todayStart,todayEnd, null, keyWord, pageNum, pageSize,storeId,drawer);
}
if("1".equals(searchDate)){//昨天
bookingManageList =bookingManageService.getBoookingManageList(status, yesterdayStart,yesterdayEnd,null,keyWord, pageNum, pageSize,storeId,drawer);
totalAmount = bookingManageService.getTotalAmount(status,storeId,yesterdayStart,yesterdayEnd,null,keyWord,drawer);//获取总计金额
count =bookingManageService.getCount(status, yesterdayStart,yesterdayEnd,null,keyWord, pageNum, pageSize,storeId,drawer);
}
if("2".equals(searchDate)){//本周
bookingManageList =bookingManageService.getBoookingManageList(status, startTimeWeek, endTimeWeek, null, keyWord, pageNum, pageSize,storeId,drawer);
totalAmount = bookingManageService.getTotalAmount(status,storeId,startTimeWeek,endTimeWeek,null,keyWord,drawer);//获取总计金额
count =bookingManageService.getCount(status, startTimeWeek, endTimeWeek, null, keyWord, pageNum, pageSize,storeId,drawer);
}
if("3".equals(searchDate)){//近7天
bookingManageList =bookingManageService.getBoookingManageList(status, DateUtil.formatDate(recentSevenDay,"yyyy-MM-dd")+" 00:00:00",yesterdayEnd, null, keyWord, pageNum, pageSize,storeId,drawer);
totalAmount = bookingManageService.getTotalAmount(status,storeId,DateUtil.formatDate(recentSevenDay,"yyyy-MM-dd")+" 00:00:00",yesterdayEnd,null,keyWord,drawer);//获取总计金额
count =bookingManageService.getCount(status, DateUtil.formatDate(recentSevenDay,"yyyy-MM-dd")+" 00:00:00",yesterdayEnd, null, keyWord, pageNum, pageSize,storeId,drawer);
}
if("4".equals(searchDate)){//本月
bookingManageList =bookingManageService.getBoookingManageList(status, startTimeMonth, endTimeMonth, null,keyWord, pageNum, pageSize,storeId,drawer);
totalAmount = bookingManageService.getTotalAmount(status,storeId,startTimeMonth,endTimeMonth,null,keyWord,drawer);//获取总计金额
count =bookingManageService.getCount(status, startTimeMonth, endTimeMonth, null,keyWord, pageNum, pageSize,storeId,drawer);
}
if("5".equals(searchDate)){//上月
bookingManageList =bookingManageService.getBoookingManageList(status,lastMonthStartTime,lastMonthEndTime, null, keyWord, pageNum, pageSize,storeId,drawer);
totalAmount = bookingManageService.getTotalAmount(status,storeId,lastMonthStartTime,lastMonthEndTime,null,keyWord,drawer);//获取总计金额
count =bookingManageService.getCount(status,lastMonthStartTime,lastMonthEndTime, null, keyWord, pageNum, pageSize,storeId,drawer);
}
if("6".equals(searchDate)){//自定义
bookingManageList =bookingManageService.getBoookingManageList(status,startDate,endDate, null, keyWord, pageNum, pageSize,storeId,drawer);
totalAmount = bookingManageService.getTotalAmount(status,storeId,startDate,endDate,null,keyWord,drawer);//获取总计金额
count =bookingManageService.getCount(status,startDate,endDate, null, keyWord, pageNum, pageSize,storeId,drawer);
}
后来优化封装了一下,立刻代码就变简洁了不少:
public Map<String,String> getDateByStr(String str,String startDate,String endDate){
Map<String,String> map = new Map<String,String>
if("3".equals(str)){
map.put("beginTime", DateUtil.getAnyDate(-7));
map.put("endTime", DateUtil.getAnyDate(0));
}else if("0".equals(str)){
map.put("beginTime", DateUtil.getAnyDate(0));
map.put("endTime", DateUtil.getAnyDate(0));
}else if("1".equals(str)){
map.put("beginTime", DateUtil.getAnyDate(-1));
map.put("endTime", DateUtil.getAnyDate(-1));
}else if("2".equals(str)){
map.put("beginTime", DateUtil.getThisWeekStartAndEnd().get("startTime"));
map.put("endTime", DateUtil.getAnyDate(0));
}else if("4".equals(str)){
map.put("beginTime", DateUtil.getThisMonthStartAndEndDay().get("startTime"));
map.put("endTime", DateUtil.getThisMonthStartAndEndDay().get("endTime"));
}else if("5".equals(str)){
map.put("beginTime", DateUtil.getLastWeekStartAndEnd().get("startTime"));
map.put("endTime", DateUtil.getLastWeekStartAndEnd().get("endTime"));
}else{
map.put("beginTime", startDate);
map.put("endTime", endTime);
}
return map;
}
}
总结:在真正开发过程之中,可能写的比较赶就没有仔细思考怎么写比较好,做事情还是不能急,要先构思好再写,没有想清楚坚决不要动手开始做,做也是白做,做开发,不是在工地上面搬砖,瞎搞就行,一个好的思路,往往就成功了一半,每个做开发的都要有这个觉悟