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

森中灵 最后提交于7月前 替换为8.0.5版本
DeptAction.java7KB
/*
 * 版权所有 (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.action;

import java.util.ArrayList;
import java.util.List;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.extend.StdSwitchAction;
import org.zhiqim.httpd.validate.ones.IsIntegerValue;
import org.zhiqim.httpd.validate.ones.IsLen;
import org.zhiqim.httpd.validate.onex.IsInteger;
import org.zhiqim.kernel.json.Jsons;
import org.zhiqim.kernel.model.tree.Tree;
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.ZmrDeptDao;
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.Selector;

/**
 * 部门管理
 *
 * @version v1.0.0 @author zouzhigang 2015-5-29 新建与整理
 */
public class DeptAction extends StdSwitchAction implements ZmrConstants
{
    @Override
    protected void validateId(HttpRequest request)
    {
        request.addValidate(new IsInteger("deptId", "请选择一个选项"));
    }

    @Override
    protected void validateForm(HttpRequest request)
    {
        request.addValidate(new IsInteger("deptId", "请选择一个选项"));
        request.addValidate(new IsLen("deptName", "部门名称不能为空且不能超过32个汉字", 1, 32));
        request.addValidate(new IsIntegerValue("deptSeq", "部门排序必须是[0, 999999]范围的非负整数", 0, 999999));
        request.addValidate(new IsLen("deptDesc", "部门描述可为空或不能超过100个汉字", 0, 100));
    }

    @Override
    protected void list(HttpRequest request) throws Exception
    {
        ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
        long orgId = sessionUser.getSelectedOrgId();
        
        Tree<ZmrDept> tree = ZmrDeptDao.tree(orgId);
        request.setAttribute("tree", tree);
    }
    
    @Override
    protected void add(HttpRequest request) throws Exception
    {
        long deptId = request.getParameterLong("deptId");
        if (deptId == -1)
        {
            request.returnHistory("请选择一个上级部门");
            return;
        }
        
        ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
        ZmrDept parent = ORM.table().item(ZmrDept.class, sessionUser.getSelectedOrgId(), deptId);
        if (parent == null)
        {
            request.returnHistory("请选择一个有效的上级部门");
            return;
        }
        
        request.setAttribute("parent", parent);
        request.setAttribute("orgId", sessionUser.getSelectedOrgId());
    }

    @Override
    protected void modify(HttpRequest request) throws Exception
    {
        ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
        long orgId = sessionUser.getSelectedOrgId();
        long deptId = request.getParameterLong("deptId");
        ZmrDept item = ORM.table().item(ZmrDept.class, orgId, deptId);
        if (item == null)
        {
            request.returnHistory("请选择一个有效的部门");
            return;
        }
        
        request.setAttribute("item", item);
    }

    @Override
    protected void insert(HttpRequest request) throws Exception
    {
        ZmrDept item = request.getParameter(ZmrDept.class);
        
        ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
        long orgId = sessionUser.getSelectedOrgId();
        if (orgId != item.getOrgId())
        {
            request.returnHistory("您选择的组织和选择的上级部门不匹配");
            return;
        }
        
        ZmrDept parent = ORM.table().item(ZmrDept.class, item.getOrgId(), item.getParentId());
        if (parent == null || !parent.isDeptValid())
        {
            request.returnHistory("上级部门不存在或停用,请选择正确的部门");
            return;
        }
        
        item.setOrgId(orgId);
        item.setDeptLevel(parent.getDeptLevel()+1);
        if (parent.getDeptLevel() == 0)
            item.setDeptParentAll(""+parent.getDeptId());
        else
            item.setDeptParentAll(parent.getDeptParentAll() + "," + parent.getDeptId());
        
        ORM.table().insert(item);
        
        //修改负责人所属部门
        List<String> operatorCodeList = new ArrayList<String>();
        if (Validates.isNotEmptyBlank(item.getDeptManager()))
            operatorCodeList.add(item.getDeptManager());
        if (Validates.isNotEmptyBlank(item.getDeptSubManager()))
            operatorCodeList.addAll(Lists.toStringList(item.getDeptSubManager()));
        
        ZmrDeptDao.doUpdateDeptManeger(request, operatorCodeList, item.getDeptId()+"", item.getDeptParentAll());
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "增加部门", Jsons.toString(item));
    }

    @Override
    protected void update(HttpRequest request) throws Exception
    {
        ZmrDept item = request.getParameter(ZmrDept.class);
        ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
        long orgId = sessionUser.getSelectedOrgId();
        if (orgId != item.getOrgId())
        {
            request.returnHistory("您选择的组织和选择的上级部门不匹配");
            return;
        }
        
        ORM.table().update(item);
        
        //修改负责人所属部门
        List<String> operatorCodeList = new ArrayList<String>();
        if(Validates.isNotEmptyBlank(item.getDeptManager()))
            operatorCodeList.add(item.getDeptManager());
        if(Validates.isNotEmptyBlank(item.getDeptSubManager()))
            operatorCodeList.addAll(Lists.toStringList(item.getDeptSubManager()));
        
        ZmrDeptDao.doUpdateDeptManeger(request, operatorCodeList, item.getDeptId()+"", item.getDeptParentAll());
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "修改部门", Jsons.toString(item));
    }

    @Override
    protected void delete(HttpRequest request) throws Exception
    {
        ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
        long orgId = sessionUser.getSelectedOrgId();
        long deptId = request.getParameterLong("deptId");
        ZmrDept item = ORM.table().item(ZmrDept.class, orgId, deptId);
        if (item == null)
        {
            request.returnHistory("请选择一个有效的部门");
            return;
        }
        
        if (item.getDeptLevel() == 0)
        {
            request.returnHistory("顶级部门不能删除,只允许修改");
            return;
        }
        
        if (ORM.table().count(ZmrDept.class, new Selector("orgId", orgId).addMust("parentId", deptId)) > 0)
        {
            request.returnHistory("该部门有下级部门,要先删除下级部门才能删除本部门");
            return;
        }
        
        if (ORM.table().count(ZmrOperator.class, new Selector("orgId", orgId).addMustLike("operatorDept", deptId)) > 0)
        {
            request.returnHistory("该部门已有操作员,请删除部门成员后再删除本部门");
            return;
        }
        
        ORM.table().delete(ZmrDept.class, orgId, deptId);
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "删除部门", Jsons.toString("orgId", orgId, "deptId", deptId));
    }
}