跳转至

流程审批历史的函数

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

脚本说明

该脚本用于获取流程的审批历史记录。通过查询审批历史表,可以获取流程中所有节点的审批信息,包括审批人、审批时间、审批意见等。

常见实现方式

方式1:获取所有审批历史

(function(){
    var doc = getCurrentDocument();
    var docId = doc.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_name,state_label,attitude,processtime ";
        sql += "from t_actorhis ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "order by processtime";

        var history = queryBySQL(sql);
        var historyList = [];

        if(history != null && history.size() > 0){
            for(var it = history.iterator(); it.hasNext(); ){
                var h = it.next();
                historyList.push({
                    actor: h.getItemValueAsString("actor_name"),
                    stateLabel: h.getItemValueAsString("state_label"),
                    attitude: h.getItemValueAsString("attitude"),
                    processTime: format(h.getItemValueAsDate("processtime"), "yyyy-MM-dd HH:mm:ss")
                });
            }
        }

        return historyList;
    }

    return [];
})();

方式2:获取最后一条审批历史

(function(){
    var doc = getCurrentDocument();
    var docId = doc.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_name,state_label,attitude,processtime ";
        sql += "from t_actorhis ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "order by processtime desc limit 1";

        var lastHistory = findBySQL(sql);

        if(lastHistory != null){
            return {
                actor: lastHistory.getItemValueAsString("actor_name"),
                stateLabel: lastHistory.getItemValueAsString("state_label"),
                attitude: lastHistory.getItemValueAsString("attitude"),
                processTime: format(lastHistory.getItemValueAsDate("processtime"), "yyyy-MM-dd HH:mm:ss")
            };
        }
    }

    return null;
})();

方式3:获取指定节点的审批历史

(function(){
    var doc = getCurrentDocument();
    var docId = doc.getId();
    var nodeName = "节点名称";

    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_name,state_label,attitude,processtime ";
        sql += "from t_actorhis ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "and state_label = '"+nodeName+"' ";
        sql += "order by processtime";

        var history = queryBySQL(sql);
        var historyList = [];

        if(history != null && history.size() > 0){
            for(var it = history.iterator(); it.hasNext(); ){
                var h = it.next();
                historyList.push({
                    actor: h.getItemValueAsString("actor_name"),
                    attitude: h.getItemValueAsString("attitude"),
                    processTime: format(h.getItemValueAsDate("processtime"), "yyyy-MM-dd HH:mm:ss")
                });
            }
        }

        return historyList;
    }

    return [];
})();

方式4:获取审批历史并格式化显示

(function(){
    var doc = getCurrentDocument();
    var docId = doc.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_name,state_label,attitude,processtime ";
        sql += "from t_actorhis ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "order by processtime";

        var history = queryBySQL(sql);
        var html = "<table border='1'><tr><th>节点</th><th>审批人</th><th>审批意见</th><th>审批时间</th></tr>";

        if(history != null && history.size() > 0){
            for(var it = history.iterator(); it.hasNext(); ){
                var h = it.next();
                html += "<tr>";
                html += "<td>" + h.getItemValueAsString("state_label") + "</td>";
                html += "<td>" + h.getItemValueAsString("actor_name") + "</td>";
                html += "<td>" + (h.getItemValueAsString("attitude") != null ? h.getItemValueAsString("attitude") : "") + "</td>";
                html += "<td>" + format(h.getItemValueAsDate("processtime"), "yyyy-MM-dd HH:mm:ss") + "</td>";
                html += "</tr>";
            }
        }

        html += "</table>";
        return html;
    }

    return "无审批历史";
})();

扩展示例

示例1:获取审批历史并设置到字段

(function(){
    var doc = getCurrentDocument();
    var docId = doc.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_name,attitude,processtime ";
        sql += "from t_actorhis ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "order by processtime desc limit 1";

        var lastHistory = findBySQL(sql);

        if(lastHistory != null){
            var attitude = lastHistory.getItemValueAsString("attitude");
            doc.findItem("最后审批意见").setValue(attitude != null ? attitude : "");
        }
    }
})();

示例2:统计审批历史信息

(function(){
    var doc = getCurrentDocument();
    var docId = doc.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_name,attitude,processtime ";
        sql += "from t_actorhis ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "order by processtime";

        var history = queryBySQL(sql);
        var totalCount = 0;
        var agreeCount = 0;
        var disagreeCount = 0;
        var actorList = [];

        if(history != null && history.size() > 0){
            totalCount = history.size();

            for(var it = history.iterator(); it.hasNext(); ){
                var h = it.next();
                var actor = h.getItemValueAsString("actor_name");
                var attitude = h.getItemValueAsString("attitude");

                if(!actorList.contains(actor)){
                    actorList.push(actor);
                }

                if(attitude != null){
                    if(attitude.indexOf("同意") >= 0 || attitude.indexOf("通过") >= 0){
                        agreeCount++;
                    }else if(attitude.indexOf("不同意") >= 0 || attitude.indexOf("不通过") >= 0){
                        disagreeCount++;
                    }
                }
            }
        }

        return {
            totalCount: totalCount,
            agreeCount: agreeCount,
            disagreeCount: disagreeCount,
            actorCount: actorList.length,
            actors: actorList.join("、")
        };
    }

    return null;
})();

