using LeanCloud.Realtime.Internal;
using LeanCloud.Storage.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace LeanCloud.Realtime
{
///
/// 对话查询类
///
public class AVIMConversationQuery : AVQueryPair, IAVQuery
{
internal AVIMClient CurrentClient { get; set; }
internal AVIMConversationQuery(AVIMClient _currentClient)
: base()
{
CurrentClient = _currentClient;
}
bool compact;
bool withLastMessageRefreshed;
private AVIMConversationQuery(AVIMConversationQuery source,
IDictionary where = null,
IEnumerable replacementOrderBy = null,
IEnumerable thenBy = null,
int? skip = null,
int? limit = null,
IEnumerable includes = null,
IEnumerable selectedKeys = null,
String redirectClassNameForKey = null)
: base(source, where, replacementOrderBy, thenBy, skip, limit, includes, selectedKeys, redirectClassNameForKey)
{
}
///
/// Creates the instance.
///
/// The instance.
/// Where.
/// Replacement order by.
/// Then by.
/// Skip.
/// Limit.
/// Includes.
/// Selected keys.
/// Redirect class name for key.
public override AVIMConversationQuery CreateInstance(
IDictionary where = null,
IEnumerable replacementOrderBy = null,
IEnumerable thenBy = null,
int? skip = null,
int? limit = null,
IEnumerable includes = null,
IEnumerable selectedKeys = null,
String redirectClassNameForKey = null)
{
var rtn = new AVIMConversationQuery(this, where, replacementOrderBy, thenBy, skip, limit, includes);
rtn.CurrentClient = this.CurrentClient;
rtn.compact = this.compact;
rtn.withLastMessageRefreshed = this.withLastMessageRefreshed;
return rtn;
}
///
/// Withs the last message refreshed.
///
/// The last message refreshed.
/// If set to true enabled.
public AVIMConversationQuery WithLastMessageRefreshed(bool enabled)
{
this.withLastMessageRefreshed = enabled;
return this;
}
public AVIMConversationQuery Compact(bool enabled)
{
this.compact = enabled;
return this;
}
internal ConversationCommand GenerateQueryCommand()
{
var cmd = new ConversationCommand();
var queryParameters = this.BuildParameters(false);
if (queryParameters != null)
{
if (queryParameters.Keys.Contains("where"))
cmd.Where(queryParameters["where"]);
if (queryParameters.Keys.Contains("skip"))
cmd.Skip(int.Parse(queryParameters["skip"].ToString()));
if (queryParameters.Keys.Contains("limit"))
cmd.Limit(int.Parse(queryParameters["limit"].ToString()));
if (queryParameters.Keys.Contains("sort"))
cmd.Sort(queryParameters["order"].ToString());
}
return cmd;
}
public override Task CountAsync(CancellationToken cancellationToken)
{
var convCmd = this.GenerateQueryCommand();
convCmd.Count();
convCmd.Limit(0);
var cmd = convCmd.Option("query");
return CurrentClient.RunCommandAsync(convCmd).OnSuccess(t =>
{
var result = t.Result.Item2;
if (result.ContainsKey("count"))
{
return int.Parse(result["count"].ToString());
}
return 0;
});
}
///
/// 查找符合条件的对话
///
///
///
public override Task> FindAsync(CancellationToken cancellationToken)
{
var convCmd = this.GenerateQueryCommand().Option("query");
return CurrentClient.RunCommandAsync(convCmd).OnSuccess(t =>
{
var result = t.Result.Item2;
IList rtn = new List();
var conList = result["results"] as IList