DBQueryJsonTask.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using MongoDB.Driver;
  4. namespace ETModel
  5. {
  6. [ObjectSystem]
  7. public class DBQueryJsonTaskAwakeSystem : AwakeSystem<DBQueryJsonTask, string, string, ETTaskCompletionSource<List<ComponentWithId>>>
  8. {
  9. public override void Awake(DBQueryJsonTask self, string collectionName, string json, ETTaskCompletionSource<List<ComponentWithId>> tcs)
  10. {
  11. self.CollectionName = collectionName;
  12. self.Json = json;
  13. self.Tcs = tcs;
  14. }
  15. }
  16. public sealed class DBQueryJsonTask : DBTask
  17. {
  18. public string CollectionName { get; set; }
  19. public string Json { get; set; }
  20. public ETTaskCompletionSource<List<ComponentWithId>> Tcs { get; set; }
  21. public override async ETTask Run()
  22. {
  23. DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
  24. try
  25. {
  26. // 执行查询数据库任务
  27. FilterDefinition<ComponentWithId> filterDefinition = new JsonFilterDefinition<ComponentWithId>(this.Json);
  28. IAsyncCursor<ComponentWithId> cursor = await dbComponent.GetCollection(this.CollectionName).FindAsync(filterDefinition);
  29. List<ComponentWithId> components = await cursor.ToListAsync();
  30. this.Tcs.SetResult(components);
  31. }
  32. catch (Exception e)
  33. {
  34. this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {this.Json}", e));
  35. }
  36. }
  37. }
  38. }