package com.fs.company.cache;

import com.fs.common.utils.spring.SpringUtils;
import com.fs.company.mapper.CompanyMapper;
import com.fs.company.mapper.CompanyUserMapper;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class CompanyUserCacheService {


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


    /**
     * 根据公司ID获取公司名称
     * @param companyId 公司ID
     * @return 公司名称：格式：公司ID_公司名
     */
    public static String getCompanyUserNameById(Long companyId){
        if(companyId == null) return "-";
        CompanyUserMapper companyUserMapper = SpringUtils.getBean(CompanyUserMapper.class);
        return COMPANY_USER_NAME_CACHE.get(companyId,
                e-> {
                    String companyUserName = companyUserMapper.selectCompanyUserNameById(companyId);
                    if(companyUserName != null){
                        return companyUserName;
                    }
                    return "-";
                }
        );
    }


}
