zouxuan il y a 3 mois
Parent
commit
91f34e0d19

+ 34 - 5
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/impl/SysMusicCompareRecordServiceImpl.java

@@ -3,7 +3,6 @@ package com.yonge.cooleshow.biz.dal.service.impl;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.google.common.collect.Lists;
-import com.yonge.cooleshow.auth.api.entity.SysUser;
 import com.yonge.cooleshow.biz.dal.dao.MusicSheetDao;
 import com.yonge.cooleshow.biz.dal.dao.SysMusicCompareRecordDao;
 import com.yonge.cooleshow.biz.dal.dto.*;
@@ -28,7 +27,6 @@ import org.apache.commons.lang3.StringUtils;
 import org.joda.time.DateTime;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -547,26 +545,57 @@ public class SysMusicCompareRecordServiceImpl extends BaseServiceImpl<Long, SysM
 		//获取学员练习时长
 		//获取分组条件,如果所选时间段只有一天,则按小时分组,超过一个月按天分组,超过一年按月分组
 		String groupBy;
+		List<String> dateList;
+		Date startDate = DateUtil.strToDate(summarySearch.getStartTime(), DateUtil.DEFAULT_PATTERN);
 		if(StringUtils.equals(summarySearch.getStartTime(),summarySearch.getEndTime())){
 			groupBy = "%Y-%m-%d %H";
+			//获取当天的小时段
+			dateList = DateUtil.getHourList(startDate);
 		}else {
-			Date startDate = DateUtil.strToDate(summarySearch.getStartTime(), DateUtil.DEFAULT_PATTERN);
 			Date endDate = DateUtil.strToDate(summarySearch.getEndTime(), DateUtil.DEFAULT_PATTERN);
 			if(DateUtil.daysBetween(startDate,endDate) <= 31){
 				groupBy = "%Y-%m-%d";
+				dateList = DateUtil.getDayList(startDate,endDate);
 			}else if(DateUtil.monthsBetween(startDate,endDate) <= 12){
 				groupBy = "%Y-%m";
+				dateList = DateUtil.getMonthList(startDate,endDate);
 			}else {
 				groupBy = "%Y";
+				dateList = DateUtil.getYearList(startDate,endDate);
 			}
 		}
-		teacherPracticeHome.setPracticeTimes(sysMusicCompareRecordDao.getStudentTrainTime(studentIds,summarySearch,groupBy));
+		List<TeacherIndexWrapper.PracticeTimeDto> studentTrainTime = sysMusicCompareRecordDao.getStudentTrainTime(studentIds, summarySearch, groupBy);
+		//补全时间段
+		fillData(studentTrainTime,dateList);
+		teacherPracticeHome.setPracticeTimes(studentTrainTime);
 		//练习人数
-		teacherPracticeHome.setPracticeCounts(sysMusicCompareRecordDao.getStudentTrainCount(studentIds,summarySearch,groupBy));
+		List<TeacherIndexWrapper.PracticeTimeDto> studentTrainCount = sysMusicCompareRecordDao.getStudentTrainCount(studentIds, summarySearch, groupBy);
+		//补全时间段
+		fillData(studentTrainCount,dateList);
+		teacherPracticeHome.setPracticeCounts(studentTrainCount);
 
         return teacherPracticeHome;
     }
 
+	//数据补全
+	private void fillData(List<TeacherIndexWrapper.PracticeTimeDto> studentTrainTime, List<String> dateList) {
+		if(CollectionUtils.isEmpty(studentTrainTime)){
+			return;
+		}
+		for (String date : dateList) {
+			boolean flag = false;
+			for (TeacherIndexWrapper.PracticeTimeDto practiceTimeDto : studentTrainTime) {
+				if(StringUtils.equals(date,practiceTimeDto.getDate())){
+					flag = true;
+					break;
+				}
+			}
+			if(!flag){
+				studentTrainTime.add(new TeacherIndexWrapper.PracticeTimeDto(date,0L));
+			}
+		}
+	}
+
 	@Override
 	public List<TeacherIndexWrapper.StudentPracticeSummaryDto> getTeacherHomeStudent(TeacherIndexWrapper.StudentSearch studentSearch) {
 		//获取老师关联的学员

+ 49 - 3
toolset/utils/src/main/java/com/yonge/toolset/utils/date/DateUtil.java

@@ -1273,6 +1273,50 @@ public class DateUtil {
 		return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
 	}
 
+	public static List<String> getHourList(Date startDate) {
+		List<String> hourList = new ArrayList<>();
+		Calendar calendar = Calendar.getInstance();
+		calendar.setTime(startDate);
+		for (int i = 0; i < 24; i++) {
+			hourList.add(DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH"));
+			calendar.add(Calendar.HOUR, 1);
+		}
+		return hourList;
+	}
+
+	public static List<String> getDayList(Date startDate, Date endDate) {
+		List<String> dayList = new ArrayList<>();
+		Calendar calendar = Calendar.getInstance();
+		calendar.setTime(startDate);
+		while (!calendar.getTime().after(endDate)) {
+			dayList.add(DateUtil.format(calendar.getTime(), "yyyy-MM-dd"));
+			calendar.add(Calendar.DAY_OF_MONTH, 1);
+		}
+		return dayList;
+	}
+
+	public static List<String> getMonthList(Date startDate, Date endDate) {
+		List<String> monthList = new ArrayList<>();
+		Calendar calendar = Calendar.getInstance();
+		calendar.setTime(startDate);
+		while (!calendar.getTime().after(endDate)) {
+			monthList.add(DateUtil.format(calendar.getTime(), "yyyy-MM"));
+			calendar.add(Calendar.MONTH, 1);
+		}
+		return monthList;
+	}
+
+	public static List<String> getYearList(Date startDate, Date endDate) {
+		List<String> yearList = new ArrayList<>();
+		Calendar calendar = Calendar.getInstance();
+		calendar.setTime(startDate);
+		while (!calendar.getTime().after(endDate)) {
+			yearList.add(DateUtil.format(calendar.getTime(), "yyyy"));
+			calendar.add(Calendar.YEAR, 1);
+		}
+		return yearList;
+	}
+
 	/**
 	 * @describe 时间区段辅助类
 	 * @author Joburgess
@@ -1520,9 +1564,11 @@ public class DateUtil {
 	}
 
 	public static void main(String[] args) throws ParseException {
-        Date from = Date.from(LocalDateTime.of(2024, 7, 27, 23, 59, 59).atZone(ZoneId.systemDefault()).toInstant());
-        Date start = Date.from(LocalDateTime.of(2024, 7, 26, 0, 0, 0).atZone(ZoneId.systemDefault()).toInstant());
-        System.out.println(daysBetween(start, from));
+		List<String> hourList = getMonthList(DateUtil.stringToDate("2024-08-01", DateUtil.DEFAULT_PATTERN),
+				DateUtil.stringToDate("2025-01-28", DateUtil.DEFAULT_PATTERN));
+//        Date from = Date.from(LocalDateTime.of(2024, 7, 27, 23, 59, 59).atZone(ZoneId.systemDefault()).toInstant());
+//        Date start = Date.from(LocalDateTime.of(2024, 7, 26, 0, 0, 0).atZone(ZoneId.systemDefault()).toInstant());
+//        System.out.println(daysBetween(start, from));
 		// DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 //		DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 ////		 System.out.println(daysBetween(df.parse("2017-07-20 10:07:42"), df.parse(df.format(new Date()))));