//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.jeecg.modules.jmreport.desreport.a.service.a;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.sql.SQLSyntaxErrorException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;

import com.boman.domain.GenTableColumn;
import com.boman.gen.api.RemoteGenTableColumnService;
import com.boman.report.service.MysqlTableService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.jmreport.common.expetion.JimuReportException;
import org.jeecg.modules.jmreport.common.util.ApiAssert;
import org.jeecg.modules.jmreport.common.util.CommonUtils;
import org.jeecg.modules.jmreport.common.util.DesUtils;
import org.jeecg.modules.jmreport.common.util.JimuSpringContextUtils;
import org.jeecg.modules.jmreport.common.util.oConvertUtils;
import org.jeecg.modules.jmreport.common.vo.Result;
import org.jeecg.modules.jmreport.config.client.JmReportTokenClient;
import org.jeecg.modules.jmreport.desreport.dao.JimuReportDbDao;
import org.jeecg.modules.jmreport.desreport.dao.JimuReportDbParamDao;
import org.jeecg.modules.jmreport.desreport.entity.*;
import org.jeecg.modules.jmreport.desreport.render.handler.convert.ApiDataConvertAdapter;
import org.jeecg.modules.jmreport.desreport.render.utils.RegexMatches;
import org.jeecg.modules.jmreport.desreport.service.IJimuReportService;
import org.jeecg.modules.jmreport.desreport.service.IJmReportDbFieldService;
import org.jeecg.modules.jmreport.desreport.service.IJmReportDbParamService;
import org.jeecg.modules.jmreport.desreport.service.IJmReportDbSourceService;
import org.jeecg.modules.jmreport.desreport.util.d;
import org.jeecg.modules.jmreport.desreport.util.j;
import org.jeecg.modules.jmreport.dyndb.JmreportDataSourceCachePool;
import org.jeecg.modules.jmreport.dyndb.JmreportDynamicDbUtil;
import org.jeecg.modules.jmreport.dyndb.util.JmreportSqlUtils;
import org.jeecg.modules.jmreport.dyndb.vo.JmreportDynamicDataSourceVo;
import org.jeecgframework.minidao.pojo.MiniDaoPage;
import org.jeecgframework.minidao.util.MiniDaoUtil;
import org.jeecgframework.minidao.util.SnowflakeIdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.ResourceAccessException;

@Service
@Primary
public class JmReportDbService implements IJmReportDbService {
    private static final Logger a = LoggerFactory.getLogger(JmReportDbService.class);
    @Autowired
    private IJmReportDbParamService jmReportParamService;
    @Autowired
    private IJmReportDbFieldService reportDbFieldService;
    @Autowired
    private IJmReportDbSourceService jmReportDbSourceService;
    @Autowired
    private JmreportDynamicDbUtil jmreportDynamicDbUtil;
    @Autowired
    private JmReportTokenClient jimuTokenClient;
    @Autowired
    private JimuReportDbDao reportDbDao;
    @Autowired
    private JimuReportDbParamDao dbParamDao;
    @Autowired
    private IJimuReportService jimuReportService;
    @Autowired(required = false)
    private ApiDataConvertAdapter apiDataConverAdapter;
    @Value("${jeecg.jmreport.apiBasePath:}")
    private String apiBasePath;
    @Autowired
    private RemoteGenTableColumnService remoteGenTableColumnService;
    @Autowired
    private MysqlTableService mysqlTableService;

