update 教师相关统计数据

develop
hhb@hotmail.com 1 month ago
parent f07b7e9257
commit 12ad733286

@ -0,0 +1,32 @@
package cn.teammodel.controller.admin.controller;
import cn.teammodel.common.R;
import cn.teammodel.controller.admin.service.TeacherService;
import cn.teammodel.model.dto.admin.exam.OverViewDto;
import cn.teammodel.model.dto.admin.teacher.TeacherDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("admin/teacher")
@Api(tags = "教师相关接口内容")
public class TeacherController {
@Resource
private TeacherService teacherService;
@PostMapping("getCount")
@ApiOperation("获取教师基础数据统计内容")
public R<List<Map<String, Integer>>> getCount(@Valid @RequestBody TeacherDto teacherDto) {
List<Map<String, Integer>> res = teacherService.getTeacherList(teacherDto);
return R.success(res);
}
}

@ -0,0 +1,10 @@
package cn.teammodel.controller.admin.service;
import cn.teammodel.model.dto.admin.teacher.TeacherDto;
import java.util.List;
import java.util.Map;
public interface TeacherService {
List<Map<String,Integer>> getTeacherList(TeacherDto teacherDto);
}

@ -0,0 +1,139 @@
package cn.teammodel.controller.admin.service.impl;
import cn.teammodel.controller.admin.service.TeacherService;
import cn.teammodel.model.dto.admin.teacher.TeacherDto;
import cn.teammodel.model.entity.common.GroupList;
import cn.teammodel.model.entity.school.School;
import cn.teammodel.model.entity.school.SchoolTeacher;
import cn.teammodel.repository.SchoolGroupListRepository;
import cn.teammodel.repository.SchoolRepository;
import cn.teammodel.repository.SchoolTeacherRepository;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
@Service
public class TeacherServiceImpl implements TeacherService {
@Resource
private SchoolGroupListRepository schoolGroupListRepository;
@Resource
private SchoolTeacherRepository schoolTeacherRepository;
@Resource
private SchoolRepository schoolRepository;
@Override
public List<Map<String, Integer>> getTeacherList(TeacherDto teacherDto) {
List<Map<String, Integer>> result = new ArrayList<>();
List<GroupList> groupLists = schoolGroupListRepository.findAllByCode("GroupList-" + teacherDto.getCode());
List<SchoolTeacher> schoolTeachers = schoolTeacherRepository.findAllByCode("Teacher-" + teacherDto.getCode());
List<School> school = schoolRepository.findBaseById(teacherDto.getCode());
List<School.Subject> subjects = new ArrayList<>();
List<School.Period> periods = new ArrayList<>();
for (School sc : school) {
if (sc.getPeriod() != null) {
// 遍历每个 Period 对象
for (School.Period period : sc.getPeriod()) {
periods.add(period);
// 检查 period 是否有 subjects 列表
if (period.getSubjects() != null) {
// 将 period 中的 subjects 添加到总的 subjects 列表中
subjects.addAll(period.getSubjects());
}
}
}
}
Map<String, Integer> updatedSubjectCountMap = getStringIntegerMap(subjects, schoolTeachers);
// 创建一个映射,用于存储每个职位的教师数量
Map<String, Integer> jobCountMap = new HashMap<>();
// 遍历所有教师
for (SchoolTeacher teacher : schoolTeachers) {
// 获取教师的职位
String job = teacher.getJob();
if (job != null) {
// 更新每个职位的教师数量
jobCountMap.put(job, jobCountMap.getOrDefault(job, 0) + 1);
}
}
jobCountMap.entrySet().removeIf(entry -> entry.getKey().isEmpty());
Map<String, Integer> periodCountMap = getPeriodCountMap(groupLists, periods);
periodCountMap.entrySet().removeIf(entry -> entry.getKey().isEmpty());
updatedSubjectCountMap.entrySet().removeIf(entry -> entry.getKey().isEmpty());
result.add(updatedSubjectCountMap);
result.add(jobCountMap);
result.add(periodCountMap);
return result;
}
private static @NotNull Map<String, Integer> getStringIntegerMap(List<School.Subject> subjects, List<SchoolTeacher> schoolTeachers) {
Map<String, String> subjectNameMap = new HashMap<>();
for (School.Subject subject : subjects) {
subjectNameMap.put(subject.getId(), subject.getName());
}
Map<String, Integer> subjectCountMap = new HashMap<>();
// 遍历所有教师
for (SchoolTeacher teacher : schoolTeachers) {
// 获取教师的科目列表
List<String> subjectIds = teacher.getSubjectIds();
if (subjectIds != null) {
for (String subjectId : subjectIds) {
// 更新每个科目的教师数量
subjectCountMap.put(subjectId, subjectCountMap.getOrDefault(subjectId, 0) + 1);
}
}
}
Map<String, Integer> updatedSubjectCountMap = new HashMap<>();
for (Map.Entry<String, Integer> entry : subjectCountMap.entrySet()) {
String subjectId = entry.getKey();
Integer count = entry.getValue();
String subjectName = subjectNameMap.get(subjectId);
if (subjectName != null) {
updatedSubjectCountMap.put(subjectName, count);
}
}
return updatedSubjectCountMap;
}
private static @NotNull Map<String, Integer> getPeriodCountMap(List<GroupList> groupLists,List<School.Period> periods) {
Map<String, String> periodNameMap = new HashMap<>();
for (School.Period period : periods) {
periodNameMap.put(period.getId(), period.getName());
}
Map<String, Integer> periodCountMap = getStringIntegerMap(groupLists);
// 更新 periodCountMap 的 key 为 period 名称
Map<String, Integer> updatedPeriodCountMap = new HashMap<>();
for (Map.Entry<String, Integer> entry : periodCountMap.entrySet()) {
String periodId = entry.getKey();
Integer count = entry.getValue();
String periodName = periodNameMap.get(periodId);
if (periodName != null) {
updatedPeriodCountMap.put(periodName, count);
}
}
return updatedPeriodCountMap;
}
private static @NotNull Map<String, Integer> getStringIntegerMap(List<GroupList> groupLists) {
Map<String, Integer> periodCountMap = new HashMap<>();
// 遍历所有 GroupList 对象
for (GroupList group : groupLists) {
// 获取 periodId
String periodId = group.getPeriodId();
if (periodId != null) {
// 计算成员数量
int memberCount = group.getMembers().size();
// 更新 periodCountMap
periodCountMap.put(periodId, periodCountMap.getOrDefault(periodId, 0) + memberCount);
}
}
return periodCountMap;
}
}

