diff --git a/src/main/java/cn/teammodel/controller/admin/service/TeacherService.java b/src/main/java/cn/teammodel/controller/admin/service/TeacherService.java index 9181968..30a64a2 100644 --- a/src/main/java/cn/teammodel/controller/admin/service/TeacherService.java +++ b/src/main/java/cn/teammodel/controller/admin/service/TeacherService.java @@ -1,5 +1,6 @@ package cn.teammodel.controller.admin.service; +import cn.teammodel.model.dto.admin.teacher.GpTeacherDto; import cn.teammodel.model.dto.admin.teacher.TeacherDto; import javax.servlet.http.HttpServletRequest; 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 index 4722080..0b652fd 100644 --- a/src/main/java/cn/teammodel/controller/admin/service/impl/TeacherServiceImpl.java +++ b/src/main/java/cn/teammodel/controller/admin/service/impl/TeacherServiceImpl.java @@ -4,11 +4,14 @@ import cn.teammodel.common.ErrorCode; import cn.teammodel.common.PK; import cn.teammodel.config.exception.ServiceException; import cn.teammodel.controller.admin.service.TeacherService; +import cn.teammodel.model.dto.admin.teacher.GpTeacherDto; import cn.teammodel.model.dto.admin.teacher.TeacherDto; import cn.teammodel.model.entity.common.GroupList; +import cn.teammodel.model.entity.school.GpTeacher; import cn.teammodel.model.entity.school.LessonRecord; import cn.teammodel.model.entity.school.School; import cn.teammodel.model.entity.school.SchoolTeacher; +import cn.teammodel.model.vo.admin.GpTeacherVo; import cn.teammodel.repository.LessonRecordRepository; import cn.teammodel.repository.SchoolGroupListRepository; import cn.teammodel.repository.SchoolRepository; @@ -29,6 +32,7 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.time.*; import java.util.*; +import java.util.stream.Collectors; import static cn.teammodel.utils.SchoolDateUtil.calculateWeekNum; @@ -107,6 +111,45 @@ public class TeacherServiceImpl implements TeacherService { String url = env.getProperty("ies.server-url-get-teacher-all"); try { teachers = GroupUtil.getGroupId(teacherDto,new GroupUtil(env), request,url); + List teachersList = new ArrayList<>(); + for (Map.Entry entry : teachers.entrySet()) { + // 假设 entry.getValue() 返回的是 List> + List> mapList = (List>) entry.getValue(); + for (Map map : mapList) { + // 将 Map 转换为 GpTeacherVo 对象 + GpTeacherVo teacher = convertToGpTeacherVo(map); + teachersList.add(teacher); + } + } + List phones = new ArrayList<>(); + // 进一步处理 teachersList + for (GpTeacherVo teacher : teachersList) { + phones.add(teacher.getPhone()); + } + GpTeacherDto gpTeacherDto = new GpTeacherDto(); + gpTeacherDto.setMobiles(phones); + Map tBase = getTeacherBase(gpTeacherDto, request); + List gpTeachers = new ArrayList<>(); + for (Map.Entry entry : tBase.entrySet()) { + if (entry.getKey().equals("teachers")) { + List> dataList = (List>) entry.getValue(); + for (Map dataMap : dataList) { + GpTeacher gpTeacher = convertToGpTeacher(dataMap); + gpTeachers.add(gpTeacher); + } + } + } + Map genderCount = gpTeachers.stream() + .collect(Collectors.groupingBy(gpTeacher -> gpTeacher.getBasic().getGender(), Collectors.counting())); + Map degreeCount = gpTeachers.stream() + .filter(gpTeacher -> gpTeacher.getDegree() != null) + .collect(Collectors.groupingBy(GpTeacher::getDegree, Collectors.counting())); + Map practiceCount = getStringIntegerMap(teacherDto, gpTeachers); + + teachers.put("practice", practiceCount); + teachers.put("gender", genderCount); + teachers.put("degree", degreeCount); + }catch (Exception e) { throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "数据转换错误"); @@ -114,6 +157,109 @@ public class TeacherServiceImpl implements TeacherService { return teachers; } + private static @NotNull Map getStringIntegerMap(TeacherDto teacherDto, List gpTeachers) { + Map practiceCount = new HashMap<>(); + for (GpTeacher gpTeacher : gpTeachers) { + if(gpTeacher.practiceTime > 0) { + // 将时间戳转换为 LocalDateTime + LocalDateTime dateTime = LocalDateTime.ofInstant( + Instant.ofEpochMilli(gpTeacher.practiceTime), + ZoneId.systemDefault() + ); + + // 获取年份 + int year = dateTime.getYear(); + practiceCount.put(gpTeacher.basic.name, teacherDto.year - year); + } + } + return practiceCount; + } + + private GpTeacherVo convertToGpTeacherVo(Map map) { + GpTeacherVo teacher = new GpTeacherVo(); + // 假设 GpTeacherVo 有以下属性:name, age, subject + teacher.setName((String) map.get("name")); + teacher.setPhone((String) map.get("phone")); + teacher.setId((String) map.get("id")); + // 根据实际属性进行转换 + return teacher; + } + private GpTeacher convertToGpTeacher(Map map) { + GpTeacher teacher = new GpTeacher(); + // 假设 GpTeacherVo 有以下属性:name, age, subject + Object describeObj = map.get("describe"); + if (describeObj instanceof String) { + teacher.setDescribe((String) describeObj); + } else { + teacher.setDescribe(null); // 或者设置一个默认值 + } + Object depictObj = map.get("depict"); + if (depictObj instanceof String) { + teacher.setDepict((String) depictObj); + } else { + teacher.setDepict(null); // 或者设置一个默认值 + } + teacher.setPeriods((ArrayList) map.get("periods")); + teacher.setSubjects((ArrayList) map.get("subjects")); + teacher.setGrades((ArrayList) map.get("grades")); + Object practiceTimeObj = map.get("practiceTime"); + if (practiceTimeObj instanceof Long) { + teacher.setPracticeTime((Long) practiceTimeObj); + } else { + teacher.setPracticeTime(0L); // 或者设置一个默认值 + } + Object titleObj = map.get("title"); + if (titleObj instanceof String) { + teacher.setTitle((String) titleObj); + } else { + teacher.setTitle(null); // 或者设置一个默认值 + } + Object degreeObj = map.get("degree"); + if (degreeObj instanceof String) { + teacher.setDegree((String) degreeObj); + } else { + teacher.setDegree(null); // 或者设置一个默认值 + } + Map basicMap = (Map) map.get("basic"); + //teacher.setBasic(new GpTeacher.GPBasicInfo(basicMap.get("name").toString(),basicMap.get("fullName").toString(),basicMap.get("picture").toString(),basicMap.get("region").toString(),basicMap.get("province").toString(),basicMap.get("city").toString(),basicMap.get("dist").toString(),basicMap.get("gender").toString(),basicMap.get("age").toString(),basicMap.get("address").toString())); + GpTeacher.GPBasicInfo basic = new GpTeacher.GPBasicInfo(); + Object nameObj = basicMap.get("name"); + if (nameObj instanceof String) { + basic.setName((String) nameObj); + } else { + basic.setName(null); // 或者设置一个默认值 + } + Object fullNameObj = basicMap.get("fullName"); + if (fullNameObj instanceof String) { + basic.setFullName((String) fullNameObj); + } else { + basic.setFullName(null); // 或者设置一个默认值 + } + Object distObj = basicMap.get("dist"); + if (distObj instanceof String) { + basic.setDist((String) distObj); + } else { + basic.setDist(null); // 或者设置一个默认值 + } + Object genderObj = basicMap.get("gender"); + if (genderObj instanceof String) { + basic.setGender((String) genderObj); + } else { + basic.setGender(null); // 或者设置一个默认值 + } + Object ageObj = basicMap.get("age"); + if (ageObj instanceof Integer) { + basic.setAge((Integer) ageObj); + } else { + basic.setAge(0); // 或者设置一个默认值 + } + teacher.basic = basic; + + + // 根据实际属性进行转换 + return teacher; + } + @Override public Map getTeacherByRecord(TeacherDto teacherDto) { final int SLICE_SIZE = 100; @@ -226,6 +372,18 @@ public class TeacherServiceImpl implements TeacherService { return gradeMap; } + public Map getTeacherBase(GpTeacherDto gpTeacherDto, HttpServletRequest request) { + + Map gradeMap; + try { + gradeMap = GroupUtil.getTeacherInfo(gpTeacherDto,new GroupUtil(env), request); + }catch (Exception e) { + throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "数据转换错误"); + + } + return gradeMap; + } + private static @NotNull Map getStringIntegerMap(List subjects, List schoolTeachers) { Map subjectNameMap = new HashMap<>(); for (School.Subject subject : subjects) { diff --git a/src/main/java/cn/teammodel/model/dto/admin/teacher/GpTeacherDto.java b/src/main/java/cn/teammodel/model/dto/admin/teacher/GpTeacherDto.java new file mode 100644 index 0000000..cc64ce6 --- /dev/null +++ b/src/main/java/cn/teammodel/model/dto/admin/teacher/GpTeacherDto.java @@ -0,0 +1,12 @@ +package cn.teammodel.model.dto.admin.teacher; + +import lombok.Data; + +import java.util.List; + +@Data +public class GpTeacherDto { + private String grant_type = "mobiles_infos"; + private String client = "7D340153-3B66-4BAF-8F1E-2EE02F7E1828"; + private List mobiles; +} diff --git a/src/main/java/cn/teammodel/model/entity/school/GpTeacher.java b/src/main/java/cn/teammodel/model/entity/school/GpTeacher.java new file mode 100644 index 0000000..ff09116 --- /dev/null +++ b/src/main/java/cn/teammodel/model/entity/school/GpTeacher.java @@ -0,0 +1,114 @@ +package cn.teammodel.model.entity.school; + +import lombok.Data; + +import java.util.ArrayList; + +@Data +public class GpTeacher { + + public GPBasicInfo basic; + + /// + /// 简介 + /// + public String describe ; + + /// + /// 描述 + /// + public String depict ; + + /// + /// 学段 + /// + public ArrayList periods ; + + /// + /// 科目 + /// + public ArrayList subjects ; + + /// + /// 年级 + /// + public ArrayList grades ; + /// + /// 从业时间 + /// + public Long practiceTime ; + + /// + /// 最高职称 + /// + public String title ; + + /// + /// 学历 + /// + public String degree ; + + public void setBasic(GnericBase basic) { + + } + + + @Data + public static class GPBasicInfo { +/// + /// 昵称 + /// + public String name ; + + /// + /// 真实名称 + /// + public String fullName ; + + /// + /// 头像 + /// + public String picture ; + + /// + /// 国家或地区 + /// + public String region ; + + /// + /// 省份 + /// + public String province ; + + /// + /// 城市 + /// + public String city ; + + /// + /// 区县 + /// + public String dist ; + + /// + /// 性别,男M 女F,保密S + /// + public String gender ; + + /// + /// 年龄 + /// + public Integer age ; + + /// + /// 详细地址 + /// + public String address ; + } + @Data + public static class GnericBase{ + + private String id ; + private String name ; + } +} diff --git a/src/main/java/cn/teammodel/model/vo/admin/GpTeacherVo.java b/src/main/java/cn/teammodel/model/vo/admin/GpTeacherVo.java new file mode 100644 index 0000000..941e401 --- /dev/null +++ b/src/main/java/cn/teammodel/model/vo/admin/GpTeacherVo.java @@ -0,0 +1,10 @@ +package cn.teammodel.model.vo.admin; + +import lombok.Data; + +@Data +public class GpTeacherVo { + public String id ; + public String name ; + public String phone ; +} diff --git a/src/main/java/cn/teammodel/utils/GroupUtil.java b/src/main/java/cn/teammodel/utils/GroupUtil.java index 1bec418..f16d86d 100644 --- a/src/main/java/cn/teammodel/utils/GroupUtil.java +++ b/src/main/java/cn/teammodel/utils/GroupUtil.java @@ -76,4 +76,52 @@ public class GroupUtil { } return mapper; } + + public static Map getTeacherInfo(Object obj, GroupUtil groupUtil, HttpServletRequest request) { + Map mapper = new HashMap<>(); + String evnStr = groupUtil.env.getProperty("spring.env"); + String apiUrl; + if (evnStr != null && evnStr.equalsIgnoreCase("dev")) { + apiUrl = "https://gp.winteach.cn/open_teacher/find"; + + }else { + + apiUrl = "https://gp.winteach.cn/open_teacher/find"; + } + + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + // 创建HttpPost对象 + HttpPost httpPost = new HttpPost(apiUrl); + + //添加请求头 + httpPost.setHeader("Accept", "application/json"); + httpPost.setHeader("Content-Type", "application/json; charset=utf-8"); + + // 设置请求体 + String jsonContent = JsonUtil.convertToJson(obj); + httpPost.setEntity(new StringEntity(jsonContent, ContentType.create("application/json", "UTF-8"))); + + // 发送请求 + try (CloseableHttpResponse response = httpClient.execute(httpPost)) { + // 获取响应实体 + HttpEntity entity = response.getEntity(); + + if (entity != null) { + // 解析响应内容 + String jsonString = EntityUtils.toString(entity); + ObjectMapper objectMapper = new ObjectMapper(); + mapper = objectMapper.readValue(jsonString, TypeFactory.defaultInstance().constructMapType(Map.class, String.class, Object.class)); + } + + // 检查响应状态码 + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != 200) throw new RuntimeException("Failed : HTTP error code : " + statusCode); + } catch (IOException e) { + throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "数据解析异常"); + } + } catch (IOException e) { + throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "请求头异常"); + } + return mapper; + } }