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

import cc.smtweb.framework.core.common.SwConsts;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.session.UserSession;
import cc.smtweb.framework.core.util.StringUtil;
import cc.smtweb.system.bpm.web.common.BpmConst;

import java.util.Map;

/**
 * Created by Akmm at 2017/5/17 15:31
 * 数据权限计算处理抽象类
 */
public abstract class AbsTreeDataRightHandler implements IDataRightHandler{
    //权限查找结果常量定义
    protected final static int RIGHT_RET_NONE = 0; //未找到
    protected final static int RIGHT_RET_IN_CHILD = 1;//找到，包含，且含下级
    protected final static int RIGHT_RET_IN_NOCHILD = 2;//找到，包含，不含下级
    protected final static int RIGHT_RET_EX = 3;//找到，不包含
    protected boolean isByRight = false;//是否需要计算权限
    // 用户session
    protected UserSession us = null;
    // 参数对象
    protected SwMap params = new SwMap();

    //权限组设置
    protected Map<String, DataRightGroupItem> mapRight = null;

    public AbsTreeDataRightHandler() {
    }

    public AbsTreeDataRightHandler(UserSession us,SwMap params) {
        init(us,params);
    }
    @Override
    public void init(UserSession us,SwMap params) {
        this.us = us;
        this.params = params;
        long menuId = params.readLong(BpmConst.MENU_KEY,0L);
        if (us == null) return;
        this.isByRight = true;
        mapRight = DataRightHelper.getDataRightItemMap(getDataRightType(), menuId, us);
        if (mapRight == null) return;
        //将本单位处理下
        DataRightGroupItem drgi = mapRight.remove(BpmConst.DataRight.CUR_VALUE+"");
        if (drgi != null) {
            DataRightGroupItem d = new DataRightGroupItem();
            d.getData().putAll(drgi.getData());
            final String curValue = getCurValue();
            if (StringUtil.isEmpty(curValue)) return;
            d.setValue(curValue);
            mapRight.put(curValue, d);
        }
    }

    /**
     * 获取权限设置中CUR_VALUE对应的值
     */
    protected abstract String getCurValue();

    /**
     * 权限类别
     */
    protected abstract String getDataRightType();

}
