com.glaf.base.modules.branch.springmvc.BranchUserController.java Source code

Java tutorial

Introduction

Here is the source code for com.glaf.base.modules.branch.springmvc.BranchUserController.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.glaf.base.modules.branch.springmvc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.glaf.base.modules.Constants;
import com.glaf.base.modules.sys.SysConstants;
import com.glaf.base.modules.sys.model.Dictory;
import com.glaf.base.modules.sys.model.SysDepartment;
import com.glaf.base.modules.sys.model.SysDeptRole;
import com.glaf.base.modules.sys.model.SysRole;
import com.glaf.base.modules.sys.model.SysTree;
import com.glaf.base.modules.sys.model.SysUser;
import com.glaf.base.modules.sys.model.UserRole;
import com.glaf.base.modules.sys.query.SysUserQuery;
import com.glaf.base.modules.sys.query.UserRoleQuery;
import com.glaf.base.modules.sys.service.ComplexUserService;
import com.glaf.base.modules.sys.service.DictoryService;
import com.glaf.base.modules.sys.service.SysDepartmentService;
import com.glaf.base.modules.sys.service.SysDeptRoleService;
import com.glaf.base.modules.sys.service.SysRoleService;
import com.glaf.base.modules.sys.service.SysTreeService;
import com.glaf.base.modules.sys.service.SysUserService;
import com.glaf.base.utils.ParamUtil;
import com.glaf.core.config.ViewProperties;
import com.glaf.core.res.MessageUtils;
import com.glaf.core.res.ViewMessage;
import com.glaf.core.res.ViewMessages;
import com.glaf.core.security.DigestUtil;
import com.glaf.core.security.LoginContext;
import com.glaf.core.util.JsonUtils;
import com.glaf.core.util.PageResult;
import com.glaf.core.util.Paging;
import com.glaf.core.util.ParamUtils;
import com.glaf.core.util.RequestUtils;
import com.glaf.core.util.ResponseUtils;
import com.glaf.core.util.StringTools;
import com.glaf.core.util.Tools;

@Controller("/branch/user")
@RequestMapping("/branch/user.do")
public class BranchUserController {
    private static final Log logger = LogFactory.getLog(BranchUserController.class);

    protected ComplexUserService complexUserService;

    protected DictoryService dictoryService;

    protected SysDepartmentService sysDepartmentService;

    protected SysDeptRoleService sysDeptRoleService;

    protected SysRoleService sysRoleService;

    protected SysTreeService sysTreeService;

    protected SysUserService sysUserService;

