liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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<TSysOrg> orgList = new LinkedList<TSysOrg>();
            if(UserHelper.isAdmin(user)){
                orgList = UserHelper.getOrgListByUserId(userMapper, user.getUserId());
            }else{
                orgList.add(user.getOrg());
            }
            List<String> orgIdList = new LinkedList<String>();
            for(TSysOrg o : orgList){
                orgIdList.add(o.getOrgId());
            }
            if(! orgIdList.contains(editUser.getOrgId())){
                throw this.exception("无权限操作");
            }
            List<TSysRole> roleList = new LinkedList<TSysRole>();
            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<TSysRole> roleList = new LinkedList<TSysRole>();
        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<TSysOrg> orgList = new LinkedList<TSysOrg>();
        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<String> orgIdList = new LinkedList<String>();
            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<TSysOrg> orgList = UserHelper.getOrgListByUserId(userMapper, user.getUserId());
        List<String> orgIdList = new LinkedList<String>();
        for(TSysOrg o : orgList){
            orgIdList.add(o.getOrgId());
        }
        this.getRequest().setAttribute("orgList", orgList);
        
        List<TSysUser> userList = null;
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("all", true);
        
        if(this.isEmpty(orgId)){
            orgId = user.getOrgId();
            map.put("orgIdList", orgIdList);
        }else if(orgIdList.contains(orgId)){
            //如果查询条件orgId不为空,且是该用户的可管理组织id
            List<TSysOrg> orgList2 = UserHelper.getOrgListTreeByRootOrgId(userMapper, orgId);
            List<String> orgIdList2 = new LinkedList<String>();
            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";
    }
}