@ -1,12 +1,16 @@
package cn.teammodel.controller.admin.service.impl ;
package cn.teammodel.controller.admin.service.impl ;
import cn.hutool.core.lang.Pair ;
import cn.hutool.core.lang.Pair ;
import cn.teammodel.common.ErrorCode ;
import cn.teammodel.common.PK ;
import cn.teammodel.common.PK ;
import cn.teammodel.common.PageVo ;
import cn.teammodel.config.exception.ServiceException ;
import cn.teammodel.controller.admin.service.AdminAppraiseService ;
import cn.teammodel.controller.admin.service.AdminAppraiseService ;
import cn.teammodel.dao.* ;
import cn.teammodel.dao.* ;
import cn.teammodel.model.dto.admin.TimeRangeDto ;
import cn.teammodel.model.dto.admin.TimeRangeDto ;
import cn.teammodel.model.entity.User ;
import cn.teammodel.model.entity.User ;
import cn.teammodel.model.entity.school.ClassInfo ;
import cn.teammodel.model.entity.school.ClassInfo ;
import cn.teammodel.model.entity.school.School ;
import cn.teammodel.model.entity.school.Student ;
import cn.teammodel.model.entity.school.Student ;
import cn.teammodel.model.entity.school.Teacher ;
import cn.teammodel.model.entity.school.Teacher ;
import cn.teammodel.model.vo.admin.IndexData ;
import cn.teammodel.model.vo.admin.IndexData ;
@ -15,26 +19,30 @@ import cn.teammodel.model.vo.admin.RankVo;
import cn.teammodel.model.vo.admin.StudentRankVo ;
import cn.teammodel.model.vo.admin.StudentRankVo ;
import cn.teammodel.model.vo.appraise.RecordVo ;
import cn.teammodel.model.vo.appraise.RecordVo ;
import cn.teammodel.security.utils.SecurityUtil ;
import cn.teammodel.security.utils.SecurityUtil ;
import cn.teammodel.utils.SchoolDateUtil ;
import com.azure.spring.data.cosmos.core.query.CosmosPageRequest ;
import com.azure.spring.data.cosmos.core.query.CosmosPageRequest ;
import org.apache.commons.lang3.ObjectUtils ;
import org.apache.commons.lang3.ObjectUtils ;
import org.apache.commons.lang3.StringUtils ;
import org.springframework.data.domain.Page ;
import org.springframework.data.domain.Page ;
import org.springframework.data.domain.Slice ;
import org.springframework.data.domain.Slice ;
import org.springframework.stereotype.Service ;
import org.springframework.stereotype.Service ;
import javax.annotation.Resource ;
import javax.annotation.Resource ;
import java.time. Instant ;
import java.time. LocalDate ;
import java.time.LocalDateTime ;
import java.time.LocalDateTime ;
import java.time.ZoneOffset ;
import java.time.temporal.WeekFields ;
import java.util.* ;
import java.util.* ;
import java.util.stream.Collectors ;
import java.util.stream.Collectors ;
import static cn.teammodel.utils.SchoolDateUtil.calculateWeekNum ;
/ * *
/ * *
* @author winter
* @author winter
* @create 2023 - 12 - 06 14 : 46
* @create 2023 - 12 - 06 14 : 46
* /
* /
@Service
@Service
public class AdminAppraiseServiceImpl implements AdminAppraiseService {
public class AdminAppraiseServiceImpl implements AdminAppraiseService {
@Resource
private SchoolRepository schoolRepository ;
@Resource
@Resource
private ClassRepository classRepository ;
private ClassRepository classRepository ;
@Resource
@Resource
@ -48,19 +56,32 @@ public class AdminAppraiseServiceImpl implements AdminAppraiseService {
private AppraiseRecordRepository appraiseRecordRepository ;
private AppraiseRecordRepository appraiseRecordRepository ;
@Override
@Override
public IndexData getIndexData ( String academicYearId ) {
public IndexData getIndexData ( String periodId ) {
final int SLICE_SIZE = 100 ;
if ( StringUtils . isBlank ( periodId ) ) {
throw new ServiceException ( ErrorCode . PARAMS_ERROR . getCode ( ) , "不能为空" ) ;
}
final IndexData indexData = new IndexData ( ) ;
final IndexData indexData = new IndexData ( ) ;
int totalCount = 0 ;
int totalCount = 0 ;
int criticalCount = 0 ;
int criticalCount = 0 ;
Set < String > creatorIdSet = new HashSet < > ( ) ;
Set < String > creatorIdSet = new HashSet < > ( ) ;
User loginUser = SecurityUtil . getLoginUser ( ) ;
User loginUser = SecurityUtil . getLoginUser ( ) ;
String schoolId = loginUser . getSchoolId ( ) ;
String schoolId = loginUser . getSchoolId ( ) ;
// 获取学期起止时间
List < School . Semester > semesters = schoolRepository . findSemestersById ( schoolId , periodId ) ;
SchoolDateUtil . semesterModel semesterModel = SchoolDateUtil . getSemesterByNow ( semesters , LocalDate . now ( ) ) ;
String academicYearId = semesterModel . getAcademicYearId ( ) ;
LocalDateTime startDatetime = semesterModel . getStartDatetime ( ) ;
LocalDateTime endDatetime = semesterModel . getEndDatetime ( ) ;
if ( startDatetime = = null | | endDatetime = = null ) throw new ServiceException ( ErrorCode . PARAMS_ERROR ) ;
long totalWeek = calculateWeekNum ( startDatetime , endDatetime , null ) ;
// slice 分段读取
// slice 分段读取
CosmosPageRequest pageRequest = new CosmosPageRequest ( 0 , 100 , null ) ;
CosmosPageRequest pageRequest = new CosmosPageRequest ( 0 , SLICE_SIZE , null ) ;
Slice < RecordVo > slice ;
Slice < RecordVo > slice ;
Map < Integer , Integer > countByWeek = new HashMap < > ( ) ;
Map < Long, Integer > countByWeek = SchoolDateUtil . createEmptyWeekMap ( totalWeek ) ;
do {
do {
slice = appraiseRecordRepository . findAllByAcademicYearId ( String . format ( PK . PK_APPRAISE_RECORD , schoolId ) , academicYearId , pageRequest ) ;
slice = appraiseRecordRepository . findAllByAcademicYearId ( String . format ( PK . PK_APPRAISE_RECORD , schoolId ) , academicYearId , pageRequest ) ;
@ -72,7 +93,7 @@ public class AdminAppraiseServiceImpl implements AdminAppraiseService {
// 分批次计算
// 分批次计算
for ( RecordVo item : content ) {
for ( RecordVo item : content ) {
// 处理每周的评价数
// 处理每周的评价数
int weekNum = calculateWeekNum ( item . getCreateTime ( ) ) ;
long weekNum = calculateWeekNum ( startDatetime , endDatetime , item . getCreateTime ( ) ) ;
countByWeek . put ( weekNum , countByWeek . getOrDefault ( weekNum , 0 ) + 1 ) ;
countByWeek . put ( weekNum , countByWeek . getOrDefault ( weekNum , 0 ) + 1 ) ;
// 处理总评价数
// 处理总评价数
totalCount + + ;
totalCount + + ;
@ -99,7 +120,7 @@ public class AdminAppraiseServiceImpl implements AdminAppraiseService {
}
}
@Override
@Override
public List < RecordVo > conditionLatestRecord ( TimeRangeDto timeRangeDto ) {
public PageVo < RecordVo > conditionLatestRecord ( TimeRangeDto timeRangeDto ) {
Long startTime = timeRangeDto . getStartTime ( ) ;
Long startTime = timeRangeDto . getStartTime ( ) ;
Long endTime = timeRangeDto . getEndTime ( ) ;
Long endTime = timeRangeDto . getEndTime ( ) ;
String academicYearId = timeRangeDto . getAcademicYearId ( ) ;
String academicYearId = timeRangeDto . getAcademicYearId ( ) ;
@ -115,7 +136,7 @@ public class AdminAppraiseServiceImpl implements AdminAppraiseService {
endTime ,
endTime ,
pageRequest ) ;
pageRequest ) ;
return page . getContent ( ) ;
return new PageVo < > ( page . getTotalPages ( ) , page . getTotalElements ( ) , page . getContent ( ) ) ;
}
}
@Override
@Override
@ -272,13 +293,4 @@ public class AdminAppraiseServiceImpl implements AdminAppraiseService {
return res ;
return res ;
}
}
private int calculateWeekNum ( Long timeStamp ) {
// second 时间戳转 LocalDateTime
LocalDateTime localDateTime = LocalDateTime . ofInstant ( Instant . ofEpochSecond ( timeStamp ) , ZoneOffset . UTC ) ;
// 获取周字段 todo: 时区或者周的计算
WeekFields weekFields = WeekFields . of ( Locale . getDefault ( ) ) ;
// 获取当前日期时间所在年的周数
return localDateTime . get ( weekFields . weekOfWeekBasedYear ( ) ) ;
}
}
}