    /**
     * 
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=addRoleUser")
    public ModelAndView addRoleUser(HttpServletRequest request, ModelMap modelMap) {
        logger.debug("--------------------addRoleUser----------------");
        RequestUtils.setRequestParameterToAttribute(request);
        int deptId = ParamUtil.getIntParameter(request, "deptId", 0);
        int roleId = ParamUtil.getIntParameter(request, "roleId", 0);
        boolean success = false;
        String actorId = RequestUtils.getActorId(request);
        List<Long> nodeIds = complexUserService.getUserManageBranchNodeIds(actorId);

        SysDepartment department = sysDepartmentService.getSysDepartment(deptId);
        if (department != null) {
            SysTree tree = sysTreeService.findById(department.getNodeId());
            if (tree != null && nodeIds.contains(tree.getId())) {
                SysDeptRole deptRole = sysDeptRoleService.find(deptId, roleId);
                if (deptRole == null) {
                    deptRole = new SysDeptRole();
                    deptRole.setDeptId(deptId);
                    deptRole.setDept(sysDepartmentService.findById(deptId));
                    deptRole.setSysRoleId(roleId);
                    deptRole.setRole(sysRoleService.findById(roleId));
                    sysDeptRoleService.create(deptRole);
                }
                if (deptRole != null) {
                    Set<SysUser> users = deptRole.getUsers();
                    long[] userIds = ParamUtil.getLongParameterValues(request, "id");
                    for (int i = 0; i < userIds.length; i++) {
                        SysUser user = sysUserService.findById(userIds[i]);
                        if (user != null) {
                            logger.debug(user.getName());
                            users.add(user);
                        }
                    }
                    deptRole.setUsers(users);
                    success = sysDeptRoleService.update(deptRole);
                }
            }
        }

        ViewMessages messages = new ViewMessages();
        if (success) {
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("user.add_success"));
        } else {// ?
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("user.add_failure"));
        }
        MessageUtils.addMessages(request, messages);

        return new ModelAndView("show_msg", modelMap);
    }

    /**
     * 
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=delRoleUser")
    public ModelAndView delRoleUser(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        int deptId = ParamUtil.getIntParameter(request, "deptId", 0);
        int roleId = ParamUtil.getIntParameter(request, "roleId", 0);
        boolean sucess = false;
        String actorId = RequestUtils.getActorId(request);
        try {
            List<Long> nodeIds = complexUserService.getUserManageBranchNodeIds(actorId);
            SysDepartment department = sysDepartmentService.getSysDepartment(deptId);
            if (department != null) {
                SysTree tree = sysTreeService.findById(department.getNodeId());
                if (tree != null && nodeIds.contains(tree.getId())) {
                    SysDeptRole deptRole = sysDeptRoleService.find(deptId, roleId);
                    long[] userIds = ParamUtil.getLongParameterValues(request, "id");
                    if (deptRole != null && deptRole.getRole() != null
                            && !StringUtils.equals(deptRole.getRole().getCode(), SysConstants.BRANCH_ADMIN)) {
                        sysUserService.deleteRoleUsers(deptRole, userIds);
                        sucess = true;
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            sucess = false;
        }
        ViewMessages messages = new ViewMessages();
        if (sucess) {
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("user.delete_success"));
        } else {// ?
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("user.delete_failure"));
        }
        MessageUtils.addMessages(request, messages);

        return new ModelAndView("show_msg2", modelMap);
    }

    @RequestMapping(params = "method=deptUsers")
    public ModelAndView deptUsers(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        String x_query = request.getParameter("x_query");
        if (StringUtils.equals(x_query, "true")) {
            Map<String, Object> paramMap = RequestUtils.getParameterMap(request);
            String x_complex_query = JsonUtils.encode(paramMap);
            x_complex_query = RequestUtils.encodeString(x_complex_query);
            request.setAttribute("x_complex_query", x_complex_query);
        } else {
            request.setAttribute("x_complex_query", "");
        }

        String x_view = ViewProperties.getString("branch.user.deptUsers");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        String view = request.getParameter("view");
        if (StringUtils.isNotEmpty(view)) {
            return new ModelAndView(view, modelMap);
        }

        return new ModelAndView("/modules/branch/user/deptUsers", modelMap);
    }

    /**
     * 
     * 
     * @param list
     * @param parentId
     */
    public void getAllSysDepartmentList(List<SysDepartment> list, int parentId) {
        List<SysDepartment> temp = new java.util.ArrayList<SysDepartment>();
        temp = this.sysDepartmentService.getSysDepartmentList(parentId);
        if (temp != null && temp.size() != 0) {
            for (int i = 0; i < temp.size(); i++) {
                SysDepartment element = (SysDepartment) temp.get(i);
                getAllSysDepartmentList(list, (int) element.getId());
            }
            list.addAll(temp);
        }
    }

