public JsonResult Save(SceneSaveModel model) { var objectId = ObjectId.GenerateNewId(); if (!string.IsNullOrEmpty(model.ID) && !ObjectId.TryParse(model.ID, out objectId)) { return(Json(new { Code = 300, Msg = "ID is not allowed." })); } if (string.IsNullOrEmpty(model.Name)) { return(Json(new { Code = 300, Msg = "Name is not allowed to be empty." })); } if (model.Name.StartsWith("_")) { return(Json(new { Code = 300, Msg = "Name is not allowed to start with _." })); } // 查询场景信息 var mongo = new MongoHelper(); var filter = Builders <BsonDocument> .Filter.Eq("ID", objectId); var doc = mongo.FindOne(Constant.SceneCollectionName, filter); var now = DateTime.Now; string collectionName; var version = -1; if (doc == null) // 新建场景 { collectionName = "Scene" + now.ToString("yyyyMMddHHmmss"); version = 0; } else // 编辑场景 { collectionName = doc["CollectionName"].ToString(); version = doc.Contains("Version") ? int.Parse(doc["Version"].ToString()) : 0; version++; } // 保存或更新场景综合信息 if (doc == null) { var pinyin = PinYinHelper.GetTotalPinYin(model.Name); doc = new BsonDocument { ["ID"] = objectId, ["Name"] = model.Name, ["TotalPinYin"] = string.Join("", pinyin.TotalPinYin), ["FirstPinYin"] = string.Join("", pinyin.FirstPinYin), ["CollectionName"] = collectionName, ["Version"] = version, ["CreateTime"] = BsonDateTime.Create(now), ["UpdateTime"] = BsonDateTime.Create(now), ["IsPublic"] = false, }; if (ConfigHelper.EnableAuthority) { var user = UserHelper.GetCurrentUser(); if (user != null) { doc["UserID"] = user.ID; } } mongo.InsertOne(Constant.SceneCollectionName, doc); } else { var update1 = Builders <BsonDocument> .Update.Set("Version", version); var update2 = Builders <BsonDocument> .Update.Set("UpdateTime", BsonDateTime.Create(now)); var update = Builders <BsonDocument> .Update.Combine(update1, update2); mongo.UpdateOne(Constant.SceneCollectionName, filter, update); // 将当前场景移入历史表 var old = mongo.FindAll(collectionName).ToList(); foreach (var i in old) { i[Constant.VersionField] = version - 1; } if (old.Count > 0) { // 移除_id,避免移入历史表时重复 for (var i = 0; i < old.Count; i++) { old[i].Remove("_id"); } mongo.InsertMany($"{collectionName}{Constant.HistorySuffix}", old); } } // 保存新的场景信息 var list = JsonHelper.ToObject <JArray>(model.Data); var docs = new List <BsonDocument>(); foreach (var i in list) { docs.Add(BsonDocument.Parse(i.ToString())); } mongo.DeleteAll(collectionName); mongo.InsertMany(collectionName, docs); return(Json(new { Code = 200, Msg = "Saved successfully!", ID = objectId })); }
public JsonResult Save(SceneSaveModel model) { var objectId = ObjectId.GenerateNewId(); if (!string.IsNullOrEmpty(model.ID) && !ObjectId.TryParse(model.ID, out objectId)) { return(Json(new { Code = 300, Msg = "场景ID不合法。" })); } if (string.IsNullOrEmpty(model.Name)) { return(Json(new { Code = 300, Msg = "场景名称不允许为空。" })); } if (model.Name.StartsWith("_")) { return(Json(new { Code = 300, Msg = "场景名称不允许以下划线开头。" })); } // 查询场景信息 var mongo = new MongoHelper(); var filter = Builders <BsonDocument> .Filter.Eq("ID", objectId); var doc = mongo.FindOne(Constant.SceneCollectionName, filter); var now = DateTime.Now; string collectionName; var version = -1; if (doc == null) // 新建场景 { collectionName = "Scene" + now.ToString("yyyyMMddHHmmss"); version = 0; } else // 编辑场景 { collectionName = doc["CollectionName"].ToString(); version = doc.Contains("Version") ? int.Parse(doc["Version"].ToString()) : 0; version++; } // 保存或更新场景综合信息 if (doc == null) { var pinyin = PinYinHelper.GetTotalPinYin(model.Name); doc = new BsonDocument { ["ID"] = objectId, ["Name"] = model.Name, ["TotalPinYin"] = string.Join("", pinyin.TotalPinYin), ["FirstPinYin"] = string.Join("", pinyin.FirstPinYin), ["CollectionName"] = collectionName, ["Version"] = version, ["CreateTime"] = BsonDateTime.Create(now), ["UpdateTime"] = BsonDateTime.Create(now) }; mongo.InsertOne(Constant.SceneCollectionName, doc); } else { var update1 = Builders <BsonDocument> .Update.Set("Version", version); var update2 = Builders <BsonDocument> .Update.Set("UpdateTime", BsonDateTime.Create(now)); var update = Builders <BsonDocument> .Update.Combine(update1, update2); mongo.UpdateOne(Constant.SceneCollectionName, filter, update); // 将当前场景移入历史表 var old = mongo.FindAll(collectionName); foreach (var i in old) { i[Constant.VersionField] = version - 1; } if (old.Count > 0) { mongo.InsertMany($"{collectionName}{Constant.HistorySuffix}", old); } } // 保存新的场景信息 var list = JsonHelper.ToObject <JArray>(model.Data); var docs = new List <BsonDocument>(); foreach (var i in list) { docs.Add(BsonDocument.Parse(i.ToString())); } mongo.DeleteAll(collectionName); mongo.InsertMany(collectionName, docs); return(Json(new { Code = 200, Msg = "保存成功!", ID = objectId })); }