@ -0,0 +1,10 @@
package cn.teammodel.model.dto.admin.teacher;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class TeacherDto {
@ApiModelProperty("学校编码")
public String code;
}

@ -0,0 +1,34 @@
package cn.teammodel.model.entity.common;
import cn.teammodel.model.entity.BaseItem;
import com.azure.spring.data.cosmos.core.mapping.Container;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.List;
@EqualsAndHashCode(callSuper = true)
@Container(containerName = "School")
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class GroupList extends BaseItem {
public String school;
public String name;
public String periodId;
public List<Member> members;
@Data
public static class Member
{
public String id ;
//学生所在的学校
public String code ;
//帐号类型
public int type;
public String groupId ;
public String groupName ;
public String nickname ;
}
}

@ -0,0 +1,33 @@
package cn.teammodel.model.entity.school;
import cn.teammodel.model.entity.BaseItem;
import com.azure.spring.data.cosmos.core.mapping.Container;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.List;
@EqualsAndHashCode(callSuper = true)
@Container(containerName = "School")
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class SchoolTeacher extends BaseItem {
public String name ;
public String picture ;
public int size ;
public String job ;
public List<String> roles ;
public List<String> permissions ;
public String status ;
public long createTime ;
/// <summary>
/// 教师的科目数组
/// </summary>
public List<String> subjectIds ;
/// <summary>
/// 默认的学段id
/// </summary>
public String periodId ;
}

