@Override
 public StudentManagerDTO saveStudent(String name, String classId, String schoolId) {
   UserDTO userDTO = new UserDTO();
   userDTO.setRealName(name);
   userDTO.setIdentityId(IdentityIdConstants.STUDENT);
   userDTO.setIdentityDescription("学生");
   userDTO.setSchoolId(schoolId);
   userDTO.setArea(mSchoolMapper.selectByPrimaryKey(schoolId).getArea());
   UserDTO users = userService.saveUser(userDTO);
   MClass mclass = classMapper.selectByPrimaryKey(classId, schoolId);
   StudentDTO studentDTO = new StudentDTO();
   studentDTO.setUserId(users.getUserId());
   studentDTO.setSchoolId(mclass.getSchoolId());
   studentDTO.setSchoolName(mclass.getSchoolName());
   studentDTO.setClassName(mclass.getName());
   studentDTO.setClassId(classId);
   studentDTO.setClassAliasName(mclass.getAliasName());
   studentDTO.setJoinClass(new Date());
   MStudent mStudent = new MStudent();
   BeanUtils.copyProperties(studentDTO, mStudent);
   studentMapper.insert(mStudent);
   MStudentManager studentManager =
       studentMapper.selectStudentByUserId(mStudent.getUserId(), schoolId);
   StudentManagerDTO studentManagerDTO = new StudentManagerDTO();
   BeanUtils.copyProperties(studentManager, studentManagerDTO);
   studentManagerDTO.setClassAliasName(
       ClassNameUtil.getFullName(studentManager.getYears(), studentManager.getClassAliasName()));
   return studentManagerDTO;
 }
 /**
  * 班级添加学生
  *
  * @param userId 学乐号
  * @param classId 班级ID
  */
 @Override
 public int saveStudents(String userId, String classId) {
   String[] userIds = userId.split(",");
   String schoolId = classService.getSchoolId(classId);
   MClass mClass = classMapper.selectByPrimaryKey(classId, schoolId);
   int in = studentMapper.updateStudents(Arrays.asList(userIds), mClass);
   // 学生换班,删除该家长的缓存信息
   deleteRedisOfParent(Arrays.asList(userIds), schoolId);
   return in;
 }
  /** 查询学生信息分页 */
  @Override
  public PageResponse<StudentManagerDTO> queryStudentPage(StudentPageRequest studentPageRequest) {
    String schoolId = studentPageRequest.getSchoolId();
    if (schoolId == null) {
      schoolId = userService.getSchoolId(studentPageRequest.getUserId());
      studentPageRequest.setSchoolId(schoolId);
    }
    // 获取学生信息的总条数
    MStudentManager studentManager = new MStudentManager();
    BeanUtils.copyProperties(studentPageRequest, studentManager);
    long count = studentMapper.selectCount(studentManager);
    // 获取当前页的信息
    Page page = PageUtils.buildPage(studentPageRequest);
    List<MStudentManager> mStudentList =
        studentMapper.selectMStudentManagerPage(page, studentManager);
    /*
    给返回接口赋值
     */
    List<StudentManagerDTO> studentManagerDTOs = new ArrayList<>();
    for (MStudentManager mstudents : mStudentList) {
      StudentManagerDTO studentManagerDTO = new StudentManagerDTO();
      BeanUtils.copyProperties(mstudents, studentManagerDTO);
      studentManagerDTO.setLastLoginDate(
          mstudents.getLastLoginDate() == null
              ? "从未登录"
              : DateFormatUtils.format(mstudents.getLastLoginDate(), "yyyy-MM-dd HH:mm"));
      if (mstudents.getYears() != null) {
        studentManagerDTO.setClassAliasName(
            getGrade(mstudents.getYears()) + studentManagerDTO.getClassAliasName());
      } else {
        studentManagerDTO.setClassAliasName("无班级");
      }
      studentManagerDTOs.add(studentManagerDTO);
    }

    // 返回对象
    PageResponse<StudentManagerDTO> pageResponse = new PageResponse<>();
    PageUtils.buldPageResponse(studentPageRequest, pageResponse);
    pageResponse.setRows(studentManagerDTOs);
    pageResponse.setRecords(count);
    return pageResponse;
  }
 /** 修改学生信息 */
 @Override
 public StudentManagerDTO updateStudentInfo(String userId, String realName, String classId) {
   String schoolId = userService.getSchoolId(userId);
   // 更换用户名称
   usersMapper.updateUserName(userId, realName, schoolId);
   // 更换班级
   MClass mClass = classMapper.selectByPrimaryKey(classId, schoolId);
   studentMapper.updateStudentMessage(userId, mClass);
   // 修改家庭关系表中用户名称
   familyRelationService.updateName(userId, realName);
   MStudentManager studentManager = studentMapper.selectStudentByUserId(userId, schoolId);
   StudentManagerDTO studentManagerDTO = new StudentManagerDTO();
   BeanUtils.copyProperties(studentManager, studentManagerDTO);
   studentManagerDTO.setClassAliasName(
       getGrade(mClass.getYears()) + studentManagerDTO.getClassAliasName());
   // 修改孩子名称,班级信息,删除缓存数据
   deleteRedisOfParent(userId, schoolId);
   cacheService.delete(MessageFormat.format(CacheConstants.KEY_USER, userId));
   return studentManagerDTO;
 }
 /** 查询无班级学生信息 */
 @Override
 public List<StudentManagerDTO> queryStudentsWithoutClassId(String schoolId) {
   List<MStudentManager> usersList = studentMapper.selectStudentsWithoutClassId(null, schoolId);
   List<StudentManagerDTO> userDTOList = new ArrayList<>();
   for (MStudentManager user : usersList) {
     StudentManagerDTO userDTO = new StudentManagerDTO();
     BeanUtils.copyProperties(user, userDTO);
     userDTOList.add(userDTO);
   }
   return userDTOList;
 }
 /** APP接口、WEB平台:根据班级ID查询该班级所有学生 */
 @Override
 public List<StudentManagerDTO> queryClassStudents(String classId) {
   String schoolId = classService.getSchoolId(classId);
   List<MStudentManager> userList = studentMapper.selectClassStudents(classId, schoolId);
   List<StudentManagerDTO> userDTOList = new ArrayList<>();
   for (MStudentManager u : userList) {
     StudentManagerDTO userDTO = new StudentManagerDTO();
     BeanUtils.copyProperties(u, userDTO);
     userDTOList.add(userDTO);
   }
   return userDTOList;
 }
 /** 导出学生信息 */
 @Override
 public List<ExcelInfo> queryExcelInfo(StudentPageRequest studentPageRequest) {
   Map<Integer, ExcelInfo> map = new TreeMap<>();
   String schoolId = studentPageRequest.getSchoolId();
   if (schoolId == null) {
     schoolId = userService.getSchoolId(studentPageRequest.getUserId());
     studentPageRequest.setSchoolId(schoolId);
   }
   // 获取学生信息的总条数
   MStudentManager studentManager = new MStudentManager();
   BeanUtils.copyProperties(studentPageRequest, studentManager);
   List<MStudentManager> mStudentList = studentMapper.selectStudents_export(studentManager);
   for (MStudentManager students : mStudentList) {
     List<List<Object>> data;
     List<Object> row = new ArrayList<>();
     row.add(students.getRealName());
     if (students.getYears() != null) {
       int grade = ClassNameUtil.getGradeNum(students.getYears());
       ExcelInfo excelInfo = map.get(grade);
       if (excelInfo == null) {
         excelInfo = new ExcelInfo();
         data = new ArrayList<>();
         excelInfo.setData(data);
         excelInfo.setSheetName(grade + "年级");
         excelInfo.setHeaders(new String[] {"学生姓名", "所在班级", "登录账号", "上线情况"});
         excelInfo.setColumnWidth(20);
         map.put(grade, excelInfo);
       } else {
         data = excelInfo.getData();
       }
       row.add(grade + "年级" + students.getClassAliasName());
     } else {
       ExcelInfo excelInfo = map.get(0);
       if (excelInfo == null) {
         excelInfo = new ExcelInfo();
         data = new ArrayList<>();
         excelInfo.setData(data);
         excelInfo.setSheetName("无班级");
         excelInfo.setColumnWidth(20);
         excelInfo.setHeaders(new String[] {"学生姓名", "所在班级", "登录账号", "上线情况"});
         map.put(0, excelInfo);
       } else {
         data = excelInfo.getData();
       }
       row.add("无班级");
     }
     row.add(students.getUserId());
     row.add(DateUtil.getLogin_date(students.getLastLoginDate()));
     data.add(row);
   }
   return new ArrayList<>(map.values());
 }
 /** 根据学生ID获取学生信息 */
 @Override
 public MyMessageDTO getMyMessageStudent(String userId) {
   String schoolId = userService.getSchoolId(userId);
   MStudentInfo studentInfo = studentMapper.getStudentInfo(userId, schoolId);
   if (studentInfo == null) {
     throw new MemberException("该学生不存在");
   }
   MyMessageDTO studentInfoDTO = new MyMessageDTO();
   BeanUtils.copyProperties(studentInfo, studentInfoDTO);
   if (studentInfo.getClassId() != null) {
     studentInfoDTO.setClassName(
         ClassNameUtil.getFullName(studentInfo.getYear(), studentInfo.getClassName()));
   }
   return studentInfoDTO;
 }
 /** APP接口、WEB平台:根据学生名称查询学生信息 */
 @Override
 public List<StudentManagerDTO> queryStudentByRealName(String realName, String userId) {
   if (StringUtils.isEmpty(userId)) {
     logger.debug("userId 不能为空");
     return null;
   }
   String schoolId = userService.getSchoolId(userId);
   List<MStudentManager> userList = studentMapper.selectStudentByRealName(realName, schoolId);
   List<StudentManagerDTO> userDTOList = new ArrayList<>();
   for (MStudentManager u : userList) {
     StudentManagerDTO userDTO = new StudentManagerDTO();
     BeanUtils.copyProperties(u, userDTO);
     userDTOList.add(userDTO);
   }
   return userDTOList;
 }
 /** APP接口:根据学生ID获取该学生所在的班级信息 */
 @Override
 public ClassInfoDTO queryUserClass(String userId) {
   String schoolId = userService.getSchoolId(userId);
   MClassInfo studentClass = studentMapper.getStudentClass(userId, schoolId);
   ClassInfoDTO tcdto = new ClassInfoDTO();
   if (studentClass != null) {
     BeanUtils.copyProperties(studentClass, tcdto);
     // 设置年级号
     tcdto.setGradeNum(ClassNameUtil.getGradeNum(studentClass.getYear()));
     // 设置学期
     tcdto.setSemester(ClassNameUtil.getSemester());
     // 设置班级名称 年级+班级
     tcdto.setClassName(
         ClassNameUtil.getFullName(studentClass.getYear(), studentClass.getClassName()));
   }
   return tcdto;
 }
  @Override
  public StudentDTO queryStudentForAPP(String userId, String schoolId) {
    UserDTO mUsers = userService.getByUserId(userId);
    if (mUsers == null) {
      logger.debug("未找到学乐号为" + userId + "的学生用户");
      return null;
    }

    MStudent mStudent = studentMapper.selectByPrimaryKey(userId, schoolId);
    if (mStudent == null) {
      logger.debug("未找到学生id为" + userId + "的学生");
      throw new MemberException("未找到学生id为" + userId + "的学生");
    }
    // 学生信息
    StudentDTO studentDTO = new StudentDTO();
    studentDTO.setUserId(userId);
    studentDTO.setRealName(mUsers.getRealName());
    studentDTO.setSchoolId(mStudent.getSchoolId());
    studentDTO.setSchoolName(mStudent.getSchoolName());
    studentDTO.setClassId(mStudent.getClassId());
    studentDTO.setClassName(mStudent.getClassName());
    studentDTO.setMobile(mUsers.getMobile());
    studentDTO.setEmail(mUsers.getEmail());

    // 学校所在区域信息
    MSchool mSchool = mSchoolMapper.selectByPrimaryKey(mUsers.getSchoolId());
    if (mSchool == null) {
      logger.debug("未找到学乐号为" + userId + "的学生用户所在的学校");
      return null;
    }
    studentDTO.setAreaCode(mSchool.getArea());
    studentDTO.setAreaName(mSchool.getAreaName());

    // 班级信息
    MClass mClass = classMapper.selectByPrimaryKey(mStudent.getClassId(), schoolId);
    if (mClass == null) {
      logger.debug("未找到学乐号为" + userId + "的学生用户所在的班级");
      return null;
    }
    studentDTO.setGradeNum(getGradeNum(mClass.getYears()));
    studentDTO.setYear(mClass.getYears());
    return studentDTO;
  }
  @Override
  public StudentDTO updateStudentForAPP(StudentDTO studentDTO) {
    // 更新联系方式
    UserDTO userDTO = new UserDTO();
    boolean isAllNot = true;
    // 学生姓名
    String name = studentDTO.getRealName();
    if (!StringUtils.isEmpty(name)) {
      userDTO.setRealName(name);
      isAllNot = false;
    }

    String school = studentDTO.getSchoolId();
    if (school != null) {
      userDTO.setSchoolId(school);
      isAllNot = false;
    } else {
      logger.warn("schoolId不能为空");
      return null;
    }

    String userId = studentDTO.getUserId();
    if (!StringUtils.isEmpty(userId)) {
      userDTO.setUserId(userId);
    } else {
      logger.warn("userId不能为空");
      return null;
    }

    String mobile = studentDTO.getMobile();
    if (!StringUtils.isEmpty(mobile)) {
      userDTO.setMobile(mobile);
      isAllNot = false;
    }

    String email = studentDTO.getEmail();
    if (!StringUtils.isEmpty(email)) {
      userDTO.setEmail(email);
      isAllNot = false;
    }

    if (!isAllNot) {
      userService.updateByUserId(userDTO);
    }

    // 更新班级
    String schoolId = userService.getSchoolId(userId);
    if (StringUtils.isEmpty(schoolId)) {
      throw new MemberException("该学生没有学校");
    }

    MSchool mSchool = mSchoolMapper.selectByPrimaryKey(schoolId);

    String classId = studentDTO.getClassId();
    if (!StringUtils.isEmpty(classId)) {
      MClass mClass = classMapper.selectByPrimaryKey(classId, schoolId);
      if (mClass == null) {
        logger.warn("找不到班级号为" + classId + "的班级");
        return null;
      }
      MStudent mStudent = new MStudent();
      mStudent.setUserId(studentDTO.getUserId());
      mStudent.setClassName(mClass.getName());
      mStudent.setClassId(classId);
      mStudent.setSchoolId(schoolId);

      studentMapper.updateByPrimaryKey(mStudent);
    }

    studentDTO.setClassId(classId);
    studentDTO.setSchoolId(schoolId);
    studentDTO.setSchoolName(mSchool.getName());
    return studentDTO;
  }