package com.fs.qw.cache.impl;

import com.fs.qw.cache.IQwUserCacheService;
import com.fs.qw.domain.QwUser;
import com.fs.qw.mapper.QwUserMapper;
import com.fs.qw.service.IQwUserService;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class QwUserCacheServiceImpl implements IQwUserCacheService {
    @Autowired
    private IQwUserService qwUserService;

    @Autowired
    private QwUserMapper qwUserMapper;
    /**
     * 企微用户名昵称缓存类
     */
    private static final Cache<String, String> QW_USER_CACHE = Caffeine.newBuilder()
            .maximumSize(5000)
            .expireAfterWrite(12, TimeUnit.HOURS)
            .build();

    private static final Cache<Long, String> QW_USER_ID_CACHE = Caffeine.newBuilder()
            .maximumSize(5000)
            .expireAfterWrite(5, TimeUnit.HOURS)
            .build();

    @Override
    public String queryQwUserNameByUserId(String userId) {
        return QW_USER_CACHE.get(userId,e-> {
            QwUser qwUser = qwUserService.selectQwUserById(Long.valueOf(userId));
            if(qwUser != null) {
                return String.format("%s_%s",qwUser.getQwUserId(),qwUser.getQwUserName());
            }
            return "-";
        });
    }

    @Override
    public String queryCorpIdByQwUserId(Long qwUserId) {
        return QW_USER_ID_CACHE.get(qwUserId,e-> qwUserMapper.selectCorpIdById(qwUserId));
    }
}
