Zhiqim Master(运营管理台)是在ZhiqimManager基础上改造成用于运营思路的管理系统,增加余额,和组织结构等,开放组织管理员,可以添加组织内的部门和角色和操作员。并增加该管理台上的一些组件,如充值支付等组件。适用于二级代理管理或该大型组织机构

森中灵 最后提交于7月前 替换为8.0.5版本
ZmrRolePresenter.java8KB
/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙,邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_components/zhiqim_master.htm
 *
 * Zhiqim Master 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.ArrayList;
import java.util.List;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.annotation.AnIntercept;
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.ZmrSessionUser;
import org.zhiqim.manager.dao.ZmrOperatorDao;
import org.zhiqim.manager.dbo.ZmrOperator;
import org.zhiqim.manager.dbo.ZmrRole;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Updater;

/**
 * 操作员角色控制器
 *
 * @version v1.0.0 @author zouzhigang 2017-6-6 新建与整理
 */
@AnAlias("ZmrRolePresenter")
@AnIntercept("chkZmrLogin")
public class ZmrRolePresenter implements ZmrConstants
{
    /**
     * 在角色成员管理界面中,增加多个角色成员
     * 
     * @param request       请求
     * @throws Exception    异常
     */
    @AnTransaction
    public static void doAddRoleOperators(HttpRequest request) throws Exception
    {
        request.addValidate(new IsNotEmpty("roleId", "请选择一个角色"));
        request.addValidate(new IsNotEmpty("operatorCodes", "请选择操作员"));
        if (!request.chkValidate())
        {//判断参数是否齐全
            request.setResponseError(request.getAlertMsg());
            return;
        }
        
        long roleId = request.getParameterLong("roleId");
        ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
        ZmrRole role = ORM.table().item(ZmrRole.class, sessionUser.getSelectedOrgId(), roleId);
        if (role == null)
        {//判断角色是否存在
            request.returnHistory("请选择一个有效的角色");
            return;
        }
        
        String operatorCodes = request.getParameter("operatorCodes");
        String[] operatorCodeArr = Arrays.toStringArray(operatorCodes);
        List<ZmrOperator> operatorList = new ArrayList<>();
        for (String operatorCode : operatorCodeArr)
        {//判断操作员有效性
            ZmrOperator operator = ORM.table().item(ZmrOperator.class, operatorCode);
            if (operator == null)
            {
                request.returnHistory("请选择一个有效的操作员");
                return;
            }
        
            if (operator.getOrgId() != role.getOrgId())
            {
                request.returnHistory("请选择一个有效的操作员组织");
                return;
            }
            
            operatorList.add(operator);
        }
        
        for (ZmrOperator operator : operatorList)
        {//逐个操作员关联角色
            String operatorRole = operator.getOperatorRole();
            if (Validates.isEmpty(operatorRole))
                operatorRole = "" + roleId;
            else if (operatorRole.contains(""+roleId))
                continue;//已包含的过滤不处理
            else
                operatorRole += "," + roleId;
            
            Updater updater = new Updater();
            updater.addField("operatorRole", operatorRole);
            updater.addMust("operatorCode", operator.getOperatorCode());
            
            ORM.table().update(ZmrOperator.class, updater);
        }
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "增加操作员角色关联", Jsons.toString("operatorCodes", operatorCodes, "roleId", roleId));
    }
    
    /**
     * 在操作员管理界面,增加操作员多个角色
     * 
     * @param request       请求
     * @throws Exception    异常
     */
    @AnTransaction
    public static void doAddOperatorRoles(HttpRequest request) throws Exception
    {
        request.addValidate(new IsNotEmpty("operatorCode", "请选择操作员"));
        request.addValidate(new IsNotEmpty("roleIds", "请选择一个角色"));
        if (!request.chkValidate())
        {//判断参数是否齐全
            request.setResponseError(request.getAlertMsg());
            return;
        }
        
        ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
        String operatorCode = request.getParameter("operatorCode");
        ZmrOperator operator = ORM.table().item(ZmrOperator.class, operatorCode);
        if (operator == null)
        {//判断操作员是否存在
            request.returnHistory("请选择一个有效的操作员");
            return;
        }
        
        String roleIds = request.getParameter("roleIds");
        long[] roleIdArr = Arrays.toLongArray(roleIds);
        for (long roleId : roleIdArr)
        {//判断操作员有效性
            ZmrRole role = ORM.table().item(ZmrRole.class, sessionUser.getSelectedOrgId(), roleId);
            if (role == null)
            {
                request.returnHistory("请选择一个有效的角色");
                return;
            }
        
            if (operator.getOrgId() != role.getOrgId())
            {
                request.returnHistory("请选择一个有效的角色组织");
                return;
            }
            
            if (Validates.isEmpty(operator.getOperatorRole()))
                operator.setOperatorRole(""+roleId);
            else if (!operator.getOperatorRole().contains(""+roleId))
                operator.setOperatorRole(operator.getOperatorRole() + "," + roleId);
        }
        
        //更新操作员角色
        Updater updater = new Updater();
        updater.addField("operatorRole", operator.getOperatorRole());
        updater.addMust("operatorCode", operatorCode);
        ORM.table().update(ZmrOperator.class, updater);
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "增加操作员角色关联", Jsons.toString("operatorCode", operatorCode, "roleIds", roleIds));
    }
    
    /**
     * 在角色成员管理和操作员管理界面中,取消角色操作员关联
     * 
     * @param request       请求
     * @throws Exception    异常
     */
    @AnTransaction
    public static void doDeleteOperatorRole(HttpRequest request) throws Exception
    {
        request.addValidate(new IsNotEmpty("operatorCode", "请选择一个操作员"));
        request.addValidate(new IsNotEmpty("roleId", "请选择一个角色"));
        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 roleId = request.getParameterLong("roleId");
        String operatorRole = operator.getOperatorRole();
        List<Long> operatorRoleList = Lists.toLongList(operatorRole);
        operatorRoleList.remove(roleId);
        
        operatorRole = Lists.toString(operatorRoleList);
        
        Updater updater = new Updater();
        updater.addField("operatorRole", operatorRole);
        updater.addMust("operatorCode", operatorCode);
        
        ORM.table().update(ZmrOperator.class, updater);
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "取消操作员角色关联", Jsons.toString("operatorCode", operatorCode, "roleId", roleId));
    }
}