    @Override
    @Transactional
    public JmReportDb saveDb(JmReportDb reportDb) {
        if ("1".equals(reportDb.getIsPage())) {
            List<JmReportDb> var2 = this.reportDbDao.selectList(reportDb.getJimuReportId());
            var2.forEach((var1) -> {
                var1.setIsPage("0");
                if (oConvertUtils.isEmpty(var1.getDbSource())) {
                    var1.setDbSource("");
                }

                this.a(var1);
            });
        }

        String var7;
        if ("0".equals(reportDb.getDbType())) {
            var7 = reportDb.getDbSource();
            if (oConvertUtils.isNotEmpty(var7)) {
                JmReportDataSource var3 = this.jmReportDbSourceService.getById(var7);
                if (var3 != null) {
                    reportDb.setDbSourceType(MiniDaoUtil.getDbType(var3.getDbUrl()));
                }
            } else {
                String var8 = CommonUtils.getDatabaseType();
                reportDb.setDbSourceType(var8);
                reportDb.setDbSource("");
            }
        }

        this.a(reportDb);
        var7 = reportDb.getId();
        this.reportDbFieldService.deleteByReportId(var7);
        List<JmReportDbField> dbFields = reportDb.getFieldList();
        if (d.a(dbFields)) {
            for (JmReportDbField dbField : dbFields) {
                dbField.setJimuReportDbId(var7);
            }

            this.reportDbFieldService.saveOrUpdateBatch(dbFields);
        }

        this.jmReportParamService.deleteByReportId(var7);
        List<JmReportDbParam> dbParams = reportDb.getParamList();
        if (d.a(dbParams)) {
            for (JmReportDbParam reportDbParam : dbParams) {
                reportDbParam.setJimuReportHeadId(var7);
                if (reportDbParam.getSearchFlag() == null) {
                    reportDbParam.setSearchFlag(0);
                }
            }

            this.jmReportParamService.saveOrUpdateBatch(dbParams);
        }

//        JmReportDb db = new JmReportDb();
//        db.setId(reportDb.getId());
//        db.setDbCode(reportDb.getDbCode());
//        db.setDbChName(reportDb.getDbChName());
        reportDbDao.update(reportDb);
        return reportDb;
    }

    @Override
    public List<List<Map>> fieldTree(String reportId) {
        ArrayList var2 = new ArrayList();
        new JmReportDb();
        List var4 = this.reportDbDao.selectList(reportId);
        if (d.a(var4)) {
            Iterator var5 = var4.iterator();

            while (var5.hasNext()) {
                JmReportDb var6 = (JmReportDb) var5.next();
                ArrayList var7 = new ArrayList();
                HashMap var8 = new HashMap();
                var8.put("dbId", var6.getId());
                var8.put("expand", true);
                var8.put("title", var6.getDbChName());
                var8.put("code", var6.getDbCode());
                var8.put("type", var6.getDbType());
                var8.put("isList", var6.getIsList());
                List var9 = this.reportDbFieldService.getByDbId(var6.getId());
                ArrayList var10 = new ArrayList();
                HashMap var13;
                if (d.a(var9)) {
                    for (Iterator var11 = var9.iterator(); var11.hasNext(); var10.add(var13)) {
                        JmReportDbField var12 = (JmReportDbField) var11.next();
                        var13 = new HashMap();
                        var13.put("expand", true);
                        if (var12.getFieldName().equals(var12.getFieldText())) {
                            var13.put("title", var12.getFieldName());
                            var13.put("fieldText", var12.getFieldName());
                        } else {
                            var13.put("title", var12.getFieldName());
                            var13.put("fieldText", var12.getFieldText());
                        }
                    }
                }

                var8.put("children", var10);
                var7.add(var8);
                var2.add(var7);
            }
        }

        return var2;
    }

    @Override
    public boolean queryIsPage(String reportId) {
        int var2 = this.reportDbDao.getCountByReportId(reportId);
        boolean var3 = false;
        if (var2 > 0) {
            var3 = true;
        }

        return var3;
    }

