package com.awspaas.user.apps.donenow_ctt.at;

import com.actionsoft.bpms.bo.design.cache.BOCache;
import com.actionsoft.bpms.bo.design.model.BOModel;
import com.actionsoft.bpms.commons.at.AbstExpression;
import com.actionsoft.bpms.commons.at.ExpressionContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.exception.AWSExpressionException;
import org.apache.commons.lang3.StringUtils;

/**
 * 通过boId获取流程实例ID
 */
public class getProcessInstIdByBoId extends AbstExpression {
    public getProcessInstIdByBoId(ExpressionContext atContext, String expressionValue) {
        super(atContext, expressionValue);
    }

    /**
     *获取流程ID
     * @param s
     * @return
     * @throws AWSExpressionException
     */
    @Override
    public String execute(String s) throws AWSExpressionException {
        String boName = getParameter(s, 1).trim();
        String boId = getParameter(s, 2).trim();

        boName = boName.toUpperCase();

        if (StringUtils.isBlank(boName) || StringUtils.isBlank(boId) || !boName.startsWith("BO_EU_DN"))
            return null;

        BOModel boModel = BOCache.getInstance().getModelByEntityName(boName);
        if (boModel == null) {
            return null;
        }
        boName = boModel.getName().toUpperCase();

        if (!boName.startsWith("BO_EU_DN"))
            return null;

        return DBSql.getString("select BINDID FROM " + boName + " where ID =?", new Object[]{boId});
    }
}
