package com.ruoyi.xq.manager;

import com.ruoyi.common.core.service.SensitiveService;
import com.ruoyi.xq.constant.RedisHttpConstant;
import com.ruoyi.xq.enums.common.SystemConfigEnum;
import com.ruoyi.xq.manager.systemconfig.SystemCheckResp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 主要用于动态配置管理
 * <p>created on 2022/8/18 17:21</p>
 * @author ZL
 */
@Component
public class SystemConfigManager {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private SensitiveService sensitiveService;

    @Value("${spring.profiles.active:'dev'}")
    private String active;
    private final static String DEV_ACTIVE = "dev";

    @PostConstruct
    public void initSensitive(){
        boolean config = getSystemConfigOfBool(SystemConfigEnum.SENSITIVE_ENABLE);
        sensitiveService.setSensitive(config);
    }

    /**
     * 获取配置信息返回boolean
     * @param systemConfig
     * @return
     */
    public boolean getSystemConfigOfBool(SystemConfigEnum systemConfig){
        String value = getSystemConfig(systemConfig);
        return "1".equals(value);
    }

    public BigDecimal getSystemConfigOfBigDecimal(SystemConfigEnum systemConfig){
        String value = getSystemConfig(systemConfig);
        return new BigDecimal(value);
    }

    /**
     * 获取配置信息返回Integer
     * @param systemConfig
     * @return
     */
    public Integer getSystemConfigOfInt(SystemConfigEnum systemConfig){
        String value = getSystemConfig(systemConfig);
        return Integer.valueOf(value);
    }


    /**
     * 获取配置信息返回Integer
     * @param systemConfig
     * @return
     */
    public Long getSystemConfigOfLong(SystemConfigEnum systemConfig){
        String value = getSystemConfig(systemConfig);
        return Long.valueOf(value);
    }

    /**
     * 获取配置信息
     * @param systemConfig
     * @return
     */
    public String getSystemConfig(SystemConfigEnum systemConfig){
        String value = (String) redisTemplate.opsForHash().get(RedisHttpConstant.SYSTEM_CONFIG, systemConfig.name());
        if(StringUtils.isBlank(value)){
            return systemConfig.getDefaultValue();
        }
        return value;
    }


    public List<String> getSystemConfigOfList(SystemConfigEnum systemConfig){
        String value = getSystemConfig(systemConfig);
        if(StringUtils.isBlank(value)){
            value =  systemConfig.getDefaultValue();
        }
        return Stream.of(value.split(",")).collect(Collectors.toList());
    }

    public Set<Long> getSystemConfigOfLongSet(SystemConfigEnum systemConfig){
        String value = getSystemConfig(systemConfig);
        if(StringUtils.isBlank(value)){
            value =  systemConfig.getDefaultValue();
        }
        return Stream.of(value.split(",")).map(Long::valueOf).collect(Collectors.toSet());
    }


    /**
     * 获取所有配置
     * @return
     */
    public Map<String,String> getAllSystemConfig(){
        HashOperations<String, String, String> stringObjectObjectHashOperations = redisTemplate.opsForHash();
        return stringObjectObjectHashOperations.entries(RedisHttpConstant.SYSTEM_CONFIG);
    }


    public SystemCheckResp setSystemConfig(SystemConfigEnum systemConfig, String value){
        if(systemConfig.getCheck() != null){
            SystemCheckResp check = systemConfig.getCheck().check(value);
            if(!check.isSuccess()){
                return check;
            }
        }
        this.set(systemConfig.name(),value);
        return SystemCheckResp.ok();
    }

    public void set(String key, String value) {
        if(key.equals(SystemConfigEnum.SENSITIVE_ENABLE.getKey())){
            sensitiveService.setSensitive(value.equals("1"));
        }
        redisTemplate.opsForHash().put(RedisHttpConstant.SYSTEM_CONFIG, key,value);
    }
}
