package com.xingxi.web.controller.system.service.impl;

import com.xingxi.common.constant.Constants;
import com.xingxi.common.core.redis.RedisCache;
import com.xingxi.system.domain.SysConfig;
import com.xingxi.system.mapper.SysConfigMapper;
import com.xingxi.web.controller.system.service.ISysConfigVoService;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * 参数配置 服务层实现
 *
 * @author biandan
 */
@Service
public class SysConfigVoServiceImpl implements ISysConfigVoService {
    @Resource
    private SysConfigMapper configMapper;

    @Resource
    private RedisCache redisCache;

    /**
     * 项目启动时，初始化参数到缓存
     */
    @PostConstruct
    public void init() {
        loadingConfigCache();
    }

    /**
     * 加载参数缓存数据
     */
    @Override
    public void loadingConfigCache() {
        List<SysConfig> configsList = configMapper.selectConfigList(new SysConfig());
        for (SysConfig config : configsList) {
            redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
        }
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey) {
        return Constants.SYS_CONFIG_KEY + configKey;
    }
}
