package com.fs.company.cache.impl;

import com.fs.company.cache.ICompanyUserCacheService;
import com.fs.company.domain.CompanyUser;
import com.fs.company.service.ICompanyService;
import com.fs.company.service.ICompanyUserService;
import com.fs.company.vo.DeptDataVO;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class CompanyUserCacheServiceImpl implements ICompanyUserCacheService {

    @Autowired
    private ICompanyUserService companyUserService;

    @Autowired
    private ICompanyService companyService;

    private static final Cache<Long, CompanyUser> USER_CACHE = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(3, TimeUnit.MINUTES)
            .build();

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

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

    private static final Cache<Long,List<DeptDataVO>> COMPANY_USER_TREE_CACHE = Caffeine.newBuilder()
            .maximumSize(1000)
            .expireAfterWrite(10, TimeUnit.MINUTES)
            .build();


    @Override
    public CompanyUser selectCompanyUserById(Long userId) {
        return USER_CACHE.get(userId,e-> companyUserService.selectCompanyUserByUserId(userId));
    }

    @Override
    public String selectCompanyUserNameUserById(Long userId) {
        return COMPANY_USER_NAME_CACHE.get(userId,e-> companyUserService.selectCompanyUserNameUserById(userId));
    }

    @Override
    public Set<Long> selectUserAllCompanyUserId(Long companyUserId) {
        return COMPANY_USER_CACHE.get(companyUserId,e->{
            List<Long> longs = companyUserService.selectUserAllCompanyUserId(companyUserId);
            Set<Long> set = new HashSet<>(longs);
            set.add(companyUserId);
            return set;
        });
    }

    @Override
    public List<DeptDataVO> getDeptData(Long companyId) {
        if(companyId == null) {
            companyId = -1L;
        }
        Long finalCompanyId = companyId;
        return COMPANY_USER_TREE_CACHE.get(companyId, e->{
            if(ObjectUtils.equals(finalCompanyId,-1L)) {
                return companyService.getDeptData(null);
            }
            return companyService.getDeptData(finalCompanyId);
        });
    }

    @Override
    public List<DeptDataVO> getDeptData(Long companyId, Long companyUserId) {
        return Collections.emptyList();
    }
}
