package com.lightinit.hsdatagateway.common.service;

import com.lightinit.hsdatagateway.common.config.Global;
import com.lightinit.hsdatagateway.common.utils.CacheUtils;
import com.lightinit.hsdatagateway.common.utils.SpringContextHolder;
import com.lightinit.hsdatagateway.modules.gateway.entity.BizApp;
import com.lightinit.hsdatagateway.modules.gateway.service.BizAppService;
import com.lightinit.hsdatagateway.modules.gateway.service.BlackWhiteListService;
import com.google.common.base.Strings;
import com.lightinit.hsdatagateway.modules.gateway.entity.BizApp;

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


/**
 * @ClassName: InitService
 * @Description: 初始化加载黑白名单进cache
 * @Author: yangrui
 * @Date 2019/3/6 上午10:34
 * @Version V1.0
 **/
public class InitService {

    @PostConstruct
    private void initBlackWhiteList(){

        // 读取配置文件中开启哪种名单验证方式
        String blackWhite = Global.getConfig("blackWhite.choose");
        if (!Strings.isNullOrEmpty(blackWhite)) {
            try {
                CacheUtils.put("blackWhite",blackWhite);
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        // 将黑白名单进行加入缓存
        BlackWhiteListService service = SpringContextHolder.getBean(BlackWhiteListService.class);

        List<String> blacks = service.findIpByType("黑名单");
        List<String> whites = service.findIpByType("白名单");

        CacheUtils.put("black", blacks);
        CacheUtils.put("whites", whites);

        //将应用管理加入缓存
        BizAppService bizAppService = SpringContextHolder.getBean(BizAppService.class);
        List<BizApp> allList = bizAppService.findAllList();
        CacheUtils.put("app",allList);

    }


}
