package com.prolog.eis.init;

import com.prolog.eis.constants.sys.SysParamConstants;
import com.prolog.eis.model.sys.SysParam;
import com.prolog.eis.service.sys.SysParamService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class SysParamInit implements CommandLineRunner {

    private final SysParamService sysParamService;
    private final RedisTemplate redisTemplate;

    @Override
    public void run(String... args) throws Exception {
        List<SysParam> sysParams = sysParamService.findAll();
        createSysMap(sysParams);
    }

    private void createSysMap(List<SysParam> sysParams) {
        for (SysParam sysParam : sysParams) {
            redisTemplate.opsForValue().set(SysParamConstants.SYSPRE_FIX + sysParam.getParamNo(), sysParam.getParamValue());
        }
    }
}
