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