package com.cf.imes.framework.organ.core.service;

import cn.hutool.core.lang.Pair;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.cache.CacheUtils;
import com.cf.imes.module.system.api.dept.DeptApi;
import com.cf.imes.module.system.api.organ.OrganApi;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;

import java.time.Duration;
import java.util.List;

/**
 * Organ 框架 Service 实现类
 *
 * @author 晨丰科技
 */
@RequiredArgsConstructor
public class OrganFrameworkServiceImpl implements OrganFrameworkService {

    private final OrganApi organApi;

    private final DeptApi deptApi;

    /**
     * 针对 {@link #getOrganIds()} 的缓存
     */
    private final LoadingCache<Object, List<Long>> getOrganIdsCache = CacheUtils.buildAsyncReloadingCache(
            Duration.ofMinutes(1L), // 过期时间 1 分钟
            new CacheLoader<>() {

                @Override
                public List<Long> load(Object key) {
                    return organApi.getOrganIdList().getCheckedData();
                }

            });

    /**
     * 针对 {@link #validOrgan(Long)} 的缓存
     */
    private final LoadingCache<Long, CommonResult<Boolean>> validOrganCache = CacheUtils.buildCache(
            Duration.ofMinutes(1L), // 过期时间 1 分钟
            new CacheLoader<>() {

                @Override
                public CommonResult<Boolean> load(Long id) {
                    return organApi.validOrgan(id);
                }

            });

    /**
     * 针对 {@link #validDept(Long)} 的缓存
     */
    private final LoadingCache<Long, CommonResult<Boolean>> validOrganDept = CacheUtils.buildCache(
            Duration.ofMinutes(1L), // 过期时间 1 分钟
            new CacheLoader<>() {

                @Override
                public CommonResult<Boolean> load(Long id) {
                    return deptApi.validateDept(id);
                }

            });

    /**
     * 针对 {@link #validOrgProduct(Long, Long)} 的缓存
     */
    private final LoadingCache<Pair<Long, Long>, CommonResult<Boolean>> validOrgProduct = CacheUtils.buildCache(
            Duration.ofMinutes(1L), // 过期时间 1 分钟
            new CacheLoader<>() {

                @Override
                public CommonResult<Boolean> load(Pair<Long, Long> pair) {
                    return organApi.validOrganProduct(pair.getKey(), pair.getValue());
                }

            });

    @Override
    @SneakyThrows
    public List<Long> getOrganIds() {
        return getOrganIdsCache.get(Boolean.TRUE);
    }

    @Override
    @SneakyThrows
    public void validOrgan(Long id) {
        validOrganCache.get(id).checkError();
    }

    @Override
    @SneakyThrows
    public void validDept(Long id) {
        validOrganDept.get(id).checkError();
    }

    @Override
    @SneakyThrows
    public void validOrgProduct(Long organId, Long productId) {
        validOrgProduct.get(new Pair<>(organId, productId)).checkError();
    }
}
