package com.csf.fbbank.repository;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import com.csf.fbbank.common.constants.FbBankConstants;
import com.csf.fbbank.common.utils.ToolUtils;
import com.csf.fbbank.dao.BnpDao;
import com.csf.fbbank.dao.CompanyDao;
import com.csf.fbbank.dao.EventDao;
import com.csf.fbbank.dao.EventNewsConDao;
import com.csf.fbbank.dao.EventProductDao;
import com.csf.fbbank.dao.IndustryDao;
import com.csf.fbbank.dao.NewsDao;
import com.csf.fbbank.dao.ProductDao;
import com.csf.fbbank.dao.UserPreferDao;
import com.csf.fbbank.dto.EventMergeDto;
import com.csf.fbbank.dto.EventMergeDto.Prod;
import com.csf.fbbank.dto.EventNewsDto;
import com.csf.fbbank.dto.MergeDto;
import com.csf.fbbank.dto.MergeNewsDto;
import com.csf.fbbank.dto.NewsDto;
import com.csf.fbbank.dto.NewsTotalCountDto;
import com.csf.fbbank.dto.Page;
import com.csf.fbbank.dto.UserPreferDto;
import com.csf.fbbank.entity.Company;
import com.csf.fbbank.entity.EnumType.EventSortType;
import com.csf.fbbank.entity.Event;
import com.csf.fbbank.entity.EventNewsCon;
import com.csf.fbbank.entity.EventProduct;
import com.csf.fbbank.entity.Industry;
import com.csf.fbbank.entity.News;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mongodb.BasicDBObject;
/**
* @author fenglei.ma 2017/09/29 16:20
*/
public class CacheAll {
private static final Logger logger = Logger.getLogger(FbBankRepository.class);
private final Cache<String,Object> comCache = CacheBuilder.newBuilder().expireAfterAccess(5, TimeUnit.MINUTES).build();
private final Cache<String,Object> indCache = CacheBuilder.newBuilder().expireAfterAccess(5, TimeUnit.MINUTES).build();
private final Cache<String,Object> companyCache = CacheBuilder.newBuilder().expireAfterAccess(5, TimeUnit.HOURS).build();
private final Cache<String,Object> industryCache = CacheBuilder.newBuilder().expireAfterAccess(5, TimeUnit.HOURS).build();
private final Cache<String,Object> prodsCache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.HOURS).build();
private final Cache<String,Object> conEventNameCache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.HOURS).build();
@SuppressWarnings("unchecked")
public List<String> prodsCache() throws Exception {
return (List<String>) prodsCache.get("product", new Callable<List<String>>() {
@Override
public List<String> call() throws Exception {
return productDao.productList();
}
});
}
@SuppressWarnings("unchecked")
public List<String> conEventNameCache(List<String> lists) throws Exception {
return (List<String>) conEventNameCache.get("con_event_name", new Callable<List<String>>() {
@Override
public List<String> call() throws Exception {
return eventProductDao.findResionEntity_(lists);
}
});
}
@SuppressWarnings("unchecked")
public Set<String> comsCache(String sdt, String edt) throws Exception {
return (Set<String>) comCache.get(sdt+edt, new Callable<Set<String>>() {
@Override
public Set<String> call() throws Exception {
return newsDao.checkComExis(sdt, edt);
}
});
}
@SuppressWarnings("unchecked")
public List<Company> companyCache(String sdt, String edt) throws Exception{
return (List<Company>) companyCache.get(sdt+edt, new Callable<List<Company>>() {
@Override
public List<Company> call() throws Exception {
return companyDao.fetchAll();
}
});
}
@SuppressWarnings("unchecked")
public Set<String> indsCache(String sdt, String edt) throws Exception {
return (Set<String>) indCache.get(sdt+edt, new Callable<Set<String>>() {
@Override
public Set<String> call() throws Exception {
return newsDao.checkIndExis(sdt, edt);
}
});
}
@SuppressWarnings("unchecked")
public List<Industry> industryCache(String sdt, String edt) throws Exception{
return (List<Industry>) industryCache.get(sdt+edt, new Callable<List<Industry>>() {
@Override
public List<Industry> call() throws Exception {
return industryDao.fetchAll();
}
});
}
private Set<String> removeNoRelProd(String sdt, String edt) {
Set<String> set = comCache.product_cache.get("dict_product_cache", () -> newsDao.checkIndExis(sdt, edt);
return set;
}
}