示例#1
0
        /// <summary>
        /// 根据用户ID集合查询用户角色列表,包含角色信息
        /// </summary>
        /// <param name="userIds">用户ID集合</param>
        /// <param name="connectionId">连接ID</param>
        /// <returns>用户角色列表</returns>
        public IList <UserRoleInfo> SelectContainsRoleByUserIds(int[] userIds, string connectionId = null)
        {
            IList <UserRoleInfo> result     = null;
            DynamicParameters    parameters = new DynamicParameters();
            StringBuilder        userIdSql  = new StringBuilder();

            for (int i = 0; i < userIds.Length; i++)
            {
                string name = $"@UserId{i}";
                parameters.Add(name, userIds[i]);
                userIdSql.AppendFormat("{0},", name);
            }
            userIdSql.Remove(userIdSql.Length - 1, 1);
            DbConnectionManager.BrainpowerExecute(connectionId, this, (connId, dbConn) =>
            {
                string sql = $"SELECT UR.user_id UserId,{RolePersistence.AllFieldMapProps().JoinSelectPropMapFields("R.")} FROM {Table} UR"
                             + $" INNER JOIN `{RolePersistence.Table}` R ON R.id=UR.role_id AND UR.user_id IN({userIdSql.ToString()})";
                result = dbConn.Query <UserRoleInfo, RoleInfo, UserRoleInfo>(sql, (ur, r) =>
                {
                    ur.Role = r;
                    return(ur);
                }, parameters).AsList();
            }, AccessMode.SLAVE);

            return(result);
        }
示例#2
0
        /// <summary>
        /// 根据用户ID查询角色列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="connectionId">连接ID</param>
        /// <returns>角色列表</returns>
        public IList <RoleInfo> SelectRolesByUserId(int userId, string connectionId = null)
        {
            IList <RoleInfo> result = null;

            DbConnectionManager.BrainpowerExecute(connectionId, this, (connId, dbConn) =>
            {
                string sql = $"SELECT {RolePersistence.AllFieldMapProps().JoinSelectPropMapFields("R.")} FROM `{RolePersistence.Table}` R"
                             + $" INNER JOIN `{Table}` UR ON R.id=UR.role_id AND UR.user_id=@UserId";
                result = dbConn.Query <RoleInfo>(sql, new { UserId = userId }).AsList();
            }, AccessMode.SLAVE);

            return(result);
        }