跳转至

判断是否为经办的sql语句

本文档内容来源于 http://doc.myapps.cn/docs/common-script/common-script-1f1a4akebjsoa

脚本说明

该脚本用于判断当前用户是否为流程的经办人。经办人是指流程中负责处理某个节点的用户。通过SQL语句查询流程执行人表,可以判断当前用户是否在经办人列表中。

常见实现方式

方式1:判断当前用户是否为经办人

(function(){
    var doc = getCurrentDocument();
    var docId = doc.getId();
    var currentUserId = getWebUser().getId();

    // 查询流程状态
    var flowStateSql = "select domainid,id ";
    flowStateSql += "from t_flowstatert ";
    flowStateSql += "where docid = '"+docId+"'";
    var flowState = findBySQL(flowStateSql);

    if(flowState != null){
        var flowStateId = flowState.getId();

        // 判断当前用户是否为经办人(state=0表示待处理)
        var sql = "select domainid,count(*) as count ";
        sql += "from t_actor ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "and actor_id = '"+currentUserId+"' ";
        sql += "and state = 0";

        var result = findBySQL(sql);

        if(result != null && result.getItemValueAsDouble("count") > 0){
            return true;  // 是经办人
        }
    }

    return false;  // 不是经办人
})();

方式2:使用countBySQL判断

(function(){
    var doc = getCurrentDocument();
    var docId = doc.getId();
    var currentUserId = getWebUser().getId();

    var flowStateSql = "select domainid,id ";
    flowStateSql += "from t_flowstatert ";
    flowStateSql += "where docid = '"+docId+"'";
    var flowState = findBySQL(flowStateSql);

    if(flowState != null){
        var flowStateId = flowState.getId();

        var sql = "select domainid ";
        sql += "from t_actor ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "and actor_id = '"+currentUserId+"' ";
        sql += "and state = 0";

        var count = countBySQL(sql);

        return count > 0;  // 大于0表示是经办人
    }

    return false;
})();

方式3:判断是否为指定节点的经办人

(function(){
    var doc = getCurrentDocument();
    var docId = doc.getId();
    var currentUserId = getWebUser().getId();
    var nodeId = "节点ID";

    var flowStateSql = "select domainid,id ";
    flowStateSql += "from t_flowstatert ";
    flowStateSql += "where docid = '"+docId+"'";
    var flowState = findBySQL(flowStateSql);

    if(flowState != null){
        var flowStateId = flowState.getId();

        var sql = "select domainid ";
        sql += "from t_actor ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "and actor_id = '"+currentUserId+"' ";
        sql += "and node_id = '"+nodeId+"' ";
        sql += "and state = 0";

        var count = countBySQL(sql);

        return count > 0;
    }

    return false;
})();

方式4:判断是否为当前节点的经办人

(function(){
    var doc = getCurrentDocument();
    var docId = doc.getId();
    var currentUserId = getWebUser().getId();
    var currentStateLabel = doc.getStateLabel();

    var flowStateSql = "select domainid,id ";
    flowStateSql += "from t_flowstatert ";
    flowStateSql += "where docid = '"+docId+"'";
    var flowState = findBySQL(flowStateSql);

    if(flowState != null){
        var flowStateId = flowState.getId();

        // 查询当前节点的节点ID
        var nodeSql = "select domainid,id ";
        nodeSql += "from t_flow_node ";
        nodeSql += "where flow_id = (select flow_id from t_flowstatert where id = '"+flowStateId+"') ";
        nodeSql += "and node_name = '"+currentStateLabel+"'";
        var node = findBySQL(nodeSql);

        if(node != null){
            var nodeId = node.getId();

            var sql = "select domainid ";
            sql += "from t_actor ";
            sql += "where flowstatert_id = '"+flowStateId+"' ";
            sql += "and actor_id = '"+currentUserId+"' ";
            sql += "and node_id = '"+nodeId+"' ";
            sql += "and state = 0";

            var count = countBySQL(sql);

            return count > 0;
        }
    }

    return false;
})();

扩展示例

示例1:判断并返回详细信息

(function(){
    var doc = getCurrentDocument();
    var docId = doc.getId();
    var currentUserId = getWebUser().getId();

    var flowStateSql = "select domainid,id ";
    flowStateSql += "from t_flowstatert ";
    flowStateSql += "where docid = '"+docId+"'";
    var flowState = findBySQL(flowStateSql);

    if(flowState != null){
        var flowStateId = flowState.getId();

        var sql = "select domainid,actor_id,actor_name,node_id,state ";
        sql += "from t_actor ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "and actor_id = '"+currentUserId+"'";

        var actor = findBySQL(sql);

        if(actor != null){
            return {
                isHandler: true,
                actorName: actor.getItemValueAsString("actor_name"),
                nodeId: actor.getItemValueAsString("node_id"),
                state: actor.getItemValueAsString("state"),
                isPending: actor.getItemValueAsString("state") === "0"
            };
        }
    }

    return {
        isHandler: false,
        actorName: "",
        nodeId: "",
        state: "",
        isPending: false
    };
})();

示例2:判断并执行不同逻辑