示例3:获取审批历史时间线

(function(){
    var doc = getCurrentDocument();
    var docId = doc.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_name,state_label,attitude,processtime ";
        sql += "from t_actorhis ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "order by processtime";

        var history = queryBySQL(sql);
        var timeline = [];

        if(history != null && history.size() > 0){
            var previousTime = null;

            for(var it = history.iterator(); it.hasNext(); ){
                var h = it.next();
                var processTime = h.getItemValueAsDate("processtime");
                var duration = "";

                if(previousTime != null){
                    var diff = processTime.getTime() - previousTime.getTime();
                    var hours = Math.floor(diff / (1000 * 60 * 60));
                    var minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
                    duration = hours + "小时" + minutes + "分钟";
                }

                timeline.push({
                    node: h.getItemValueAsString("state_label"),
                    actor: h.getItemValueAsString("actor_name"),
                    attitude: h.getItemValueAsString("attitude"),
                    time: format(processTime, "yyyy-MM-dd HH:mm:ss"),
                    duration: duration
                });

                previousTime = processTime;
            }
        }

        return timeline;
    }

    return [];
})();

示例4:获取审批历史并生成报告

(function(){
    var doc = getCurrentDocument();
    var docId = doc.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_name,state_label,attitude,processtime ";
        sql += "from t_actorhis ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "order by processtime";

        var history = queryBySQL(sql);
        var report = "审批历史报告\n";
        report += "==================\n\n";

        if(history != null && history.size() > 0){
            var index = 1;
            for(var it = history.iterator(); it.hasNext(); ){
                var h = it.next();
                report += index + ". " + h.getItemValueAsString("state_label") + "\n";
                report += "   审批人:" + h.getItemValueAsString("actor_name") + "\n";
                report += "   审批意见:" + (h.getItemValueAsString("attitude") != null ? h.getItemValueAsString("attitude") : "无") + "\n";
                report += "   审批时间:" + format(h.getItemValueAsDate("processtime"), "yyyy-MM-dd HH:mm:ss") + "\n\n";
                index++;
            }
        }else{
            report += "暂无审批历史\n";
        }

        return report;
    }

    return "无法获取审批历史";
})();

示例5:获取审批历史并判断流程状态

(function(){
    var doc = getCurrentDocument();
    var docId = doc.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_name,state_label,attitude,processtime ";
        sql += "from t_actorhis ";
        sql += "where flowstatert_id = '"+flowStateId+"' ";
        sql += "order by processtime";

        var history = queryBySQL(sql);
        var hasReject = false;
        var hasAgree = false;

        if(history != null && history.size() > 0){
            for(var it = history.iterator(); it.hasNext(); ){
                var h = it.next();
                var attitude = h.getItemValueAsString("attitude");

                if(attitude != null){
                    if(attitude.indexOf("不同意") >= 0 || attitude.indexOf("不通过") >= 0 || attitude.indexOf("驳回") >= 0){
                        hasReject = true;
                    }
                    if(attitude.indexOf("同意") >= 0 || attitude.indexOf("通过") >= 0){
                        hasAgree = true;
                    }
                }
            }
        }

        return {
            history: history != null ? history.size() : 0,
            hasReject: hasReject,
            hasAgree: hasAgree,
            status: hasReject ? "有驳回" : (hasAgree ? "有通过" : "进行中")
        };
    }

    return null;
})();

关键函数说明

  • getCurrentDocument(): 获取当前文档对象
  • doc.getId(): 获取文档ID
  • queryBySQL(sql): 执行SQL查询
  • findBySQL(sql): 执行SQL查询并返回第一条记录
  • format(date, format): 格式化日期

系统表说明

  • t_flowstatert: 流程状态运行时表,存储流程的运行时状态
  • t_actorhis: 流程执行人历史表,存储审批历史记录
  • actor_name: 审批人姓名
  • state_label: 节点名称(状态标签)
  • attitude: 审批意见
  • processtime: 处理时间

审批历史字段说明

  • actor_name: 审批人姓名
  • state_label: 节点名称(状态标签)
  • attitude: 审批意见,可能为空
  • processtime: 处理时间(审批时间)

注意事项

  • ⚠️ 流程状态ID: 需要通过文档ID查询流程状态ID,才能查询审批历史
  • ⚠️ 排序方式: 通常按 processtime 排序,获取按时间顺序的审批历史
  • ⚠️ 审批意见可能为空: attitude 字段可能为null,需要判断
  • ⚠️ 时间格式: 处理时间需要格式化后显示
  • 查询时注意字段名称的正确性
  • 审批历史记录的是已处理的审批,不包括待处理的审批
  • 建议在需要展示审批历史时使用

使用场景

  • 显示审批历史记录
  • 统计审批信息
  • 生成审批报告
  • 审批历史分析
  • 流程状态判断

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