ProjectFilingPreRuleAction.java 3.69 KB
package com.cntytz;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

/**
 * 工程备案流程前置控制规则
 * 主规则:根据工程名称,同一工程创建超过3次的,将不再被允许创建
 * 分支规则:被驳回的将失去资格,计数减一
 *
 * @author tjx
 * @date 2020/4/15
 */
public class ProjectFilingPreRuleAction extends BaseBean implements Action {

    Log logger = LogFactory.getLog(ProjectFilingPreRuleAction.class);

    /**
     * 判断次数
     */
    private Integer times = 3;

    /**
     * 表单名称,比如: formtable_main_16
     * 参数
     */
    private String tableName;
    /**
     * 表单字段,比如gcmc
     * 参数
     */
    private String colName;

    /**
     * 错误提示信息
     */
    private String errorMsg;

    /**
     * 流程ID
     * 参数
     */
    private String flowId;

    /**
     * 调试模式 D:debug;R:release
     */
    private String debug = "R";

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getColName() {
        return colName;
    }

    public void setColName(String colName) {
        this.colName = colName;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public String getFlowId() {
        return flowId;
    }

    public void setFlowId(String flowId) {
        this.flowId = flowId;
    }

    public Integer getTimes() {
        return times;
    }

    public void setTimes(Integer times) {
        this.times = times;
    }

    /**
     * 执行器
     *
     * @param requestInfo
     * @return
     */
    @Override
    public String execute(RequestInfo requestInfo) {

        if ("D".equals(this.debug.toUpperCase())) {
            logger.debug("参数设置为:tableName:" + tableName + " colName: " + colName + " flowId: " + flowId + " times: " + times + " errorMsg: " + errorMsg);
        }
        RecordSet record = new RecordSet();
        String sql = getRuleSql(requestInfo);
        if ("D".equals(this.debug.toUpperCase())) {
            logger.debug("sql: " + sql);
        }
        record.execute(sql);
        while (record.next()) {
            int count = record.getInt("num");
            if (count >= times) {
                requestInfo.getRequestManager().setMessagecontent(errorMsg);
                return FAILURE_AND_CONTINUE;
            } else {
                return SUCCESS;
            }
        }
        return SUCCESS;
    }

    /**
     * 获取判断sql
     *
     * @param requestInfo
     * @return sql
     */
    private String getRuleSql(RequestInfo requestInfo) {
        String ruleSql = "select count(1) as num from " + tableName + " f inner join workflow_requestbase wr on wr.REQUESTID = f.requestId where wr.WORKFLOWID = " + flowId + " and wr.CURRENTNODETYPE in (1,3)and f." + colName + " = '";
        Property[] properties = requestInfo.getMainTableInfo().getProperty();
        for (Property property : properties) {
            if (property.getName().equals(colName.trim())) {
                if (property.getValue() == null || property.getValue().length() == 0) {
                    return "";
                }
                String value = property.getValue().trim();
                ruleSql += value + "'";
                return ruleSql;
            }
        }
        return "";
    }
}