/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.platform.service.impl;

import com.crunii.micro.domain.entity.main.TsmUrl;
import com.crunii.micro.platform.jdbc.LogUrlJdbcDao;
import com.crunii.micro.platform.service.LogUrlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author 田平 create 2020年1月22日下午3:27:47
 */
@Slf4j
public class LogUrlServiceImpl implements LogUrlService {

	@Autowired
	private LogUrlJdbcDao logUrlJdbcDao;

	private AntPathMatcher pathMatcher = new AntPathMatcher();

	@Override
	@Cacheable(cacheNames = "GlobalCacheLogUrls")
	public boolean checkLogUrl(String url) {
		if (StringUtils.hasText(url)) {
			boolean exists = logUrlJdbcDao.checkLogUrl(url);
			if (!exists) {
				//ANT方式匹配
				List<TsmUrl> antUrls = logUrlJdbcDao.getAntUrls();
				exists = antUrls.stream().anyMatch(tsmUrl -> {
					return pathMatcher.match(tsmUrl.getUrlPath(), url);
				});
			}
			log.info("load checkLogUrl:{},result:{} from database!", url, exists);
			return exists;
		} else {
			return false;
		}
	}

	@Override
	@CacheEvict(cacheNames = "GlobalCacheLogUrls")
	public void evictLogUrlCache(String url) {

	}

	@Override
	@CacheEvict(cacheNames = "GlobalCacheLogUrls", allEntries = true)
	public void evictAllLogUrlCache() {

	}

}