(function(){
    var doc = getCurrentDocument();
    var docId = doc.getId();
    var currentUserId = getWebUser().getId();

    var flowStateSql = "select domainid,id ";
    flowStateSql += "from t_flowstatert ";
    flowStateSql += "where docid = '"+docId+"'";
    var flowState = findBySQL(flowStateSql);

    if(flowState != null){
        var flowStateId = flowState.getId();

        var sql = "select domainid ";
        sql += "from t_actor ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "and actor_id = '"+currentUserId+"' ";
        sql += "and state = 0";

        var count = countBySQL(sql);
        var isHandler = count > 0;

        if(isHandler){
            // 是经办人,执行经办人逻辑
            doc.findItem("处理权限").setValue("经办人");
            return "您是经办人,可以处理此流程";
        }else{
            // 不是经办人,执行非经办人逻辑
            doc.findItem("处理权限").setValue("非经办人");
            return "您不是经办人,无法处理此流程";
        }
    }

    return "无法判断";
})();

示例3:判断是否为已处理的经办人

(function(){
    var doc = getCurrentDocument();
    var docId = doc.getId();
    var currentUserId = getWebUser().getId();

    var flowStateSql = "select domainid,id ";
    flowStateSql += "from t_flowstatert ";
    flowStateSql += "where docid = '"+docId+"'";
    var flowState = findBySQL(flowStateSql);

    if(flowState != null){
        var flowStateId = flowState.getId();

        // 判断是否为已处理的经办人(state=1)
        var sql = "select domainid ";
        sql += "from t_actor ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "and actor_id = '"+currentUserId+"' ";
        sql += "and state = 1";

        var count = countBySQL(sql);

        return count > 0;  // 是已处理的经办人
    }

    return false;
})();

示例4:判断是否为任何节点的经办人

(function(){
    var doc = getCurrentDocument();
    var docId = doc.getId();
    var currentUserId = getWebUser().getId();

    var flowStateSql = "select domainid,id ";
    flowStateSql += "from t_flowstatert ";
    flowStateSql += "where docid = '"+docId+"'";
    var flowState = findBySQL(flowStateSql);

    if(flowState != null){
        var flowStateId = flowState.getId();

        // 判断是否为任何节点的经办人(不限制state)
        var sql = "select domainid ";
        sql += "from t_actor ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "and actor_id = '"+currentUserId+"'";

        var count = countBySQL(sql);

        return count > 0;
    }

    return false;
})();

示例5:获取经办人列表并判断

(function(){
    var doc = getCurrentDocument();
    var docId = doc.getId();
    var currentUserId = getWebUser().getId();

    var flowStateSql = "select domainid,id ";
    flowStateSql += "from t_flowstatert ";
    flowStateSql += "where docid = '"+docId+"'";
    var flowState = findBySQL(flowStateSql);

    if(flowState != null){
        var flowStateId = flowState.getId();

        // 获取所有经办人
        var sql = "select domainid,actor_id,actor_name ";
        sql += "from t_actor ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "and state = 0";

        var actors = queryBySQL(sql);
        var isHandler = false;
        var handlerList = [];

        if(actors != null && actors.size() > 0){
            for(var it = actors.iterator(); it.hasNext(); ){
                var actor = it.next();
                var actorId = actor.getItemValueAsString("actor_id");
                var actorName = actor.getItemValueAsString("actor_name");

                handlerList.push({
                    id: actorId,
                    name: actorName
                });

                if(actorId === currentUserId){
                    isHandler = true;
                }
            }
        }

        return {
            isHandler: isHandler,
            handlers: handlerList,
            handlerCount: handlerList.length
        };
    }

    return {
        isHandler: false,
        handlers: [],
        handlerCount: 0
    };
})();

关键函数说明

  • getCurrentDocument(): 获取当前文档对象
  • doc.getId(): 获取文档ID
  • getWebUser().getId(): 获取当前用户ID
  • queryBySQL(sql): 执行SQL查询
  • findBySQL(sql): 执行SQL查询并返回第一条记录
  • countBySQL(sql): 统计查询结果数量

系统表说明

  • t_flowstatert: 流程状态运行时表,存储流程的运行时状态
  • t_actor: 流程执行人表,存储当前待处理的执行人信息
  • actor_id: 执行人ID(经办人ID)
  • actor_name: 执行人姓名
  • node_id: 节点ID
  • state: 状态(0=待处理,1=已处理)
  • flowstatert_id: 流程状态运行时ID

SQL语句说明

基本查询结构

SELECT domainid 
FROM t_actor 
WHERE flowstatert_id = '流程状态ID' 
  AND actor_id = '用户ID' 
  AND state = 0

关键条件

  • flowstatert_id: 流程状态运行时ID,通过文档ID查询获取
  • actor_id: 经办人ID,当前用户ID
  • state = 0: 待处理状态,表示是当前待处理的经办人
  • state = 1: 已处理状态,表示是已处理过的经办人

经办人状态说明

  • state = 0: 待处理状态,表示该经办人还未处理
  • state = 1: 已处理状态,表示该经办人已经处理过
  • 判断经办人: 通常判断 state = 0 的记录,表示当前待处理的经办人

注意事项

  • ⚠️ 流程状态ID: 需要通过文档ID查询流程状态ID,才能查询经办人
  • ⚠️ 状态判断: 通常判断 state = 0(待处理)的经办人
  • ⚠️ 用户ID: 使用 getWebUser().getId() 获取当前用户ID
  • ⚠️ 空值判断: 查询结果可能为空,需要判断返回值
  • 查询时注意字段名称的正确性
  • 一个节点可能有多个经办人
  • 建议在需要判断权限时使用

使用场景

  • 权限控制:判断用户是否有处理权限
  • 按钮显示:根据是否为经办人显示/隐藏按钮
  • 字段编辑:根据是否为经办人控制字段可编辑性
  • 业务逻辑:根据是否为经办人执行不同逻辑
  • 信息展示:显示经办人相关信息

如需查看完整脚本代码,请访问源文档链接。