@ -6,13 +6,18 @@ import cn.teammodel.config.exception.ServiceException;
import cn.teammodel.controller.admin.service.AdminIndexDutyService ;
import cn.teammodel.model.dto.admin.appraise.TimeRangeDto ;
import cn.teammodel.model.entity.User ;
import cn.teammodel.model.entity.school.ClassInfo ;
import cn.teammodel.model.entity.school.School ;
import cn.teammodel.model.entity.school.Teacher ;
import cn.teammodel.model.entity.weekDuty.WeekDuty ;
import cn.teammodel.model.vo.admin.DutyIndexData ;
import cn.teammodel.model.vo.admin.DutyIndexPo ;
import cn.teammodel.model.vo.appraise.RecordVo ;
import cn.teammodel.model.vo.admin.DutyNodeRankVo ;
import cn.teammodel.model.vo.admin.DutyRankPo ;
import cn.teammodel.repository.ClassRepository ;
import cn.teammodel.repository.DutyRecordRepository ;
import cn.teammodel.repository.DutyRepository ;
import cn.teammodel.repository.SchoolRepository ;
import cn.teammodel.repository.TeacherRepository ;
import cn.teammodel.security.utils.SecurityUtil ;
import cn.teammodel.utils.SchoolDateUtil ;
import com.azure.spring.data.cosmos.core.query.CosmosPageRequest ;
@ -24,10 +29,8 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource ;
import java.time.LocalDate ;
import java.time.LocalDateTime ;
import java.util.HashSet ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
import java.util.* ;
import java.util.stream.Collectors ;
import static cn.teammodel.utils.SchoolDateUtil.calculateWeekNum ;
@ -42,8 +45,10 @@ public class AdminIndexDutyServiceImpl implements AdminIndexDutyService {
private SchoolRepository schoolRepository ;
@Resource
private DutyRepository duty Repository;
private TeacherRepository teacher Repository;
@Resource
private ClassRepository classRepository ;
@Resource
private DutyRecordRepository dutyRecordRepository ;
@ -116,7 +121,76 @@ public class AdminIndexDutyServiceImpl implements AdminIndexDutyService {
}
@Override
public List < RecordVo > conditionLatestRecord ( TimeRangeDto timeRangeDto ) {
return null ;
public List < DutyRankPo > classRank ( TimeRangeDto timeRangeDto ) {
Long startTime = timeRangeDto . getStartTime ( ) ;
Long endTime = timeRangeDto . getEndTime ( ) ;
String academicYearId = timeRangeDto . getAcademicYearId ( ) ;
String schoolId = SecurityUtil . getLoginUser ( ) . getSchoolId ( ) ;
List < DutyRankPo > res = dutyRecordRepository . classRank ( String . format ( PK . WEEK_DUTY_RECORD , schoolId ) , academicYearId , startTime , endTime ) ;
if ( ObjectUtils . isEmpty ( res ) ) return null ;
List < String > classIds = res . stream ( ) . map ( DutyRankPo : : getId ) . collect ( Collectors . toList ( ) ) ;
if ( ObjectUtils . isEmpty ( classIds ) ) {
return null ;
}
List < ClassInfo > classes = classRepository . findAllByCodeAndIdIn ( String . format ( PK . CLASS , schoolId ) , classIds ) ;
Map < String , String > idNameMap = classes . stream ( ) . collect ( Collectors . toMap ( ClassInfo : : getId , ClassInfo : : getName ) ) ;
// reversed sort by score
res = res . stream ( ) . peek ( s - > s . setName ( idNameMap . get ( s . getId ( ) ) ) ) . sorted ( ( a , b ) - > b . getScore ( ) - a . getScore ( ) ) . collect ( Collectors . toList ( ) ) ;
return res ;
}
@Override
public List < DutyRankPo > teacherRank ( TimeRangeDto timeRangeDto ) {
Long startTime = timeRangeDto . getStartTime ( ) ;
Long endTime = timeRangeDto . getEndTime ( ) ;
String academicYearId = timeRangeDto . getAcademicYearId ( ) ;
String schoolId = SecurityUtil . getLoginUser ( ) . getSchoolId ( ) ;
List < DutyRankPo > res = dutyRecordRepository . teacherRank ( String . format ( PK . WEEK_DUTY_RECORD , schoolId ) , academicYearId , startTime , endTime ) ;
// set teacher name
if ( ObjectUtils . isEmpty ( res ) ) return null ;
Set < String > teacherIdSet = res . stream ( ) . map ( DutyRankPo : : getId ) . collect ( Collectors . toSet ( ) ) ;
if ( ObjectUtils . isEmpty ( teacherIdSet ) ) return res ;
List < Teacher > teachers = teacherRepository . findAllByCodeAndIdIn ( PK . COMMON_BASE , teacherIdSet ) ;
Map < String , String > idNameMap = teachers . stream ( ) . collect ( Collectors . toMap ( Teacher : : getId , Teacher : : getName ) ) ;
res = res . stream ( ) . peek ( s - > s . setName ( idNameMap . get ( s . getId ( ) ) ) ) . sorted ( ( a , b ) - > b . getScore ( ) - a . getScore ( ) ) . collect ( Collectors . toList ( ) ) ;
return res ;
}
@Override
public List < DutyNodeRankVo > appraiseNodeRank ( TimeRangeDto timeRangeDto ) {
Long startTime = timeRangeDto . getStartTime ( ) ;
Long endTime = timeRangeDto . getEndTime ( ) ;
String academicYearId = timeRangeDto . getAcademicYearId ( ) ;
String schoolId = SecurityUtil . getLoginUser ( ) . getSchoolId ( ) ;
List < DutyNodeRankVo > res = dutyRecordRepository . dutyNodeRank ( String . format ( PK . WEEK_DUTY_RECORD , schoolId ) , academicYearId , startTime , endTime ) ;
if ( ObjectUtils . isEmpty ( res ) ) return null ;
List < String > names = res . stream ( ) . map ( DutyNodeRankVo : : getName ) . collect ( Collectors . toList ( ) ) ;
if ( ObjectUtils . isEmpty ( names ) ) return null ;
// 去重后的 nodes
List < WeekDuty . DutyTreeNode > nodesByName = dutyRecordRepository . findDutyRecordInNames ( String . format ( PK . WEEK_DUTY_RECORD , schoolId ) , academicYearId , names ) ;
// 正常情况下 name 一一对应 // TODO: 2024/2/29
Map < String , WeekDuty . DutyTreeNode > nameNodeMap = nodesByName . stream ( )
. collect ( Collectors . toMap ( WeekDuty . DutyTreeNode : : getName , item - > item , ( existing , replacement ) - > {
if ( replacement . getPath ( ) ! = null ) {
return replacement ;
}
return existing ;
} ) ) ;
res = res . stream ( )
. sorted ( Comparator . comparing ( DutyNodeRankVo : : getCount ) . reversed ( ) )
// 流中对元素操作但不改变流
. peek ( s - > {
WeekDuty . DutyTreeNode node = nameNodeMap . get ( s . getName ( ) ) ;
if ( node ! = null ) {
s . setPath ( node . getPath ( ) ) ;
s . setScore ( node . getScore ( ) ) ;
}
} )
. collect ( Collectors . toList ( ) ) ;
return res ;
}
}