import com.sdtool.business.dao.SystemDao
import com.sdtool.common.api.NoSqlKeysService
import com.sdtool.common.datas.RedisKeys
import com.sdtool.common.entity.system.ConfigValue
import com.dySweetFishPlugin.redis.RedisService
import com.sweetfish.convert.json.JsonConvert
import org.apache.commons.lang3.StringUtils
import org.rex.RMap

import java.util.stream.Collectors

static def createRunnable(String runEnvironment, String sysRunMode, RedisService redisService, NoSqlKeysService keysService, SystemDao systemDao, JsonConvert jsonConvert, String dataSourceId, long supplierCode) {
    return {
        RMap params = new RMap()

        redisService.del(keysService.getRedisKey(RedisKeys.KEY_CONFIGVALUE, supplierCode))

        //增加默认需要的
        for (String key : RedisKeys.SUPPLIER_SYSTEM_CONFIGARR) {
            RMap initConfigParams = new RMap()
            initConfigParams.put("configKey", key)
            ConfigValue initConfig = systemDao.queryConfigValue(initConfigParams, dataSourceId, supplierCode)
            if ((initConfig == null) || (StringUtils.isEmpty(initConfig.getConfigKey()))) {
                initConfig = new ConfigValue()
                initConfig.setConfigKey(key)
                initConfig.setConfigName("")
                initConfig.setConfigValue1("")
                initConfig.setConfigValue2("")
                initConfig.setConfigValue3("")

                systemDao.addConfigValueSync(initConfig, dataSourceId, supplierCode)
            }
        }

        for (String key : RedisKeys.SUPPLIER_SITEINFO_CONFIGARR) {
            RMap initConfigParams = new RMap()
            initConfigParams.put("configKey", key)
            ConfigValue initConfig = systemDao.queryConfigValue(initConfigParams, dataSourceId, supplierCode)
            if ((initConfig == null) || (StringUtils.isEmpty(initConfig.getConfigKey()))) {
                initConfig = new ConfigValue()
                initConfig.setConfigKey(key)
                initConfig.setConfigName("")
                initConfig.setConfigValue1("")
                initConfig.setConfigValue2("")
                initConfig.setConfigValue3("")

                systemDao.addConfigValueSync(initConfig, dataSourceId, supplierCode)
            }
        }

        //如果不是产品中心的，需要去掉两个，但开发需要：（
        if (!StringUtils.equalsIgnoreCase(runEnvironment, "dev")) {
            if (!"ProductCenter".equalsIgnoreCase(sysRunMode)) {
                ConfigValue c1 = new ConfigValue(
                        configKey: "QuoteFileDownloadUrl"
                )
                systemDao.deleteConfigValue(c1, dataSourceId, supplierCode)

                ConfigValue c2 = new ConfigValue(
                        configKey: "QuoteTableFileDownloadUrl"
                )
                systemDao.deleteConfigValue(c2, dataSourceId, supplierCode)
            }
        }

        List<ConfigValue> configValueList = systemDao.queryAllConfigValues(params, dataSourceId, supplierCode)

        Map<String, String> configValueMap = configValueList.stream().collect(
                Collectors.toMap(ConfigValue::getConfigKey, x -> jsonConvert.convertTo(x))
        )

        if (!configValueMap.isEmpty()) {
            redisService.hmset(keysService.getRedisKey(RedisKeys.KEY_CONFIGVALUE, supplierCode), configValueMap)
        }
    } as Runnable
}

//groovy最后一个表达式的值为返回
createRunnable(runEnvironment, sysRunMode, redisService, keysService, systemDao, jsonConvert, dataSourceId, supplierCode)