    @Override
    public Map<String, Object> executeSelectSql(String sql, String dbKey, Map<String, Object> params) {
        List var4 = this.jmReportParamService.list(params.get("jimuReportHeadId").toString());
        JmReportDbParam var6;
        String var8;
        if (var4 != null && var4.size() > 0) {
            for (Iterator var5 = var4.iterator(); var5.hasNext(); sql = sql.replace("${" + var6.getParamName() + "}", var8)) {
                var6 = (JmReportDbParam) var5.next();
                Object var7 = params.get("self_" + var6.getParamName());
                var8 = "";
                if (var7 != null) {
                    var8 = var7.toString();
                } else if (var7 == null && oConvertUtils.isNotEmpty(var6.getParamValue())) {
                    var8 = var6.getParamValue();
                }
            }
        }

        HashMap var15 = new HashMap();
        Integer var16 = oConvertUtils.getInt(params.get("pageSize"), 10);
        Integer var17 = oConvertUtils.getInt(params.get("pageNo"), 1);
        var8 = "select * from (" + sql + ") jeecg_rp_temp  where 1=1";
        Object var9 = params.get("column");
        if (var9 != null) {
            var8 = var8 + " order by jeecg_rp_temp." + var9.toString() + " " + params.get("order").toString();
        }

        a.info("报表查询sql=>\r\n" + var8);
        if (oConvertUtils.isNotEmpty(dbKey)) {
            String var10 = JmreportSqlUtils.getCountSql(sql);
            Map var11 = (Map) this.jmreportDynamicDbUtil.findOne(dbKey, var10, new Object[0]);
            var15.put("total", var11.get("total"));
            JmreportDynamicDataSourceVo var12 = JmreportDataSourceCachePool.getCacheDynamicDataSourceModel(dbKey);
            String var13 = MiniDaoUtil.createPageSql(var12.getDbUrl(), var8, var17, var16);
            List var14 = this.jmreportDynamicDbUtil.findList(dbKey, var13, new Object[0]);
            var15.put("records", d.b(var14));
        } else {
            MiniDaoPage var18 = this.reportDbDao.selectPageBySql(var8, var17, var16);
            var15.put("total", var18.getResults());
            var15.put("records", d.b(var18.getResults()));
        }

        return var15;
    }

    public Map<String, Object> executeSelectApi(String api, String method, String token, String apiConvert, String paramArray) {
        HashMap var6 = new HashMap();
        if (oConvertUtils.isNotEmpty(paramArray)) {
            api = this.b(paramArray, api);
        }

        String var12;
        if (api.contains("token")) {
            String var7 = api.substring(api.indexOf("token"), api.length());
            String[] var8 = var7.split("&");
            String[] var9 = var8;
            int var10 = var8.length;

            for (int var11 = 0; var11 < var10; ++var11) {
                var12 = var9[var11];
                String[] var13 = var12.split("=");
                if (var13.length > 1 && var13[0].equals("token")) {
                    var13[1] = var13[1].replace("'", "").replace("'", "");
                    var13[1] = var13[1].replaceAll("\"", "");
                    if (oConvertUtils.isNotEmpty(var13[1])) {
                        token = var13[1];
                    }
                }
            }
        }

        JSONObject var16 = this.a(api, method, token);
        ArrayList var17 = new ArrayList();
        if (var16 != null && !"".equals(var16)) {
            Object var18 = null;
            String var19 = new String();
            String var14;
            Object var20;
            JSONObject var22;
            if (!var16.containsKey("data")) {
                if (oConvertUtils.isNotEmpty(apiConvert)) {
                    var20 = JimuSpringContextUtils.getBean(apiConvert);
                    if (var20 != null && var20 instanceof ApiDataConvertAdapter) {
                        var22 = RegexMatches.e(var16.toJSONString());
                        ApiDataConvertAdapter var24 = (ApiDataConvertAdapter) var20;
                        var14 = var24.getData(var22);
                        var19 = String.valueOf(var14);
                        var18 = JSONObject.parse(var19, new Feature[]{Feature.OrderedField});
                    }
                } else if (oConvertUtils.isNotEmpty(this.apiDataConverAdapter)) {
                    JSONObject var21 = RegexMatches.e(var16.toJSONString());
                    var12 = this.apiDataConverAdapter.getData(var21);
                    var19 = String.valueOf(var12);
                    var18 = JSONObject.parse(var19, new Feature[]{Feature.OrderedField});
                } else {
                    var18 = var16;
                    var19 = String.valueOf(var16);
                }
            } else {
                var20 = var16.get("data");
                if (null != var20) {
                    var19 = JSON.toJSONString(var20, new SerializerFeature[]{SerializerFeature.WriteMapNullValue});
                }

                var18 = JSONObject.parse(var19, new Feature[]{Feature.OrderedField});
            }

            LinkedHashMap var23 = new LinkedHashMap();
            if (var18 instanceof JSONObject) {
                var22 = JSON.parseObject(var19, new Feature[]{Feature.OrderedField});
                Iterator var26 = var22.keySet().iterator();

                while (var26.hasNext()) {
                    var14 = (String) var26.next();
                    var23.put(var14, var22.get(var14));
                    var17.add(var23);
                }
            } else if (var18 instanceof JSONArray) {
                JSONArray var25 = (JSONArray) JSONArray.parse(var19, new Feature[]{Feature.OrderedField});
                if (var25.size() > 0) {
                    JSONObject var27 = var25.getJSONObject(0);
                    Iterator var28 = var27.keySet().iterator();

                    while (var28.hasNext()) {
                        String var15 = (String) var28.next();
                        var23.put(var15, var27.get(var15));
                        var17.add(var23);
                    }
                }
            }
        }

        var6.put("records", d.b(var17));
        return var6;
    }

