package com.inspect.system.service.impl;

import com.inspect.base.core.exception.ServiceException;
import com.inspect.base.core.text.Convert;
import com.inspect.base.core.utils.StringUtils;
import com.inspect.base.redis.service.RedisService;
import com.inspect.system.domain.SysConfig;
import com.inspect.system.mapper.SysConfigMapper;
import com.inspect.system.service.ISysConfigService;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;

@Service
public class SysConfigServiceImpl implements ISysConfigService {
    private final SysConfigMapper configMapper;
    private final RedisService redisService;

    public SysConfigServiceImpl(SysConfigMapper configMapper, RedisService redisService) {
        this.configMapper = configMapper;
        this.redisService = redisService;
    }

    @PostConstruct
    public void init() {
        this.loadingConfigCache();
    }

    public SysConfig selectConfigById(Long configId) {
        SysConfig config = new SysConfig();
        config.setConfigId(configId);
        return this.configMapper.selectConfig(config);
    }

    public String selectConfigByKey(String configKey) {
        String configValue = Convert.toStr(this.redisService.getCacheObject(this.getCacheKey(configKey)));
        if (StringUtils.isNotEmpty(configValue)) {
            return configValue;
        } else {
            SysConfig config = new SysConfig();
            config.setConfigKey(configKey);
            SysConfig retConfig = this.configMapper.selectConfig(config);
            if (StringUtils.isNotNull(retConfig)) {
                this.redisService.setCacheObject(this.getCacheKey(configKey), retConfig.getConfigValue());
                return retConfig.getConfigValue();
            } else {
                return "";
            }
        }
    }

    public List<SysConfig> selectConfigByKeys(String configKeys) {
        return this.configMapper.selectConfigByKeys(Arrays.asList(configKeys.split(StringUtils.COMMA)));
    }

    public List<SysConfig> selectConfigList(SysConfig config) {
        return this.configMapper.selectConfigList(config);
    }

    public int insertConfig(SysConfig config) {
        int row = this.configMapper.insertConfig(config);
        if (row > 0) {
            this.redisService.setCacheObject(this.getCacheKey(config.getConfigKey()), config.getConfigValue());
        }

        return row;
    }

    public int updateConfig(SysConfig config) {
        int row = this.configMapper.updateConfig(config);
        if (row > 0) {
            this.redisService.setCacheObject(this.getCacheKey(config.getConfigKey()), config.getConfigValue());
        }

        return row;
    }

    public void deleteConfigByIds(Long[] configIds) {
        for (Long configId : configIds) {
            SysConfig config = this.selectConfigById(configId);
            if (StringUtils.equals("Y", config.getConfigType())) {
                throw new ServiceException(String.format("Inner Parameter [%1$s] Delete Not Allowed ", config.getConfigKey()));
            }

            this.configMapper.deleteConfigById(configId);
            this.redisService.deleteObject(this.getCacheKey(config.getConfigKey()));
        }

    }

    public void loadingConfigCache() {
        List<SysConfig> configsList = this.configMapper.selectConfigList(new SysConfig());

        for (SysConfig config : configsList) {
            this.redisService.setCacheObject(this.getCacheKey(config.getConfigKey()), config.getConfigValue());
        }

    }

    public void clearConfigCache() {
        Collection<String> keys = this.redisService.keys("sys_config:*");
        this.redisService.deleteObject(keys);
    }

    public void resetConfigCache() {
        this.clearConfigCache();
        this.loadingConfigCache();
    }

    public String checkConfigKeyUnique(SysConfig config) {
        long configId = StringUtils.isNull(config.getConfigId()) ? -1L : config.getConfigId();
        SysConfig info = this.configMapper.checkConfigKeyUnique(config.getConfigKey());
        return StringUtils.isNotNull(info) && info.getConfigId() != configId ? "1" : "0";
    }

    private String getCacheKey(String configKey) {
        return "sys_config:" + configKey;
    }
}