    @RequestMapping(params = "method=json")
    @ResponseBody
    public byte[] json(HttpServletRequest request) throws IOException {
        Long deptId = ParamUtil.getLongParameter(request, "deptId", 0);
        Long nodeId = RequestUtils.getLong(request, "nodeId");
        Map<String, Object> params = RequestUtils.getParameterMap(request);
        SysUserQuery query = new SysUserQuery();
        Tools.populate(query, params);
        if (nodeId > 0) {
            SysDepartment dept = sysDepartmentService.getSysDepartmentByNodeId(nodeId);
            if (dept != null) {
                query.deptId(dept.getId());
            }
        } else if (deptId > 0) {
            query.deptId(deptId);
        } else {
            query.deptId(-1L);
        }

        String gridType = ParamUtils.getString(params, "gridType");
        if (gridType == null) {
            gridType = "easyui";
        }
        int start = 0;
        int limit = 10;
        String orderName = null;
        String order = null;

        int pageNo = ParamUtils.getInt(params, "page");
        limit = ParamUtils.getInt(params, "rows");
        start = (pageNo - 1) * limit;
        orderName = ParamUtils.getString(params, "sortName");
        order = ParamUtils.getString(params, "sortOrder");

        if (start < 0) {
            start = 0;
        }

        if (limit <= 0) {
            limit = Paging.DEFAULT_PAGE_SIZE;
        }

        JSONObject result = new JSONObject();
        int total = sysUserService.getSysUserCountByQueryCriteria(query);
        if (total > 0) {
            result.put("total", total);
            result.put("totalCount", total);
            result.put("totalRecords", total);
            result.put("start", start);
            result.put("startIndex", start);
            result.put("limit", limit);
            result.put("pageSize", limit);

            if (StringUtils.isNotEmpty(orderName)) {
                query.setSortOrder(orderName);
                if (StringUtils.equals(order, "desc")) {
                    query.setSortOrder(" desc ");
                }
            }

            List<SysUser> list = sysUserService.getSysUsersByQueryCriteria(start, limit, query);

            if (list != null && !list.isEmpty()) {
                JSONArray rowsJSON = new JSONArray();

                result.put("rows", rowsJSON);

                for (SysUser sysUser : list) {
                    JSONObject rowJSON = sysUser.toJsonObject();
                    rowJSON.put("id", sysUser.getId());
                    rowJSON.put("actorId", sysUser.getAccount());
                    rowJSON.put("startIndex", ++start);
                    rowsJSON.add(rowJSON);
                }

            }
        } else {
            result.put("total", total);
            result.put("totalCount", total);
            JSONArray rowsJSON = new JSONArray();
            result.put("rows", rowsJSON);
        }
        return result.toString().getBytes("UTF-8");
    }

    @RequestMapping
    public ModelAndView list(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        String x_query = request.getParameter("x_query");
        if (StringUtils.equals(x_query, "true")) {
            Map<String, Object> paramMap = RequestUtils.getParameterMap(request);
            String x_complex_query = JsonUtils.encode(paramMap);
            x_complex_query = RequestUtils.encodeString(x_complex_query);
            request.setAttribute("x_complex_query", x_complex_query);
        } else {
            request.setAttribute("x_complex_query", "");
        }

        String x_view = ViewProperties.getString("branch.user.list");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        String view = request.getParameter("view");
        if (StringUtils.isNotEmpty(view)) {
            return new ModelAndView(view, modelMap);
        }

        return new ModelAndView("/modules/branch/user/list", modelMap);
    }

    /**
     *  
     * 
     * @param set
     * @param deptId
     * @param code
     */
    public void loadRoleUsers(Set<SysUser> set, long deptId, String code) {
        if (StringUtils.isNotEmpty(code)) {
            Set<SysUser> temp = sysDeptRoleService.findRoleUser(deptId, "R011");
            set.addAll(temp);
        } else {
            List<SysUser> list = sysUserService.getSysUserList((int) deptId);
            set.addAll(list);
        }
    }

    @RequestMapping(params = "method=permission")
    public ModelAndView permission(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);

        List<SysRole> roleList = new ArrayList<SysRole>();
        List<SysRole> roles = sysRoleService.getSysRoleList();
        for (SysRole role : roles) {
            if (StringUtils.isNotEmpty(role.getCode())
                    && (StringUtils.startsWithIgnoreCase(role.getCode(), SysConstants.BRANCH_PREFIX)
                            || StringUtils.equals(role.getIsUseBranch(), "Y"))) {
                roleList.add(role);
            }
        }

        request.setAttribute("roleList", roleList);

        String op_view = request.getParameter("op_view");
        if (StringUtils.isEmpty(op_view)) {
            op_view = "user";
        }

        request.setAttribute("op_view", op_view);

        long parentId = 0;
        if (StringUtils.isNotEmpty(request.getParameter("parentId"))) {
            parentId = RequestUtils.getLong(request, "parentId");
        } else {
            SysUser user = com.glaf.base.utils.RequestUtil.getLoginUser(request);
            parentId = user.getDeptId();
        }

        List<Long> deptIds = new ArrayList<Long>();
        List<SysTree> treeList = new ArrayList<SysTree>();
        sysTreeService.loadSysTrees(treeList, parentId, 1);
        if (treeList != null && !treeList.isEmpty()) {
            for (SysTree tree : treeList) {
                if (tree.getDepartment() != null) {
                    deptIds.add(tree.getDepartment().getId());
                }
            }
        }

