@ -1,18 +1,18 @@
package cn.teammodel.controller.admin.service.impl ;
import cn. teammodel.controller.admin.service.AdminAppraiseService ;
import cn. hutool.core.lang.Pair ;
import cn.teammodel.common.PK ;
import cn.teammodel.dao.AppraiseRecordRepository ;
import cn.teammodel.dao.AppraiseRepository ;
import cn.teammodel.dao.ClassRepository ;
import cn.teammodel.dao.TeacherRepository ;
import cn.teammodel.controller.admin.service.AdminAppraiseService ;
import cn.teammodel.dao.* ;
import cn.teammodel.model.dto.admin.TimeRangeDto ;
import cn.teammodel.model.entity.User ;
import cn.teammodel.model.entity.appraise.AppraiseTreeNode ;
import cn.teammodel.model.entity.school.ClassInfo ;
import cn.teammodel.model.entity.school.Student ;
import cn.teammodel.model.entity.school.Teacher ;
import cn.teammodel.model.vo.admin.IndexData ;
import cn.teammodel.model.vo.admin.RankPo ;
import cn.teammodel.model.vo.admin.RankVo ;
import cn.teammodel.model.vo.admin.StudentRankVo ;
import cn.teammodel.model.vo.appraise.RecordVo ;
import cn.teammodel.security.utils.SecurityUtil ;
import com.azure.spring.data.cosmos.core.query.CosmosPageRequest ;
@ -22,7 +22,9 @@ import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service ;
import javax.annotation.Resource ;
import java.time.Instant ;
import java.time.LocalDateTime ;
import java.time.ZoneOffset ;
import java.time.temporal.WeekFields ;
import java.util.* ;
import java.util.stream.Collectors ;
@ -38,22 +40,25 @@ public class AdminAppraiseServiceImpl implements AdminAppraiseService {
@Resource
private TeacherRepository teacherRepository ;
@Resource
private StudentRepository studentRepository ;
@Resource
private AppraiseRepository appraiseRepository ;
@Resource
private AppraiseRecordRepository appraiseRecordRepository ;
@Override
public IndexData getIndexData ( String period, String academicYearId) {
public IndexData getIndexData ( String academicYearId) {
final IndexData indexData = new IndexData ( ) ;
int totalCount = 0 ;
int criticalCount = 0 ;
Set < String > creatorIdSet = new HashSet < > ( ) ;
User loginUser = SecurityUtil . getLoginUser ( ) ;
String schoolId = loginUser . getSchoolId ( ) ;
// slice 分段读取
CosmosPageRequest pageRequest = new CosmosPageRequest ( 0 , 10 , null ) ;
CosmosPageRequest pageRequest = new CosmosPageRequest ( 0 , 10 0 , null ) ;
Slice < RecordVo > slice ;
Map < Integer , Integer > countByWeek = new HashMap < > ( ) ;
@ -75,6 +80,8 @@ public class AdminAppraiseServiceImpl implements AdminAppraiseService {
if ( ! item . isPraise ( ) ) {
criticalCount + + ;
}
// 处理已评价老师总数
creatorIdSet . add ( item . getCreatorId ( ) ) ;
}
if ( slice . hasNext ( ) ) {
@ -87,13 +94,14 @@ public class AdminAppraiseServiceImpl implements AdminAppraiseService {
indexData . setTotalCount ( totalCount ) ;
indexData . setCriticalCount ( criticalCount ) ;
indexData . setPraiseCount ( totalCount - criticalCount ) ;
indexData . setTeacherCount ( creatorIdSet . size ( ) ) ;
return indexData ;
}
@Override
public List < RecordVo > conditionLatestRecord ( TimeRangeDto timeRangeDto ) {
Lo calDateTime startTime = timeRangeDto . getStartTime ( ) ;
Lo calDateTime endTime = timeRangeDto . getEndTime ( ) ;
Lo ng startTime = timeRangeDto . getStartTime ( ) ;
Lo ng endTime = timeRangeDto . getEndTime ( ) ;
String academicYearId = timeRangeDto . getAcademicYearId ( ) ;
Integer current = timeRangeDto . getCurrent ( ) ;
Integer size = timeRangeDto . getSize ( ) ;
@ -103,77 +111,170 @@ public class AdminAppraiseServiceImpl implements AdminAppraiseService {
Page < RecordVo > page = appraiseRecordRepository . pageLatestRecords (
String . format ( PK . PK_APPRAISE_RECORD , schoolId ) ,
academicYearId ,
startTime .toString ( ) ,
endTime .toString ( ) ,
startTime ,
endTime ,
pageRequest ) ;
return page . getContent ( ) ;
}
@Override
public List < Rank V o> classRank ( TimeRangeDto timeRangeDto ) {
// todo 不要 page
Stri ng startTime = timeRangeDto . getStartTime ( ) = = null ? null : timeRangeDto . getStartTime ( ) . toString ( ) ;
Stri ng endTime = timeRangeDto . getEndTime ( ) = = null ? null : timeRangeDto . getEndTime ( ) . toString ( ) ;
public List < Rank P o> classRank ( TimeRangeDto timeRangeDto ) {
// todo 不要 page | 批评和表扬数量
Lo ng startTime = timeRangeDto . getStartTime ( ) ;
Lo ng endTime = timeRangeDto . getEndTime ( ) ;
String academicYearId = timeRangeDto . getAcademicYearId ( ) ;
String schoolId = SecurityUtil . getLoginUser ( ) . getSchoolId ( ) ;
List < Rank Vo> rankV oList = appraiseRecordRepository . classRank (
List < Rank Po> rankP oList = appraiseRecordRepository . classRank (
String . format ( PK . PK_APPRAISE_RECORD , schoolId ) ,
academicYearId ,
startTime ,
endTime
) ;
Set < String > classIdSet = rankVoList . stream ( ) . map ( RankVo : : getId ) . collect ( Collectors . toSet ( ) ) ;
if ( ObjectUtils . isEmpty ( rankPoList ) ) return null ;
Set < String > classIdSet = rankPoList . stream ( ) . map ( RankPo : : getId ) . collect ( Collectors . toSet ( ) ) ;
// 注意: 如果查询 in 的查询集在数据库中不存在,则在结果集也不会为 null.
if ( ObjectUtils . isEmpty ( classIdSet ) ) return rankPoList ;
List < ClassInfo > classes = classRepository . findAllByCodeAndIdIn ( String . format ( PK . CLASS , schoolId ) , classIdSet ) ;
Map < String , String > idNameMap = classes . stream ( ) . collect ( Collectors . toMap ( ClassInfo : : getId , ClassInfo : : getName ) ) ;
rank V oList. forEach ( rankVo - > rankVo . setName ( idNameMap . get ( rankVo . getId ( ) ) ) ) ;
return rank V oList;
rank P oList. forEach ( rankVo - > rankVo . setName ( idNameMap . get ( rankVo . getId ( ) ) ) ) ;
return rank P oList;
}
@Override
public List < RankVo > teacherRank ( TimeRangeDto timeRangeDto ) {
Stri ng startTime = timeRangeDto . getStartTime ( ) = = null ? null : timeRangeDto . getStartTime ( ) . toString ( ) ;
Stri ng endTime = timeRangeDto . getEndTime ( ) = = null ? null : timeRangeDto . getEndTime ( ) . toString ( ) ;
Lo ng startTime = timeRangeDto . getStartTime ( ) ;
Lo ng endTime = timeRangeDto . getEndTime ( ) ;
String academicYearId = timeRangeDto . getAcademicYearId ( ) ;
String schoolId = SecurityUtil . getLoginUser ( ) . getSchoolId ( ) ;
List < Rank Vo> rankV oList = appraiseRecordRepository . teacherRank (
List < Rank Po> rankP oList = appraiseRecordRepository . teacherRank (
String . format ( PK . PK_APPRAISE_RECORD , schoolId ) ,
academicYearId ,
startTime ,
endTime
) ;
Set < String > teacherIdSet = rankVoList . stream ( ) . map ( RankVo : : getId ) . collect ( Collectors . toSet ( ) ) ;
// 根据 id 分组
Map < String , List < RankPo > > idRankPoMap = rankPoList . stream ( ) . collect ( Collectors . groupingBy ( RankPo : : getId ) ) ;
final List < RankVo > rankVos = new ArrayList < > ( ) ;
idRankPoMap . forEach ( ( id , list ) - > {
RankVo rankVo = new RankVo ( ) ;
int praiseCount = 0 ;
int criticalCount = 0 ;
rankVo . setId ( id ) ;
for ( RankPo po : list ) {
if ( po . getIsPraise ( ) ) {
praiseCount + = po . getCount ( ) ;
} else {
criticalCount + = po . getCount ( ) ;
}
}
rankVo . setPraiseCount ( praiseCount ) ;
rankVo . setCriticalCount ( criticalCount ) ;
rankVo . setTotalCount ( praiseCount + criticalCount ) ;
rankVos . add ( rankVo ) ;
} ) ;
// 排序
List < RankVo > res = rankVos . stream ( )
. sorted ( Comparator . comparing ( RankVo : : getTotalCount ) . reversed ( ) )
. collect ( Collectors . toList ( ) ) ;
// 设置 name
if ( ObjectUtils . isEmpty ( res ) ) return null ;
Set < String > teacherIdSet = res . stream ( ) . map ( RankVo : : 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 ) ) ;
rankVoList . forEach ( rankVo - > rankVo . setName ( idNameMap . get ( rankVo . getId ( ) ) ) ) ;
return rankVoList ;
r es . forEach ( rankVo - > rankVo . setName ( idNameMap . get ( rankVo . getId ( ) ) ) ) ;
return r es ;
}
@Override
public List < RankVo > appraiseNodeRank ( TimeRangeDto timeRangeDto ) {
String startTime = timeRangeDto . getStartTime ( ) = = null ? null : timeRangeDto . getStartTime ( ) . toString ( ) ;
String endTime = timeRangeDto . getEndTime ( ) = = null ? null : timeRangeDto . getEndTime ( ) . toString ( ) ;
public List < Rank P o> appraiseNodeRank ( TimeRangeDto timeRangeDto ) {
Lo ng startTime = timeRangeDto . getStartTime ( ) ;
Lo ng endTime = timeRangeDto . getEndTime ( ) ;
String academicYearId = timeRangeDto . getAcademicYearId ( ) ;
String schoolId = SecurityUtil . getLoginUser ( ) . getSchoolId ( ) ;
List < RankVo > rankVoList = appraiseRecordRepository . appraiseNodeRank (
List < Rank Po> rankP oList = appraiseRecordRepository . appraiseNodeRank (
String . format ( PK . PK_APPRAISE_RECORD , schoolId ) ,
academicYearId ,
startTime ,
endTime
) ;
Set < String > appraiseNodeIdSet = rankVoList . stream ( ) . map ( RankVo : : getId ) . collect ( Collectors . toSet ( ) ) ;
List < AppraiseTreeNode > appraises = appraiseRepository . findAllByCodeAndIdIn ( PK . PK_APPRAISE , appraiseNodeIdSet ) ;
Map < String , String > idNameMap = appraises . stream ( ) . collect ( Collectors . toMap ( AppraiseTreeNode : : getId , AppraiseTreeNode : : getName ) ) ;
rankVoList . forEach ( rankVo - > rankVo . setName ( idNameMap . get ( rankVo . getId ( ) ) ) ) ;
return rankVoList ;
if ( rankPoList = = null ) return null ;
rankPoList = rankPoList . stream ( )
. sorted ( Comparator . comparing ( RankPo : : getCount ) . reversed ( ) )
. collect ( Collectors . toList ( ) ) ;
return rankPoList ;
}
@Override
public List < StudentRankVo > studentRank ( TimeRangeDto timeRangeDto ) {
Long startTime = timeRangeDto . getStartTime ( ) ;
Long endTime = timeRangeDto . getEndTime ( ) ;
String academicYearId = timeRangeDto . getAcademicYearId ( ) ;
String schoolId = SecurityUtil . getLoginUser ( ) . getSchoolId ( ) ;
List < RankPo > rankPoList = appraiseRecordRepository . studentRank (
String . format ( PK . PK_APPRAISE_RECORD , schoolId ) ,
academicYearId ,
startTime ,
endTime
) ;
// 根据 id 分组
Map < String , List < RankPo > > idRankPoMap = rankPoList . stream ( ) . collect ( Collectors . groupingBy ( RankPo : : getId ) ) ;
final List < StudentRankVo > rankVos = new ArrayList < > ( ) ;
idRankPoMap . forEach ( ( id , list ) - > {
StudentRankVo rankVo = new StudentRankVo ( ) ;
int praiseCount = 0 ;
int criticalCount = 0 ;
rankVo . setId ( id ) ;
for ( RankPo po : list ) {
if ( po . getIsPraise ( ) ) {
praiseCount + = po . getCount ( ) ;
} else {
criticalCount + = po . getCount ( ) ;
}
}
rankVo . setPraiseCount ( praiseCount ) ;
rankVo . setCriticalCount ( criticalCount ) ;
rankVo . setTotalCount ( praiseCount + criticalCount ) ;
rankVos . add ( rankVo ) ;
} ) ;
// 排序
List < StudentRankVo > res = rankVos . stream ( )
. sorted ( Comparator . comparing ( StudentRankVo : : getTotalCount ) . reversed ( ) )
. collect ( Collectors . toList ( ) ) ;
// 设置 student name
if ( ObjectUtils . isEmpty ( res ) ) return null ;
Set < String > studentIdSet = res . stream ( ) . map ( StudentRankVo : : getId ) . collect ( Collectors . toSet ( ) ) ;
if ( ObjectUtils . isEmpty ( studentIdSet ) ) return res ;
List < Student > students = studentRepository . findAllByCodeAndIdIn ( String . format ( PK . STUDENT , schoolId ) , studentIdSet ) ;
// 提取 Student 中的 name 和 classId
Map < String , Pair < String , String > > idNameMap = students . stream ( ) . collect ( Collectors . toMap ( Student : : getId , item - > new Pair < > ( item . getName ( ) , item . getClassId ( ) ) ) ) ;
res . forEach ( rankVo - > {
rankVo . setName ( idNameMap . get ( rankVo . getId ( ) ) . getKey ( ) ) ;
rankVo . setClassName ( idNameMap . get ( rankVo . getId ( ) ) . getValue ( ) ) ;
} ) ;
// 设置 class name
Set < String > classIds = students . stream ( ) . map ( Student : : getClassId ) . collect ( Collectors . toSet ( ) ) ;
List < ClassInfo > classes = classRepository . findAllByCodeAndIdIn ( String . format ( PK . CLASS , schoolId ) , classIds ) ;
Map < String , String > idClassNameMap = classes . stream ( ) . collect ( Collectors . toMap ( ClassInfo : : getId , ClassInfo : : getName ) ) ;
res . forEach ( rankVo - > rankVo . setClassName ( idClassNameMap . getOrDefault ( rankVo . getClassName ( ) , null ) ) ) ;
return res ;
}
private int calculateWeekNum ( LocalDateTime localDateTime ) {
private int calculateWeekNum ( Long timeStamp ) {
// second 时间戳转 LocalDateTime
LocalDateTime localDateTime = LocalDateTime . ofInstant ( Instant . ofEpochSecond ( timeStamp ) , ZoneOffset . UTC ) ;
// 获取周字段 todo: 时区或者周的计算
WeekFields weekFields = WeekFields . of ( Locale . getDefault ( ) ) ;
// 获取当前日期时间所在年的周数