package com.fs.company.cache;

import com.fs.common.utils.spring.SpringUtils;
import com.fs.company.domain.Company;
import com.fs.company.mapper.CompanyMapper;
import com.fs.company.service.ICompanyService;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class CompanyCacheService {


    private static final Cache<Long, String> COMPANY_NAME_CACHE = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(5, TimeUnit.MINUTES)
            .build();


    /**
     * 根据公司ID获取公司名称
     * @param companyId 公司ID
     * @return 公司名称：格式：公司ID_公司名
     */
    public static String getCompanyNameById(Long companyId){

        CompanyMapper companyMapper = SpringUtils.getBean(CompanyMapper.class);
        return COMPANY_NAME_CACHE.get(companyId,e-> companyMapper.selectCompanyNameById(companyId));
    }


}
