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; if (conList != null) { foreach (var c in conList) { var cData = c as IDictionary; if (cData != null) { var con = AVIMConversation.CreateWithData(cData, CurrentClient); rtn.Add(con); } } } return rtn.AsEnumerable(); }); } public override Task FirstAsync(CancellationToken cancellationToken) { return this.FirstOrDefaultAsync(); } public override Task FirstOrDefaultAsync(CancellationToken cancellationToken) { var firstQuery = this.Limit(1); return firstQuery.FindAsync().OnSuccess(t => { return t.Result.FirstOrDefault(); }); } public override Task GetAsync(string objectId, CancellationToken cancellationToken) { var idQuery = this.WhereEqualTo("objectId", objectId); return idQuery.FirstAsync(); } } }