    private List<String> a(String var1, String var2) {
        if (oConvertUtils.isEmpty(var1)) {
            return null;
        } else {
            MiniDaoPage var3 = this.reportDbDao.selectPageBySql(var1, 1, 1);
            List var4 = var3.getResults();
            if (var4.size() < 1) {
                throw new JimuReportException("该报表sql没有数据");
            } else {
                Set var5 = ((Map) var4.get(0)).keySet();
                ArrayList var6 = new ArrayList();
                Iterator var7 = var5.iterator();

                while (var7.hasNext()) {
                    String var8 = (String) var7.next();
                    var6.add(var8.toLowerCase());
                }

                return var6;
            }
        }
    }

    @Override
    public Map loadDbData(String dbId) {
        HashMap var2 = new HashMap();
        JmReportDb var3 = this.reportDbDao.get(dbId);
        if (var3 == null) {
            return var2;
        } else {
            List var4 = this.reportDbFieldService.getByDbId(dbId);
            List var5 = this.jmReportParamService.list(dbId);
            var2.put("dbId", dbId);
            var2.put("reportDb", var3);
            var2.put("fieldList", var4);
            var2.put("paramList", var5);
            return var2;
        }
    }

    @Override
    public List<JmReportDataSource> initDataSource(String createBy) {
        List var2 = this.jmReportDbSourceService.querySourceList(createBy);
        if (oConvertUtils.isNotEmpty(var2)) {
            Iterator var3 = var2.iterator();

            while (var3.hasNext()) {
                JmReportDataSource var4 = (JmReportDataSource) var3.next();
                String var5 = var4.getDbPassword();
                if (var5.startsWith("@JimuReport")) {
                    var4.setDbPassword(DesUtils.encode(var5.replace("@JimuReport", "")));
                }
            }
        }

        return var2;
    }

    @Override
    public void saveDbSource(JmReportDataSource sysDataSource) {
        this.jmReportDbSourceService.saveOrUpdate(sysDataSource);
    }

    @Override
    public boolean querySourceCode(JmReportDataSource sysDataSource) {
        boolean var2 = false;
        Integer var3 = 0;
        if (oConvertUtils.isNotEmpty(sysDataSource.getId())) {
            JmReportDataSource var4 = this.jmReportDbSourceService.selectById(sysDataSource.getId());
            if (var4.getCode() != null) {
                if (!var4.getCode().equals(sysDataSource.getCode())) {
                    var3 = this.jmReportDbSourceService.excelQueryCode(sysDataSource.getCode());
                    if (var3 == 0) {
                        var2 = true;
                    }
                } else {
                    var2 = true;
                }
            } else {
                var3 = this.jmReportDbSourceService.excelQueryCode(sysDataSource.getCode());
                if (var3 == 0) {
                    var2 = true;
                }
            }
        } else {
            var3 = this.jmReportDbSourceService.excelQueryCode(sysDataSource.getCode());
            if (var3 == 0) {
                var2 = true;
            }
        }

        return var2;
    }

    @Override
    public void delDataSource(JmReportDataSource sysDataSource) {
        this.jmReportDbSourceService.removeById(sysDataSource.getId());
    }

    @Override
    public List<Map<String, Object>> qurestechSql(JmReportDb jmReportDb) {
        if (jmReportDb == null) {
            return null;
        } else {
            String var2 = jmReportDb.getDbDynSql();
            List var3 = this.dbParamDao.list(jmReportDb.getId());
            JSONObject var4 = new JSONObject();
            Iterator var5 = var3.iterator();

            while (var5.hasNext()) {
                JmReportDbParam var6 = (JmReportDbParam) var5.next();
                if (oConvertUtils.isNotEmpty(var6.getParamValue())) {
                    var4.put(var6.getParamName(), var6.getParamValue());
                }
            }

            String var7 = this.jimuReportService.getBaseSql(jmReportDb, var4);
            if (oConvertUtils.isEmpty(jmReportDb.getDbSource())) {
                return this.reportDbDao.selectListBySql(var7);
            } else {
                return this.jmreportDynamicDbUtil.findList(jmReportDb.getDbSource(), var7, new Object[0]);
            }
        }
    }

