Zhiqim Manager(知启蒙管理台)是知启蒙框架中最核心的基础组件,大部分后台组件和产品都依赖该组件。因为管理台提供了核心的系统配置、菜单、操作员、部门、角色等权限功能,以及6种皮肤样式可供选择

森中灵 最后提交于4月前 修复切换frame模式时未情况includeUrl
ZmrDeptPresenter.java9KB
/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙,邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_components/zhiqim_manager.htm
 *
 * Zhiqim Manager is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.manager.presenter;

import java.util.List;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.annotation.AnIntercept;
import org.zhiqim.httpd.context.annotation.AnPathRMI;
import org.zhiqim.httpd.validate.ones.IsNotEmpty;
import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.annotation.AnTransaction;
import org.zhiqim.kernel.json.Jsons;
import org.zhiqim.kernel.util.Arrays;
import org.zhiqim.kernel.util.Lists;
import org.zhiqim.kernel.util.Validates;
import org.zhiqim.manager.ZmrConstants;
import org.zhiqim.manager.dao.ZmrOperatorDao;
import org.zhiqim.manager.dbo.ZmrDept;
import org.zhiqim.manager.dbo.ZmrOperator;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Updater;

/**
 * 部门控制器
 *
 * @version v1.0.0 @author zouzhigang 2017-6-6 新建与整理
 */
