实现功能截图
1.前台传参
2.
/*
* createTime 前台传字符串
* @param indexNoticeDto
* @return
*/
@Override
public List<IndexNoticeDto> selectIndexNotice(IndexNoticeDto indexNoticeDto) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<IndexNoticeDto> indexNoticeDtoList = new ArrayList<>();
//不传月份日期表示当月,传日期表示之前月份
String month = indexNoticeDto.getStrMonth();
try {
month = indexNoticeDto.getStrMonth();
} catch (Exception e) {
e.printStackTrace();
}
if (month == null) {
indexNoticeDtoList.addAll(publicSqlMapper.selectIndexNotice(indexNoticeDto));
List<IndexNoticeDto> dayList = getMonthEveryDays(indexNoticeDtoList, null);
return dayList;
} else {
indexNoticeDto.setStrStartTime(month.substring(0, month.length() - 3) + "-01");
indexNoticeDto.setDtStartTime(sdf.parse(indexNoticeDto.getStrStartTime()));
int year = Integer.parseInt(month.substring(0, 4));
int month1 = Integer.parseInt(month.substring(5, 7));
int max = LocalDate.of(year, month1, 1).lengthOfMonth();
//月份最大天数
indexNoticeDto.setStrEndTime(month.substring(0, month.length() - 3) + "-" + String.valueOf(max));
indexNoticeDto.setDtEndTime(sdf.parse(indexNoticeDto.getStrEndTime()));
indexNoticeDto.setStrStartTime("'" + month.substring(0, month.length() - 3) + "-01" + "'");
indexNoticeDto.setStrEndTime("'" + month.substring(0, month.length() - 3) + "-" + String.valueOf(max) + "'");
List<IndexNoticeDto> indexNoticeDtoList1 = publicSqlMapper.selectIndexNoticeHis(indexNoticeDto);
List<IndexNoticeDto> dayList = getMonthEveryDays(indexNoticeDtoList1, month.substring(0, month.length() - 3));
return dayList;
}
}
上月月末+当月全月+下月月初数据
/**- 上月月末+当月全月+下月月初数据
- @param date
@return
*/
public List getMonthEveryDays(List list, String month) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");int year = Integer.parseInt(month.substring(0, 4));
int month1 = Integer.parseInt(month.substring(5, 7));
//指定当前月份最大天数
int max = LocalDate.of(year, month1, 1).lengthOfMonth();//当前月初开始日期属于周几
int nowStartWeek = getWeekday(month + "-01");
//当前月末结束日期属于周几
int nowEndWeek = getWeekday(month + "-" + max);//上月年
int lastYear = 0;
//上月月份
int lastMonth = Integer.parseInt(month.substring(5, 7)) - 1;
String strLastMonth = "";
if (lastMonth == 1) {lastMonth = 12; lastYear = Integer.parseInt(month.substring(0, 4)) - 1;
} else {
lastYear = year;
}
//上月最大天数
int lastMonthMaxDay = LocalDate.of(lastYear, lastMonth, 1).lengthOfMonth();
//拼接上月月末数据
List lastIndexNoticeDtoList = new ArrayList<>();
int sort = 0;
//如果当前月份开始日期是周天,则不拼上月天数,我是根据前台的日期组件,一周开始属于周天拼接的
if (nowStartWeek != 7) {for (int i = 0; i < nowStartWeek - 1; i++) { int lastDay = lastMonthMaxDay - i; IndexNoticeDto indexNoticeDto = new IndexNoticeDto(); if (lastMonth < 10) { indexNoticeDto.setCreateTime(lastYear + "-" + "0" + lastMonth + "-" + lastDay); } else { indexNoticeDto.setCreateTime(lastYear + "-" + lastMonth + "-" + lastDay); } indexNoticeDto.setDtCreateTime(sdf.parse(lastYear + "-" + lastMonth + "-" + lastDay)); if (nowStartWeek == 1) { indexNoticeDto.setWeek(7); } else { indexNoticeDto.setWeek(nowStartWeek - (1 + i)); } indexNoticeDto.setDay(lastDay); indexNoticeDto.setTotal(0);//拼给前台的默认值,没有任何意义 sort = i; indexNoticeDto.setSort(sort); lastIndexNoticeDtoList.add(indexNoticeDto); }
}
//本月数据
List indexNoticeDtoList = new ArrayList<>();
//上月数据
List collect1 = lastIndexNoticeDtoList.stream().sorted(Comparator.comparing(IndexNoticeDto::getSort).reversed()).collect(Collectors.toList());
indexNoticeDtoList.addAll(collect1);
Calendar calendar = Calendar.getInstance();for (int i = 0; i < max; i++) {
IndexNoticeDto indexNoticeDto = new IndexNoticeDto(); if (month == null) { calendar.set(Calendar.DAY_OF_MONTH, i + 1); } String day = sdf.format(calendar.getTime()); indexNoticeDto.setDay(i + 1); if (month != null) { int ii = i + 1; if (i < 9) { day = month + "-" + "0" + ii; } else { day = month + "-" + ii; } } indexNoticeDto.setCreateTime(day); indexNoticeDto.setDtCreateTime(sdf.parse(day)); indexNoticeDto.setWeek(getWeekday(day)); String finalDay = day; List<IndexNoticeDto> collect = list.stream().filter(s -> s.getCreateTime().equals(finalDay)).collect(Collectors.toList()); if (collect.size() > 0) { indexNoticeDto.setTotal(collect.size()); } else { indexNoticeDto.setTotal(0); } indexNoticeDto.setSort(sort + 1 + i); indexNoticeDtoList.add(indexNoticeDto);
}
//下个月数据
int nextYear = 0;
int nextMonth = 0;
String strNextMonth = "";
if (month1 == 12) {nextYear = year + 1; nextMonth = 1; strNextMonth = "0" + nextMonth;
} else if (month1 != 12) {
nextYear = year; nextMonth = month1 + 1; if (nextMonth < 1 && nextMonth < 10) { strNextMonth = "0" + nextMonth; } else { strNextMonth = String.valueOf(nextMonth); }
}
//日历组件方格 一共展示42天的数据
int nextMonthDayNum = 42 - indexNoticeDtoList.size();
for (int i = 0; i < nextMonthDayNum; i++) {String nextDay = ""; if (i < 10) { nextDay = "0" + String.valueOf(i + 1); } IndexNoticeDto indexNoticeDto = new IndexNoticeDto(); indexNoticeDto.setCreateTime(nextYear + "-" + strNextMonth + "-" + nextDay); indexNoticeDto.setDtCreateTime(sdf.parse(nextYear + "-" + strNextMonth + "-" + nextDay)); indexNoticeDto.setDay(i + 1); indexNoticeDto.setWeek(getWeekday(nextYear + "-" + strNextMonth + "-" + nextDay)); indexNoticeDto.setTotal(0);//拼给前台的默认值,没有任何意义 sort = i; indexNoticeDto.setSort(sort); indexNoticeDtoList.add(indexNoticeDto);
}
return indexNoticeDtoList;
}
4.制定月份每一天数据,与数据库中的数据进行比较,有数据的时候total的值 >0
5.计算指定日期星期几
/*
* 实现给定某日期,判断是星期几
* 必须yyyy-MM-dd
* @param date
* @return
*/
public static int getWeekday(String date) {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdw = new SimpleDateFormat("E");
Date d = null;
try {
d = sd.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
String strWeek = sdw.format(d); //输出 星期一。。。。。
int intWeek = 0;
switch (strWeek) {
case "星期-":
intWeek = 1;
break;
case "星期二":
intWeek = 2;
break;
case "星期三":
intWeek = 3;
break;
case "星期四":
intWeek = 4;
break;
case "星期五":
intWeek = 5;
break;
case "星期六":
intWeek = 6;
break;
case "星期天":
intWeek = 7;
break;
default:
break;
}
return intWeek;
}
6.数据库查询
SELECT
total,
day,
CONCAT((date_format( ${strStartTime},'%yyy-')),(date_format( ${strStartTime},'%m-')), day) as create_time
FROM
(
SELECT
DAY(create_time) as day,
count(notice_id) AS total
FROM sys_notice
WHERE create_time >= ${strStartTime} and create_time <= ${strEndTime}
GROUP BY DAY(create_time)
) as a
WHERE a.day is not null
</select>
7.数据返回(一个日期组件一共展示42天的数据)