package com.iflytek.edu.portalcenter.web.controller;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.iflytek.edu.elp.common.dto.json.StandardJSONResult;
import com.iflytek.edu.elp.common.dto.page.PageInfo;
import com.iflytek.edu.elp.common.dto.page.Pager;
import com.iflytek.edu.elp.common.exception.ELPBizException;
import com.iflytek.edu.elp.common.util.DateUtils;
import com.iflytek.edu.elp.common.util.JSONUtils;
import com.iflytek.edu.elp.user.domain.Clazz;
import com.iflytek.edu.elp.user.domain.Grade;
import com.iflytek.edu.elp.user.domain.Role;
import com.iflytek.edu.elp.user.domain.School;
import com.iflytek.edu.elp.user.domain.SchoolAdministrator;
import com.iflytek.edu.elp.user.domain.Subject;
import com.iflytek.edu.elp.user.domain.User;
import com.iflytek.edu.elp.user.domain.UserExpand;
import com.iflytek.edu.elp.user.dto.ClassSubjectDTO;
import com.iflytek.edu.elp.user.dto.TeacherInfoDTO;
import com.iflytek.edu.elp.user.dto.TeacherRegDTO;
import com.iflytek.edu.elp.user.dto.UserDTO;
import com.iflytek.edu.elp.user.dto.UserInfoDTO;
import com.iflytek.edu.elp.user.service.ClazzService;
import com.iflytek.edu.elp.user.service.CommonService;
import com.iflytek.edu.elp.user.service.ParentService;
import com.iflytek.edu.elp.user.service.RoleService;
import com.iflytek.edu.elp.user.service.SchoolService;
import com.iflytek.edu.elp.user.service.UserExpandService;
import com.iflytek.edu.elp.user.service.UserRegService;
import com.iflytek.edu.elp.user.service.UserService;
import com.iflytek.edu.elp.user.service.impl.RoleServiceImpl;
import com.iflytek.edu.elp.user.service.impl.UserRegServiceImpl;
import com.iflytek.edu.elp.user.util.UserUtil;
import com.iflytek.edu.portalcenter.web.dto.ClassInfoDTO;
import com.iflytek.edu.portalcenter.web.dto.ClassSubject;
import com.iflytek.edu.portalcenter.web.dto.ClazzTeachingDTO;
import com.iflytek.edu.portalcenter.web.dto.MemberType;
import com.iflytek.edu.portalcenter.web.dto.ParentInfoDTO;
import com.iflytek.edu.portalcenter.web.dto.TeacherClassDTO;
import com.iflytek.edu.portalcenter.web.dto.TeacherDTO;
import com.iflytek.edu.portalcenter.web.util.WebUtil;
import com.iflytek.edu.zx.bizservice.bizuser.agent.UserAccount;
import com.iflytek.edu.zx.bizservice.bizuser.service.BizUserInfoService;
/**
* 教师管理Controller
* @author hanzhang
*
*/
@Controller
public class TeacherManageController {
/**
* 用户服务
*/
@Resource
private UserService userService;
/**
* 用户注册服务
*/
@Resource
private UserRegService userRegService;
/**
* 角色服务
*/
@Resource
private RoleService roleService;
/**
* 用户扩展信息
*/
@Resource
private UserExpandService userExpandService;
/**
* 班级服务
*/
@Resource
private ClazzService clazzService;
/**
* 学校服务
*/
@Resource
private SchoolService schoolService;
/**
* 公共服务
*/
@Resource
private CommonService commonService;
/**
* 家长服务
*/
@Resource
private ParentService parentService;
/**
* 会员信息
*/
@Resource
private BizUserInfoService bizUserInfoService;
/**
* 重试时间
*/
private static final int RETRY_TIME = 2 * 1000;
/**
* 教师管理页面
* @param model 模型
* @param subjectCode 学科编码
* @param key 关键字
* @param index 页码
* @param size 分页大小
* @param response response
* @return 教师管理页面
*/
@RequestMapping("/teacher/teamanage/main/")
public String teacherManage(ModelMap model, String subjectCode, String key, String index, String size, HttpServletResponse response) {
String schoolId = getAdminSchoolId(response);
String userId = WebUtil.getCurrentUser(response).getId();
if(StringUtils.isEmpty(schoolId)) {
return "redirect:/home/index/";
}
School school = userExpandService.getLocalSchool(WebUtil.getCurrentUser(response).getId());
SchoolAdministrator administor = userRegService.getSchoolAdministrator(userId, school.getSchoolId());
List<Grade> gradeList = commonService.getGrades(school.getPhase(), administor.getEduSystemCode(),false);
model.addAttribute("gradeList",JSONUtils.toJSONString(gradeList));
model.addAttribute("schoolInfo", JSONUtils.toJSONString(school));
return "/teacher/teamanage/main/index";
}
/**
* 获取教师列表
* @param key 关键字
* @param index 页码
* @param size 分页大小
* @param response response
* @return 教师列表
*/
@RequestMapping("/teacher/teamanage/main/getTeacherList")
@ResponseBody
public String getTeacherList(String key, String index, String size, HttpServletResponse response) {
String schoolId = getAdminSchoolId(response);
if(StringUtils.isEmpty(schoolId)) {
return "redirect:/home/index/";
}
int pageIndex = (StringUtils.isNumeric(index))? Integer.parseInt(index)-1 : 0;
int pageSize = (StringUtils.isNumeric(size))? Integer.parseInt(size) : 50;
Pager<TeacherClassDTO> teacherList = sortSchoolTeacher(schoolId, schoolService.retrieveTeacherList(schoolId, key, pageIndex, pageSize), (pageIndex == 0), StringUtils.isNotEmpty(key));
Map<String, Object> teachers = new HashMap<String, Object>();
teachers.put("pageInfo", new PageInfo(teacherList.getTotalCount(), pageIndex + 1, pageSize));
teachers.put("teacherList", teacherList.getList());
return JSONUtils.toJSONString(teachers);
}
/**
* 获取教师学科班级列表
* @param schoolId 学校Id
* @param gradeCode 年级编码
* @param subjectCode 学科编码
* @param teacherId 教师
* @param response response
* @return 班级列表JSON
*/
@RequestMapping("/teacher/teamanage/main/getClassList")
@ResponseBody
public String getClassList(String schoolId, String gradeCode, String subjectCode, String teacherId, HttpServletResponse response) {
Assert.notNull(subjectCode, "subjectCode is required");
Assert.notNull(teacherId, "teacherId is required");
String sId = schoolId;
if(StringUtils.isEmpty(sId)) {
sId = getAdminSchoolId(response);
}
List<Clazz> clazzList = clazzService.getClazzsBySchoolIdAndGrade(gradeCode, sId);
List<ClazzTeachingDTO> clazzTeachingList = new ArrayList<ClazzTeachingDTO>();
for(Clazz clazz : clazzList) {
ClazzTeachingDTO clazzTeaching = new ClazzTeachingDTO();
clazzTeaching.setId(clazz.getId());
clazzTeaching.setName(clazz.getName());
/**
* 判断班级学科是否由老师代课
*/
List<UserDTO> teachers = userService.getTeachersByClassListSubject(new String[]{clazz.getId()}, subjectCode);
for(UserDTO teacher : teachers) {
clazzTeaching.setHasSubjectTeacher(Boolean.TRUE);
if(teacher.getId().equals(teacherId)) {
clazzTeaching.setIsTeaching(Boolean.TRUE);
break;
}
}
User headteacher = clazzService.getHeadteacher(clazz.getId());
if(null != headteacher) {
clazzTeaching.setHasHeadteacher(Boolean.TRUE);
if(headteacher.getId().equals(teacherId)) {
clazzTeaching.setIsHeadteacher(Boolean.TRUE);
}
}
clazzTeachingList.add(clazzTeaching);
}
return JSONUtils.toJSONString(clazzTeachingList);
}
/**
* 批量添加老师
* @param list 教师列表
* @param response response
* @return 成功失败标志
*/
@RequestMapping("/teacher/teamanage/main/createTeacher")
@ResponseBody
public String createTeacher(String list, HttpServletResponse response) {
Assert.notNull(list, "list is required");
String schoolId = getAdminSchoolId(response);
if(StringUtils.isEmpty(schoolId)) {
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance("该用户无学校管理权限!"));
}
List<TeacherRegDTO> teachers = JSONUtils.parseList(list, TeacherRegDTO.class);
List<TeacherRegDTO> failedTeachers = userRegService.createTeachers(schoolId, WebUtil.getCurrentUser(response).getId(), teachers);
if(CollectionUtils.isEmpty(failedTeachers)) {
return JSONUtils.toJSONString(StandardJSONResult.getSuccessInstance());
} else {
try {
Thread.sleep(RETRY_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<TeacherRegDTO> result = userRegService.createTeachers(schoolId, WebUtil.getCurrentUser(response).getId(), teachers);
if(CollectionUtils.isEmpty(result)) {
return JSONUtils.toJSONString(StandardJSONResult.getSuccessInstance());
}else{
List<TeacherRegDTO> filterTeachers = new ArrayList<TeacherRegDTO>();
for (TeacherRegDTO teacher : result){
if(teacher.getMobile() == null){
throw new ELPBizException("教师添加异常!");
}
User user = userService.getUserByMobile(teacher.getMobile());
if(user == null){
filterTeachers.add(teacher);
}else{
List<ClassSubjectDTO> classSubjects = userService.getClassSubjectsByTeacherId(user.getId());
if(classSubjects.size() == 0){
List<String> usersList = new ArrayList<String>();
usersList.add(user.getId());
UserInfoDTO userInfoDTO = new UserInfoDTO();
userInfoDTO.setId(user.getId());
userInfoDTO.setLoginName(user.getLoginName());
userInfoDTO.setName(teacher.getName());
userInfoDTO.setMobile(user.getMobile());
userService.updateUser(userInfoDTO);
userService.setUserToSchool(usersList,schoolId,Role.teacher.toString());
}else{
filterTeachers.add(teacher);
}
}
}
if(filterTeachers.size() == 0){
return JSONUtils.toJSONString(StandardJSONResult.getSuccessInstance());
}else{
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance(JSONUtils.toJSONString(filterTeachers)));
}
}
}
}
/**
* 批量删除老师(含校长)
* @param teacherIds 教师Id(以|分隔)
* @param response response
* @return 成功失败标志
*/
@RequestMapping("/teacher/teamanage/main/deleteTeacher")
@ResponseBody
public String deleteTeacher(String teacherIds, HttpServletResponse response) {
Assert.notNull(teacherIds, "teacherIds is required");
String schoolId = getAdminSchoolId(response);
if(StringUtils.isEmpty(schoolId)) {
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance("该用户无学校管理权限!"));
}
schoolService.removeTeachersFromSchool(schoolId, StringUtils.split(teacherIds, "|"),true);
return JSONUtils.toJSONString(StandardJSONResult.getSuccessInstance());
}
/**
* 分配班级
* @param teacherId 教师Id
* @param joinClazzIds 加入班级Id
* @param quitClazzIds 退出班级Id
* @param subjectCode 学科
* @param changeHeadmaster 是否变更校长
* @param response response
* @return 成功失败标志
*/
@RequestMapping("/teacher/teamanage/main/allotClass")
@ResponseBody
public String allotClass(String teacherId, String joinClazzIds, String quitClazzIds,
String subjectCode, String changeHeadmaster, HttpServletResponse response) {
Assert.notNull(teacherId, "teacherId is required");
String errorMsg = "";
String schoolId = getAdminSchoolId(response);
if(StringUtils.isEmpty(schoolId)) {
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance("该用户无学校管理权限!"));
}
// 设置教师班级
if(StringUtils.isNotEmpty(joinClazzIds)) {
errorMsg += this.setTeacherClass(teacherId, StringUtils.split(joinClazzIds, "|"), subjectCode);
}
// 移除教师班级
if(StringUtils.isNotEmpty(quitClazzIds)) {
errorMsg += this.removeTeacherClass(teacherId, StringUtils.split(quitClazzIds, "|"), subjectCode);
}
// 更变校长
if(StringUtils.isNotEmpty(changeHeadmaster) && "true".equals(changeHeadmaster)) {
errorMsg += this.changeHeadmaster(teacherId, schoolId);
}
if(StringUtils.isNotEmpty(errorMsg)) {
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance(errorMsg));
}
return JSONUtils.toJSONString(StandardJSONResult.getSuccessInstance());
}
/**
* 批量设置教师班级
*
* @param teacherId 教师Id
* @param clazzIds 班级列表
* @param subjectCode 学科编码
* @return 错误日志
*/
private String setTeacherClass(String teacherId, String[] clazzIds, String subjectCode) {
String errorMsg = "";
for(String joinClazzId : clazzIds) {
try{
clazzService.joinClazz(teacherId, joinClazzId, Role.teacher);
userService.setTeacherSubject(teacherId, joinClazzId, subjectCode);
} catch(Exception e) {
errorMsg += "教师加入班级[" + joinClazzId + "]失败:" + e.getMessage() + ";";
continue;
}
}
return errorMsg;
}
/**
* 批量移除教师班级
*
* @param teacherId 教师Id
* @param clazzIds 班级列表
* @param subjectCode 学科编码
* @return 错误日志
*/
private String removeTeacherClass(String teacherId, String[] clazzIds, String subjectCode) {
String errorMsg = "";
for(String quitClazzId : clazzIds) {
try{
clazzService.quitClazz(teacherId, quitClazzId, Role.teacher.toString(), subjectCode,true);
} catch(Exception e) {
errorMsg += "教师退出班级[" + quitClazzId + "]失败:" + e.getMessage() + ";";
continue;
}
}
return errorMsg;
}
/**
* 设置/移除校长
*
* @param teacherId 教师id
* @param schoolId 学校id
* @return 错误日志
*/
private String changeHeadmaster(String teacherId, String schoolId) {
List<User> headmasters = schoolService.getHeadmaster(schoolId);
try {
for(User headmaster : headmasters) {
if(headmaster.getId().equals(teacherId)) {
roleService.removeHeadmaster(teacherId, schoolId);
return "";
}
}
roleService.setHeadmaster(teacherId, schoolId);
} catch (Exception e) {
return e.getMessage();
}
return "";
}
/**
* 修改班主任
* @param teacherId 教师Id
* @param setHtClsIds 设置班主任班级Id
* @param removeHtClsIds 删除班主任班级Id
* @param changeHeadmaster 是否变更校长
* @param response response
* @return 成功失败标志
*/
@RequestMapping("/teacher/teamanage/main/changeHeadteacher")
@ResponseBody
public String changeHeadteacher(String teacherId, String setHtClsIds, String removeHtClsIds,
String changeHeadmaster, HttpServletResponse response) {
Assert.notNull(teacherId, "teacherId is required");
String schoolId = getAdminSchoolId(response);
if(StringUtils.isEmpty(schoolId)) {
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance("该用户无学校管理权限!"));
}
String errorMsg = "";
/**
* 添加班主任
*/
if(StringUtils.isNotEmpty(setHtClsIds)) {
errorMsg += this.setHeadteacher(teacherId, StringUtils.split(setHtClsIds, "|"));
}
/**
* 删除班主任
*/
if(StringUtils.isNotEmpty(removeHtClsIds)) {
errorMsg += this.removeHeadteacher(teacherId, StringUtils.split(removeHtClsIds, "|"));
}
/**
* 变更校长
*/
if(StringUtils.isNotEmpty(changeHeadmaster) && "true".equals(changeHeadmaster)) {
errorMsg += this.changeHeadmaster(teacherId, schoolId);
}
if(StringUtils.isNotEmpty(errorMsg)) {
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance("设置班主任出现错误:" + errorMsg));
}
return JSONUtils.toJSONString(StandardJSONResult.getSuccessInstance());
}
/**
* 设置班主任
*
* @param teacherId 教师id
* @param clazzIds 班级id
* @return 错误日志
*/
private String setHeadteacher(String teacherId, String[] clazzIds) {
String errorMsg = "";
roleService.initHeadteacherRole();
for(String setHtClsId : clazzIds) {
User headteacher = clazzService.getHeadteacher(setHtClsId);
if(null != headteacher && !headteacher.getId().equals(teacherId)) {
errorMsg += "班级[" + setHtClsId + "]已设置班主任,请先撤销班主任!";
continue;
}
if(null == headteacher) {
roleService.setHeadteacher(teacherId, setHtClsId);
}
}
return errorMsg;
}
/**
* 撤销班主任
*
* @param teacherId 教师id
* @param clazzIds 班级id
* @return 错误日志
*/
private String removeHeadteacher(String teacherId, String[] clazzIds) {
String errorMsg = "";
for(String removeHtClsId : clazzIds) {
User headteacher = clazzService.getHeadteacher(removeHtClsId);
if(null != headteacher && headteacher.getId().equals(teacherId)) {
roleService.removeHeadteacher(teacherId, removeHtClsId);
} else {
errorMsg += "该教师不是班级[" + removeHtClsId + "]的班主任。";
}
}
return errorMsg;
}
/**
* 重置教师密码
* @param teacherId 教师Id
* @param response response
* @return 成功失败标志
*/
@RequestMapping("/teacher/teamanage/main/resetPassword")
@ResponseBody
public String resetTeacherPassword(String teacherId, HttpServletResponse response) {
String schoolId = getAdminSchoolId(response);
if(StringUtils.isEmpty(schoolId)) {
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance("该用户无学校管理权限!"));
}
User user = userService.getUserByUserId(teacherId);
if(null == user) {
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance("该教师用户不存在!"));
}
School school = clazzService.getSchoolByUserId(teacherId);
if(null == school || StringUtils.isEmpty(schoolId) || !schoolId.equals(school.getSchoolId())) {
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance("没有修改该用户密码的权限!"));
}
userService.resetPassword(user.getLoginName(), UserRegServiceImpl.DEFAULT_PWD);
return JSONUtils.toJSONString(StandardJSONResult.getSuccessInstance());
}
/**
* 删除管理员
*
* @param schoolId 学校Id
* @return 成功失败标志
*/
@RequestMapping("/teacher/teamanage/main/removeAdmin")
@ResponseBody
public String removeAdministrator(String schoolId) {
try{
List<User> admins = schoolService.getAdministrator(schoolId);
for(User admin : admins) {
roleService.removeAdministrator(admin.getId(), schoolId);
}
} catch(Exception e) {
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance(e.getMessage()));
}
return JSONUtils.toJSONString(StandardJSONResult.getSuccessInstance());
}
/**
* 删除所有学校管理员
*
* @param schoolId 学校Id
* @return 成功失败标志
*/
@RequestMapping("/teacher/teamanage/main/removeAllAdmin")
@ResponseBody
public String removeAllAdministrator(String schoolId) {
roleService.removeAllSchoolManager(schoolId);
return JSONUtils.toJSONString(StandardJSONResult.getSuccessInstance());
}
/**
* 下载班级学生列表
*
* @param request request
* @param subjectCode 学科编码
* @param response 返回
* @throws Exception 异常
*/
@RequestMapping("/teacher/teamanage/main/downloadTeacherList/")
public void downloadTeacherList(HttpServletRequest request, String subjectCode, HttpServletResponse response) throws Exception {
response.setContentType("application/octet-stream;charset=UTF-8");
String schoolId = getAdminSchoolId(response);
if(StringUtils.isNotEmpty(schoolId)) {
School school = schoolService.getShoolById(schoolId);
if(null != school){
String fileName = school.getSchoolName() + "教师名单.xls";
String agent = request.getHeader("user-agent");
if (agent != null && agent.toLowerCase().indexOf("msie") != -1) {
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName.replace(" ", ""), "utf-8"));
} else {
response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("utf-8"), "iso-8859-1") + "\"");
}
schoolService.downloadTeacherList(schoolId, subjectCode, response.getOutputStream());
response.getOutputStream().flush();
}
}
}
/**
* 排序学校老师列表
*
* @param schoolId 学校Id
* @param oldList 排序前列表
* @param isFirst 是否首页
* @param isSearch 是否查询
* @return newList 排序后列表
*/
private Pager<TeacherClassDTO> sortSchoolTeacher(String schoolId, Pager<TeacherInfoDTO> oldList, Boolean isFirst, Boolean isSearch) {
List<TeacherClassDTO> newList = new ArrayList<TeacherClassDTO>();
List<User> headmasters = schoolService.getHeadmaster(schoolId);
if(isFirst && !isSearch){
for(User headmaster : headmasters) {
TeacherClassDTO headmasterDTO = new TeacherClassDTO();
headmasterDTO.setId(headmaster.getId());
headmasterDTO.setLoginName(headmaster.getLoginName());
headmasterDTO.setMobile(headmaster.getMobile());
headmasterDTO.setName(headmaster.getName());
headmasterDTO.setPassword(headmaster.getPassword());
headmasterDTO.setIsHeadMaster(Boolean.TRUE);
newList.add(headmasterDTO);
}
}
List<User> admins = schoolService.getAdministrator(schoolId);
for(TeacherInfoDTO teacher : oldList.getList()) {
TeacherClassDTO teacherClass = new TeacherClassDTO();
teacherClass.setId(teacher.getId());
teacherClass.setLoginName(teacher.getLoginName());
teacherClass.setMobile(teacher.getMobile());
teacherClass.setName(teacher.getName());
teacherClass.setPassword(teacher.getPassword());
if(isInUserList(teacher.getId(), headmasters)) {
teacherClass.setIsHeadMaster(Boolean.TRUE);
if(!isSearch) {
continue;
}
} else if(isInUserList(teacher.getId(), admins)) {
continue;
}
newList.add(teacherClass);
}
return new Pager<TeacherClassDTO>(oldList.getTotalCount(), newList);
}
/**
* 是否包含在用户列表中
*
* @param userId 用户Id
* @param users 用户列表
* @return 是否包含结果
*/
private Boolean isInUserList(String userId, List<User> users) {
if(CollectionUtils.isNotEmpty(users)) {
for(User user : users) {
if(user.getId().equals(userId)) {
return Boolean.TRUE;
}
}
}
return Boolean.FALSE;
}
/**
* 获取教师学科
* @param teacherId 教师Id
* @param response response
* @return 教师列表
*/
@RequestMapping("/teacher/teamanage/main/getTeacherSubject/")
@ResponseBody
public String getTeacherSubject(String teacherId, HttpServletResponse response) {
String schoolId = getAdminSchoolId(response);
if(StringUtils.isEmpty(schoolId)) {
return "redirect:/home/index/";
}
List<Clazz> clazzs = clazzService.getClassesByTeacherId(teacherId);
List<ClassSubject> classSubjects = new ArrayList<ClassSubject>();
List<String> subjectList = new ArrayList<String>();
/**
* 班级学科
*/
for(Clazz clazz : clazzs) {
ClassSubject classSubject = new ClassSubject();
classSubject.setClassId(clazz.getId());
classSubject.setClassName(clazz.getName());
List<String> subjectCodes = userService.getTeacherSubjects(clazz.getId(), teacherId);
List<Subject> subjects = new ArrayList<Subject>();
if(CollectionUtils.isNotEmpty(subjectCodes)) {
for(String subjectCode : subjectCodes) {
if(!RoleServiceImpl.OTHER_SUBJECT_CODE.equals(subjectCode)) {
Subject subject = new Subject();
subject.setCode(subjectCode);
subject.setName(clazzService.getSubjectName(subjectCode));
subjects.add(subject);
subjectList.add(subject.getName());
} else {
if(roleService.isHeadteacher(teacherId, clazz.getId())) {
Subject subject = new Subject();
subject.setCode("");
subject.setName("班主任");
subjects.add(0, subject);
}
}
}
}
if(CollectionUtils.isNotEmpty(subjects)) {
classSubject.setSubjects(subjects);
classSubjects.add(classSubject);
}
}
UserUtil.removeDuplicate(subjectList);
Map<String, Object> teachers = new HashMap<String, Object>();
teachers.put("clazzs", classSubjects);
teachers.put("subjects",subjectList);
return JSONUtils.toJSONString(teachers);
}
/**
* 修改用户姓名
* @param userId 用户Id
* @param userName 用户名
* @param response response
* @return 成功失败标志
*/
@RequestMapping("/teacher/teamanage/modifyUserName/")
@ResponseBody
public String modifyUserName(String userId, String userName, HttpServletResponse response) {
if(StringUtils.isEmpty(userName)) {
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance("请输入用户姓名"));
}
User user = userService.getUserByUserId(userId);
if(null == user) {
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance("用户不存在!"));
}
try{
UserInfoDTO userInfoDTO = new UserInfoDTO();
userInfoDTO.setId(user.getId());
userInfoDTO.setLoginName(user.getLoginName());
userInfoDTO.setName(userName);
userService.updateUser(userInfoDTO);
} catch(Exception e) {
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance(e.getMessage()));
}
return JSONUtils.toJSONString(StandardJSONResult.getSuccessInstance());
}
/**
* 根据学校id获取所有老师
* @param schoolId 学校id
* @return 教师list
*/
@RequestMapping("/teacher/teamanage/getAllTByS/")
@ResponseBody
public String getAllTByS(String schoolId){
List<TeacherInfoDTO> teacherList = new ArrayList<TeacherInfoDTO>();
try{
teacherList = schoolService.getTeacherListbySchool(schoolId, null);
}catch (Exception e) {
// TODO: handle exception
return JSONUtils.toJSONString(null);
}
return JSONUtils.toJSONString(teacherList);
}
/**
* 获取某年级下的所有班级
* @param gradeCode 年级code
* @param schoolId 学校id
* @return 班级
*/
@RequestMapping("/teacher/teamanage/getCByG/")
@ResponseBody
public String getClazzsByGrade(String gradeCode,String schoolId){
List<Clazz> clazzs = clazzService.getClazzsBySchoolIdAndGrade(gradeCode, schoolId);
return JSONUtils.toJSONString(clazzs);
}
/**
* 获取班主任
* @param classId 班级id
* @return 班主任
*/
@RequestMapping("/teacher/teamanage/getHeadTeacher/")
@ResponseBody
public String getHeadTeacher(String classId){
User user = clazzService.getHeadteacher(classId);
return JSONUtils.toJSONString(user);
}
/**
* 根据学科获取任课老师
* @param classId 班级id
* @param subjectCode 学科code
* @return 任课老师
*/
@RequestMapping("/teacher/teamanage/getNormalTeacher/")
@ResponseBody
public String getNormalTeacher(String classId,String subjectCode){
List<UserDTO> userList = new ArrayList<UserDTO>();
try{
userList = userService.getTeachersByClassListSubject(new String[]{classId}, subjectCode);
}catch (Exception e) {
// TODO: handle exception
return JSONUtils.toJSONString(null);
}
return JSONUtils.toJSONString(userList.size()==0?null:userList.get(0));
}
/**
* 班级移除已分配老师
* @param classId 班级id
* @param userId 老师id
* @param subjectCode 学科code
* @return 移除状态
*/
@RequestMapping("/teacher/teamanage/removeTeacher/")
@ResponseBody
public String removeTeacher(String classId,String userId,String subjectCode){
try{
if(StringUtils.isNotEmpty(subjectCode)){
clazzService.quitClazz(userId, classId, Role.teacher.toString(), subjectCode,true);
}else{
roleService.removeHeadteacher(userId,classId);
}
}catch (Exception e) {
// TODO: handle exception
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance(e.getMessage()));
}
return JSONUtils.toJSONString(StandardJSONResult.getSuccessInstance());
}
/**
* 根据姓名/登录名/手机号/搜索教师
* @param schoolId 学校id
* @param name 姓名
* @param loginName 登录名
* @param phone 手机号
* @return 教师
*/
@RequestMapping("/teacher/teamanage/searchTByN/")
@ResponseBody
public String searchTByN(String schoolId,String name,String loginName,String phone){
List<TeacherInfoDTO> resultList = new ArrayList<TeacherInfoDTO>();
try{
List<TeacherInfoDTO> teacherList = schoolService.getTeacherListbySchool(schoolId, null);
int size = teacherList.size();
for(int i=0; i<size; i++){
TeacherInfoDTO teacher = new TeacherInfoDTO();
if(StringUtils.isNotEmpty(name)){
if(teacherList.get(i).getName().equals(name)){
teacher = teacherList.get(i);
resultList.add(teacher);
}
}else if(StringUtils.isNotEmpty(loginName)){
if(teacherList.get(i).getLoginName().equals(loginName)){
teacher = teacherList.get(i);
resultList.add(teacher);
}
}else if(StringUtils.isNotEmpty(phone)){
if(phone.equals(teacherList.get(i).getMobile())){
teacher = teacherList.get(i);
resultList.add(teacher);
}
}
}
}catch (Exception e) {
// TODO: handle exception
return JSONUtils.toJSONString(null);
}
return JSONUtils.toJSONString(resultList);
}
/**
* 设置班主任/任课老师
* @param userId 老师id
* @param classId 班级id
* @param subjectCode 学科code 不存在设置班主任,存在 设置任课老师
* @return 设置状态
*/
@RequestMapping("/teacher/teamanage/setHTorNT/")
@ResponseBody
public String setHTorNT(String userId,String classId,String subjectCode){
try{
if(StringUtils.isNotEmpty(subjectCode)){
clazzService.joinClazz(userId, classId, Role.teacher);
userService.setTeacherSubject(userId, classId, subjectCode);
}else{
roleService.setHeadteacher(userId, classId);
}
}catch (Exception e) {
// TODO: handle exception
return JSONUtils.toJSONString(StandardJSONResult.getFailedInstance(e.getMessage()));
}
return JSONUtils.toJSONString(StandardJSONResult.getSuccessInstance());
}
/**
* 根据年级获取班级信息
* @param schoolId 学校id
* @param gradeCode 年级code
* @return 班级信息
*/
@RequestMapping("/teacher/teamanage/getClassInfo/")
@ResponseBody
public String getClassInfo(String schoolId,String gradeCode){
List<ClassInfoDTO> classInfoList = new ArrayList<ClassInfoDTO>();
List<Clazz> clazzList = clazzService.getClazzsBySchoolIdAndGrade(gradeCode, schoolId);
int size = clazzList.size();
for(int i=0; i<size; i++){
ClassInfoDTO classInfoDTO = new ClassInfoDTO();
Clazz clazz = clazzList.get(i);
classInfoDTO.setId(clazz.getId());
classInfoDTO.setName(clazz.getName());
classInfoDTO.setGradeCode(gradeCode);
classInfoDTO.setHeadTeacher(clazzService.getHeadteacher(clazz.getId()));
List<TeacherDTO> subTeacherList = new ArrayList<TeacherDTO>();
List<User> teachers = userService.getTeachersByClassId(clazz.getId());
int count = teachers.size();
for(int j=0; j<count; j++){
List<String> subjectCodes = userService.getTeacherSubjects(clazz.getId(),teachers.get(j).getId());
int length = subjectCodes.size();
for(int k=0; k<length; k++){
TeacherDTO teacherDTO = new TeacherDTO();
teacherDTO.setId(teachers.get(j).getId());
teacherDTO.setName(teachers.get(j).getName());
teacherDTO.setSubjectCode(subjectCodes.get(k));
teacherDTO.setSubjectName(clazzService.getSubjectName(subjectCodes.get(k)));
subTeacherList.add(teacherDTO);
}
}
classInfoDTO.setTeachers(subTeacherList);
classInfoList.add(classInfoDTO);
}
return JSONUtils.toJSONString(classInfoList);
}
/**
* 获取管理的学校Id
* @param response response
* @return 学校Id
*/
private String getAdminSchoolId(HttpServletResponse response) {
String userId = WebUtil.getCurrentUser(response).getId();
if(roleService.isAdministrator(userId)) {
/**
* 通常只管理一个学校
*/
return userService.getManageSchool(userId).get(0).getSchoolId();
} else {
return null;
}
}
/**
* 根据班级id,获取班级所有学生信息
* 根据学生id获取家长登录时间、开通vip信息、手机号。
* @param schoolId 学校id
* @param gradeCode 年级code
* @return 班级信息
*/
@RequestMapping("/teacher/parentInfo/getParentInfo/")
public String getParentInfo(ModelMap model,String beginTime,String endTime,String classId,HttpServletResponse response){
String userId = WebUtil.getCurrentUser(response).getId();
// List<String> clazzIds = getManagerClazz(userId);
HashMap<String, Object> map = new HashMap<String, Object>();
List<ParentInfoDTO> allUsers = new ArrayList<ParentInfoDTO>();
List<User> users = userService.getStudentsByClassId(classId);
int loginCount = 0;
int vipCount = 0;
Date beginTimeD = (!"".equals(beginTime) && StringUtils
.isNumeric(beginTime)) ? DateUtils.timestampToDate(beginTime)
: null;
Date endTimeD = (!"".equals(endTime) && StringUtils.isNumeric(endTime)) ? DateUtils
.timestampToDate(endTime) : null;
for(User user : users){
//Parent parent = parentService.getParentInfo(user.getId());
List<User> parents = parentService.getParents(user.getId());
UserAccount userAccount = bizUserInfoService.getUserAccount(user.getId());
//List<User> parents = userService.getParentsByChildId(user.getId());
ParentInfoDTO parentInfo = new ParentInfoDTO();
parentInfo.setUserId(user.getId());
parentInfo.setUserName(user.getName());
List<String> mobile = new ArrayList<String>();
if(null != parents && parents.size() > 0){
UserExpand userExpand = userExpandService.getByCycoreUserId(user.getId());
if(userExpand != null && userExpand.getLoginTime() != null && beginTimeD.getTime() < userExpand.getLoginTime().getTime() && endTimeD.getTime() > userExpand.getLoginTime().getTime()){
parentInfo.setIsLogin(true);
loginCount++;
}else{
parentInfo.setIsLogin(false);
}
mobile.add(parents.get(0).getMobile());
}else{
parentInfo.setIsLogin(false);
}
if(userAccount != null && userAccount.getVipInfo() != null && userAccount.getVipInfo().getVipEndTime().getTime() > new Date().getTime()){
switch(userAccount.getVipInfo().getVipLevel()){
case 0:
parentInfo.setMemberType(MemberType.t01Normal);
break;
case 1:
vipCount++;
parentInfo.setMemberType(MemberType.t02Exp);
break;
case 2:
vipCount++;
parentInfo.setMemberType(MemberType.t03Pay);
break;
default:
parentInfo.setMemberType(MemberType.t01Normal);
break;
}
}else{
parentInfo.setMemberType(MemberType.t01Normal);
}
parentInfo.setMobile(mobile);
allUsers.add(parentInfo);
}
map.put("parentList", allUsers);
map.put("active", loginCount == 0 ? 0 : Float.valueOf(loginCount)/Float.valueOf(users.size()));
map.put("vipCount", vipCount);
map.put("vipPrecent", vipCount == 0 ? 0 : Float.valueOf(vipCount)/Float.valueOf(users.size()));
model.put("data", map);
return "/branch/teacher/parentInfo/main/index";
}
/**
* 返回教师所属班级(班主任或者任课教师)列表
* @param userId 用户Id
* @return 班级列表
*/
private List<String> getManagerClazz(String userId){
try {
List<String> managerList = new ArrayList<String>();
List<Clazz> clazzList = clazzService.getClassesByTeacherId(userId);
for(Clazz clazz : clazzList){
List<String> subjectCodes = userService.getTeacherSubjects(clazz.getId(), userId);
Boolean isTeacher = false;
if(CollectionUtils.isNotEmpty(subjectCodes)) {
for(String subjectCode : subjectCodes) {
if(!RoleServiceImpl.OTHER_SUBJECT_CODE.equals(subjectCode)) {
isTeacher = true;
}
}
}
if(roleService.isHeadteacher(userId, clazz.getId()) || isTeacher){
managerList.add(clazz.getId());
}
}
return managerList;
} catch (Exception e) {
return new ArrayList<String>();
}
}
}
TeacherManageController 代码记录
最新推荐文章于 2022-04-20 23:51:26 发布