package com.cpop.core.service.impl;

import com.cpop.core.base.table.Config;
import com.cpop.core.constant.Constants;
import com.cpop.core.mapper.ConfigMapper;
import com.cpop.core.service.ConfigService;
import com.cpop.core.service.RedisService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统配置表 服务层实现。
 *
 * @author DB
 * @since 2024-02-01
 */
@Service("configService")
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService {

    @Autowired
    private RedisService redisService;

    /**
     * 加载参数缓存数据
     */
    @Override
    public void loadingConfigCache() {
        //获取所有配置(可自定义)
        List<Config> configsList = this.list();
        if (!configsList.isEmpty()) {
            for (Config config : configsList) {
                redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
            }
        }
    }

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