package com.awspaas.user.apps.donenow_crm.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.Arrays;
import java.util.List;
import java.util.Map;

public class TestForm implements DataWindowFormatSQLEventInterface {
    private static final Logger LOGGER = SDK.getLogAPI().getLogger(TestForm.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 paramName = "EXTTEXT1";
            // 1. 先处理参数值：自动加%（避免前端未加导致模糊匹配失效）
            if (sqlParam.containsKey(paramName)) {
                String paramValue = sqlParam.get(paramName).toString().trim();
                // 若参数值不含%，则自动添加前后%（实现包含匹配）
                if (!paramValue.contains("%")) {
                    sqlParam.put(paramName, "%" + paramValue + "%");
                    LOGGER.info("【处理后参数】" + JSON.toJSONString(sqlParam));
                }
            }

            List<String> targetFields = Arrays.asList(
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.REFERENCE_NAME",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.CLOSED",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.ACCOUNT_ID",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.REMARK",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT1",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT2",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT3",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT4",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT5",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT6",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT7",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT8",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT9",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT10",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT11",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT12",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT13",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT14",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT15",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT16",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT17",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT18",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT19",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT20",
                    "BO_EU_DNCRM_INSTALLED_PRODUCT.LOCATION"

            );

            if (sqlParam.containsKey(paramName)) {
                // 2. 精确删除原SQL中的ACCOUNT_ID等于条件（字符串必须和原SQL完全一致）
                String oldAccountCondition = "AND BO_EU_DNCRM_INSTALLED_PRODUCT.EXTTEXT1 = :EXTTEXT1";
                sql = sql.replace(oldAccountCondition, ""); // 替换为空，删除该条件
                LOGGER.info("【删除旧条件后SQL】" + sql);

                // 3. 拼接多字段OR条件（不变）
                StringBuilder orConditions = new StringBuilder();
                for (int i = 0; i < targetFields.size(); i++) {
                    String field = targetFields.get(i);
                    orConditions.append("UPPER(").append(field).append(") LIKE :").append(paramName);
                    if (i != targetFields.size() - 1) {
                        orConditions.append(" OR ");
                    }
                }

                // 4. 追加OR条件
                sql = sql + " AND (" + orConditions + ")";
            }

            LOGGER.info("【最终拼接后SQL】" + sql);

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

        return sql;
    }
}