package com.basic.x01.system.controller; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestMapping; import com.basic.x01.base.BaseController; import com.basic.x01.helper.UserHelper; import com.basic.x01.system.mapper.SystemMapper; import com.basic.x01.system.model.TSysOrg; import com.basic.x01.system.model.TSysRole; import com.basic.x01.system.model.TSysUser; /** * 学校的教职工用户管理 * * @company 北京贝思科技术有限公司 * @author liuyajun, 8384503@qq.com * @date 2016年1月26日 * @time 下午9:20:00 */ @Controller @Transactional(rollbackFor=Throwable.class) public class OrgSystemUser extends BaseController { public static final String SEARCH_ACTION = "orgSystemUser"; public static final String EDIT_ACTION = "orgSystemUserEdit"; @Resource private SystemMapper userMapper; @RequestMapping(value=EDIT_ACTION) public String edit( @Param("edit") String edit, TSysUser editUser, //用来指定组织下拉框的默认显示 @Param("orgId") String orgId ){ TSysUser user = this.getLoingedUser(); if(UserHelper.isSchoolUser(user)){ throw this.exception("当前用户不是组织用户"); } if(! this.isEmpty(edit) && edit.equals("getRoleListByOrgId")){ if(editUser==null || this.isEmpty(editUser.getOrgId())){ throw this.exception("输入参数错误"); } List orgList = new LinkedList(); if(UserHelper.isAdmin(user)){ orgList = UserHelper.getOrgListByUserId(userMapper, user.getUserId()); }else{ orgList.add(user.getOrg()); } List orgIdList = new LinkedList(); for(TSysOrg o : orgList){ orgIdList.add(o.getOrgId()); } if(! orgIdList.contains(editUser.getOrgId())){ throw this.exception("无权限操作"); } List roleList = new LinkedList(); if(UserHelper.isAdmin(user)){ //管理员,使用全部该组织的角色 roleList = userMapper.getRoleListByOrgId(editUser.getOrgId(), false); }else{ //不是管理员,只使用当前用户的角色 roleList.add(user.getRole()); } StringBuffer s = new StringBuffer("ok["); for(TSysRole r : roleList){ s.append("[\"").append(r.getRoleId()).append("\",\"") .append(r.getRoleName()).append("\"],"); } s.append("]"); return this.ajax(s.toString()); } if(! this.isEmpty(edit) && edit.equals("edit")){ if(editUser==null || this.isEmpty(editUser.getLoginName()) || this.isEmpty(editUser.getRealName()) || this.isEmpty(editUser.getRoleId())){ throw this.exception("1"); // } TSysRole role = userMapper.getRoleByRoleId(editUser.getRoleId()); editUser.setOrgId(role.getOrgId()); TSysUser sameUser = userMapper.getUserByLoginName(editUser.getLoginName()); if(this.isEmpty(editUser.getUserId())){ if(sameUser !=null){ throw this.exception("2"); //same user } editUser.setCreateUserId(user.getUserId()); userMapper.createUser(editUser); }else{ if(user.getUserId().equals(editUser.getUserId())){ throw this.exception("3"); //不能修改自己 } if(sameUser !=null && ! sameUser.getUserId().equals(editUser.getUserId())){ throw this.exception("2"); //same user } userMapper.updateUser(editUser); } return this.ajax("ok"); } List roleList = new LinkedList(); if(UserHelper.isAdmin(user)){ //管理员,使用全部该组织的角色 roleList = userMapper.getRoleListByOrgId(user.getOrgId(), false); }else{ //不是管理员,只使用当前用户的角色 roleList.add(user.getRole()); } this.getRequest().setAttribute("roleList", roleList); if(editUser !=null && !this.isEmpty(editUser.getUserId())){ //修改时带出 editUser = userMapper.getUserByUserId(editUser.getUserId()); this.getRequest().setAttribute("editUser", editUser); orgId = editUser.getOrgId(); } this.getRequest().setAttribute("editAction", EDIT_ACTION); List orgList = new LinkedList(); if(UserHelper.isAdmin(user)){ orgList = UserHelper.getOrgListByUserId(userMapper, user.getUserId()); }else{ orgList.add(user.getOrg()); } this.getRequest().setAttribute("orgList", orgList); if(orgList.size()> 1 && ! this.isEmpty(orgId)){ List orgIdList = new LinkedList(); for(TSysOrg o : orgList){ orgIdList.add(o.getOrgId()); } if(orgIdList.contains(orgId)){ this.getRequest().setAttribute("orgId", orgId); } } return "system/user-create"; } @RequestMapping(value=SEARCH_ACTION) public String search( @Param("orgId") String orgId ) { TSysUser user = this.getLoingedUser(); if(UserHelper.isSchoolUser(user)){ throw this.exception("当前用户不是组织用户"); } List orgList = UserHelper.getOrgListByUserId(userMapper, user.getUserId()); List orgIdList = new LinkedList(); for(TSysOrg o : orgList){ orgIdList.add(o.getOrgId()); } this.getRequest().setAttribute("orgList", orgList); List userList = null; Map map = new HashMap(); map.put("all", true); if(this.isEmpty(orgId)){ orgId = user.getOrgId(); map.put("orgIdList", orgIdList); }else if(orgIdList.contains(orgId)){ //如果查询条件orgId不为空,且是该用户的可管理组织id List orgList2 = UserHelper.getOrgListTreeByRootOrgId(userMapper, orgId); List orgIdList2 = new LinkedList(); for(TSysOrg o : orgList2){ orgIdList2.add(o.getOrgId()); } map.put("orgIdList", orgIdList2); } userList = userMapper.getUserListByOrgIdList(this.wrapPageSearchParam(map)); this.getRequest().setAttribute("orgId", orgId); this.getRequest().setAttribute("userList", userList); boolean editAccess = this.checkAccess(EDIT_ACTION); this.getRequest().setAttribute("editAccess", editAccess?"y":"n"); this.getRequest().setAttribute("editAction", EDIT_ACTION); return "system/org-user"; } }