        SysDepartment dept = sysDepartmentService.getSysDepartmentByNodeId(parentId);
        if (dept != null) {
            deptIds.add(dept.getId());
        }

        logger.debug("----deptIds:" + deptIds);

        SysUserQuery query = new SysUserQuery();
        query.deptIds(deptIds);
        List<SysUser> users = sysUserService.getSysUsersByQueryCriteria(0, 1000, query);
        if (users != null && !users.isEmpty()) {
            List<String> actorIds = new ArrayList<String>();
            for (SysUser user : users) {
                actorIds.add(user.getAccount());
            }
            UserRoleQuery userRoleQuery = new UserRoleQuery();
            userRoleQuery.setActorIds(actorIds);
            List<UserRole> userRoles = sysUserService.getRoleUserViews(userRoleQuery);
            if (userRoles != null && !userRoles.isEmpty()) {
                for (SysUser user : users) {
                    for (UserRole userRole : userRoles) {
                        if (StringUtils.equals(user.getAccount(), userRole.getActorId())) {
                            user.getRoleCodes().add(userRole.getRoleCode());
                        }
                    }
                }
            }
            request.setAttribute("users", users);
        }

        String x_query = request.getParameter("x_query");
        if (StringUtils.equals(x_query, "true")) {
            Map<String, Object> paramMap = RequestUtils.getParameterMap(request);
            String x_complex_query = JsonUtils.encode(paramMap);
            x_complex_query = RequestUtils.encodeString(x_complex_query);
            request.setAttribute("x_complex_query", x_complex_query);
        } else {
            request.setAttribute("x_complex_query", "");
        }

        String x_view = ViewProperties.getString("branch.user.permission");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        String view = request.getParameter("view");
        if (StringUtils.isNotEmpty(view)) {
            return new ModelAndView(view, modelMap);
        }

