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

import com.actionsoft.bpms.dw.design.event.DataWindowFormatSQLEventInterface;
import com.actionsoft.bpms.dw.exec.component.DataView;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.Logger;
import com.alibaba.fastjson.JSON;

import java.util.Map;

public class contractTest implements DataWindowFormatSQLEventInterface {
    private static final Logger LOGGER = SDK.getLogAPI().getLogger(contractTest.class);

    @Override
    public String formatSQL(UserContext userContext, DataView dataView, String sql) {
        try {
            Map<String, Object> sqlParam = dataView.getDatagrid().getSqlParams();
            LOGGER.info("【初始SQL】" + sql);
            LOGGER.info("【查询参数】" + JSON.toJSONString(sqlParam));

            String sql1 = " 1=1 ";

            if (sqlParam.containsKey("TEST_SELECT")) {
                sql1 += " AND exists(select 1 from bo_eu_dncrm_installed_product p " +
                        "where p.contract_id = bo_eu_dnctt_contract.id " +
                        "and exttext3 like concat('%', :TEST_SELECT, '%')) ";

                sql = sql.replace("AND BO_EU_DNCTT_CONTRACT.TEST_SELECT = :TEST_SELECT", "");
            }

            sql = sql.replace("1=1", sql1);
            LOGGER.info("【拼接后SQL】" + sql);

        } catch (Exception e) {
            LOGGER.error("SQL条件拼接失败", e);
            e.printStackTrace();
        }

        return sql;
    }
}