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

package com.ag.psamathe.api.impl;

import com.ag.psamathe.api.ModuleConfig;
import com.ag.psamathe.api.pojo.ModuleVersion;
import com.ag.psamathe.api.util.DataSourceUtil;
import com.ag.util.ResourceUtil;
import com.alibaba.fastjson.JSON;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

public class ModuleRefreshSchedulerImpl extends AbstractModuleRefreshScheduler {
    private List<ModuleConfig> moduleConfigs;
    private static final Logger LOGGER = LoggerFactory.getLogger(ModuleRefreshSchedulerImpl.class);
    private ResourceBundle rb = ResourceUtil.getResourceBundle("system");

    public ModuleRefreshSchedulerImpl() {
    }

    public List<ModuleConfig> queryModuleConfigs() {
        this.setModuleConfigs(this.buildModuleConfig());
        return this.moduleConfigs;
    }

    public void setModuleConfigs(List<ModuleConfig> moduleConfigs) {
        this.moduleConfigs = moduleConfigs;
    }

    public List<ModuleConfig> buildModuleConfig() {
        DataSource ds = null;
        String dataSourceBeanName = this.rb.getString("dataSourceBeanName");
        if (this.isContainsBean(dataSourceBeanName)) {
            JdbcTemplate jdbcTemplate = (JdbcTemplate)this.getModuleLoader().getBean(dataSourceBeanName);
            ds = jdbcTemplate.getDataSource();
        }

        String userName = this.rb.getString("userName");
        List<ModuleVersion> versions = DataSourceUtil.getInstance().queryVersions(ds, userName);
        Assert.notEmpty(versions, "表MODULE_VERSION未配置name:======" + userName);
        List<ModuleConfig> configs = new ArrayList();
        Iterator var6 = versions.iterator();

        while(var6.hasNext()) {
            ModuleVersion version = (ModuleVersion)var6.next();
            ModuleConfig moduleConfig = new ModuleConfig();
            ArrayList moduleUrls = new ArrayList();

            try {
                String[] var10 = version.getJarPath().split(",");
                int var11 = var10.length;

                for(int var12 = 0; var12 < var11; ++var12) {
                    String jarPath = var10[var12];
                    moduleUrls.add(new URL("file:" + jarPath));
                }
            } catch (Exception var14) {
                LOGGER.error(var14.getMessage());
            }

            moduleConfig.setModuleUrl(moduleUrls);
            if (StringUtils.isEmpty(version.getOverridePackages())) {
                moduleConfig.setOverridePackages(this.strForList(version.getScanPackage()));
            } else {
                moduleConfig.setOverridePackages(this.strForList(version.getOverridePackages()));
            }

            Map<String, Object> properties = new HashMap();
            properties.put("url", "127.0.0.1");
            moduleConfig.setProperties(properties);
            moduleConfig.setName(version.getName());
            moduleConfig.setEnabled(version.getEnabled() == 1);
            moduleConfig.setVersion(version.getVersion());
            moduleConfig.setDesc(version.getDesc());
            configs.add(moduleConfig);
        }

        return configs;
    }

    public List<String> strForList(String str) {
        return Arrays.asList(str.split(","));
    }

    public static void main(String[] args) {
        String str = "aa";
        String[] arr = str.split(",");
        List<String> strings = Arrays.asList(arr);
        System.out.println(JSON.toJSONString(strings));
    }
}