        return new ModelAndView("/modules/branch/user/permission", modelMap);
    }

    /**
     * ?
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=prepareAdd")
    public ModelAndView prepareAdd(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        List<Dictory> dictories = dictoryService.getDictoryList(SysConstants.USER_HEADSHIP);
        modelMap.put("dictories", dictories);

        String x_view = ViewProperties.getString("branch.user.prepareAdd");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/modules/branch/user/user_add", modelMap);
    }

    /**
     * ?
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=prepareModify")
    public ModelAndView prepareModify(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        long id = ParamUtil.getLongParameter(request, "id", 0);
        SysUser bean = sysUserService.findById(id);
        request.setAttribute("bean", bean);

        if (bean != null && StringUtils.isNotEmpty(bean.getSuperiorIds())) {
            List<String> userIds = StringTools.split(bean.getSuperiorIds());
            StringBuffer buffer = new StringBuffer();
            if (userIds != null && !userIds.isEmpty()) {
                for (String userId : userIds) {
                    SysUser u = sysUserService.findByAccount(userId);
                    if (u != null) {
                        buffer.append(u.getName()).append("[").append(u.getAccount()).append("] ");
                    }
                }
                request.setAttribute("x_users_name", buffer.toString());
            }
        }

        List<Dictory> dictories = dictoryService.getDictoryList(SysConstants.USER_HEADSHIP);
        modelMap.put("dictories", dictories);

        SysTree parent = sysTreeService.getSysTreeByCode(Constants.TREE_DEPT);
        List<SysTree> list = new java.util.ArrayList<SysTree>();
        parent.setDeep(0);
        list.add(parent);
        sysTreeService.loadSysTrees(list, parent.getId(), 1);
        request.setAttribute("parent", list);

        String x_view = ViewProperties.getString("branch.user.prepareModify");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/modules/branch/user/user_modify", modelMap);
    }

    /**
     * ???
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=prepareResetPwd")
    public ModelAndView prepareResetPwd(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        long id = ParamUtil.getLongParameter(request, "id", 0);
        SysUser bean = sysUserService.findById(id);
        request.setAttribute("bean", bean);

        String x_view = ViewProperties.getString("user.prepareResetPwd");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/modules/branch/user/user_reset_pwd", modelMap);
    }

    /**
     * ??
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=resetPwd")
    public ModelAndView resetPwd(HttpServletRequest request, ModelMap modelMap) {
        boolean ret = false;
        String actorId = RequestUtils.getActorId(request);
        List<Long> nodeIds = complexUserService.getUserManageBranchNodeIds(actorId);

        long id = ParamUtil.getIntParameter(request, "id", 0);
        SysUser bean = sysUserService.findById(id);

        /**
         * ?????
         */
        if (bean != null && !bean.isSystemAdministrator()) {
            /**
             * ??
             */
            SysDepartment department = sysDepartmentService.findById(bean.getDeptId());
            if (department != null) {
                SysTree tree = sysTreeService.findById(department.getNodeId());
                if (tree != null && nodeIds.contains(tree.getId())) {
                    String newPwd = ParamUtil.getParameter(request, "newPwd");
                    if (bean != null && StringUtils.isNotEmpty(newPwd)) {
                        try {
                            bean.setPassword(DigestUtil.digestString(newPwd, "MD5"));
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                        bean.setUpdateBy(bean.getAccount());
                        ret = sysUserService.update(bean);
                    }
                }
            }
        }

        ViewMessages messages = new ViewMessages();
        if (ret) {// ??
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("user.modify_success"));
        } else {// ?
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("user.modify_failure"));
        }
        MessageUtils.addMessages(request, messages);
        return new ModelAndView("show_msg", modelMap);
    }

    /**
     * ???
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=saveAdd")
    public ModelAndView saveAdd(HttpServletRequest request, ModelMap modelMap) {
        SysUser bean = new SysUser();
        String actorId = RequestUtils.getActorId(request);
        List<Long> nodeIds = complexUserService.getUserManageBranchNodeIds(actorId);

        int ret = 0;
        SysDepartment department = null;
        long nodeId = RequestUtils.getLong(request, "nodeId");
        if (nodeId > 0) {
            department = sysDepartmentService.getSysDepartmentByNodeId(nodeId);
            bean.setDepartment(department);
        } else {
            department = sysDepartmentService.findById(ParamUtil.getIntParameter(request, "deptId", 0));
            bean.setDepartment(department);
        }
        /**
         * ???
         */
        if (department != null) {
            SysTree tree = sysTreeService.findById(department.getNodeId());
            if (tree != null && nodeIds.contains(tree.getId())) {
                bean.setDeptId(department.getId());
                bean.setCode(ParamUtil.getParameter(request, "code"));
                bean.setAccount(bean.getCode());
                bean.setName(ParamUtil.getParameter(request, "name"));
                String password = ParamUtil.getParameter(request, "password");
                try {
                    String pwd = DigestUtil.digestString(password, "MD5");
                    bean.setPassword(pwd);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                bean.setSuperiorIds(ParamUtil.getParameter(request, "superiorIds"));
                bean.setGender(ParamUtil.getIntParameter(request, "gender", 0));
                bean.setMobile(ParamUtil.getParameter(request, "mobile"));
                bean.setEmail(ParamUtil.getParameter(request, "email"));
                bean.setTelephone(ParamUtil.getParameter(request, "telephone"));
                bean.setBlocked(ParamUtil.getIntParameter(request, "blocked", 0));
                bean.setHeadship(ParamUtil.getParameter(request, "headship"));
                bean.setUserType(ParamUtil.getIntParameter(request, "userType", 0));
                bean.setEvection(0);
                bean.setCreateTime(new Date());
                bean.setLastLoginTime(new Date());
                bean.setCreateBy(RequestUtils.getActorId(request));
                bean.setUpdateBy(RequestUtils.getActorId(request));

                if (sysUserService.findByAccount(bean.getAccount()) == null) {
                    if (sysUserService.create(bean)) {
                        ret = 2;
                    }
                } else {// ??
                    ret = 1;
                }
            }
        }

        ViewMessages messages = new ViewMessages();
        if (ret == 2) {// ??
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("user.add_success"));
        } else if (ret == 1) {// ?
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("user.existed"));
        } else {
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("user.add_failure"));
        }
        MessageUtils.addMessages(request, messages);

        // ?
        return new ModelAndView("show_msg", modelMap);
    }

    /**
     * ???
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=saveModify")
    public ModelAndView saveModify(HttpServletRequest request, ModelMap modelMap) {
        long id = ParamUtil.getIntParameter(request, "id", 0);
        SysUser bean = sysUserService.findById(id);
        boolean ret = false;
        if (bean != null) {
            SysDepartment department = sysDepartmentService
                    .findById(ParamUtil.getIntParameter(request, "deptId", 0));
            String actorId = RequestUtils.getActorId(request);
            List<Long> nodeIds = complexUserService.getUserManageBranchNodeIds(actorId);

            /**
             * ???
             */
            if (department != null) {
                SysTree tree = sysTreeService.findById(department.getNodeId());
                if (tree != null && nodeIds.contains(tree.getId())) {
                    bean.setDepartment(department);
                    bean.setName(ParamUtil.getParameter(request, "name"));
                    bean.setSuperiorIds(ParamUtil.getParameter(request, "superiorIds"));
                    bean.setGender(ParamUtil.getIntParameter(request, "gender", 0));
                    bean.setMobile(ParamUtil.getParameter(request, "mobile"));
                    bean.setEmail(ParamUtil.getParameter(request, "email"));
                    bean.setTelephone(ParamUtil.getParameter(request, "telephone"));
                    bean.setEvection(ParamUtil.getIntParameter(request, "evection", 0));
                    bean.setBlocked(ParamUtil.getIntParameter(request, "blocked", 0));
                    bean.setHeadship(ParamUtil.getParameter(request, "headship"));
                    bean.setUserType(ParamUtil.getIntParameter(request, "userType", 0));
                    bean.setUpdateBy(RequestUtils.getActorId(request));
                    ret = sysUserService.update(bean);
                }
            }
        }

        ViewMessages messages = new ViewMessages();
        if (ret) {// ??
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("user.modify_success"));
        } else {// ?
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("user.modify_failure"));
        }
        MessageUtils.addMessages(request, messages);
        return new ModelAndView("show_msg", modelMap);
    }

    /**
     * ???
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @ResponseBody
    @RequestMapping(params = "method=saveUserRole")
    public byte[] saveUserRole(HttpServletRequest request) {
        long roleId = ParamUtil.getLongParameter(request, "roleId", 0);
        String actorId = request.getParameter("actorId");
        String operation = request.getParameter("operation");
        SysRole bean = sysRoleService.findById(roleId);
        SysUser user = sysUserService.findByAccountWithAll(actorId);
        if (bean != null && user != null) {
            LoginContext loginContext = RequestUtils.getLoginContext(request);

            List<Long> nodeIds = new ArrayList<Long>();

            if (!loginContext.isSystemAdministrator()) {
                nodeIds = complexUserService.getUserManageBranchNodeIds(loginContext.getActorId());
            }

            /**
             * ???
             */
            if (loginContext.isSystemAdministrator()
                    || (user.getDepartment() != null && nodeIds.contains(user.getDepartment().getNodeId()))) {
                if (StringUtils.equals(operation, "revoke")) {
                    sysUserService.deleteRoleUser(roleId, actorId);
                } else {
                    sysUserService.createRoleUser(roleId, actorId);
                }
                return ResponseUtils.responseResult(true);
            }
        }

        return ResponseUtils.responseResult(false);
    }

    /**
     * ?
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=selectSysUser")
    public ModelAndView selectSysUser(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        int pageNo = ParamUtil.getIntParameter(request, "page_no", 1);
        int pageSize = Constants.PAGE_SIZE;
        int deptId = ParamUtil.getIntParameter(request, "deptId", 0);
        int multDate = ParamUtil.getIntParameter(request, "multDate", 0);
        String name = ParamUtil.getParameter(request, "fullName", null);
        PageResult pager = sysUserService.getSysUserList(deptId, name, pageNo, pageSize);
        request.setAttribute("pager", pager);
        request.setAttribute("multDate", new Integer(multDate));

        String x_view = ViewProperties.getString("branch.user.selectSysUser");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/modules/branch/user/user_select", modelMap);
    }

    /**
     * ?
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=selectSysUserByDept")
    public ModelAndView selectSysUserByDept(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        int pageNo = ParamUtil.getIntParameter(request, "page_no", 1);
        int pageSize = Constants.PAGE_SIZE;
        int deptId = ParamUtil.getIntParameter(request, "deptId", 0);
        String name = ParamUtil.getParameter(request, "fullName", null);

        SysDepartment sysDepartment = sysDepartmentService.findById(deptId);
        request.setAttribute("sysDepartment", sysDepartment);
        PageResult pager = null;
        if (StringUtils.isNotEmpty(name)) {
            pager = sysUserService.getSysUserList(deptId, name, pageNo, pageSize);
        } else {
            pager = sysUserService.getSysUserList(deptId, pageNo, pageSize);
        }
        request.setAttribute("pager", pager);

        String x_view = ViewProperties.getString("branch.user.selectSysUserByDept");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/modules/branch/user/userByDept_list", modelMap);
    }

    @javax.annotation.Resource
    public void setComplexUserService(ComplexUserService complexUserService) {
        this.complexUserService = complexUserService;
    }

    @javax.annotation.Resource
    public void setDictoryService(DictoryService dictoryService) {
        this.dictoryService = dictoryService;
    }

    /**
     * 
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=saveRole")
    public ModelAndView saveRole(HttpServletRequest request, ModelMap modelMap) {
        logger.debug(RequestUtils.getParameterMap(request));
        ViewMessages messages = new ViewMessages();
        long userId = ParamUtil.getIntParameter(request, "user_id", 0);
        SysUser user = sysUserService.findById(userId);// 
        if (user != null && user.getDeptId() > 0) {// 
            String actorId = RequestUtils.getActorId(request);
            List<Long> nodeIds = complexUserService.getUserManageBranchNodeIds(actorId);

            SysDepartment department = sysDepartmentService.findById(user.getDeptId());
            /**
             * ???
             */
            if (department != null && department.getNodeId() > 0) {
                SysTree tree = sysTreeService.findById(department.getNodeId());
                if (tree != null && nodeIds.contains(tree.getId())) {
                    long[] id = ParamUtil.getLongParameterValues(request, "id");// ???
                    if (id != null) {
                        Set<SysDeptRole> delRoles = new HashSet<SysDeptRole>();
                        Set<SysDeptRole> oldRoles = user.getRoles();
                        Set<SysDeptRole> newRoles = new HashSet<SysDeptRole>();
                        for (int i = 0; i < id.length; i++) {
                            logger.debug("id[" + i + "]=" + id[i]);
                            SysDeptRole role = sysDeptRoleService.findById(id[i]);// 
                            if (role != null) {
                                newRoles.add(role);// 
                            }
                        }

                        oldRoles.retainAll(newRoles);// ??
                        delRoles.removeAll(newRoles);// ??
                        newRoles.removeAll(oldRoles);// ??
                        user.setUpdateBy(RequestUtils.getActorId(request));

                        if (sysUserService.updateRole(user, delRoles, newRoles)) {// ??
                            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("user.role_success"));
                        } else {// ?
                            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("user.role_failure"));
                        }
                    }
                }
            }
        }
        MessageUtils.addMessages(request, messages);
        return new ModelAndView("show_msg", modelMap);
    }

    @javax.annotation.Resource
    public void setSysDepartmentService(SysDepartmentService sysDepartmentService) {
        this.sysDepartmentService = sysDepartmentService;
    }

    @javax.annotation.Resource
    public void setSysDeptRoleService(SysDeptRoleService sysDeptRoleService) {
        this.sysDeptRoleService = sysDeptRoleService;
    }

    @javax.annotation.Resource
    public void setSysRoleService(SysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }

    @javax.annotation.Resource
    public void setSysTreeService(SysTreeService sysTreeService) {
        this.sysTreeService = sysTreeService;
    }

    @javax.annotation.Resource
    public void setSysUserService(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    /**
     * 
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=showDeptUsers")
    public ModelAndView showDeptUsers(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        List<SysDepartment> list = new java.util.ArrayList<SysDepartment>();
        Set<SysUser> set = new HashSet<SysUser>();

        long deptId = ParamUtil.getLongParameter(request, "dept", 5);
        String roleCode = ParamUtil.getParameter(request, "code", "");
        SysDepartment node = this.sysDepartmentService.findById(deptId);
        if (node != null) {
            list.add(node);
            this.getAllSysDepartmentList(list, (int) node.getId());
        } else {
            this.getAllSysDepartmentList(list, (int) deptId);
        }
        for (Iterator<SysDepartment> iter = list.iterator(); iter.hasNext();) {
            SysDepartment element = (SysDepartment) iter.next();
            this.loadRoleUsers(set, element.getId(), roleCode);
        }
        request.setAttribute("user", set);

        String x_view = ViewProperties.getString("branch.user.showDeptUsers");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/modules/branch/user/duty_select", modelMap);
    }

    /**
     * 
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=showList")
    public ModelAndView showList(HttpServletRequest request, ModelMap modelMap) {
        int deptId = ParamUtil.getIntParameter(request, "parent", 0);
        int pageNo = ParamUtil.getIntParameter(request, "page_no", 1);
        int pageSize = ParamUtil.getIntParameter(request, "page_size", Constants.PAGE_SIZE);
        PageResult pager = sysUserService.getSysUserList(deptId, pageNo, pageSize);
        request.setAttribute("department", sysDepartmentService.getSysDepartment(deptId));
        request.setAttribute("pager", pager);

        SysDepartment dept = sysDepartmentService.getSysDepartment(deptId);
        List<SysDepartment> list = new java.util.ArrayList<SysDepartment>();
        sysDepartmentService.findNestingDepartment(list, dept);
        request.setAttribute("nav", list);

        String x_view = ViewProperties.getString("branch.user.showList");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/modules/branch/user/user_list", modelMap);
    }

    /**
     * 
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=showRole")
    public ModelAndView showRole(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        long id = ParamUtil.getIntParameter(request, "user_id", 0);
        SysUser bean = sysUserService.findById(id);
        SysUser user = sysUserService.findByAccountWithAll(bean.getAccount());
        List<SysDeptRole> deptRoles = sysDeptRoleService.getRoleList(user.getDepartment().getId());

        List<SysRole> roles = sysRoleService.getSysRoleList();
        if (roles != null && !roles.isEmpty()) {
            for (SysRole role : roles) {
                if (StringUtils.isNotEmpty(role.getCode())
                        && (StringUtils.startsWithIgnoreCase(role.getCode(), SysConstants.BRANCH_PREFIX)
                                || StringUtils.equals(role.getIsUseBranch(), "Y"))) {
                    if (sysDeptRoleService.find(user.getDepartment().getId(), role.getId()) == null) {
                        SysDeptRole dr = new SysDeptRole();
                        dr.setDeptId(user.getDepartment().getId());
                        dr.setRole(role);
                        dr.setSysRoleId(role.getId());
                        dr.setCreateBy(RequestUtils.getActorId(request));
                        sysDeptRoleService.create(dr);
                        deptRoles.add(dr);
                    }
                }
            }
        }

        request.setAttribute("user", user);
        request.setAttribute("list", deptRoles);

        String x_view = ViewProperties.getString("branch.user.showRole");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        // ?
        return new ModelAndView("/modules/branch/user/user_role", modelMap);
    }

    /**
     * 
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=showRoleUser")
    public ModelAndView showRoleUser(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        int deptId = ParamUtil.getIntParameter(request, "deptId", 0);
        long roleId = ParamUtil.getLongParameter(request, "roleId", 0);

        // ?
        SysDepartment dept = sysDepartmentService.findById(deptId);
        List<SysDepartment> list = new java.util.ArrayList<SysDepartment>();
        sysDepartmentService.findNestingDepartment(list, dept);
        request.setAttribute("nav", list);

        // 
        SysRole role = sysRoleService.findById(roleId);
        request.setAttribute("role", role.getName());

        Set<?> users = sysDeptRoleService.findRoleUser(deptId, role.getCode());
        request.setAttribute("list", users);

        String x_view = ViewProperties.getString("branch.user.showRoleUser");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/modules/branch/user/deptRole_user", modelMap);
    }

    /**
     * 
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=showSelUser")
    public ModelAndView showSelUser(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        int deptId = ParamUtil.getIntParameter(request, "deptId2", 0);
        if (deptId != 0) {
            request.setAttribute("list", sysUserService.getSysUserList(deptId));
        }

        String x_view = ViewProperties.getString("branch.user.showSelUser");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/modules/branch/user/dept_user_sel", modelMap);
    }

    /**
     * ?
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=showUser")
    public ModelAndView showUser(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        long userId = ParamUtil.getLongParameter(request, "userId", 0);
        SysUser user = sysUserService.findById(userId);
        user.setPassword(null);
        request.setAttribute("user", user);

        String x_view = ViewProperties.getString("branch.user.showUser");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/modules/branch/user/user_info", modelMap);
    }
}