    @Override
    public List<Map> parseData(Map<String, Object> map) {
        ArrayList var2 = new ArrayList();
        ArrayList var3 = (ArrayList) map.get("records");
        if (var3 != null && var3.size() >= 1) {
            LinkedHashMap var4 = (LinkedHashMap) var3.get(0);
            Integer var5 = 1;
            Iterator var6 = var4.entrySet().iterator();

            while (var6.hasNext()) {
                Entry var7 = (Entry) var6.next();
                String var8 = (String) var7.getKey();
                LinkedHashMap var9 = new LinkedHashMap();
                var9.put("fieldName", var8);
                var9.put("fieldText", var8);
                var9.put("widgetType", "String");
                var9.put("isShow", true);
                Integer var10 = var5;
                var5 = var5 + 1;
                var9.put("orderNum", var10);
                var2.add(var9);
            }
        }

        return var2;
    }

    @Override
    public List<Map<String, Object>> loadDataSourceTable(String dbKey) {
        List var2 = null;
        JmreportDynamicDataSourceVo var3 = JmreportDataSourceCachePool.getCacheDynamicDataSourceModel(dbKey);
        String var4 = MiniDaoUtil.getDbType(var3.getDbUrl());
        String var5 = JmreportSqlUtils.getAllTableSql(var3.getDbUrl());
        if (oConvertUtils.isNotEmpty(var5)) {
            var2 = this.jmreportDynamicDbUtil.findList(dbKey, var5, new Object[0]);
            Iterator var6;
            Map var7;
            if (JmreportSqlUtils.dbTypeIsOracle(var4)) {
                var6 = var2.iterator();

                while (var6.hasNext()) {
                    var7 = (Map) var6.next();
                    var7.put("Comment", var7.get("COMMENTS"));
                    var7.put("Name", var7.get("NAME"));
                }
            } else if (JmreportSqlUtils.dbTypeIsPostgre(var4)) {
                var6 = var2.iterator();

                while (var6.hasNext()) {
                    var7 = (Map) var6.next();
                    var7.put("Comment", var7.get("comment"));
                    var7.put("Name", var7.get("name"));
                }
            }
        }

        return var2;
    }

    @Override
    public Map<String, Object> loadTableData(String dbKey, String tableName, Integer pageNo, Integer pageSize, String sql, String paramArray) {
        HashMap var7 = new HashMap();
        if (oConvertUtils.isNotEmpty(paramArray)) {
            sql = this.a((Object) paramArray, sql);
        }

        String var8 = this.jimuTokenClient.getToken();
        if (oConvertUtils.isNotEmpty(var8)) {
            Map var9 = this.jimuTokenClient.getUserInfo(var8);
            sql = d.a(sql, var9);
        }

        a.info("报表查询querySql=>\r\n" + sql);
        String var15 = d.f(sql);
        if (StringUtils.isNotBlank(var15)) {
            List var10 = this.jmreportDynamicDbUtil.executeProcedure(dbKey, var15);
            var7.put("records", d.b(var10));
        } else if (oConvertUtils.isNotEmpty(dbKey)) {
            JmreportDynamicDataSourceVo var13 = JmreportDataSourceCachePool.getCacheDynamicDataSourceModel(dbKey);
            String var11 = MiniDaoUtil.createPageSql(var13.getDbUrl(), sql, pageNo, pageSize);
            a.info("报表查询pageSQL=>\r\n" + var11);
            List var12 = this.jmreportDynamicDbUtil.findList(dbKey, var11, new Object[0]);
            var7.put("records", d.b(var12));
        } else {
            MiniDaoPage var14 = this.reportDbDao.selectPageBySql(sql, pageNo, pageSize);
            var7.put("total", var14.getResults());
            var7.put("records", d.b(var14.getResults()));
        }

        return var7;
    }

