DBQueryJsonTask.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using MongoDB.Driver;
  5. namespace ETModel
  6. {
  7. [ObjectSystem]
  8. public class DBQueryJsonTaskAwakeSystem : AwakeSystem<DBQueryJsonTask, string, string, TaskCompletionSource<List<ComponentWithId>>>
  9. {
  10. public override void Awake(DBQueryJsonTask self, string collectionName, string json, TaskCompletionSource<List<ComponentWithId>> tcs)
  11. {
  12. self.CollectionName = collectionName;
  13. self.Json = json;
  14. self.Tcs = tcs;
  15. }
  16. }
  17. public sealed class DBQueryJsonTask : DBTask
  18. {
  19. public string CollectionName { get; set; }
  20. public string Json { get; set; }
  21. public TaskCompletionSource<List<ComponentWithId>> Tcs { get; set; }
  22. public override async Task Run()
  23. {
  24. DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
  25. try
  26. {
  27. // 执行查询数据库任务
  28. FilterDefinition<ComponentWithId> filterDefinition = new JsonFilterDefinition<ComponentWithId>(this.Json);
  29. List<ComponentWithId> components = await dbComponent.GetCollection(this.CollectionName).FindAsync(filterDefinition).Result.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. }