package cn.reghao.tnb.file.app.service;

import cn.reghao.tnb.account.api.iface.AccountQuery;
import cn.reghao.tnb.common.auth.AccountRole;
import cn.reghao.tnb.common.util.ConstantId;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @author reghao
 * @date 2024-09-09 11:35:55
 */
@Service
public class StoreConfigService {
    @DubboReference(check = false, retries = 0, timeout = 60_000)
    private AccountQuery accountQuery;

    public int getLocalOssUser() {
        Integer ossUser = getOssUser();
        return ossUser != null ? ossUser : (int) ConstantId.ANONYMOUS_USER_ID;
    }

    @Cacheable(cacheNames = "tnb:file:oss_user", key = "'local_oss_user'", unless = "#result == null")
    public Integer getOssUser() {
        long ossUser = accountQuery.getByRole(AccountRole.admin.getValue());
        if (ossUser != ConstantId.ANONYMOUS_USER_ID) {
            return (int) ossUser;
        }
        return null;
    }
}
