package cc.smtweb.system.bpm.web.sys.user.user;

import cc.smtweb.framework.core.annotation.SwCache;
import cc.smtweb.framework.core.cache.AbstractEntityCache;
import cc.smtweb.framework.core.cache.CacheManager;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by 1 at 2022-07-28 15:24:49
 * 实体【[用户账号](SYS_USER)】的缓存类
 */
@SwCache(ident = "SYS_USER", title = "用户账号")
public class UserCache extends AbstractEntityCache<User> {
    //缓存key：按居家号
    public final static String mk_code = "code";
    //缓存key：按登录账号
    public final static String mk_nicky = "nicky";
    //缓存key：按手机号
    public final static String mk_tel = "tel";

    public static UserCache getInstance() {
        return CacheManager.getIntance().getCache(UserCache.class);
    }

    public UserCache() {
        //缓存key：按居家号
        regMap(mk_code, "sur_code");
        //缓存key：按登录账号
        regMap(mk_nicky, "sur_nicky");
        //缓存key：按手机号
        regMap(mk_tel, "sur_tel");
    }

    //缓存key：按居家号
    public final User getByCode(String key) {
        return getByKey(mk_code, key);
    }

    //缓存key：按登录账号
    public final User getByNicky(String key) {
        return getByKey(mk_nicky, key);
    }

    //缓存key：按手机号
    public final User getByTel(String key) {
        return getByKey(mk_tel, key);
    }

    //获取登录账号
    public final User getByKey(String key) {
        User user = getByCode(key);
        if (user != null) {
            return user;
        }
        user = getByNicky(key);
        if (user != null) {
            return user;
        }
        return getByTel(key);
    }

    public final String getNameById(long id) {
        User user = get(id);
        if (user == null) return "";
        return user.getName();
    }

    public final Set<User> getUserList(Set<Long> ids) {
        Set<User> set = new HashSet<>();
        ids.forEach(k -> {
            set.add(get(k));
        });
        return set;
    }
}