    @Override
    @Transactional
    public void delDbData(String dbId) {
        this.reportDbDao.deleteById(dbId);
        this.reportDbFieldService.deleteByReportId(dbId);
        this.jmReportParamService.deleteByReportId(dbId);
    }

    private JSONObject a(String var1, String var2, String var3) {
        a.info("报表API 查询sendHttpRequest 原始API=> " + var1);
        Map var4 = this.jimuTokenClient.getUserInfo(var3);
        var1 = d.a(var1, var4, this.apiBasePath);
        a.info("报表API 查询sendHttpRequest 替换后API=> " + var1);
        a.info("报表API 查询sendHttpRequest Method=> " + var2);
        if (var1.contains("'")) {
            var1 = var1.replace("'", "");
        }

        new String();

        try {
            String var5;
            if ("0".equals(var2)) {
                var5 = d.b(var1, var3);
            } else {
                var5 = d.a(var1, var3, (JSONObject) null);
            }

            a.info("报表API 查询sendHttpRequest result=> " + var5);
            JSONObject var6 = JSONObject.parseObject(var5, new Feature[]{Feature.OrderedField});
            return var6;
        } catch (ResourceAccessException var7) {
            a.warn("连接超时:" + var7.getMessage());
            throw new JimuReportException("api连接超时，请重试！");
        } catch (Exception var8) {
            var8.printStackTrace();
            return null;
        }
    }

    @Override
    public Map<String, Object> parseReportSql(String sql, String dbKey, Object paramArray, String type, String tableName) throws JimuReportException {
        HashMap<String, Object> var5 = new HashMap<>();
        List<Map<String, Object>> var6 = new ArrayList<>();
        if (oConvertUtils.isNotEmpty(paramArray)) {
            sql = this.a(paramArray, sql);
        }

        List var7 = d.b(sql);
        sql = d.a(sql, (Map) null);
        String var8 = d.f(sql);
        if (StringUtils.isNotBlank(var8)) {
            var6 = this.jmreportDynamicDbUtil.executeProcedure(dbKey, var8);
        } else if (oConvertUtils.isEmpty(dbKey)) {
            try {
                if (sql.contains("*")) {
                    MiniDaoPage var9 = this.reportDbDao.selectPageBySql(sql, 1, 1);
                    var6 = var9.getResults();
                }

                if (oConvertUtils.isEmpty(var6)) {
                    var6 = j.a(sql);
                }
            } catch (Exception var17) {
                Throwable var10 = var17.getCause();
                if ("0".equals(type)) {
                    a.warn(var17.getMessage());
                    var5.put("message", "解析失败");
                    return var5;
                }

                if (var10 instanceof SQLSyntaxErrorException) {
                    throw new JimuReportException(var17.getMessage());
                }

                if (var17 instanceof UncategorizedSQLException) {
                    throw new JimuReportException("SQL语法错误！");
                }

                throw new JimuReportException(var17);
            }
        } else {
            JmreportDynamicDataSourceVo var18 = JmreportDataSourceCachePool.getCacheDynamicDataSourceModel(dbKey);
            String var20 = MiniDaoUtil.createPageSql(var18.getDbUrl(), sql, 1, 1);

            try {
                if (sql.contains("*")) {
                    var6 = this.jmreportDynamicDbUtil.findList(dbKey, var20, new Object[0]);
                }

                if (oConvertUtils.isEmpty(var6)) {
                    var6 = j.a(sql);
                }
            } catch (Exception var16) {
                if ("0".equals(type)) {
                    a.warn(var16.getMessage());
                    var5.put("message", "解析失败");
                    return var5;
                }

                if (var16.getMessage().contains("bad SQL grammar")) {
                    throw new JimuReportException("SQL语法错误！");
                }

                throw new JimuReportException(var16);
            }
        }


        if (CollectionUtils.isEmpty(var6)) {
            return var5;
        }

        // 查询我们自己的gen_table_column
        List<GenTableColumn> columns = remoteGenTableColumnService.listColumnsByTableName(tableName);
        if (CollectionUtils.isEmpty(columns)) {
            return var5;
        }
        List<JSONObject> jiaoyuju = mysqlTableService.getColumnNames(tableName, "jiaoyuju");
        List<LinkedHashMap<String, Object>> var19 = new ArrayList<>();

        int cnt = 1;
        for (JSONObject jsonObject : jiaoyuju) {
            String columnName = jsonObject.getString("column_name");
            for (GenTableColumn column : columns) {
                if (!column.getColumnName().equals(columnName)) {
                    continue;
                }

                LinkedHashMap<String, Object> temp = new LinkedHashMap<>();
                temp.put("fieldName", columnName);
                temp.put("fieldText", column.getColumnComment());
                temp.put("widgetType", "String");
                temp.put("orderNum", cnt++);
                var19.add(temp);
                break;
            }
        }

        var5.put("fieldList", var19);
        var5.put("paramList", var7);
        return var5;
    }

