package ${basePackage}.frame.auth;

import ${basePackage}.frame.utils.IDgenerator;
import ${basePackage}.module.system.ent.Res;
import ${basePackage}.module.system.mgr.ResManager;
import ${basePackage}.module.system.req.ResCreateRequest;
import ${basePackage}.module.system.req.ResFindRequest;
import ${basePackage}.module.system.rsp.ResFindResponse;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Token - 通行证类
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
public class Token implements Serializable {
    private static final Long serialVersionUID = 1L;
    /**
     * ID
     */
    private long id;
    /**
     * TOKEN
     */
    private String token;
    /**
     * 用户ID
     */
    private long userId;
    /**
     * 用户名称
     */
    private String userName;

    private Set<String> resSet = new HashSet<>();

    public boolean hasRes(String res) {

        {// todo 开发初期收集资源，后期删除
            String active = LocalData.getActive();
            if (active.contains("dev")) {//测试环境捕获资源
                ResFindRequest resFindRequest = new ResFindRequest();
                resFindRequest.setPageSize(0);
                resFindRequest.setResValue(res);
                ResManager resManager = LocalData.getBean(ResManager.class);
                ResFindResponse resFindResponse = resManager.find(resFindRequest, LocalData.getSysToken());

                if (resFindResponse.getResult().size() == 0) {//资源不存在，主动收集资源
                    ResCreateRequest resCreateRequest = new ResCreateRequest();
                    resCreateRequest.setResCode(IDgenerator.nextUUID());
                    resCreateRequest.setResName("默认");
                    resCreateRequest.setResValue(res);
                    resCreateRequest.setValid(true);
                    resCreateRequest.setResType("");
                    resManager.create(resCreateRequest, LocalData.getSysToken());
                }
            }
        }

        for (String s : resSet) {
            if (res.matches(s)) {
                return true;
            }
        }

        return false;
    }

    public void putRes(String resource) {
        resSet.add(resource);
    }

    public void putRes(Set<String> resourceSet) {
        this.resSet.addAll(resourceSet);
    }

    public void putRes(List<Res> resList) {
        if (resList == null) return;
        for (Res res : resList) {
            this.resSet.add(res.getResValue());
        }
    }

    public Set<String> getResSet() {
        return resSet;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}