@ -0,0 +1,13 @@
package cn.teammodel.repository;
import cn.teammodel.model.entity.common.GroupList;
import cn.teammodel.model.entity.school.SchoolTeacher;
import com.azure.spring.data.cosmos.repository.CosmosRepository;
import com.azure.spring.data.cosmos.repository.Query;
import java.util.List;
public interface SchoolGroupListRepository extends CosmosRepository<GroupList, String> {
@Query("select * from GroupList as c where c.code = @code")
List<GroupList> findAllByCode(String code);
}

@ -0,0 +1,16 @@
package cn.teammodel.repository;
import cn.teammodel.model.entity.school.SchoolTeacher;
import cn.teammodel.model.entity.school.Teacher;
import com.azure.spring.data.cosmos.repository.CosmosRepository;
import com.azure.spring.data.cosmos.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.List;
@Repository
public interface SchoolTeacherRepository extends CosmosRepository<SchoolTeacher, String> {
@Query("select c.id, c.name,c.job,c.subjectIds from SchoolTeacher as c where c.code = @code")
List<SchoolTeacher> findAllByCode(String code);
}

@ -0,0 +1,94 @@
package cn.teammodel.utils;
import java.util.concurrent.atomic.AtomicLong;
public class SnowflakeIdGenerator {
// 定义起始时间戳Epoch
private static final long EPOCH = 1677609600000L; // 2023-03-01 00:00:00 UTC
// 定义工作机器ID的位数
private static final long WORKER_ID_BITS = 10L;
// 定义序列号的位数
private static final long SEQUENCE_BITS = 12L;
// 计算最大工作机器ID
private static final long MAX_WORKER_ID = ~(-1L << WORKER_ID_BITS);
// 计算最大序列号
private static final long MAX_SEQUENCE = ~(-1L << SEQUENCE_BITS);
// 工作机器ID的左移位数
private static final long WORKER_ID_SHIFT = SEQUENCE_BITS;
// 时间戳的左移位数
private static final long TIMESTAMP_LEFT_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS;
// 当前工作机器ID
private final long workerId;
// 序列号
private final AtomicLong sequence = new AtomicLong(0L);
// 上一次生成ID的时间戳
private long lastTimestamp = -1L;
public SnowflakeIdGenerator(long workerId) {
if (workerId > MAX_WORKER_ID || workerId < 0) {
throw new IllegalArgumentException(String.format("workerId must be between 0 and %d", MAX_WORKER_ID));
}
this.workerId = workerId;
}
public synchronized long nextId() {
long timestamp = timeGen();
// 如果时间戳回退,则抛出异常
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
// 如果时间戳相同,则递增序列号
if (lastTimestamp == timestamp) {
long sequence = this.sequence.getAndIncrement();
if (sequence > MAX_SEQUENCE) {
// 序列号溢出,则等待下一毫秒
timestamp = tilNextMillis(lastTimestamp);
this.sequence.set(0L);
}
} else {
// 时间戳不同,则重置序列号
this.sequence.set(0L);
}
// 更新上一次生成ID的时间戳
lastTimestamp = timestamp;
// 构造最终的ID
return ((timestamp - EPOCH) << TIMESTAMP_LEFT_SHIFT) |
(workerId << WORKER_ID_SHIFT) |
(sequence.get());
}
private long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
private long timeGen() {
return System.currentTimeMillis();
}
/* public static void main(String[] args) {
SnowflakeIdGenerator generator = new SnowflakeIdGenerator(1); // 设置 workerId 为 1
for (int i = 0; i < 10; i++) {
long id = generator.nextId();
System.out.println(id);
}
}*/
}
Loading…
Cancel
Save