From 12ad733286553dfaeb1fcc587414b424b47b2079 Mon Sep 17 00:00:00 2001 From: "hhb@hotmail.com" Date: Wed, 13 Nov 2024 11:46:30 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=95=99=E5=B8=88=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controller/TeacherController.java | 32 ++++ .../admin/service/TeacherService.java | 10 ++ .../service/impl/TeacherServiceImpl.java | 139 ++++++++++++++++++ .../model/dto/admin/teacher/TeacherDto.java | 10 ++ .../model/entity/common/GroupList.java | 34 +++++ .../model/entity/school/SchoolTeacher.java | 33 +++++ .../repository/SchoolGroupListRepository.java | 13 ++ .../repository/SchoolTeacherRepository.java | 16 ++ .../teammodel/utils/SnowflakeIdGenerator.java | 94 ++++++++++++ 9 files changed, 381 insertions(+) create mode 100644 src/main/java/cn/teammodel/controller/admin/controller/TeacherController.java create mode 100644 src/main/java/cn/teammodel/controller/admin/service/TeacherService.java create mode 100644 src/main/java/cn/teammodel/controller/admin/service/impl/TeacherServiceImpl.java create mode 100644 src/main/java/cn/teammodel/model/dto/admin/teacher/TeacherDto.java create mode 100644 src/main/java/cn/teammodel/model/entity/common/GroupList.java create mode 100644 src/main/java/cn/teammodel/model/entity/school/SchoolTeacher.java create mode 100644 src/main/java/cn/teammodel/repository/SchoolGroupListRepository.java create mode 100644 src/main/java/cn/teammodel/repository/SchoolTeacherRepository.java create mode 100644 src/main/java/cn/teammodel/utils/SnowflakeIdGenerator.java diff --git a/src/main/java/cn/teammodel/controller/admin/controller/TeacherController.java b/src/main/java/cn/teammodel/controller/admin/controller/TeacherController.java new file mode 100644 index 0000000..1de1f12 --- /dev/null +++ b/src/main/java/cn/teammodel/controller/admin/controller/TeacherController.java @@ -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>> getCount(@Valid @RequestBody TeacherDto teacherDto) { + List> res = teacherService.getTeacherList(teacherDto); + return R.success(res); + } +} diff --git a/src/main/java/cn/teammodel/controller/admin/service/TeacherService.java b/src/main/java/cn/teammodel/controller/admin/service/TeacherService.java new file mode 100644 index 0000000..423b3d0 --- /dev/null +++ b/src/main/java/cn/teammodel/controller/admin/service/TeacherService.java @@ -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> getTeacherList(TeacherDto teacherDto); +} diff --git a/src/main/java/cn/teammodel/controller/admin/service/impl/TeacherServiceImpl.java b/src/main/java/cn/teammodel/controller/admin/service/impl/TeacherServiceImpl.java new file mode 100644 index 0000000..4d4086d --- /dev/null +++ b/src/main/java/cn/teammodel/controller/admin/service/impl/TeacherServiceImpl.java @@ -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> getTeacherList(TeacherDto teacherDto) { + List> result = new ArrayList<>(); + List groupLists = schoolGroupListRepository.findAllByCode("GroupList-" + teacherDto.getCode()); + List schoolTeachers = schoolTeacherRepository.findAllByCode("Teacher-" + teacherDto.getCode()); + List school = schoolRepository.findBaseById(teacherDto.getCode()); + List subjects = new ArrayList<>(); + List 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 updatedSubjectCountMap = getStringIntegerMap(subjects, schoolTeachers); + + // 创建一个映射,用于存储每个职位的教师数量 + Map 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 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 getStringIntegerMap(List subjects, List schoolTeachers) { + Map subjectNameMap = new HashMap<>(); + for (School.Subject subject : subjects) { + subjectNameMap.put(subject.getId(), subject.getName()); + } + Map subjectCountMap = new HashMap<>(); + + // 遍历所有教师 + for (SchoolTeacher teacher : schoolTeachers) { + // 获取教师的科目列表 + List subjectIds = teacher.getSubjectIds(); + if (subjectIds != null) { + for (String subjectId : subjectIds) { + // 更新每个科目的教师数量 + subjectCountMap.put(subjectId, subjectCountMap.getOrDefault(subjectId, 0) + 1); + } + } + } + Map updatedSubjectCountMap = new HashMap<>(); + for (Map.Entry 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 getPeriodCountMap(List groupLists,List periods) { + + Map periodNameMap = new HashMap<>(); + for (School.Period period : periods) { + periodNameMap.put(period.getId(), period.getName()); + } + Map periodCountMap = getStringIntegerMap(groupLists); + // 更新 periodCountMap 的 key 为 period 名称 + Map updatedPeriodCountMap = new HashMap<>(); + for (Map.Entry 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 getStringIntegerMap(List groupLists) { + Map 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; + } +} diff --git a/src/main/java/cn/teammodel/model/dto/admin/teacher/TeacherDto.java b/src/main/java/cn/teammodel/model/dto/admin/teacher/TeacherDto.java new file mode 100644 index 0000000..f133cfc --- /dev/null +++ b/src/main/java/cn/teammodel/model/dto/admin/teacher/TeacherDto.java @@ -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; +} diff --git a/src/main/java/cn/teammodel/model/entity/common/GroupList.java b/src/main/java/cn/teammodel/model/entity/common/GroupList.java new file mode 100644 index 0000000..e001d32 --- /dev/null +++ b/src/main/java/cn/teammodel/model/entity/common/GroupList.java @@ -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 members; + @Data + public static class Member + { + public String id ; + //学生所在的学校 + public String code ; + //帐号类型 + public int type; + public String groupId ; + public String groupName ; + public String nickname ; + + } +} diff --git a/src/main/java/cn/teammodel/model/entity/school/SchoolTeacher.java b/src/main/java/cn/teammodel/model/entity/school/SchoolTeacher.java new file mode 100644 index 0000000..c3c7981 --- /dev/null +++ b/src/main/java/cn/teammodel/model/entity/school/SchoolTeacher.java @@ -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 roles ; + public List permissions ; + public String status ; + public long createTime ; + /// + /// 教师的科目数组 + /// + public List subjectIds ; + /// + /// 默认的学段id + /// + public String periodId ; + +} diff --git a/src/main/java/cn/teammodel/repository/SchoolGroupListRepository.java b/src/main/java/cn/teammodel/repository/SchoolGroupListRepository.java new file mode 100644 index 0000000..ccce930 --- /dev/null +++ b/src/main/java/cn/teammodel/repository/SchoolGroupListRepository.java @@ -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 { + @Query("select * from GroupList as c where c.code = @code") + List findAllByCode(String code); +} diff --git a/src/main/java/cn/teammodel/repository/SchoolTeacherRepository.java b/src/main/java/cn/teammodel/repository/SchoolTeacherRepository.java new file mode 100644 index 0000000..cfb3587 --- /dev/null +++ b/src/main/java/cn/teammodel/repository/SchoolTeacherRepository.java @@ -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 { + @Query("select c.id, c.name,c.job,c.subjectIds from SchoolTeacher as c where c.code = @code") + List findAllByCode(String code); +} diff --git a/src/main/java/cn/teammodel/utils/SnowflakeIdGenerator.java b/src/main/java/cn/teammodel/utils/SnowflakeIdGenerator.java new file mode 100644 index 0000000..f8db98f --- /dev/null +++ b/src/main/java/cn/teammodel/utils/SnowflakeIdGenerator.java @@ -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); + } + }*/ +}