package ink.wgink.common.service.access.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.common.dao.access.IWhiteListDao;
import ink.wgink.common.manager.access.WhiteListManager;
import ink.wgink.common.pojo.vos.access.WhiteListVO;
import ink.wgink.common.service.access.IWhiteListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: WhiteListServiceImpl
 * @Description: 白名单业务
 * @Author: wanggeng
 * @Date: 2022/7/27 14:11
 * @Version: 1.0
 */
@Service
public class WhiteListServiceImpl extends DefaultBaseService implements IWhiteListService {

    @Autowired
    private IWhiteListDao whiteListDao;

    @Override
    public void update(WhiteListVO whiteListVO) {
        List<String> pathMatchers = whiteListVO.getPathMatchers();
        whiteListDao.delete();
        Map<String, Object> params = getHashMap(2);
        for (String pathMatcher : pathMatchers) {
            if (StringUtils.isBlank(pathMatcher)) {
                continue;
            }
            params.put("pathMatcher", pathMatcher);
            whiteListDao.save(params);
        }
        WhiteListManager.getInstance().refreshWhiteList();
    }

    @Override
    public List<String> list() {
        return whiteListDao.list();
    }
}
