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

import cc.smtweb.framework.core.session.UserSession;
import cc.smtweb.system.bpm.web.common.BpmConst;
import cc.smtweb.system.bpm.web.sys.user.role.RoleHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: tanghp
 * @Date: 2022-09-22 11:34
 * @Desc: 数据权限辅助类
 */
public class DataRightHelper {

    /**
     * 获取数据权限组明细
     * @param dType 数据权限类型
     * @param menuId 菜单ID
     * @param us 用户session
     * @return
     */
    public static Map<String,DataRightGroupItem> getDataRightItemMap(String dType, long menuId, UserSession us ){
        Set<DataRightGroup> setGroup = RoleHelper.getDataRightGroup(us.getUserId(), us.getPartyId(), menuId,dType);
        //以value作为key
        Map<String, DataRightGroupItem> map = new HashMap<>();
        for (DataRightGroup group : setGroup) {
            List<DataRightGroupItem> items = group.getDataRightItemList();
            for (DataRightGroupItem item : items) {
                if (!map.containsKey(item.getValue())) {
                    map.put(item.getValue(), item);
                }
                DataRightGroupItem i = map.get(item.getValue());
                if ((!i.isInclude() && item.isInclude()) || (i.isInclude() == item.isInclude() && item.getType() == BpmConst.DataRight.V_INCLUDE_CHILDREN)) {
                    map.put(item.getValue(), item);
                }
            }
        }
        // 如果没有配置数据权限，默认设置个本级及下级
        if(map.size()==0){
            DataRightGroupItem drgi = new DataRightGroupItem();
            drgi.setKind(BpmConst.DataRight.V_INCLUDE);
            drgi.setType(BpmConst.DataRight.V_INCLUDE_CHILDREN);
            drgi.setValue(BpmConst.DataRight.CUR_VALUE.toString());
            map.put(drgi.getValue(),drgi);
        }
        return map;
    }
}