@AnAlias("ZmrDeptPresenter")
@AnIntercept("chkZmrLogin")
@AnPathRMI("/${zhiqim_manager}/dept.htm")
public class ZmrDeptPresenter implements ZmrConstants
{
    /**
     * 在部门成员管理界面中,增加多个部门成员
     * 
     * @param request       请求
     * @throws Exception    异常
     */
    @AnTransaction
    public static void doAddDeptOperators(HttpRequest request) throws Exception
    {
        request.addValidate(new IsNotEmpty("deptId", "请选择一个部门"));
        request.addValidate(new IsNotEmpty("operatorCodes", "请选择操作员"));
        if (!request.chkValidate())
        {
            request.setResponseError(request.getAlertMsg());
            return;
        }
        
        //判断部门是否存在
        long deptId = request.getParameterLong("deptId");
        ZmrDept dept = ORM.table().item(ZmrDept.class, deptId);
        if (dept == null)
        {
            request.returnHistory("您选择的部门不存在,请选择一个有效的部门");
            return;
        }
        
        String operatorCodes = request.getParameter("operatorCodes");
        String[] operatorCodeArr = Arrays.toStringArray(operatorCodes);
        for (String operatorCode : operatorCodeArr)
        {
            ZmrOperator operator = ORM.table().item(ZmrOperator.class, operatorCode);
            if (operator == null)
                continue;
        
            //操作员部门
            String operatorDept = operator.getOperatorDept();
            if (Validates.isEmpty(operatorDept))
                operatorDept = "" + deptId;
            else
                operatorDept += "," + deptId;
            
            //操作员所有部门
            String operatorDeptAll = operator.getOperatorDeptAll();
            if (dept.getDeptLevel() > 0)
            {//不是根组织的,加上部门对应的所有父部门
                if (Validates.isEmpty(operatorDeptAll))
                    operatorDeptAll = dept.getDeptParentAll();
                else
                    operatorDeptAll += "," + dept.getDeptParentAll();
            }
            
            if (Validates.isEmpty(operatorDeptAll))
                operatorDeptAll = "" + deptId;
            else
                operatorDeptAll += "," + deptId;
            
            //去重
            operatorDeptAll = Arrays.toFilterSameStr(Arrays.toStringArray(operatorDeptAll));
            
            Updater updater = new Updater();
            updater.addField("operatorDept", operatorDept);
            updater.addField("operatorDeptAll", operatorDeptAll);
            updater.addMust("operatorCode", operatorCode);
            
            ORM.table().update(ZmrOperator.class, updater);
        }
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "增加部门操作员关联", Jsons.toString("deptId", deptId, "operatorCodes", operatorCodes));
    }
    
    /**
     * 在操作员管理界面中,增加操作员多个部门
     * 
     * @param request       请求
     * @throws Exception    异常
     */
    @AnTransaction
    public static void doAddOperatorDepts(HttpRequest request) throws Exception
    {
        request.addValidate(new IsNotEmpty("operatorCode", "请选择操作员"));
        request.addValidate(new IsNotEmpty("deptIds", "请选择一个部门"));
        if (!request.chkValidate())
        {
            request.setResponseError(request.getAlertMsg());
            return;
        }
        
        //判断操作员是否存在
        String operatorCode = request.getParameter("operatorCode");
        ZmrOperator operator = ORM.table().item(ZmrOperator.class, operatorCode);
        if (operator == null)
        {
            request.returnHistory("您选择的操作员不存在,请选择一个有效的操作员");
            return;
        }
        
        //判断部门是否存在
        String deptIds = request.getParameter("deptIds");
        long[] deptIdArr = Arrays.toLongArray(deptIds);
        for (long deptId : deptIdArr)
        {
            ZmrDept dept = ORM.table().item(ZmrDept.class, deptId);
            if (dept == null)
                continue;
        
            //操作员部门
            if (Validates.isEmpty(operator.getOperatorDept()))
                operator.setOperatorDept("" + deptId);
            else
                operator.setOperatorDept(operator.getOperatorDept() + "," + deptId);
            
            //操作员所有部门
            String operatorDeptAll = operator.getOperatorDeptAll();
            if (dept.getDeptLevel() > 0)
            {//不是根组织的,加上部门对应的所有父部门
                if (Validates.isEmpty(operatorDeptAll))
                    operatorDeptAll = dept.getDeptParentAll();
                else
                    operatorDeptAll += "," + dept.getDeptParentAll();
            }
            
            if (Validates.isEmpty(operatorDeptAll))
                operatorDeptAll = "" + dept.getDeptId();
            else
                operatorDeptAll += "," + dept.getDeptId();
            operator.setOperatorDeptAll(operatorDeptAll);
        }
        
        Updater updater = new Updater();
        updater.addField("operatorDept", operator.getOperatorDept());
        updater.addField("operatorDeptAll", Arrays.toFilterSameStr(operator.getOperatorDeptAll()));
        updater.addMust("operatorCode", operatorCode);
        
        ORM.table().update(ZmrOperator.class, updater);
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "增加操作员部门关联", Jsons.toString("operatorCode", operatorCode, "deptIds", deptIds));
    }
    
    /**
     * 在部门成员管理和操作员管理界面中,取消部门操作员关联
     * 
     * @param request       请求
     * @throws Exception    异常
     */
    @AnTransaction
    public static void doDeleteOperatorDept(HttpRequest request) throws Exception
    {
        request.addValidate(new IsNotEmpty("operatorCode", "请选择一个操作员"));
        request.addValidate(new IsNotEmpty("deptId", "请选择一个部门"));
        if (!request.chkValidate())
        {
            request.setResponseError(request.getAlertMsg());
            return;
        }
        
        String operatorCode = request.getParameter("operatorCode");
        ZmrOperator operator = ORM.table().item(ZmrOperator.class, operatorCode);
        if (operator == null)
        {
            request.setResponseError("请选择一个有效的操作员");
            return;
        }
        
        //删除对应的部门
        long deptId = request.getParameterLong("deptId");
        String operatorDept = operator.getOperatorDept();
        List<Long> operatorDeptList = Lists.toLongList(operatorDept);
        operatorDeptList.remove(deptId);
        
        operatorDept = Lists.toString(operatorDeptList);
        
        //重置所有部门
        String operatorDeptAll = null;
        for (long oprDeptId : operatorDeptList)
        {
            ZmrDept dept = ORM.table().item(ZmrDept.class, oprDeptId);
            if (dept == null)
                continue;
            
            if (dept.getDeptLevel() > 0)
            {//不是根组织的,加上部门对应的所有父部门
                if (operatorDeptAll == null)
                    operatorDeptAll = dept.getDeptParentAll();
                else
                    operatorDeptAll += "," + dept.getDeptParentAll();
            }

            if (operatorDeptAll == null)
                operatorDeptAll = "" + oprDeptId;
            else
                operatorDeptAll += "," + oprDeptId;
        }
        operatorDeptAll = Arrays.toFilterSameStr(operatorDeptAll);
        
        Updater updater = new Updater();
        updater.addField("operatorDept", operatorDept);
        updater.addField("operatorDeptAll", operatorDeptAll);
        updater.addMust("operatorCode", operatorCode);
        
        ORM.table().update(ZmrOperator.class, updater);
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "取消部门操作员关联", Jsons.toString("deptId", deptId, "operatorCode", operatorCode));
    }
}