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

import cc.smtweb.framework.core.session.UserSession;
import cc.smtweb.system.bpm.web.engine.flow.entity.ProcInst;

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

/**
 * Created by Akmm at 2022-08-02 20:19
 * 用户分组辅助类
 */
public class UserGroupHelper {
    /**
     * 用户是否在权限组
     *
     * @param user_id
     * @param group_id
     * @return
     */
    public static boolean isInGroup(long user_id, long group_id) {
        /*if (group_id <= 0L) return true;//未设置分组，默认true
        UserGroup group = UserGroupCache.getInstance().get(group_id);
        List<UserGroupItem> items = group.getItems();
        //设置了却没有找到分组，默认无权限
        if (CommUtil.isEmpty(items)) {
            return false;
        }*/

        return true;
    }

    /**
     * 获取分组下的所有用户信息
     *
     * @param group_id
     * @return
     */
    public static Set<Long> getUserIdList(long group_id, ProcInst pro_inst, int handle_range, UserSession us) {
        if (group_id <= 0L) {
            Set<Long> set = new HashSet<>();
            set.add(1L);
            return set;//未设置分组，默认所有
        }
        return null;
        /*
        List<UserGroupItemEntity> items = UserGroupItemEntityBuffer.getInstance().getListByGroup(group_id);
        //设置了却没有找到分组，默认无权限
        if (UtilPub.isEmpty(items)) {
            return null;
        }

        DepartmentDataRightHandler depRight = new DepartmentDataRightHandler(context);
        List<String> listRet = new ArrayList();

        //include系列
        Map<String, Boolean> mapPartyInc = new HashMap<>();
        Map<String, Boolean> mapDeptInc = new HashMap<>();
        Map<String, Boolean> mapRoleInc = new HashMap<>();
        Set<String> mapUserInc = new HashSet<>();
        //exclude系列
        Map<String, Boolean> mapPartyExc = new HashMap<>();
        Map<String, Boolean> mapDeptExc = new HashMap<>();
        Map<String, Boolean> mapRoleExc = new HashMap<>();
        Set<String> mapUserExc = new HashSet<>();
        //先分门归类
        for (UserGroupItemEntity item : items) {
            //单位
            if (item.getTypeCode() == CmEnum.UserGroupType.PARTY.value) {
                if (!item.isRecursion()) {//不含下级
                    if (item.isInclude()) {
                        mapPartyInc.put(item.getValue(), false);
                    } else {
                        mapPartyExc.put(item.getValue(), false);
                    }
                } else {
                    if (item.isInclude()) {
                        mapPartyInc.put(item.getValue(), true);
                    } else {
                        mapPartyExc.put(item.getValue(), true);
                    }
                }
            } else if (item.getTypeCode() == CmEnum.UserGroupType.PARTY_TYPE.value) {
                //单位性质
                List<PartyEntity> listAllParty = PartyEntityBuffer.getInstance().getCacheList(null);
                for (PartyEntity p : listAllParty) {
                    if (item.getValue().equals(p.getPartyType())) {
                        if (item.isInclude()) {
                            mapPartyInc.put(item.getValue(), false);
                        } else {
                            mapPartyExc.put(item.getValue(), false);
                        }
                    }
                }
            } else if (item.getTypeCode() == CmEnum.UserGroupType.DEPT.value) {
                //部门
                if (!item.isRecursion()) {//不含下级
                    if (item.isInclude()) {
                        mapDeptInc.put(item.getValue(), false);
                    } else {
                        mapDeptExc.put(item.getValue(), false);
                    }
                } else {
                    if (item.isInclude()) {
                        mapDeptInc.put(item.getValue(), true);
                    } else {
                        mapDeptExc.put(item.getValue(), true);
                    }
                }
            } else if (item.getTypeCode() == CmEnum.UserGroupType.DEPT_TYPE.value) {
                //部门性质
                List<DepartmentEntity> listAllParty = DepartmentEntityBuffer.getInstance().getCacheList(null);
                for (DepartmentEntity p : listAllParty) {
                    if (item.getValue().equals(p.getDeptType())) {
                        if (item.isInclude()) {
                            mapDeptInc.put(item.getValue(), false);
                        } else {
                            mapDeptExc.put(item.getValue(), false);
                        }
                    }
                }
            } else if (item.getTypeCode() == CmEnum.UserGroupType.ROLE.value) {
                //角色
                if (item.isInclude() && depRight.hasRight(item.getValue(), pro_inst.getDeptId())) {
                    mapRoleInc.put(item.getValue(), false);
                } else {
                    mapRoleExc.put(item.getValue(), false);
                }
            } else if (item.getTypeCode() == CmEnum.UserGroupType.USER.value) {
                //用户
                if (item.isInclude()) {
                    mapUserInc.add(item.getValue());
                } else {
                    mapUserExc.add(item.getValue());
                }
            }
        }
        //再一个个的找
        StringBuilder sqlPartyId = new StringBuilder(256);
        for (String id : mapPartyInc.keySet()) {
            buildPartyIn(id, mapPartyInc.get(id), sqlPartyId, mapPartyExc);
        }
        StringBuilder sqlDeptId = new StringBuilder(256);
        for (String id : mapDeptInc.keySet()) {
            buildDeptIn(id, mapDeptInc.get(id), sqlPartyId, mapDeptExc);
        }

        //用户结果集
        Set<String> setUsers = new HashSet<>();
        //按单位和部门的
        if (sqlPartyId.length() > 0 || sqlDeptId.length() > 0) {
            StringBuilder sql = new StringBuilder(256);
            sql.append("select distinct e.user_id from " + EntityDaoHelper.getSchemaTable(UserExEntity.ENTITY_NAME) + " e\n");
            boolean b = false;
            if (sqlPartyId.length() > 0) {
                sql.append("where party_id in (" + sqlPartyId.substring(1) + ")");
                b = true;
            }
            if (sqlDeptId.length() > 0) {
                if (b) sql.append(" or ");
                else sql.append(" where ");
                sql.append("where dept_id in (" + sqlDeptId.substring(1) + ")");
            }
            DbEngine.query(sql.toString(), new ResultSetExtractor<Object>() {
                @Override
                public Object extractData(ResultSet resultSet) throws SQLException, DataAccessException {
                    while (resultSet.next()) {
                        setUsers.add(resultSet.getString(1));
                    }
                    return null;
                }
            });
        }
        //有包含角色权限的
        buildRole(mapRoleInc, true, setUsers);
        buildRole(mapRoleExc, false, setUsers);

        setUsers.addAll(mapUserInc);
        setUsers.removeAll(mapUserExc);

        Set<String> userRange = new HashSet<>();
        if (handle_range == CmEnum.HandlerRange.DEPT.value) {
            buildDeptRange(userRange, pro_inst.getDeptId());
        }
        if (handle_range == CmEnum.HandlerRange.PARTY.value) {
            buildPartyRange(userRange, pro_inst.getPartyId());
        }
        if (UtilPub.isNotEmpty(userRange)) setUsers.retainAll(userRange);
        return setUsers;
         */
    }
}
