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

import com.actionsoft.bpms.commons.at.AbstExpression;
import com.actionsoft.bpms.commons.at.ExpressionContext;
import com.actionsoft.exception.AWSExpressionException;
import com.actionsoft.sdk.local.SDK;

public class getSNListSql extends AbstExpression {
    public getSNListSql(ExpressionContext atContext, String expressionValue) {
        super(atContext, expressionValue);
    }

    /**
     *把输入的SN列表转换成SQL
     * @param s
     * @return
     * @throws AWSExpressionException
     */
    @Override
    public String execute(String s) throws AWSExpressionException {
        String sns = getParameter(s, 1).trim();

        if ((sns.startsWith("select") || sns.startsWith("SELECT")) == false)
            throw new AWSExpressionException("请输入正确的SQL语句！");
        //需要对sql加一些校验，避免sql注入攻击
        if (sns.contains(";") || sns.contains("--") || sns.contains("/*") || sns.contains("*/"))
            throw new AWSExpressionException("请输入正确的SQL语句！");
        sns = SDK.getRuleAPI().executeAtScript("@sqlValue(" + sns + ")", getExpressionContext().getUserContext());

        System.out.println("产品序列号：" + sns);
        String snSql = null;
        int i = 0;
        for (String sn : sns.split(",")) {
            if (i == 0) {
                snSql = "SELECT '" + sn + "' AS sn";
            } else {
                snSql += " UNION SELECT '" + sn + "' AS sn";

            }
            i++;
        }
        return snSql;
    }
}
