package com.fs.qw.cache.impl;

import com.fs.qw.cache.IQwExternalContactCacheService;
import com.fs.qw.domain.QwExternalContact;
import com.fs.qw.service.IQwExternalContactService;
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 QwExternalContactCacheServiceImpl implements IQwExternalContactCacheService {

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

    private static final Cache<Long,Integer> QW_REPEAT_CACHE = Caffeine.newBuilder()
            .maximumSize(5000)
            .expireAfterWrite(3, TimeUnit.MINUTES)
            .build();


    @Override
    public String selectQwExternalContactById(Long id) {
        return QW_EXTERNAL_CONTACT_CACHE.get(id,e->{
            QwExternalContact qwExternalContact = qwExternalContactService.selectQwExternalContactById(id);
            if(qwExternalContact != null) {
                return qwExternalContact.getName();
            }
            return "-";
        });
    }

    @Override
    public Integer selectQwIsRepeat(Long id) {
        return QW_REPEAT_CACHE.get(id,e->{
            return qwExternalContactService.selectQwIsRepeat(id);
        });
    }
}