    private String a(Object var1, String var2) {
        JSONArray var3 = JSONArray.parseArray(String.valueOf(var1));
        new HashMap();

        for (int var5 = 0; var5 < var3.size(); ++var5) {
            JSONObject var6 = var3.getJSONObject(var5);
            Object var7 = var6.get("paramName");
            Object var8 = var6.get("paramValue");
            if (oConvertUtils.isNotEmpty(var7) && oConvertUtils.isNotEmpty(var8)) {
                var2 = var2.replace("${" + var7 + "}", String.valueOf(var8));
                var2 = var2.replace("'${" + var7 + "}'", String.valueOf(var8));
            }
        }

        return var2;
    }

    private String b(Object var1, String var2) {
        JSONArray var3 = JSONArray.parseArray(String.valueOf(var1));
        new HashMap();

        for (int var5 = 0; var5 < var3.size(); ++var5) {
            JSONObject var6 = var3.getJSONObject(var5);
            Object var7 = var6.get("paramName");
            Object var8 = var6.get("paramValue");
            if (oConvertUtils.isNotEmpty(var7) && oConvertUtils.isNotEmpty(var8)) {
                var2 = var2.replace("${" + var7 + "}", String.valueOf(var8));
                var2 = var2.replace("'${" + var7 + "}'", String.valueOf(var8));
            }
        }

        return var2;
    }

    public Result addDataSource(JmReportDataSource dataSource, HttpServletRequest request) {
        String var3 = this.jimuTokenClient.getUsername(request);
        ApiAssert.isNotEmpty("未实现JmReportTokenServiceI", var3);
        String var4 = DesUtils.decode(dataSource.getDbPassword());
        dataSource.setDbPassword("@JimuReport" + var4);
        dataSource.setCreateBy(var3);
        dataSource.setCreateTime(new Date());
        dataSource.setConnectTimes(0);
        this.saveDbSource(dataSource);
        JmreportDataSourceCachePool.deleteCacheDataSource(dataSource.getId());
        return Result.OK("操作成功！", true);
    }

    public List<Map<String, Object>> queryTableName(String key, String tableName) {
        ArrayList var3 = new ArrayList();
        List var4 = this.loadDataSourceTable(key);
        if (oConvertUtils.isEmpty(tableName)) {
            return var4;
        } else {
            Pattern var5 = Pattern.compile(tableName.toLowerCase());
            Iterator var6 = var4.iterator();

            while (var6.hasNext()) {
                Map var7 = (Map) var6.next();
                Object var8 = new HashMap();
                String var9 = String.valueOf(var7.get("Name"));
                Matcher var10 = var5.matcher(var9.toLowerCase());
                if (var10.find()) {
                    var8 = var7;
                    var7.put("Name", var7.get("Name"));
                }

                if (((Map) var8).size() > 0 && null != var8) {
                    var3.add(var8);
                }
            }

            return var3;
        }
    }

    @Override
    public void updateById(JmReportDb reportDb) {
        this.reportDbDao.update(reportDb);
    }

    @Override
    public JmReportDb getById(String apiSelectId) {
        return this.reportDbDao.get(apiSelectId);
    }

    @Override
    public int getCount(String reportId, String code) {
        return this.reportDbDao.getCount(reportId, code);
    }

    @Override
    public List<JmReportDb> getListReportDb(String reportId) {
        return this.reportDbDao.selectList(reportId);
    }

    public void a(JmReportDb var1) {
        if (oConvertUtils.isNotEmpty(var1.getId())) {
            this.reportDbDao.update(var1);
        } else {
            var1.setId(String.valueOf(SnowflakeIdWorker.generateId()));
            this.reportDbDao.insert(var1);
        }

    }
}
