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 java.util.Map;

public class DeductionPayDWFormatSQLEvent implements DataWindowFormatSQLEventInterface {
    private static final Logger LOGGER = SDK.getLogAPI().getLogger(DeductionPayDWFormatSQLEvent.class);//记录

    /**
     * 格式化sql
     * @param userContext
     * @param dataView
     * @param sql
     * @return
     */
    @Override
    public String formatSQL(UserContext userContext, DataView dataView, String sql) {
        LOGGER.info("初始sql:" + sql);
        try {
            Map<String, Object> sqlParam = dataView.getDatagrid().getSqlParams();
            LOGGER.info("sqlParam:" + sqlParam.toString());
            if (sqlParam.containsKey("ID") && sqlParam.get("ID") != null) {
                sql = sql.replace("UPPER(d.ID) = :ID", "FIND_IN_SET(ID,:ID)>0");
                sql = sql.replace("d.ID = :ID", "FIND_IN_SET(ID,:ID)>0");
                sql = sql.replace("UPPER(ID) = :ID", "FIND_IN_SET(ID,:ID)>0");
                sql = sql.replace("ID = :ID", "FIND_IN_SET(ID,:ID)>0");
            }
        } catch (Exception ex) {
            LOGGER.error("格式化sql异常:" + ex.getMessage());
        }
        LOGGER.info("格式化后的sql:" + sql);
        return sql;
    }
}
