using System; using System.Collections.Generic; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; namespace Model { [ObjectSystem] public class DbSaveBatchTaskSystem : ObjectSystem, IAwake, string, TaskCompletionSource> { public void Awake(List entitys, string collectionName, TaskCompletionSource tcs) { DBSaveBatchTask self = this.Get(); self.Entitys = entitys; self.CollectionName = collectionName; self.Tcs = tcs; } } public sealed class DBSaveBatchTask : DBTask { public string CollectionName { get; set; } public List Entitys; public TaskCompletionSource Tcs; public override async Task Run() { DBComponent dbComponent = Game.Scene.GetComponent(); foreach (Entity entity in this.Entitys) { if (entity == null) { continue; } try { // 执行保存数据库任务 await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == entity.Id, entity, new UpdateOptions { IsUpsert = true }); } catch (Exception e) { Log.Debug($"{entity.GetType().Name} {entity.ToJson()}" + e.ToString()); this.Tcs.SetException(new Exception($"保存数据失败! {CollectionName} {this.Entitys.ListToString()}", e)); } } this.Tcs.SetResult(true); } } }